Flops-Counter for PyTorch: A Comprehensive Guide
The flops-counter.pytorch
project is a valuable tool for deep learning practitioners using the PyTorch framework. Its primary purpose is to compute the theoretical number of floating-point operations (multiply-add operations) in neural networks. Additionally, it can calculate the number of parameters and provide per-layer computational cost analysis, making it an essential instrument for those looking to understand and optimize their neural network models.
Dual Backend System
The tool offers two backends to cater to different needs within the PyTorch ecosystem:
-
PyTorch Backend: This is the legacy option, which only considers
nn.Module
components. It is particularly beneficial for providing detailed per-layer analysis in Convolutional Neural Networks (CNNs), although it is not suitable for transformer-based architectures. -
ATen Backend: The default and recommended choice for most cases, especially complex models including transformers. This backend analyzes ATen operations, thus covering a broader range of model architectures.
Key Features of ATen Backend
- Operations Analyzed: It considers various operations like matrix multiplications (aten.mm, aten.matmul), batch matrix multiplications (aten.bmm), and convolutions (aten.convolution).
- Usability Enhancements:
- Enabling
verbose=True
displays operations excluded from complexity calculations. - It primarily offers statistics for modules nested directly within the root
nn.Module
. - Specific modules can be ignored via the
ignore_modules
option, beneficial for focused research, such as excluding all convolution operations from analysis.
- Enabling
Features of PyTorch Backend
- Supported Layers: The PyTorch backend covers an extensive array of layers, including different convolution types, batch normalization, various activation functions, linear layers, and pooling operations.
- Advanced Support: It experimentally supports models like RNN, LSTM, GRU, and visual transformers from the
timm
collection. - Configuration Options:
- Can suppress counting for certain
functional
andtensor
operations using backend-specific configurations. - Allows complex input scenario handling through
input_constructor
, accommodating multi-input models.
- Can suppress counting for certain
Installation and Usage
To use flops-counter.pytorch
, ensure you have PyTorch version 2.0 or higher. The tool can be installed via PyPI with:
pip install ptflops
Or, for the latest updates directly from the repository:
pip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git
Practical Example
Here's a basic example to demonstrate using the tool with a DenseNet model:
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))
Community and Contributions
flops-counter.pytorch
has been instrumental in numerous research projects and academic papers. Users are encouraged to cite the project in their work if it contributes to their research documentation. The tool has received contributions and support from several developers, making it an evolving and robust platform within the machine learning community.
Benchmarking Performance
The tool provides benchmarking for various models, offering insights into their parameter count and multiply-add computation demands. For instance, models like ResNet and VGG are analyzed both with PyTorch and ATen backends, providing comprehensive data on their computational complexity.
In conclusion, flops-counter.pytorch
is a sophisticated yet user-friendly tool for PyTorch users, designed to facilitate an in-depth understanding of model efficiency, foster optimization, and streamline research processes in neural network development.