FVCore: FAIR Computer Vision Core Library
fvcore is a lightweight core library that provides common and essential functionalities shared across various computer vision frameworks developed by FAIR (Facebook AI Research), such as Detectron2, PySlowFast, and ClassyVision. It includes features like PyTorch layers, hierarchical flop/parameter counting tools, checkpointing utilities, configuration management, and hyperparameter schedulers. The library is actively maintained by the FAIR computer vision team and features type-annotated, tested, and benchmarked components.
Warnings
- gotcha fvcore'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.
- deprecated The `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.
- gotcha Using `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.
Install
-
pip install -U fvcore
Imports
- 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
Quickstart
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()}")