{"id":13443009,"url":"https://github.com/sovrasov/flops-counter.pytorch","last_synced_at":"2025-05-12T15:25:28.256Z","repository":{"id":37663507,"uuid":"145103892","full_name":"sovrasov/flops-counter.pytorch","owner":"sovrasov","description":"Flops counter for neural networks in pytorch framework","archived":false,"fork":false,"pushed_at":"2025-01-20T10:12:22.000Z","size":183,"stargazers_count":2891,"open_issues_count":28,"forks_count":309,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-05-12T02:03:03.265Z","etag":null,"topics":["deep-neural-networks","deeplearning","flops-counter","pytorch","pytorch-cnn","pytorch-utils","transformer","transformer-models"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sovrasov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-17T09:54:59.000Z","updated_at":"2025-05-07T14:40:52.000Z","dependencies_parsed_at":"2023-02-17T20:31:26.958Z","dependency_job_id":"e3080bbb-c2f5-4c6b-bc71-c6165fbc1560","html_url":"https://github.com/sovrasov/flops-counter.pytorch","commit_stats":{"total_commits":122,"total_committers":13,"mean_commits":9.384615384615385,"dds":"0.25409836065573765","last_synced_commit":"13a7ef81c4095d98abbb74949dc4f67db21e1b78"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sovrasov%2Fflops-counter.pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sovrasov%2Fflops-counter.pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sovrasov%2Fflops-counter.pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sovrasov%2Fflops-counter.pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sovrasov","download_url":"https://codeload.github.com/sovrasov/flops-counter.pytorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253764513,"owners_count":21960590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-neural-networks","deeplearning","flops-counter","pytorch","pytorch-cnn","pytorch-utils","transformer","transformer-models"],"created_at":"2024-07-31T03:01:54.726Z","updated_at":"2025-05-12T15:25:28.225Z","avatar_url":"https://github.com/sovrasov.png","language":"Python","funding_links":[],"categories":["Python","Model Analyzer"],"sub_categories":["**[Tutorials/Blogs]**"],"readme":"# Flops counting tool for neural networks in pytorch framework\n[![Pypi version](https://img.shields.io/pypi/v/ptflops.svg)](https://pypi.org/project/ptflops/)\n\nThis tool is designed to compute the theoretical amount of multiply-add operations\nin neural networks. It can also compute the number of parameters and\nprint per-layer computational cost of a given network.\n\n`ptflops` has two backends, `pytorch` and `aten`. `pytorch` backend is a legacy one, it considers `nn.Modules` only. However,\nit's still useful, since it provides a better par-layer analytics for CNNs. In all other cases it's recommended to use\n`aten` backend, which considers aten operations, and therefore it covers more model architectures (including transformers).\nThe default backend is `aten`. Please, don't use `pytorch` backend for transformer architectures.\n\n## `aten` backend\n### Operations considered:\n- aten.mm, aten.matmul, aten.addmm, aten.bmm\n- aten.convolution\n\n### Usage tips\n- Use `verbose=True` to see the operations which were not considered during complexity computation.\n- This backend prints per-module statistics only for modules directly nested into the root `nn.Module`.\nDeeper modules at the second level of nesting are not shown in the per-layer statistics.\n- `ignore_modules` option forces `ptflops` to ignore the listed modules. This can be useful\nfor research purposes. For instance, one can drop all convolutions from the counting process\nspecifying `ignore_modules=[torch.ops.aten.convolution, torch.ops.aten._convolution]`.\n\n## `pytorch` backend\n### Supported layers:\n- Conv1d/2d/3d (including grouping)\n- ConvTranspose1d/2d/3d (including grouping)\n- BatchNorm1d/2d/3d, GroupNorm, InstanceNorm1d/2d/3d, LayerNorm\n- Activations (ReLU, PReLU, ELU, ReLU6, LeakyReLU, GELU)\n- Linear\n- Upsample\n- Poolings (AvgPool1d/2d/3d, MaxPool1d/2d/3d and adaptive ones)\n\nExperimental support:\n- RNN, LSTM, GRU (NLH layout is assumed)\n- RNNCell, LSTMCell, GRUCell\n- torch.nn.MultiheadAttention\n- torchvision.ops.DeformConv2d\n- visual transformers from [timm](https://github.com/huggingface/pytorch-image-models)\n\n### Usage tips\n\n- This backend doesn't take into account some of the `torch.nn.functional.*` and `tensor.*` operations. Therefore unsupported operations are\nnot contributing to the final complexity estimation. See `ptflops/pytorch_ops.py:FUNCTIONAL_MAPPING,TENSOR_OPS_MAPPING` to check supported ops.\nSometimes functional-level hooks conflict with hooks for `nn.Module` (for instance, custom ones). In that case, counting with these ops can be disabled by\npassing `backend_specific_config={\"count_functional\" : False}`.\n- `ptflops` launches 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 the `input_constructor` argument of the `get_model_complexity_info`. `input_constructor` is 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.\n- `verbose` parameter allows to get information about modules that don't contribute to the final numbers.\n- `ignore_modules` option forces `ptflops` to ignore the listed modules. This can be useful\nfor research purposes. For instance, one can drop all convolutions from the counting process\nspecifying `ignore_modules=[torch.nn.Conv2d]`.\n\n## Requirements\nPytorch \u003e= 2.0. Use `pip install ptflops==0.7.2.2` to work with torch 1.x.\n\n## Install the latest version\nFrom PyPI:\n```bash\npip install ptflops\n```\n\nFrom this repository:\n```bash\npip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git\n```\n\n## Example\n```python\nimport torchvision.models as models\nimport torch\nfrom ptflops import get_model_complexity_info\n\nwith torch.cuda.device(0):\n  net = models.densenet161()\n  macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, backend='pytorch'\n                                           print_per_layer_stat=True, verbose=True)\n  print('{:\u003c30}  {:\u003c8}'.format('Computational complexity: ', macs))\n  print('{:\u003c30}  {:\u003c8}'.format('Number of parameters: ', params))\n\n  macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, backend='aten'\n                                           print_per_layer_stat=True, verbose=True)\n  print('{:\u003c30}  {:\u003c8}'.format('Computational complexity: ', macs))\n  print('{:\u003c30}  {:\u003c8}'.format('Number of parameters: ', params))\n```\n\n## Citation\nIf ptflops was useful for your paper or tech report, please cite me:\n```\n@online{ptflops,\n  author = {Vladislav Sovrasov},\n  title = {ptflops: a flops counting tool for neural networks in pytorch framework},\n  year = {2018-2024},\n  url = {https://github.com/sovrasov/flops-counter.pytorch},\n}\n```\n\n## Credits\n\nThanks to @warmspringwinds and Horace He for the initial version of the script.\n\n## Benchmark\n\n### [torchvision](https://pytorch.org/vision/0.16/models.html)\n\nModel                  | Input Resolution | Params(M) | MACs(G) (`pytorch`) | MACs(G) (`aten`)\n---                    |---               |---        |---                  |---\nalexnet                | 224x224          | 61.10     | 0.72                | 0.71\nconvnext_base          | 224x224          | 88.59     | 15.43               | 15.38\ndensenet121            | 224x224          | 7.98      | 2.90                |\nefficientnet_b0        | 224x224          | 5.29      | 0.41                |\nefficientnet_v2_m      | 224x224          | 54.14     | 5.43                |\ngooglenet              | 224x224          | 13.00     | 1.51                |\ninception_v3           | 224x224          | 27.16     | 5.75                | 5.71\nmaxvit_t               | 224x224          | 30.92     | 5.48                |\nmnasnet1_0             | 224x224          | 4.38      | 0.33                |\nmobilenet_v2           | 224x224          | 3.50      | 0.32                |\nmobilenet_v3_large     | 224x224          | 5.48      | 0.23                |\nregnet_y_1_6gf         | 224x224          | 11.20     | 1.65                |\nresnet18               | 224x224          | 11.69     | 1.83                | 1.81\nresnet50               | 224x224          | 25.56     | 4.13                | 4.09\nresnext50_32x4d        | 224x224          | 25.03     | 4.29                |\nshufflenet_v2_x1_0     | 224x224          | 2.28      | 0.15                |\nsqueezenet1_0          | 224x224          | 1.25      | 0.84                | 0.82\nvgg16                  | 224x224          | 138.36    | 15.52               | 15.48\nvit_b_16               | 224x224          | 86.57     | 17.61 (wrong)       | 16.86\nwide_resnet50_2        | 224x224          | 68.88     | 11.45               |\n\n\n### [timm](https://github.com/huggingface/pytorch-image-models)\n\nModel                  | Input Resolution | Params(M) | MACs(G)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsovrasov%2Fflops-counter.pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsovrasov%2Fflops-counter.pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsovrasov%2Fflops-counter.pytorch/lists"}