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

CNNCalculator([num_cnv_layers, ...])

Analytical FLOP calculator for a Convolutional Neural Network (CNN).

CalFlopsCalculatorHF()

FLOP calculator for HuggingFace models, backed by calflops.calculate_flops_hf.

CalFlopsCalculatorPT()

FLOP calculator for PyTorch nn.Module models, backed by calflops.calculate_flops.

FLOPCalculator()

Abstract base class for FLOP calculators.

FlopsCalculatorFactory()

Factory that selects a FLOPCalculator implementation based on whether the model is a HuggingFace model name (str) or a torch.nn.Module.

KANCalculator(num_layers, grid_size, din, dout)

Analytical FLOP calculator for a Kolmogorov-Arnold Network (KAN).

MLPCalculator(num_layers, din, dout[, ...])

Analytical FLOP calculator for a Multilayer Perceptron (MLP).

TransformerCalculator(context_length, ...)

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: FLOPCalculator

Analytical 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:
Return type:

Dict[str, int | Dict]

Notes

num_cnv_layers: Number of convolutional layers. num_pool_layers: Number of pooling layers.

Returns:

"total_flops" (int) and "total_params" (None, not computed by this calculator).

Return type:

Dict[str, Union[int, Dict]]

Parameters:
class ecal.calculators.model_flops.CalFlopsCalculatorHF[source]

Bases: FLOPCalculator

FLOP calculator for HuggingFace models, backed by calflops.calculate_flops_hf.

calculate(model, input_size)[source]

Calculate FLOPs for a HuggingFace model by name.

Parameters:
  • model (str) – HuggingFace model identifier (e.g. "bert-base-uncased").

  • input_size (Tuple) – Input shape passed to calculate_flops_hf.

Returns:

"total_flops" and "total_params".

Return type:

Dict[str, Union[int, Dict]]

class ecal.calculators.model_flops.CalFlopsCalculatorPT[source]

Bases: FLOPCalculator

FLOP calculator for PyTorch nn.Module models, backed by calflops.calculate_flops.

calculate(model, input_size)[source]

Calculate FLOPs for a PyTorch model.

Parameters:
  • model (Module) – A torch.nn.Module instance.

  • input_size (Tuple) – Input shape passed to calculate_flops.

Returns:

"total_flops" and "total_params".

Return type:

Dict[str, Union[int, Dict]]

class ecal.calculators.model_flops.FLOPCalculator[source]

Bases: ABC

Abstract base class for FLOP calculators.

Subclasses implement calculate() to estimate the floating-point operations required for a single forward pass of a model.

abstractmethod calculate(model, input_size)[source]

Calculate FLOPs (and, where available, parameter count) for a model.

Parameters:
  • model (Module | str) – The model to analyze (nn.Module or HuggingFace model name, depending on the concrete implementation).

  • input_size (Tuple) – Shape of a single input sample, e.g. (1, sample_size).

Returns:

At minimum a "total_flops" key; implementations may also include "total_params" and a "breakdown" of FLOPs by component.

Return type:

Dict[str, Union[int, Dict]]

class ecal.calculators.model_flops.FlopsCalculatorFactory[source]

Bases: object

Factory that selects a FLOPCalculator implementation based on whether the model is a HuggingFace model name (str) or a torch.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 CalFlopsCalculatorHF for string model names, or a CalFlopsCalculatorPT for nn.Module instances.

Return type:

FLOPCalculator

Raises:

ValueError – If model is neither a string nor an nn.Module.

class ecal.calculators.model_flops.KANCalculator(num_layers, grid_size, din, dout, k=3, num_samples=1, num_classes=2)[source]

Bases: FLOPCalculator

Analytical 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:
Return type:

Dict[str, int | Dict]

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).

Returns:

"total_flops" (int) and "total_params" (None, not computed by this calculator).

Return type:

Dict[str, Union[int, Dict]]

Parameters:
class ecal.calculators.model_flops.MLPCalculator(num_layers, din, dout, num_samples=1, num_classes=2)[source]

Bases: FLOPCalculator

Analytical FLOP calculator for a Multilayer Perceptron (MLP).

Parameters:
  • num_layers (int)

  • din (int)

  • dout (int)

  • num_samples (int)

  • num_classes (int)

__init__(num_layers, din, dout, num_samples=1, num_classes=2)[source]

Initialize the MLP calculator.

Parameters:
  • num_layers (int) – Number of layers L in the network.

  • din (int) – Input dimension of the network.

  • dout (int) – Output dimension of the network.

  • 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 Multilayer Perceptron.

Parameters:
Return type:

Dict[str, int | Dict]

Notes

L: Number of layers. M_l-1: Input dimension of the layer. M_l: Output dimension of the layer.

Returns:

"total_flops" (int) and "total_params" (None, not computed by this calculator).

Return type:

Dict[str, Union[int, Dict]]

Parameters:
class ecal.calculators.model_flops.TransformerCalculator(context_length, embedding_size, num_heads, num_decoder_blocks, feed_forward_size, vocab_size)[source]

Bases: FLOPCalculator

Analytical FLOP calculator for a decoder-only Transformer model.

Parameters:
  • context_length (int)

  • embedding_size (int)

  • num_heads (int)

  • num_decoder_blocks (int)

  • feed_forward_size (int)

  • vocab_size (int)

__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:
Return type:

Dict[str, int | Dict]

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, and per_block_flops.

Return type:

Dict[str, Union[int, Dict]]

Parameters: