Oven logo

Oven

Published

Ultralytics THOP package for fast computation of PyTorch model FLOPs and parameters.

pip install ultralytics-thop

Package Downloads

Weekly DownloadsMonthly Downloads

Authors

Ligeng Zhu

Requires Python

>=3.8

Dependencies

Ultralytics logo

English | 简体中文

🚀 THOP: PyTorch-OpCounter

THOP profiles PyTorch models by counting Multiply-Accumulate Operations (MACs) and parameters. It is lightweight, easy to extend, and maintained by Ultralytics.

THOP CI Ultralytics Actions Ultralytics Discord Ultralytics Forums Ultralytics Reddit

📄 Description

THOP measures a model with one forward pass, making it useful for comparing architecture complexity before training or deployment. It includes counting rules for common convolutional, normalization, pooling, activation, linear, and recurrent layers, with support for custom rules.

📦 Installation

PyPI - Version Downloads PyPI - Python Version

pip install ultralytics-thop

To install the latest development version:

pip install --upgrade git+https://github.com/ultralytics/thop.git

🛠️ How to Use

Basic Usage

Pass the model and a tuple of example inputs to profile():

import torch
from torchvision.models import resnet50

from thop import profile

model = resnet50()
inputs = (torch.randn(1, 3, 224, 224),)
macs, params = profile(model, inputs=inputs)

print(f"MACs: {macs}, Parameters: {params}")
# Expected output: MACs: 4133742592.0, Parameters: 25557032.0

For image models, pass the target-size input and the model stride to estimate MACs from smaller stride-aligned profiles. profile() retains a single-profile fast path for spatial-only models, fits two points when size-independent operations may be present, fits three points and checks a fourth when attention, functional products or custom_ops may make the cost quadratic in image area, and falls back to the target input when the smaller inputs are unsuitable or no smaller than a quarter of it. Raise min_cells when the cost only takes its final form past some input width, e.g. a decoder selecting a fixed number of queries from its anchors:

inputs = (torch.randn(1, 3, 640, 640),)
macs, params = profile(model, inputs=inputs, stride=32)

Calls that omit stride retain the exact profiling behavior shown in the basic example.

On torch>=1.13, profile() also counts the products a forward runs functionally (@, torch.matmul, torch.bmm, two-operand torch.einsum, linear, multi_head_attention_forward and scaled_dot_product_attention), which no module hook observes, except inside a module whose own counting rule already accounts for them.

Define Custom Rules for Third-Party Modules

Map an unsupported module type to a forward-hook function. The hook receives the module, its inputs, and its output, then adds the operation count to module.total_ops. Parameters need no hook — they are read from the module tree. A rule covers subclasses of the type it is registered for, the nearest registered ancestor winning, so an entry for nn.Conv2d also counts an nn.Conv2d subclass. A subclass whose forward computes something different needs its own entry, or it is counted as its base.

import torch
from torch import nn

from thop import profile


def count_silu(module, inputs, output):
    """Count one operation per output element as a simple example."""
    module.total_ops += output.numel()


model = nn.Sequential(nn.Conv2d(3, 64, 3, padding=1), nn.SiLU())
inputs = (torch.randn(1, 3, 224, 224),)
macs, params = profile(model, inputs=inputs, custom_ops={nn.SiLU: count_silu})

print(f"Custom MACs: {macs}, Parameters: {params}")
# Expected output: Custom MACs: 89915392.0, Parameters: 1792.0

Improve Output Readability

Use clever_format() to convert raw counts into human-readable values:

import torch
from torchvision.models import resnet50

from thop import clever_format, profile

model = resnet50()
inputs = (torch.randn(1, 3, 224, 224),)
macs, params = profile(model, inputs=inputs)
macs_readable, params_readable = clever_format([macs, params], "%.3f")

print(f"Formatted MACs: {macs_readable}, Formatted Parameters: {params_readable}")
# Expected output: Formatted MACs: 4.134G, Formatted Parameters: 25.557M

📊 Results of Recent Models

The following detection models were profiled at 640 × 640 from their fused architecture definitions using ultralytics==8.4.106. Install that version, then run python benchmark/evaluate_famous_models.py to reproduce the table without downloading model weights. FLOPs are often approximated as twice the MAC count.

Modelsize
(pixels)
params
(M)
MACs
(B)
YOLOv8n6403.154.37
YOLOv8s64011.1614.30
YOLOv8m64025.8939.47
YOLOv8l64043.6782.57
YOLOv8x64068.20128.90
YOLO11n6402.623.24
YOLO11s6409.4410.73
YOLO11m64020.0933.99
YOLO11l64025.3443.46
YOLO11x64056.9297.45
YOLO26n6402.412.68
YOLO26s6409.5010.34
YOLO26m64020.4134.09
YOLO26l64024.8143.21
YOLO26x64055.7396.93

🤝 Contribute

We thrive on community collaboration! THOP wouldn't be the tool it is without contributions from developers like you. Please see our Contributing Guide to get started. We also welcome your feedback—share your experience by completing our Survey. A huge Thank You 🙏 to everyone who contributes!

Ultralytics open-source contributors

We look forward to your contributions to help make the Ultralytics ecosystem even better!

📜 License

Ultralytics offers two licensing options to suit different needs:

  • AGPL-3.0 License: This OSI-approved open-source license is perfect for students, researchers, and enthusiasts. It encourages open collaboration and knowledge sharing. See the LICENSE file for full details.
  • Ultralytics Enterprise License: For development and production use, this license enables seamless integration of Ultralytics software and AI models into business products and services, including internal tools, automated workflows, and production deployments, bypassing the open-source requirements of AGPL-3.0. To get started, please contact us via Ultralytics Licensing.

📞 Contact

For bug reports and feature requests related to THOP, please visit GitHub Issues. For questions, discussions, and community support, join our active communities on Discord, Reddit, and the Ultralytics Community Forums. We're here to help with all things Ultralytics!


Ultralytics GitHub space Ultralytics LinkedIn space Ultralytics Twitter space Ultralytics YouTube space Ultralytics TikTok space Ultralytics BiliBili space Ultralytics Discord