Registry / ai-ml / torchprofile

torchprofile

JSON →
library0.1.0pypypi✓ verified 89d ago

torchprofile is a lightweight Python library designed to accurately count the Multiply-Accumulate Operations (MACs) or FLOPs of PyTorch models. Its current version is 0.1.0. Releases are infrequent but indicate ongoing maintenance, focusing on core profiling capabilities.

pip install torchprofile
INSTALL
IMPORT
SIG · TORCHPROFILE
T
torchprofile
ai-mlpythonv0.1.0
Install
64.8s avg
Import
5316ms
Disk
4787MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.1.0 · 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
✓ 73.35s
py 3.11
✕ build_error
✓ 67.75s
py 3.12
✕ build_error
✓ 61.55s
py 3.13
✕ build_error
✓ 56.75s
py 3.9
✕ build_error
1/2 runs
4787MB installed
● package 4787MB
Code
Verified usage

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

profile_macs
✓ from torchprofile import profile_macs

This quickstart demonstrates how to initialize a standard PyTorch model and use `torchprofile.profile_macs` to count its Multiply-Accumulate Operations (MACs) for a given input tensor. The result is printed in GigaMACs.

import torch from torchvision.models import resnet18 from torchprofile import profile_macs # Create a sample PyTorch model model = resnet18() # Define a dummy input tensor (batch_size, channels, height, width) # Ensure the input device matches the model's device if applicable inputs = torch.randn(1, 3, 224, 224) # Profile the model MACs macs = profile_macs(model, inputs) print(f"ResNet-18 MACs: {macs / 1e9:.2f} G")
Debug
Known issues
gotchatorchprofile strictly counts Multiply-Accumulate Operations (MACs). Be aware that different communities or tools might define FLOPs differently (e.g., 1 MAC = 2 FLOPs), leading to discrepancies if comparing counts across tools.
fix
Understand the definition used by torchprofile and adjust comparisons accordingly, or consult the source for exact definitions. The library's focus is on MACs.
affects: All versions
gotchatorchprofile might not automatically count operations for highly custom or non-standard `nn.Module` implementations. This can lead to underestimated MACs for models containing such layers.
fix
For custom modules, you may need to implement and register a specific MACs handler using `torchprofile.register_macs_handler`. Refer to the `torchprofile.handlers` module and documentation for examples.
affects: All versions
breakingAs a 0.x version library, torchprofile's API is subject to change without strict semantic versioning. Updates, especially between minor versions (e.g., 0.0.x to 0.1.x), might introduce breaking changes.
fix
Pin your dependency version (e.g., `torchprofile==0.1.0`) in your project and review release notes or the GitHub changelog when upgrading to new versions.
affects: < 1.0.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'torchprofile'
The `torchprofile` library is not installed in the current Python environment.
fix
`pip install torchprofile`
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
The input tensor and the model are on different devices (e.g., CPU vs. GPU). `torchprofile` requires them to be consistent.
fix
Ensure both the model and the input tensor are on the same device before profiling, for example: `model.to('cuda')` and `inputs.to('cuda')`.
TypeError: object of type <class '...'> has no len()
`torchprofile` encountered a custom layer or operation within your model for which it does not have a predefined handler, causing a failure during graph traversal.
fix
You likely need to implement and register a custom MACs handler for that specific module type. Refer to `torchprofile.handlers` for examples and instructions on `register_macs_handler`.
Upgrade
Version history
0.1.0latest on PyPI · released Mar 10, 2026
Audit
Dependencies
torchrequiredCore dependency for PyTorch model definition and tensor operations.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
torchprofile — pip install torchprofile · libregistry