CalFlops

raw JSON →
0.3.2 verified Mon Apr 27 auth: no python

CalFlops is a PyTorch-based FLOPs, MACs, and parameter counter for neural networks including CNNs, RNNs, GCNs, and Transformers (e.g., BERT, LLaMA). Version 0.3.2 supports custom models. Released as needed on GitHub.

pip install calflops
error ModuleNotFoundError: No module named 'calflops'
cause Package not installed or installed in a different environment.
fix
Run 'pip install calflops' in the correct Python environment.
error AttributeError: module 'calflops' has no attribute 'calculate_flops'
cause Outdated version of calflops or wrong import path.
fix
Update calflops: 'pip install --upgrade calflops' and use 'from calflops import calculate_flops'.
error RuntimeError: Expected all tensors to be on the same device, but found at least two devices
cause Model and input tensors are on different devices (CPU vs GPU).
fix
Ensure both model and input tensors are on the same device: model.to(device), input_tensor = input_tensor.to(device).
gotcha Torchvision models may include non-parameter operations (like softmax) that are counted differently. Ensure model is in eval mode.
fix Set model.eval() before calling calculate_flops to disable dropout/batchnorm effects.
gotcha If model has multiple inputs (e.g., encoders), use input_constructor instead of input_shape.
fix Pass a callable that returns a tuple of tensors: input_constructor=lambda: (x,).
gotcha FLOPs counting for dynamic architectures (e.g., with torch.where) may be inaccurate due to static graph assumption.
fix If using control flow, test with a simple forward pass first and verify counts are reasonable.

Compute FLOPs, MACs, and parameters for a ResNet-18 model.

import torch
import torchvision.models as models
from calflops import calculate_flops

model = models.resnet18()
batch_size = 1
input_shape = (batch_size, 3, 224, 224)
flops, macs, params = calculate_flops(
    model=model,
    input_shape=input_shape,
    output_as_string=True,
    output_precision=4
)
print(f"FLOPs: {flops}, MACs: {macs}, Params: {params}")