### Install ptflops from Repository Source: https://github.com/sovrasov/flops-counter.pytorch/blob/master/README.md Install or upgrade to the latest version directly from the GitHub repository. ```bash pip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git ``` -------------------------------- ### Install ptflops from PyPI Source: https://github.com/sovrasov/flops-counter.pytorch/blob/master/README.md Install the latest stable version of the ptflops library using pip. ```bash pip install ptflops ``` -------------------------------- ### Redirect Per-Layer Stats to a File Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Shows how to redirect the detailed per-layer complexity statistics to a file using the `ost` argument in `get_model_complexity_info`. This example uses the 'pytorch' backend. ```python # ── Redirect per-layer output to a file ────────────────────────────────────── with open("complexity_report.txt", "w") as f: macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='pytorch', print_per_layer_stat=True, ost=f, # write per-layer stats to file ) ``` -------------------------------- ### GPU-aware usage with CUDA Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Example of running get_model_complexity_info on CUDA devices, showing how the library respects device placement. ```APIDOC ## GPU-aware usage — Running on CUDA devices ### Description ptflops respects device placement. Move the model to the desired GPU before calling `get_model_complexity_info`; the synthetic input tensor is automatically created on the same device as the model's parameters. ### Method `get_model_complexity_info` on CUDA devices ### Request Example ```python import torch import torchvision.models as models from ptflops import get_model_complexity_info if torch.cuda.is_available(): with torch.cuda.device(0): net = models.densenet161(weights=None).cuda(device=0) macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False, ) print(f"Computational complexity: {macs}") print(f"Number of parameters: {params}") else: # CPU fallback — no code change required net = models.densenet161(weights=None) macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False) print(f"MACs: {macs}, Params: {params}") ``` ### Response Example ``` Computational complexity: ~7.82 GMac Number of parameters: ~28.68 M ``` ``` -------------------------------- ### Basic Usage of get_model_complexity_info with Aten Backend Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Demonstrates the basic usage of `get_model_complexity_info` with the default 'aten' backend to get human-readable MACs and parameter counts. Ensure the model is in eval mode and provide the input shape without the batch dimension. ```python import torch import torchvision.models as models from ptflops import get_model_complexity_info # ── Basic usage with the aten backend (default) ────────────────────────────── net = models.resnet50(weights=None) macs, params = get_model_complexity_info( net, (3, 224, 224), # input shape WITHOUT batch dim as_strings=True, # return human-readable strings backend='aten', # 'aten' (recommended) or 'pytorch' print_per_layer_stat=True, verbose=True, ) # Computational complexity: 4.09 GMac # Number of parameters: 25.56 M print(f"MACs: {macs}, Params: {params}") ``` -------------------------------- ### Get Raw Integer Output from get_model_complexity_info Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Shows how to obtain raw integer values for MACs and parameters using `get_model_complexity_info` by setting `as_strings=False`. This is useful for further programmatic processing. ```python # ── Raw integer output ──────────────────────────────────────────────────────── macs_int, params_int = get_model_complexity_info( net, (3, 224, 224), as_strings=False, # returns (int, int) backend='aten', print_per_layer_stat=False, ) print(f"MACs (raw): {macs_int:,}") # e.g. 4,090,000,000 print(f"Params (raw): {params_int:,}") ``` -------------------------------- ### get_model_complexity_info with PyTorch backend Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Example of using get_model_complexity_info with the PyTorch backend, disabling functional-level op patching. ```APIDOC ## get_model_complexity_info with PyTorch backend ### Description This example demonstrates how to use the `get_model_complexity_info` function with the PyTorch backend, specifically disabling functional-level operation patching. ### Method `get_model_complexity_info` ### Parameters - `net`: The neural network model. - `input_shape`: The shape of the input tensor (e.g., (3, 224, 224)). - `as_strings` (bool): If True, return MACs and params as human-readable strings. - `backend` (str): The backend to use ('pytorch' or 'aten'). - `print_per_layer_stat` (bool): If True, print statistics for each layer. - `backend_specific_config` (dict): Configuration specific to the chosen backend. For PyTorch, `{"count_functional": False}` disables functional-level op patching. ### Request Example ```python from ptflops import get_model_complexity_info import torchvision.models as models net = models.mobilenet_v2(weights=None) macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='pytorch', print_per_layer_stat=False, backend_specific_config={"count_functional": False}, ) print(f"MACs (module-level only): {macs}") print(f"Params: {params}") ``` ### Response Example ``` MACs (module-level only): 0.32 GMac Params: 3.5 M ``` ``` -------------------------------- ### Control Output Units and Precision in get_model_complexity_info Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Illustrates how to customize the output units for MACs and parameters (e.g., 'MMac', 'K') and control the precision of the floating-point output using `flops_units`, `param_units`, and `output_precision` arguments. ```python # ── Control output units and precision ─────────────────────────────────────── macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False, flops_units='MMac', # 'GMac', 'MMac', 'KMac', or None (auto) param_units='K', # 'M', 'K', 'B', or None (auto) output_precision=3, ) print(f"MACs: {macs}") # e.g. "4090.12 MMac" print(f"Params: {params}") # e.g. "25557.03 K" ``` -------------------------------- ### Handle multi-input models with `input_constructor` Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Use `input_constructor` when a model requires multiple inputs or non-standard input types like tokenized text. It must return a dictionary of keyword arguments for the model's `forward` method. ```python from functools import partial import torch from transformers import BertForSequenceClassification, BertTokenizer from ptflops import get_model_complexity_info def bert_input_constructor(input_shape, tokenizer): """ input_shape = (batch_size, seq_len) Returns a dict suitable for BertForSequenceClassification.forward(**inputs) """ # Build a fake padded sequence (no real text needed for FLOPs counting) pad_seq = tokenizer.pad_token * (input_shape[1] - 2) # minus [CLS] + [SEP] inputs = tokenizer( [pad_seq] * input_shape[0], padding=True, truncation=True, return_tensors="pt", ) inputs = dict(inputs) inputs["labels"] = torch.tensor([1] * input_shape[0]) return inputs bert_tokenizer = BertTokenizer.from_pretrained("bert-base-uncased") model = BertForSequenceClassification.from_pretrained("bert-base-uncased") macs, params = get_model_complexity_info( model, (2, 128), # (batch_size, seq_len) as_strings=True, input_constructor=partial(bert_input_constructor, tokenizer=bert_tokenizer), print_per_layer_stat=False, backend='pytorch', # use pytorch backend for BERT ) print(f"Computational complexity: {macs}") # ~21.74 GMac print(f"Number of parameters: {params}") # ~109.48 M ``` -------------------------------- ### flops_to_string utility Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Shows how to use the flops_to_string utility to format raw MAC counts into human-readable strings. ```APIDOC ## `flops_to_string` — Format a raw MAC count as a human-readable string ### Description Converts an integer MAC count into a scaled, unit-annotated string. It automatically selects the best unit (GMac / MMac / KMac / Mac) when `units` is `None`, or forces a specific unit. ### Method `flops_to_string` ### Parameters - `raw_macs` (int): The raw MAC count. - `units` (str, optional): The desired unit ('GMac', 'MMac', 'KMac', 'Mac'). Defaults to None (auto-select). - `precision` (int, optional): The number of decimal places for the output string. Defaults to 2. ### Request Example ```python from ptflops.utils import flops_to_string raw_macs = 4_090_000_000 print(flops_to_string(raw_macs)) print(flops_to_string(raw_macs, units='MMac')) print(flops_to_string(raw_macs, units='GMac', precision=4)) print(flops_to_string(500_000)) print(flops_to_string(800)) ``` ### Response Example ``` 4.09 GMac 4090.0 MMac 4.09 GMac 500.0 KMac 800 Mac ``` ``` -------------------------------- ### Using FLOPS_BACKEND enum Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Demonstrates using the FLOPS_BACKEND enum for type-safe backend selection with get_model_complexity_info. ```APIDOC ## `FLOPS_BACKEND` enum ### Description The `FLOPS_BACKEND` enum provides named constants for backend selection, enhancing code safety compared to raw string literals. ### Method `get_model_complexity_info` with `FLOPS_BACKEND` enum ### Parameters - `backend`: Can be set to `FLOPS_BACKEND.ATEN` or `FLOPS_BACKEND.PYTORCH`. ### Request Example ```python from ptflops import get_model_complexity_info, FLOPS_BACKEND import torchvision.models as models net = models.mobilenet_v2(weights=None) for backend in FLOPS_BACKEND: macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend=backend, # FLOPS_BACKEND.ATEN or FLOPS_BACKEND.PYTORCH print_per_layer_stat=False, ) print(f"[{backend.value}] MACs: {macs}, Params: {params}") ``` ### Response Example ``` [pytorch] MACs: 0.32 GMac, Params: 3.5 M [aten] MACs: 0.32 GMac, Params: 3.5 M ``` ``` -------------------------------- ### Compute Model Complexity with ATen Backend Source: https://github.com/sovrasov/flops-counter.pytorch/blob/master/README.md Calculate MACs and parameters for a DenseNet model using the 'aten' backend. Set `print_per_layer_stat=True` and `verbose=True` for detailed output. This backend is recommended for transformer architectures. ```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='aten' print_per_layer_stat=True, verbose=True) print('{:<30} {:<8}'.format('Computational complexity: ', macs)) print('{:<30} {:<8}'.format('Number of parameters: ', params)) ``` -------------------------------- ### Compute Model Complexity with PyTorch Backend Source: https://github.com/sovrasov/flops-counter.pytorch/blob/master/README.md Calculate MACs and parameters for a DenseNet model using the 'pytorch' backend. Set `print_per_layer_stat=True` and `verbose=True` for detailed output. This backend is useful for CNNs. ```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)) ``` -------------------------------- ### params_to_string utility Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Demonstrates using the params_to_string utility to format raw parameter counts into human-readable strings. ```APIDOC ## `params_to_string` — Format a raw parameter count as a human-readable string ### Description Converts an integer parameter count into a scaled, unit-annotated string. It automatically selects the best unit (M / k) when `units` is `None`, or forces a specific unit. ### Method `params_to_string` ### Parameters - `raw_params` (int): The raw parameter count. - `units` (str, optional): The desired unit ('M', 'K', 'B'). Defaults to None (auto-select). - `precision` (int, optional): The number of decimal places for the output string. Defaults to 2. ### Request Example ```python from ptflops.utils import params_to_string raw_params = 25_557_032 print(params_to_string(raw_params)) print(params_to_string(raw_params, units='K')) print(params_to_string(raw_params, units='B', precision=4)) print(params_to_string(1_200)) print(params_to_string(500)) ``` ### Response Example ``` 25.56 M 25557.03 K 0.0256 B 1.2 k 500 ``` ``` -------------------------------- ### Register custom FLOPs hooks with `custom_modules_hooks` Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Provide a dictionary mapping custom `nn.Module` classes to hook functions for unsupported layers. Each hook must manually increment `module.__flops__`. ```python import torch import torch.nn as nn import numpy as np from ptflops import get_model_complexity_info class MyCustomLayer(nn.Module): """A custom layer whose MACs ptflops wouldn't know about otherwise.""" def __init__(self, in_features, out_features): super().__init__() self.linear = nn.Linear(in_features, out_features, bias=False) def forward(self, x): return torch.relu(self.linear(x)) def custom_layer_hook(module, input, output): """Hook that manually counts MACs: input_features * output_features per sample.""" inp = input[0] output_last_dim = output.shape[-1] input_last_dim = inp.shape[-1] batch_elements = np.prod(inp.shape[:-1], dtype=np.int64) module.__flops__ += int(input_last_dim * output_last_dim * batch_elements) class MyModel(nn.Module): def __init__(self): super().__init__() self.custom = MyCustomLayer(512, 256) def forward(self, x): return self.custom(x) model = MyModel() macs, params = get_model_complexity_info( model, (512,), as_strings=True, backend='pytorch', print_per_layer_stat=True, custom_modules_hooks={MyCustomLayer: custom_layer_hook}, ) print(f"MACs: {macs}, Params: {params}") ``` -------------------------------- ### Format MAC Count with flops_to_string Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Use `flops_to_string` to convert raw MAC counts into human-readable strings. It supports auto-selection of units (GMac, MMac, KMac, Mac) or forcing a specific unit and precision. ```python from ptflops.utils import flops_to_string raw_macs = 4_090_000_000 print(flops_to_string(raw_macs)) # "4.09 GMac" print(flops_to_string(raw_macs, units='MMac')) # "4090.0 MMac" print(flops_to_string(raw_macs, units='GMac', precision=4)) # "4.09 GMac" print(flops_to_string(500_000)) # "500.0 KMac" print(flops_to_string(800)) # "800 Mac" ``` -------------------------------- ### Fine-tune backend behavior with `backend_specific_config` Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt The `backend_specific_config` dictionary passes extra options to the selected backend. For the `pytorch` backend, `count_functional=False` can disable patching of functional operations. ```python import torchvision.models as models from ptflops import get_model_complexity_info net = models.vgg16(weights=None) ``` -------------------------------- ### GPU-Aware Usage with CUDA Devices Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Ensure the model is moved to the desired GPU before calling `get_model_complexity_info`. The input tensor will automatically be created on the same device. ```python import torch import torchvision.models as models from ptflops import get_model_complexity_info if torch.cuda.is_available(): with torch.cuda.device(0): net = models.densenet161(weights=None).cuda(device=0) macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False, ) print(f"Computational complexity: {macs}") # ~7.82 GMac print(f"Number of parameters: {params}") # ~28.68 M else: # CPU fallback — no code change required net = models.densenet161(weights=None) macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False) print(f"MACs: {macs}, Params: {params}") ``` -------------------------------- ### Format Parameter Count with params_to_string Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Use `params_to_string` to convert raw parameter counts into human-readable strings. It supports auto-selection of units (M, k) or forcing a specific unit and precision. ```python from ptflops.utils import params_to_string raw_params = 25_557_032 print(params_to_string(raw_params)) # "25.56 M" print(params_to_string(raw_params, units='K')) # "25557.03 K" print(params_to_string(raw_params, units='B', precision=4)) # "0.0256 B" print(params_to_string(1_200)) # "1.2 k" print(params_to_string(500)) # "500" ``` -------------------------------- ### Type-Safe Backend Selection with FLOPS_BACKEND Enum Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Iterate through the `FLOPS_BACKEND` enum to perform complexity analysis with different backends. This ensures type safety compared to using raw string literals. ```python from ptflops import get_model_complexity_info, FLOPS_BACKEND import torchvision.models as models net = models.mobilenet_v2(weights=None) for backend in FLOPS_BACKEND: macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend=backend, # FLOPS_BACKEND.ATEN or FLOPS_BACKEND.PYTORCH print_per_layer_stat=False, ) print(f"[{backend.value}] MACs: {macs}, Params: {params}") ``` -------------------------------- ### Exclude modules from FLOPs counting with `ignore_modules` Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Use `ignore_modules` to exclude specific layer types (PyTorch backend) or operations (ATEN backend) from the MACs total. This is useful for ablation studies. ```python import torch import torchvision.models as models from ptflops import get_model_complexity_info net = models.resnet50(weights=None) # ── pytorch backend: exclude all Conv2d layers ──────────────────────────────── macs_no_conv, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='pytorch', print_per_layer_stat=False, ignore_modules=[torch.nn.Conv2d], ) print(f"MACs without convolutions: {macs_no_conv}") # ── aten backend: exclude convolution ops ──────────────────────────────────── macs_no_conv_aten, _ = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False, ignore_modules=[torch.ops.aten.convolution, torch.ops.aten._convolution], ) print(f"MACs without convolutions (aten): {macs_no_conv_aten}") ``` -------------------------------- ### get_model_complexity_info Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Computes the theoretical number of multiply-accumulate operations (MACs) and parameters in a neural network. It runs the model in evaluation mode on a single synthetic batch, attaches counting hooks, and returns the total MACs and parameter count. Returns (None, None) on failure. ```APIDOC ## `get_model_complexity_info` — Compute MACs and parameter count for any `nn.Module` ### Description The primary API function. It runs the model in eval mode on a single synthetic batch, attaches counting hooks, and returns the total multiply-accumulate operations and parameter count. The `backend` argument selects between `'aten'` (recommended, covers all aten-level ops) and `'pytorch'` (legacy, module-level hooks only). Returns `(None, None)` on failure instead of raising. ### Parameters - **net** (`nn.Module`) - The neural network model to analyze. - **input_shape** (`tuple`) - The shape of the input tensor, excluding the batch dimension. - **as_strings** (`bool`, optional) - If True, returns human-readable strings for MACs and parameters. Defaults to True. - **backend** (`str`, optional) - The backend to use for counting. Options are `'aten'` (recommended) or `'pytorch'` (legacy). Defaults to `'aten'`. - **print_per_layer_stat** (`bool`, optional) - If True, prints per-layer statistics. Defaults to False. - **verbose** (`bool`, optional) - If True, enables verbose output. Defaults to False. - **flops_units** (`str`, optional) - Units for FLOPS output (e.g., 'GMac', 'MMac', 'KMac', None for auto). Defaults to None. - **param_units** (`str`, optional) - Units for parameter output (e.g., 'M', 'K', 'B', None for auto). Defaults to None. - **output_precision** (`int`, optional) - Number of decimal places for output precision. Defaults to None. - **ost** (file-like object, optional) - An output stream to write per-layer statistics to. Defaults to None. ### Returns - **macs** (str or int or None) - The total multiply-accumulate operations. - **params** (str or int or None) - The total number of parameters. ### Request Example ```python import torch import torchvision.models as models from ptflops import get_model_complexity_info net = models.resnet50(weights=None) macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=True, verbose=True, ) print(f"MACs: {macs}, Params: {params}") macs_int, params_int = get_model_complexity_info( net, (3, 224, 224), as_strings=False, backend='aten', print_per_layer_stat=False, ) print(f"MACs (raw): {macs_int:,}") print(f"Params (raw): {params_int:,}") macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='aten', print_per_layer_stat=False, flops_units='MMac', param_units='K', output_precision=3, ) print(f"MACs: {macs}") print(f"Params: {params}") with open("complexity_report.txt", "w") as f: macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='pytorch', print_per_layer_stat=True, ost=f, ) ``` ### Response Example ``` Computational complexity: 4.09 GMac Number of parameters: 25.56 M MACs: 4.09 GMac, Params: 25.56 M MACs (raw): 4,090,000,000 Params (raw): 25,557,032 MACs: 4090.12 MMac Params: 25557.03 K ``` ``` -------------------------------- ### Disable Functional-Level Op Patching Source: https://context7.com/sovrasov/flops-counter.pytorch/llms.txt Use the `backend_specific_config` to disable functional-level operator patching for the PyTorch backend. This affects how MACs are counted. ```python from ptflops import get_model_complexity_info import torchvision.models as models net = models.mobilenet_v2(weights=None) macs, params = get_model_complexity_info( net, (3, 224, 224), as_strings=True, backend='pytorch', print_per_layer_stat=False, backend_specific_config={"count_functional": False}, ) print(f"MACs (module-level only): {macs}") print(f"Params: {params}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.