THOP: PyTorch-OpCounter

0.1.1.post2209072238 · maintenance · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `thop` to calculate the Multiply-Accumulate Operations (MACs) and parameters of a standard PyTorch model, then format the output for better readability.

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}")

view raw JSON →