Registry / ai-ml / ultralytics-thop

ultralytics-thop

JSON →
library2.1.6pypypi✓ verified 27d ago

Ultralytics THOP is a Python library for fast computation of PyTorch model Multiply-Accumulate Operations (MACs) and parameters. It is based on the original `thop` project, maintained by Ultralytics, and provides a robust tool for analyzing the computational cost of deep learning models. The current version is 2.0.18, with a maintenance-focused release cadence.

pip install ultralytics-thop
INSTALL
IMPORT
SIG · ULTRALYTICS-THOP
U
ultralytics-thop
ai-mlpythonv2.1.6
Install
67.5s avg
Import
6140ms
Disk
4813MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 77.8s
py 3.11
✕ build_error
✓ 70.7s
py 3.12
✕ build_error
✓ 62.5s
py 3.13
✕ build_error
✓ 58.8s
py 3.9
✕ build_error
✕ timeout
4813MB installed
● package 4813MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

profile
from thop import profile
clever_format
from thop import clever_format
profile_origin
from thop import profile_origin

This example defines a basic convolutional neural network, creates a dummy input tensor, and then uses `thop.profile` to compute its MACs and parameters. `clever_format` is used to make the output human-readable.

import torch import torch.nn as nn from thop import profile, clever_format # Define a simple PyTorch model class SimpleModel(nn.Module): def __init__(self): super(SimpleModel, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1) self.relu = nn.ReLU() self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.fc = nn.Linear(128 * 8 * 8, 10) # Assuming input size 3x32x32 initially def forward(self, x): x = self.pool(self.relu(self.conv1(x))) x = self.pool(self.relu(self.conv2(x))) x = torch.flatten(x, 1) x = self.fc(x) return x # Instantiate the model and create a dummy input model = SimpleModel() input_tensor = torch.randn(1, 3, 32, 32) # Profile the model macs, params = profile(model, inputs=(input_tensor,), verbose=False) # Format the output for readability macs_formatted, params_formatted = clever_format([macs, params], "%.3f") print(f"Model MACs: {macs_formatted}") print(f"Model Params: {params_formatted}")
Debug
Known issues
gotchaUltralytics THOP reports 'MACs' (Multiply-Accumulate Operations) by default, not 'FLOPs' (Floating Point Operations). While often used interchangeably, MACs are technically different from FLOPs and can lead to different interpretations of computational cost.
fix
Be aware of the distinction when comparing with other profiling tools. The v2.0.16 release explicitly clarified this terminology.
affects: >=2.0.16
gotchaBy default, `thop.profile` primarily counts operations for common layers (e.g., convolutions, linear layers). It may not accurately count all operations, especially for non-linearities (like ReLU, Sigmoid), element-wise operations, or custom layers.
fix
For custom layers or specific non-counted operations, you may need to define and pass `custom_ops` to the `profile` function to ensure comprehensive counting. Consult the `thop` documentation for how to implement custom hooks.
affects: all
gotchaThe MACs and parameter counts are sensitive to the input tensor's shape. Different input sizes will yield different profiling results for many layers (e.g., convolutional layers, pooling).
fix
Always profile your model with an input shape representative of its actual use case to get meaningful and accurate performance estimates.
affects: all
Upgrade
Version history
2.1.6latest on PyPI · released Jul 30, 2026
Audit
Dependencies
torchrequiredRequired for PyTorch model definition and tensor operations.
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
ultralytics-thop — pip install ultralytics-thop · libregistry