{"id":21018,"library":"calflops","title":"CalFlops","description":"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.","status":"active","version":"0.3.2","language":"python","source_language":"en","source_url":"https://github.com/MrYxJ/calculate-flops.pytorch","tags":["flops","macs","parameters","pytorch","model-analysis"],"install":[{"cmd":"pip install calflops","lang":"bash","label":"pip install"}],"dependencies":[{"reason":"Core dependency for model definitions and tensors.","package":"torch","optional":false}],"imports":[{"note":"Older documentation suggested deep import; the top-level import is simpler and stable.","wrong":"from calflops.flops_counter import calculate_flops","symbol":"calculate_flops","correct":"from calflops import calculate_flops"},{"note":"Common mistake: FlopsCounter is exposed at package level, not in a submodule.","wrong":"from calflops.counter import FlopsCounter","symbol":"FlopsCounter","correct":"from calflops import FlopsCounter"}],"quickstart":{"code":"import torch\nimport torchvision.models as models\nfrom calflops import calculate_flops\n\nmodel = models.resnet18()\nbatch_size = 1\ninput_shape = (batch_size, 3, 224, 224)\nflops, macs, params = calculate_flops(\n    model=model,\n    input_shape=input_shape,\n    output_as_string=True,\n    output_precision=4\n)\nprint(f\"FLOPs: {flops}, MACs: {macs}, Params: {params}\")","lang":"python","description":"Compute FLOPs, MACs, and parameters for a ResNet-18 model."},"warnings":[{"fix":"Set model.eval() before calling calculate_flops to disable dropout/batchnorm effects.","message":"Torchvision models may include non-parameter operations (like softmax) that are counted differently. Ensure model is in eval mode.","severity":"gotcha","affected_versions":"all"},{"fix":"Pass a callable that returns a tuple of tensors: input_constructor=lambda: (x,).","message":"If model has multiple inputs (e.g., encoders), use input_constructor instead of input_shape.","severity":"gotcha","affected_versions":"all"},{"fix":"If using control flow, test with a simple forward pass first and verify counts are reasonable.","message":"FLOPs counting for dynamic architectures (e.g., with torch.where) may be inaccurate due to static graph assumption.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Run 'pip install calflops' in the correct Python environment.","cause":"Package not installed or installed in a different environment.","error":"ModuleNotFoundError: No module named 'calflops'"},{"fix":"Update calflops: 'pip install --upgrade calflops' and use 'from calflops import calculate_flops'.","cause":"Outdated version of calflops or wrong import path.","error":"AttributeError: module 'calflops' has no attribute 'calculate_flops'"},{"fix":"Ensure both model and input tensors are on the same device: model.to(device), input_tensor = input_tensor.to(device).","cause":"Model and input tensors are on different devices (CPU vs GPU).","error":"RuntimeError: Expected all tensors to be on the same device, but found at least two devices"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}