flops
shrinkai.profiler.flops
FLOPs (floating point operations) counting for edge-deployment profiling.
Unlike parameter count or disk size, FLOPs are hardware-independent: they characterize the amount of compute a forward pass requires.
Functions:
| Name | Description |
|---|---|
count_flops |
Counts the total FLOPs of one forward pass of |
Functions:
count_flops
count_flops(
model: Module,
sample_input: Tensor | tuple[Tensor, ...],
device: device | str = "auto",
) -> int
Counts the total FLOPs of one forward pass of model on sample_input.
Uses torch.utils.flop_counter.FlopCounterMode, which instruments the actual
tensor operations dispatched during the forward pass, covering standard
layers (Conv, Linear, matmul, attention, ...) precisely, rather than a
hand-maintained per-layer-type formula. 1 multiply-add (MAC) is counted as 2
FLOPs, matching the usual convention.
Note
Custom/opaque kernels (e.g. the quantized ops produced by
shrinkai.compression.quantization.Quantizer) have no FLOPs formula
registered and are silently counted as 0. A warning is logged if the
total comes back as 0 despite the model having parameters, since that
usually signals an undercount rather than a genuinely free model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Model to profile. |
required |
sample_input
|
Tensor | tuple[Tensor, ...]
|
Representative input tensor (or tuple of tensors, for multi-input models) matching the model's forward signature. |
required |
device
|
device | str
|
Device to run the single, untimed forward pass on. FLOPs are a static property of the computation graph and do not depend on the device; this only needs to be a device the model can actually run on. |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total FLOPs for one forward pass on |