{"id":10303,"library":"torchprofile","title":"torchprofile","description":"torchprofile is a lightweight Python library designed to accurately count the Multiply-Accumulate Operations (MACs) or FLOPs of PyTorch models. Its current version is 0.1.0. Releases are infrequent but indicate ongoing maintenance, focusing on core profiling capabilities.","status":"active","version":"0.1.0","language":"en","source_language":"en","source_url":"https://github.com/zhijian-liu/torchprofile","tags":["pytorch","profiling","macs","flops","deep learning","performance"],"install":[{"cmd":"pip install torchprofile","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for PyTorch model definition and tensor operations.","package":"torch","optional":false}],"imports":[{"symbol":"profile_macs","correct":"from torchprofile import profile_macs"}],"quickstart":{"code":"import torch\nfrom torchvision.models import resnet18\nfrom torchprofile import profile_macs\n\n# Create a sample PyTorch model\nmodel = resnet18()\n\n# Define a dummy input tensor (batch_size, channels, height, width)\n# Ensure the input device matches the model's device if applicable\ninputs = torch.randn(1, 3, 224, 224)\n\n# Profile the model MACs\nmacs = profile_macs(model, inputs)\n\nprint(f\"ResNet-18 MACs: {macs / 1e9:.2f} G\")\n","lang":"python","description":"This quickstart demonstrates how to initialize a standard PyTorch model and use `torchprofile.profile_macs` to count its Multiply-Accumulate Operations (MACs) for a given input tensor. The result is printed in GigaMACs."},"warnings":[{"fix":"Understand the definition used by torchprofile and adjust comparisons accordingly, or consult the source for exact definitions. The library's focus is on MACs.","message":"torchprofile strictly counts Multiply-Accumulate Operations (MACs). Be aware that different communities or tools might define FLOPs differently (e.g., 1 MAC = 2 FLOPs), leading to discrepancies if comparing counts across tools.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For custom modules, you may need to implement and register a specific MACs handler using `torchprofile.register_macs_handler`. Refer to the `torchprofile.handlers` module and documentation for examples.","message":"torchprofile might not automatically count operations for highly custom or non-standard `nn.Module` implementations. This can lead to underestimated MACs for models containing such layers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pin your dependency version (e.g., `torchprofile==0.1.0`) in your project and review release notes or the GitHub changelog when upgrading to new versions.","message":"As a 0.x version library, torchprofile's API is subject to change without strict semantic versioning. Updates, especially between minor versions (e.g., 0.0.x to 0.1.x), might introduce breaking changes.","severity":"breaking","affected_versions":"< 1.0.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"`pip install torchprofile`","cause":"The `torchprofile` library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'torchprofile'"},{"fix":"Ensure both the model and the input tensor are on the same device before profiling, for example: `model.to('cuda')` and `inputs.to('cuda')`.","cause":"The input tensor and the model are on different devices (e.g., CPU vs. GPU). `torchprofile` requires them to be consistent.","error":"RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same"},{"fix":"You likely need to implement and register a custom MACs handler for that specific module type. Refer to `torchprofile.handlers` for examples and instructions on `register_macs_handler`.","cause":"`torchprofile` encountered a custom layer or operation within your model for which it does not have a predefined handler, causing a failure during graph traversal.","error":"TypeError: object of type <class '...'> has no len()"}]}