ecal.calculators.model_flops¶
FLOP calculators for supported model architectures (MLP, CNN, KAN, Transformer)
and thin wrappers around the third-party calflops library for arbitrary
PyTorch/HuggingFace models.
Classes
|
Analytical FLOP calculator for a Convolutional Neural Network (CNN). |
FLOP calculator for HuggingFace models, backed by |
|
FLOP calculator for PyTorch |
|
Abstract base class for FLOP calculators. |
|
Factory that selects a |
|
|
Analytical FLOP calculator for a Kolmogorov-Arnold Network (KAN). |
|
Analytical FLOP calculator for a Multilayer Perceptron (MLP). |
|
Analytical FLOP calculator for a decoder-only Transformer model. |
- class ecal.calculators.model_flops.CNNCalculator(num_cnv_layers=3, num_pool_layers=1, i_r=10, i_c=1, k_r=3, k_c=1, c_in=1, s_r=1, s_c=1, N_f=3, num_samples=1, num_classes=2)[source]¶
Bases:
FLOPCalculatorAnalytical FLOP calculator for a Convolutional Neural Network (CNN).
- Parameters:
- __init__(num_cnv_layers=3, num_pool_layers=1, i_r=10, i_c=1, k_r=3, k_c=1, c_in=1, s_r=1, s_c=1, N_f=3, num_samples=1, num_classes=2)[source]¶
Initialize the CNN calculator.
- Parameters:
num_cnv_layers (int) – Number of convolutional layers.
num_pool_layers (int) – Number of pooling layers.
i_r (int) – Input height.
i_c (int) – Input width.
k_r (int) – Kernel height.
k_c (int) – Kernel width.
c_in (int) – Number of input channels.
s_r (int) – Stride along height.
s_c (int) – Stride along width.
N_f (int) – Number of filters.
num_samples (int) – Number of samples the FLOP count will later be scaled by.
num_classes (int) – Number of output classes.
- calculate(model, input_size)[source]¶
Calculate FLOPs and parameters for a Convolutional Neural Network.
- Parameters:
model (Module) – Unused; present to satisfy the
FLOPCalculatorinterface.input_size (Tuple) – Unused; present to satisfy the
FLOPCalculatorinterface.
- Return type:
Notes
num_cnv_layers: Number of convolutional layers. num_pool_layers: Number of pooling layers.
- class ecal.calculators.model_flops.CalFlopsCalculatorHF[source]¶
Bases:
FLOPCalculatorFLOP calculator for HuggingFace models, backed by
calflops.calculate_flops_hf.
- class ecal.calculators.model_flops.CalFlopsCalculatorPT[source]¶
Bases:
FLOPCalculatorFLOP calculator for PyTorch
nn.Modulemodels, backed bycalflops.calculate_flops.
- class ecal.calculators.model_flops.FLOPCalculator[source]¶
Bases:
ABCAbstract base class for FLOP calculators.
Subclasses implement
calculate()to estimate the floating-point operations required for a single forward pass of a model.
- class ecal.calculators.model_flops.FlopsCalculatorFactory[source]¶
Bases:
objectFactory that selects a
FLOPCalculatorimplementation based on whether the model is a HuggingFace model name (str) or atorch.nn.Module.- static create_calculator(model)[source]¶
Create a FLOP calculator appropriate for the given model.
- Parameters:
model (Module | str) – A HuggingFace model name (str) or an instantiated
torch.nn.Module.- Returns:
A
CalFlopsCalculatorHFfor string model names, or aCalFlopsCalculatorPTfornn.Moduleinstances.- Return type:
- Raises:
ValueError – If
modelis neither a string nor annn.Module.
- class ecal.calculators.model_flops.KANCalculator(num_layers, grid_size, din, dout, k=3, num_samples=1, num_classes=2)[source]¶
Bases:
FLOPCalculatorAnalytical FLOP calculator for a Kolmogorov-Arnold Network (KAN).
- Parameters:
- __init__(num_layers, grid_size, din, dout, k=3, num_samples=1, num_classes=2)[source]¶
Initialize the KAN calculator.
- Parameters:
num_layers (int) – Number of layers
L.grid_size (int) – B-spline grid size
G.din (int) – Input dimension of the network.
dout (int) – Output dimension of the network.
k (int) – B-spline degree (default 3).
num_samples (int) – Number of samples the FLOP count will later be scaled by.
num_classes (int) – Number of output classes.
- calculate(model, input_size)[source]¶
Calculate FLOPs and parameters for a Kolmogorov-Arnold Network (KAN).
- Parameters:
model (Module) – Unused; present to satisfy the
FLOPCalculatorinterface.input_size (Tuple) – Unused; present to satisfy the
FLOPCalculatorinterface.
- Return type:
Notes
K: B-spline degree (typically 3). G: Grid size. L: Number of layers. M_l-1: Input dimension of the layer. M_l: Output dimension of the layer. M_NLF: FLOPs for the non-linear function (B-spline activation).
- class ecal.calculators.model_flops.MLPCalculator(num_layers, din, dout, num_samples=1, num_classes=2)[source]¶
Bases:
FLOPCalculatorAnalytical FLOP calculator for a Multilayer Perceptron (MLP).
- __init__(num_layers, din, dout, num_samples=1, num_classes=2)[source]¶
Initialize the MLP calculator.
- calculate(model, input_size)[source]¶
Calculate FLOPs and parameters for a Multilayer Perceptron.
- Parameters:
model (Module) – Unused; present to satisfy the
FLOPCalculatorinterface.input_size (Tuple) – Unused; present to satisfy the
FLOPCalculatorinterface.
- Return type:
Notes
L: Number of layers. M_l-1: Input dimension of the layer. M_l: Output dimension of the layer.
- class ecal.calculators.model_flops.TransformerCalculator(context_length, embedding_size, num_heads, num_decoder_blocks, feed_forward_size, vocab_size)[source]¶
Bases:
FLOPCalculatorAnalytical FLOP calculator for a decoder-only Transformer model.
- Parameters:
- __init__(context_length, embedding_size, num_heads, num_decoder_blocks, feed_forward_size, vocab_size)[source]¶
Initialize the Transformer calculator.
- Parameters:
context_length (int) – Sequence/context length
C.embedding_size (int) – Embedding dimension
N_embed.num_heads (int) – Number of attention heads
N_head.num_decoder_blocks (int) – Number of decoder blocks
N_decoder_blocks.feed_forward_size (int) – Feed-forward layer width
FFS.vocab_size (int) – Vocabulary size.
- calculate(model, input_size)[source]¶
Calculate FLOPs for a Transformer model.
- Parameters:
model (Module) – Unused; present to satisfy the
FLOPCalculatorinterface.input_size (Tuple) – Unused; present to satisfy the
FLOPCalculatorinterface.
- Return type:
Notes
C: Context length. N_embed: Embedding size. N_head: Number of attention heads. N_decoder_blocks: Number of decoder blocks. FFS: Feed forward size.
- Returns:
"total_flops"(int),"total_params"(None), and a"breakdown"dict with per-component attention FLOPs (kqv_embedding_flops,attention_score_flops,reduce_flops,projection_flops,total_attention_flops),mlp_blocks_flops, andper_block_flops.- Return type:
- Parameters: