Install & Compatibility
Where this runs
tested against v0.1.5.post20221221 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 115.5MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 6.7s · import 0.000s · 113MB
116MB installed
● package 116MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
FlopCountAnalysis
✓ from fvcore.nn import FlopCountAnalysis
ActivationCountAnalysis
✓ from fvcore.nn import ActivationCountAnalysis
CfgNode
✓ from fvcore.common.config import CfgNode
Registry
✓ from fvcore.common.registry import Registry
Checkpointer
✓ from fvcore.common.checkpoint import Checkpointer
This quickstart demonstrates how to use `fvcore.nn.FlopCountAnalysis` to calculate the computational FLOPs (Floating Point Operations) of a simple PyTorch model. It initializes a model, creates dummy input, and then uses `FlopCountAnalysis` to report total FLOPs and FLOPs aggregated by operator and module.
import torch
import torch.nn as nn
from fvcore.nn import FlopCountAnalysis
class SimpleModel(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc = nn.Linear(16 * 16 * 16, 10) # Assuming input size 3x32x32
def forward(self, x):
x = self.pool(self.relu(self.conv1(x)))
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
model = SimpleModel()
inputs = (torch.randn(1, 3, 32, 32),) # Batch size 1, 3 channels, 32x32 image
flops = FlopCountAnalysis(model, inputs)
print(f"Total FLOPs: {flops.total()}")
print(f"FLOPs by operator: {flops.by_operator()}")
print(f"FLOPs by module: {flops.by_module()}")
Debug
Known issues
gotchafvcore's `FlopCountAnalysis` is known to severely undercount FLOPs for certain operations. It may not count activation functions, trigonometric functions, element-wise operations (like addition/multiplication), and bias in linear/convolution layers. This can lead to significant discrepancies between reported and actual FLOPs, especially for complex models or models with many such operations.fixBe aware of these limitations. For precise FLOP counting, manual verification or alternative tools might be necessary, or consider `ActivationCountAnalysis` for memory footprint as a supplementary metric.
affects: All versions
deprecatedThe `fvcore.common.file_io.PathManager` utility has been deprecated. Users are advised to migrate to `iopath.common.file_io.PathManager` from the `iopath` library. Continued use of the fvcore version might result in deprecation warnings.fixReplace `from fvcore.common.file_io import PathManager` with `from iopath.common.file_io import PathManager` in your code. Ensure `iopath` is installed (`pip install iopath`).
affects: All versions, specifically from late 2020/early 2021 onwards
gotchaUsing `FlopCountAnalysis` or other tracing tools with functions optimized by `torch.compile` or traced by `torch.jit.trace` can lead to `NotImplementedError` or `RuntimeError`. The tracing mechanism might not be fully compatible with these advanced PyTorch features.fixAvoid combining `FlopCountAnalysis` directly with `torch.compile` or `torch.jit.trace` on the same model/function for which FLOPs are being counted. Consider performing FLOP analysis on the base model before applying such optimizations, or use a separate model instance for analysis.
affects: All versions, particularly with newer PyTorch features (>= 1.8 for some quantization features, >= 1.11 for others).
Upgrade
Version history
0.1.5.post20221221latest on PyPI · released Dec 21, 2022
Audit
Dependencies
torchrequiredCore functionality is built on PyTorch.
iopathrequiredThe PathManager utility was deprecated in fvcore and moved to iopath. Direct migration is recommended.
yacsoptionalUsed for configuration management via CfgNode.