THOP: PyTorch-OpCounter
THOP (PyTorch-OpCounter) is a Python library designed to calculate the Multiply-Accumulate Operations (MACs) and parameters of PyTorch models. It provides an intuitive API for deep learning practitioners to evaluate the computational efficiency and memory footprint of their models, aiding in optimization and architecture selection. The current PyPI version is 0.1.1.post2209072238, released in September 2022, indicating a less active maintenance cadence for this specific branch.
Warnings
- gotcha THOP primarily reports Multiply-Accumulate Operations (MACs). The interpretation of 1 MAC as 1 FLOP or 2 FLOPs (multiplication + addition) varies across literature and tools. Be mindful of this distinction when comparing results with other FLOPs counters.
- gotcha For models containing custom PyTorch modules or third-party layers not natively supported by `thop`, accurate MACs and parameter counting requires defining custom profiling rules via the `custom_ops` argument in the `profile` function.
- deprecated The `thop` library at `github.com/Lyken17/pytorch-OpCounter` (and PyPI version 0.1.1.post2209072238) has not been updated since September 2022. A more actively maintained fork, `ultralytics/thop`, exists with newer versions and continuous development.
Install
-
pip install thop
Imports
- profile
from thop import profile
- clever_format
from thop import clever_format
Quickstart
import torch
import torch.nn as nn
from torchvision.models import resnet18
from thop import profile, clever_format
# 1. Define a model and a dummy input
model = resnet18()
dummy_input = torch.randn(1, 3, 224, 224)
# 2. Profile the model
macs, params = profile(model, inputs=(dummy_input, ), verbose=False)
# 3. Format the output for readability
macs, params = clever_format([macs, params], "%.3f")
print(f"MACs: {macs}")
print(f"Parameters: {params}")