ptflops


Nameptflops JSON
Version 0.7.3 PyPI version JSON
download
home_pageNone
SummaryFlops counter for neural networks in pytorch framework
upload_time2024-05-03 19:24:48
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT License Copyright (c) 2019 Vladislav Sovrasov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords pytorch cnn transformer tomatoes lobster thermidor
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flops counting tool for neural networks in pytorch framework
[![Pypi version](https://img.shields.io/pypi/v/ptflops.svg)](https://pypi.org/project/ptflops/)
[![Build Status](https://travis-ci.com/sovrasov/flops-counter.pytorch.svg?branch=master)](https://travis-ci.com/sovrasov/flops-counter.pytorch)

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).

## `aten` backend
### Operations considered:
- aten.mm, aten.matmul, aten.addmm, aten.bmm
- aten.convolution

### Usage tips
- Use `verbose=True` to 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.

## `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](https://github.com/huggingface/pytorch-image-models)

### Usage tips

- This backend doesn't take into account some of the `torch.nn.functional.*` and `tensor.*` operations. Therefore unsupported operations are
not contributing to the final complexity estimation. See `ptflops/pytorch_ops.py:FUNCTIONAL_MAPPING,TENSOR_OPS_MAPPING` to check supported ops.
- `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.
- `verbose` parameter allows to get information about modules that don't contribute to the final numbers.
- `ignore_modules` option forces `ptflops` to ignore the listed modules. This can be useful
for research purposes. For instance, one can drop all convolutions from the counting process
specifying `ignore_modules=[torch.nn.Conv2d]`.

Requirements: Pytorch >= 1.1, torchvision >= 0.3

Thanks to @warmspringwinds and Horace He for the initial version of the script.

## Install the latest version
From PyPI:
```bash
pip install ptflops
```

From this repository:
```bash
pip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git
```

## Example
```python
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},
}
```

## Benchmark

### [torchvision](https://pytorch.org/vision/0.16/models.html)

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](https://github.com/huggingface/pytorch-image-models)

Model                  | Input Resolution | Params(M) | MACs(G)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "ptflops",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "Vladislav Sovrasov <sovrasov.vlad@gmail.com>",
    "keywords": "pytorch, cnn, transformer, tomatoes, Lobster Thermidor",
    "author": null,
    "author_email": "Vladislav Sovrasov <sovrasov.vlad@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/95/3d/d2af80d6b90d74f1b73e688c287434912fc4416517c6f7505bb6d2ca77d1/ptflops-0.7.3.tar.gz",
    "platform": null,
    "description": "# 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[![Build Status](https://travis-ci.com/sovrasov/flops-counter.pytorch.svg?branch=master)](https://travis-ci.com/sovrasov/flops-counter.pytorch)\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).\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\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.\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\nRequirements: Pytorch >= 1.1, torchvision >= 0.3\n\nThanks to @warmspringwinds and Horace He for the initial version of the script.\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('{:<30}  {:<8}'.format('Computational complexity: ', macs))\n  print('{:<30}  {:<8}'.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('{:<30}  {:<8}'.format('Computational complexity: ', macs))\n  print('{:<30}  {:<8}'.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## 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",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2019 Vladislav Sovrasov  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Flops counter for neural networks in pytorch framework",
    "version": "0.7.3",
    "project_urls": {
        "Bug Tracker": "https://github.com/sovrasov/flops-counter.pytorch/issues",
        "Changelog": "https://github.com/sovrasov/flops-counter.pytorch/blob/master/CHANGELOG.md",
        "Documentation": "https://github.com/sovrasov/flops-counter.pytorch/blob/master/README.md",
        "Homepage": "https://github.com/sovrasov/flops-counter.pytorch/",
        "Repository": "https://github.com/sovrasov/flops-counter.pytorch.git"
    },
    "split_keywords": [
        "pytorch",
        " cnn",
        " transformer",
        " tomatoes",
        " lobster thermidor"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fffb4a96fa18f0519b6055e560b52caebc95bac905b9f73e5f31b80c31926006",
                "md5": "65323825a1183b7a98095dc3baface76",
                "sha256": "19272f25f716fd1edafba9212698b8b0961b0e8b4fd6c8ffc0a47f02c33e0d15"
            },
            "downloads": -1,
            "filename": "ptflops-0.7.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "65323825a1183b7a98095dc3baface76",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 18958,
            "upload_time": "2024-05-03T19:24:45",
            "upload_time_iso_8601": "2024-05-03T19:24:45.875187Z",
            "url": "https://files.pythonhosted.org/packages/ff/fb/4a96fa18f0519b6055e560b52caebc95bac905b9f73e5f31b80c31926006/ptflops-0.7.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "953dd2af80d6b90d74f1b73e688c287434912fc4416517c6f7505bb6d2ca77d1",
                "md5": "7dd0747c43c2e1633209887df8b52eee",
                "sha256": "59c0af2505255108df019674fb805eb61178f0baad6515720bb30dcb29592071"
            },
            "downloads": -1,
            "filename": "ptflops-0.7.3.tar.gz",
            "has_sig": false,
            "md5_digest": "7dd0747c43c2e1633209887df8b52eee",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 18482,
            "upload_time": "2024-05-03T19:24:48",
            "upload_time_iso_8601": "2024-05-03T19:24:48.201145Z",
            "url": "https://files.pythonhosted.org/packages/95/3d/d2af80d6b90d74f1b73e688c287434912fc4416517c6f7505bb6d2ca77d1/ptflops-0.7.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-03 19:24:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sovrasov",
    "github_project": "flops-counter.pytorch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ptflops"
}
        
Elapsed time: 0.26183s