Flops counter for neural networks in pytorch framework
Project description
Flops counting tool for neural networks in pytorch framework
This tool is designed to compute the theoretical amount of multiply-add operations in neural networks. It can also compute the number of parameters and print per-layer computational cost of a given network.
ptflops has two backends, pytorch and aten. pytorch backend is a legacy one, it considers nn.Modules only. However,
it's still useful, since it provides a better par-layer analytics for CNNs. In all other cases it's recommended to use
aten backend, which considers aten operations, and therefore it covers more model architectures (including transformers).
The default backend is aten. Please, don't use pytorch backend for transformer architectures.
aten backend
Operations considered:
- aten.mm, aten.matmul, aten.addmm, aten.bmm
- aten.convolution
Usage tips
- Use
verbose=Trueto see the operations which were not considered during complexity computation. - This backend prints per-module statistics only for modules directly nested into the root
nn.Module. Deeper modules at the second level of nesting are not shown in the per-layer statistics. ignore_modulesoption forcesptflopsto ignore the listed modules. This can be useful for research purposes. For instance, one can drop all convolutions from the counting process specifyingignore_modules=[torch.ops.aten.convolution, torch.ops.aten._convolution].
pytorch backend
Supported layers:
- Conv1d/2d/3d (including grouping)
- ConvTranspose1d/2d/3d (including grouping)
- BatchNorm1d/2d/3d, GroupNorm, InstanceNorm1d/2d/3d, LayerNorm
- Activations (ReLU, PReLU, ELU, ReLU6, LeakyReLU, GELU)
- Linear
- Upsample
- Poolings (AvgPool1d/2d/3d, MaxPool1d/2d/3d and adaptive ones)
Experimental support:
- RNN, LSTM, GRU (NLH layout is assumed)
- RNNCell, LSTMCell, GRUCell
- torch.nn.MultiheadAttention
- torchvision.ops.DeformConv2d
- visual transformers from timm
Usage tips
- This backend doesn't take into account some of the
torch.nn.functional.*andtensor.*operations. Therefore unsupported operations are not contributing to the final complexity estimation. Seeptflops/pytorch_ops.py:FUNCTIONAL_MAPPING,TENSOR_OPS_MAPPINGto check supported ops. Sometimes functional-level hooks conflict with hooks fornn.Module(for instance, custom ones). In that case, counting with these ops can be disabled by passingbackend_specific_config={"count_functional" : False}. ptflopslaunches a given model on a random tensor and estimates amount of computations during inference. Complicated models can have several inputs, some of them could be optional. To construct non-trivial input one can use theinput_constructorargument of theget_model_complexity_info.input_constructoris a function that takes the input spatial resolution as a tuple and returns a dict with named input arguments of the model. Next, this dict would be passed to the model as a keyword arguments.verboseparameter allows to get information about modules that don't contribute to the final numbers.ignore_modulesoption forcesptflopsto ignore the listed modules. This can be useful for research purposes. For instance, one can drop all convolutions from the counting process specifyingignore_modules=[torch.nn.Conv2d].
Requirements
Pytorch >= 2.0. Use pip install ptflops==0.7.2.2 to work with torch 1.x.
Install the latest version
From PyPI:
pip install ptflops
From this repository:
pip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git
Example
import torchvision.models as models
import torch
from ptflops import get_model_complexity_info
with torch.cuda.device(0):
net = models.densenet161()
macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, backend='pytorch'
print_per_layer_stat=True, verbose=True)
print('{:<30} {:<8}'.format('Computational complexity: ', macs))
print('{:<30} {:<8}'.format('Number of parameters: ', params))
macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, backend='aten'
print_per_layer_stat=True, verbose=True)
print('{:<30} {:<8}'.format('Computational complexity: ', macs))
print('{:<30} {:<8}'.format('Number of parameters: ', params))
Citation
If ptflops was useful for your paper or tech report, please cite me:
@online{ptflops,
author = {Vladislav Sovrasov},
title = {ptflops: a flops counting tool for neural networks in pytorch framework},
year = {2018-2024},
url = {https://github.com/sovrasov/flops-counter.pytorch},
}
Credits
Thanks to @warmspringwinds and Horace He for the initial version of the script.
Benchmark
torchvision
| Model | Input Resolution | Params(M) | MACs(G) (pytorch) |
MACs(G) (aten) |
|---|---|---|---|---|
| alexnet | 224x224 | 61.10 | 0.72 | 0.71 |
| convnext_base | 224x224 | 88.59 | 15.43 | 15.38 |
| densenet121 | 224x224 | 7.98 | 2.90 | |
| efficientnet_b0 | 224x224 | 5.29 | 0.41 | |
| efficientnet_v2_m | 224x224 | 54.14 | 5.43 | |
| googlenet | 224x224 | 13.00 | 1.51 | |
| inception_v3 | 224x224 | 27.16 | 5.75 | 5.71 |
| maxvit_t | 224x224 | 30.92 | 5.48 | |
| mnasnet1_0 | 224x224 | 4.38 | 0.33 | |
| mobilenet_v2 | 224x224 | 3.50 | 0.32 | |
| mobilenet_v3_large | 224x224 | 5.48 | 0.23 | |
| regnet_y_1_6gf | 224x224 | 11.20 | 1.65 | |
| resnet18 | 224x224 | 11.69 | 1.83 | 1.81 |
| resnet50 | 224x224 | 25.56 | 4.13 | 4.09 |
| resnext50_32x4d | 224x224 | 25.03 | 4.29 | |
| shufflenet_v2_x1_0 | 224x224 | 2.28 | 0.15 | |
| squeezenet1_0 | 224x224 | 1.25 | 0.84 | 0.82 |
| vgg16 | 224x224 | 138.36 | 15.52 | 15.48 |
| vit_b_16 | 224x224 | 86.57 | 17.61 (wrong) | 16.86 |
| wide_resnet50_2 | 224x224 | 68.88 | 11.45 |
timm
Model | Input Resolution | Params(M) | MACs(G)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ptflops-0.7.5.tar.gz.
File metadata
- Download URL: ptflops-0.7.5.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84e437a3ccad1bb050c14c599a76555b345533287767da87758e38c9101a40a7
|
|
| MD5 |
572e77517fc4a2c5a43c4ed7c34141f4
|
|
| BLAKE2b-256 |
05dce679036cc222a9995e37754aefeee4a2b6e92d1b4956bee27b48182efec2
|
Provenance
The following attestation bundles were made for ptflops-0.7.5.tar.gz:
Publisher:
publish.yaml on sovrasov/flops-counter.pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ptflops-0.7.5.tar.gz -
Subject digest:
84e437a3ccad1bb050c14c599a76555b345533287767da87758e38c9101a40a7 - Sigstore transparency entry: 413614737
- Sigstore integration time:
-
Permalink:
sovrasov/flops-counter.pytorch@3c5c3e3fdd551c15d26e3d9a858fd33ad1d0a4fc -
Branch / Tag:
refs/tags/v0.7.5 - Owner: https://github.com/sovrasov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@3c5c3e3fdd551c15d26e3d9a858fd33ad1d0a4fc -
Trigger Event:
release
-
Statement type:
File details
Details for the file ptflops-0.7.5-py3-none-any.whl.
File metadata
- Download URL: ptflops-0.7.5-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c47543d5209a21e0805b39a50d452c8f8f6f5b648684777490db6f9459bdda07
|
|
| MD5 |
1f7992958081063251937e0ef3a26a3d
|
|
| BLAKE2b-256 |
11663eea7b5f6cc3f6ae7acb125ef82c083d9bb759c7e43fd333cb39eb1e1cfe
|
Provenance
The following attestation bundles were made for ptflops-0.7.5-py3-none-any.whl:
Publisher:
publish.yaml on sovrasov/flops-counter.pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ptflops-0.7.5-py3-none-any.whl -
Subject digest:
c47543d5209a21e0805b39a50d452c8f8f6f5b648684777490db6f9459bdda07 - Sigstore transparency entry: 413614741
- Sigstore integration time:
-
Permalink:
sovrasov/flops-counter.pytorch@3c5c3e3fdd551c15d26e3d9a858fd33ad1d0a4fc -
Branch / Tag:
refs/tags/v0.7.5 - Owner: https://github.com/sovrasov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@3c5c3e3fdd551c15d26e3d9a858fd33ad1d0a4fc -
Trigger Event:
release
-
Statement type: