Ultralytics THOP
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.
Warnings
- gotcha Ultralytics 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.
- gotcha By 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.
- gotcha The 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).
Install
-
pip install ultralytics-thop
Imports
- profile
from thop import profile
- clever_format
from thop import clever_format
- profile_origin
from thop import profile_origin
Quickstart
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}")