Computation Cost: How It Works and Supported Operator Formulas#

This example explains how FLOPs (floating-point operations) cost is estimated for ONNX models in yobx, and programmatically lists the formula used for every supported operator.

The estimator is built around estimate_node_flops() and is exposed through NativeShapeInference via inference=InferenceMode.COST. When model inputs have symbolic dimensions (strings like "batch" or "seq"), the cost values are symbolic arithmetic expressions that can be evaluated later with concrete shapes.

For a complete worked example using a real attention model, see Symbolic Cost of a Model: Transpose Fusion.

1. Quick start: cost of a tiny model#

We build a small two-node ONNX graph (MatMul + Relu) with symbolic input dimensions and compute its cost with run_model().

from yobx._onnx_shim import onnx
import onnx_light.onnx.helper as oh

from yobx.xshape import NativeShapeInference, InferenceMode

TFLOAT = onnx.TensorProto.FLOAT

model = oh.make_model(
    oh.make_graph(
        [oh.make_node("MatMul", ["A", "B"], ["C"]), oh.make_node("Relu", ["C"], ["out"])],
        "tiny",
        [
            oh.make_tensor_value_info("A", TFLOAT, ["batch", "M", "K"]),
            oh.make_tensor_value_info("B", TFLOAT, ["batch", "K", "N"]),
        ],
        [oh.make_tensor_value_info("out", TFLOAT, None)],
    ),
    opset_imports=[oh.make_opsetid("", 18)],
    ir_version=10,
)

builder = NativeShapeInference()
cost_list = builder.run_model(model, inference=InferenceMode.COST)

print("Symbolic FLOPs per node:")
for op_type, flops, _ in cost_list:
    print(f"  {op_type:<12s}  {flops}")
Symbolic FLOPs per node:
  MatMul        2*K*M*N*batch
  Relu          M*N*batch

2. Evaluating symbolic costs with concrete input shapes#

Once the graph has been analysed with symbolic shapes, pass actual numpy arrays to evaluate_cost_with_true_inputs() to substitute the dimension values and get integer FLOPs counts.

import numpy as np  # noqa: E402

rng = np.random.default_rng(0)
feeds = {
    "A": rng.standard_normal((4, 32, 64)).astype(np.float32),
    "B": rng.standard_normal((4, 64, 16)).astype(np.float32),
}

concrete = builder.evaluate_cost_with_true_inputs(feeds, cost_list)

print("Concrete FLOPs per node:")
total = 0
for op_type, flops, _ in concrete:
    total += flops or 0
    print(f"  {op_type:<12s}  {flops:>12,}")
print(f"  {'TOTAL':<12s}  {total:>12,}")
Concrete FLOPs per node:
  MatMul             262,144
  Relu                 2,048
  TOTAL              264,192

3. How the cost estimator works#

Each ONNX operator type is mapped to a handler function in yobx.xshape.cost_inference. The handler receives the ONNX node plus two callables for resolving tensor shapes and integer literals, and returns the FLOPs count (integer, symbolic string, or None when shapes are unavailable).

Operators are grouped by their counting convention:

Group

Formula

Element-wise unary (Relu, Sqrt, …)

1 FLOPs per output element

Element-wise binary (Add, Mul, …)

1 FLOPs per output element

Sigmoid

3 FLOPs per element (exp+add+div)

Softmax / LogSoftmax

3 FLOPs per element (exp+sum+div)

MatMul

2·batch·M·K·N

Gemm

2·M·K·N + M·N

Conv / ConvTranspose

2·N·C_out·C_in/group·kernel·spatial_out

MaxPool / AveragePool

N·C·spatial_out·kernel_size

GlobalAveragePool / GlobalMaxPool

N·C·spatial_in

BatchNormalization

2 FLOPs per output element

LayerNorm / GroupNorm / InstanceNorm

6 FLOPs per output element

ReduceSum / ReduceMean / … (9 ops)

Input element count

LSTM

2·seq·batch·(input+hidden)·4·hidden

GRU

2·seq·batch·(input+hidden)·3·hidden

RNN

2·seq·batch·(input+hidden)·hidden

Data-movement (Cast, Transpose, …)

Output element count

Shape-manipulation (Reshape, …)

Rank of output tensor

Identity

0 (zero cost)

The full list of supported operators (and the exact description used) is returned by list_op_cost_formulas() — see section 4 below.

4. Programmatic listing of all supported operator formulas#

list_op_cost_formulas() returns a sorted dictionary that maps every registered op_type to the symbolic FLOPs expression obtained by running the cost estimator on a representative ONNX backend test example. All static input dimensions are first replaced by symbolic variables (DIM<n>) so that the result shows the general formula rather than a single concrete number.

from yobx.xshape import list_op_cost_formulas  # noqa: E402

formulas = list_op_cost_formulas()

print(f"{'Op type':<35s}  Symbolic FLOPs")
print("-" * 80)
for op_type, formula in formulas.items():
    print(f"{op_type:<35s}  {formula}")
Op type                              Symbolic FLOPs
--------------------------------------------------------------------------------
Abs                                  DIM3*DIM4*DIM5
Acos                                 DIM3*DIM4*DIM5
Acosh                                DIM3*DIM4*DIM5
Add                                  DIM3*DIM4*DIM5
And                                  DIM3*DIM4
Asin                                 DIM3*DIM4*DIM5
Asinh                                DIM3*DIM4*DIM5
Atan                                 DIM3*DIM4*DIM5
Atanh                                DIM3*DIM4*DIM5
AveragePool                          9*DIM1*DIM1*DIM3*DIM3
BatchNormalization                   2*DIM2*DIM3*DIM4*DIM5
BitShift                             DIM3
Cast                                 DIM3*DIM5
CastLike                             DIM3*DIM5
Ceil                                 DIM2*DIM3
Celu                                 DIM2*DIM3
Concat                               DIM4
Constant                             6
ConstantOfShape                      ConstantOfShape_dim0
Conv                                 2*DIM1*DIM1*DIM1*DIM3*DIM3*DIM5*DIM5
ConvTranspose                        2*DIM1*DIM2*DIM2*DIM3*DIM3*DIM5*DIM5
Cos                                  DIM2*DIM3
Cosh                                 DIM2*DIM3
Div                                  DIM2*DIM3
Elu                                  DIM2*DIM3
Equal                                DIM2*DIM2
Erf                                  DIM2*DIM3
Exp                                  DIM2*DIM3
Expand                               Expand_dim0
Flatten                              dim1_b
Floor                                DIM2*DIM3
GRU                                  2*DIM1*DIM18*(DIM18//3+DIM2)*DIM3
Gather                               DIM2*DIM2*DIM4
GatherElements                       DIM2*DIM2
GatherND                             DIM2
Gemm                                 2*DIM3*DIM4*DIM5+DIM3*DIM4
GlobalAveragePool                    dim0_x*dim1_x*dim2_x*dim3_x
GlobalMaxPool                        dim0_x*dim1_x*dim2_x*dim3_x
Greater                              DIM2*DIM2
GreaterOrEqual                       DIM2*DIM2
GroupNormalization                   6*DIM2*DIM2*DIM3*DIM4
HardSigmoid                          DIM2*DIM3
HardSwish                            DIM2*DIM3
Identity                             0
InstanceNormalization                6*DIM1*DIM1*DIM256
LSTM                                 2*DIM1*(DIM2+DIM28//4)*DIM28*DIM3
LayerNormalization                   6*DIM3*DIM4
LeakyRelu                            DIM2*DIM3
Less                                 DIM2*DIM2
LessOrEqual                          DIM2*DIM2
Log                                  DIM2*DIM3
LogSoftmax                           3*DIM2*DIM3
MatMul                               2*DIM3
MaxPool                              2*DIM1*DIM3*DIM31
Mish                                 DIM2*DIM3
Mod                                  DIM3
Mul                                  DIM2*DIM3
Neg                                  DIM2*DIM3
Not                                  DIM2*DIM2
OneHot                               DIM2*DIM3
Or                                   DIM2*DIM2
PRelu                                DIM2*DIM3
Pad                                  DIM1*DIM12*DIM3*DIM7
Pow                                  DIM3
RNN                                  2*DIM2*DIM3*(DIM3+DIM5)*DIM5
ReduceL1                             dim0_data*dim1_data*dim2_data
ReduceL2                             dim0_data*dim1_data*dim2_data
ReduceLogSum                         dim0_data*dim1_data*dim2_data
ReduceLogSumExp                      dim0_data*dim1_data*dim2_data
ReduceMax                            dim0_data*dim1_data*dim2_data
ReduceMean                           dim0_data*dim1_data*dim2_data
ReduceMin                            dim0_data*dim1_data*dim2_data
ReduceProd                           dim0_data*dim1_data*dim2_data
ReduceSum                            dim0_data*dim1_data*dim2_data
ReduceSumSquare                      dim0_data*dim1_data*dim2_data
Relu                                 DIM2*DIM3
Reshape                              1
Round                                DIM2*DIM3
Scatter                              DIM1*DIM5
ScatterElements                      DIM1*DIM5
ScatterND                            DIM4*DIM4*DIM4
Selu                                 DIM2*DIM3
Shape                                3
Shrink                               DIM2*DIM3
Sigmoid                              3*DIM2*DIM3
Sign                                 DIM2*DIM3
Sin                                  DIM2*DIM3
Sinh                                 DIM2*DIM3
Slice                                B*DIM3
Softmax                              3*DIM2*DIM3
Softplus                             DIM2*DIM3
Softsign                             DIM2*DIM3
Split                                DIM2*b
Sqrt                                 DIM2*DIM3
Squeeze                              4
Sub                                  DIM2*DIM3
Tan                                  DIM5
Tanh                                 DIM2*DIM3
ThresholdedRelu                      DIM2*DIM3
Tile                                 DIM9
Transpose                            DIM2*DIM3
Unsqueeze                            1
Xor                                  DIM2*DIM2

Total running time of the script: (0 minutes 0.175 seconds)

Related examples

Symbolic Cost of a Model: Transpose Fusion

Symbolic Cost of a Model: Transpose Fusion

Comparing Computational Cost of Three Einsum→ONNX Strategies

Comparing Computational Cost of Three Einsum→ONNX Strategies

Computed Shapes: Add + Concat + Reshape

Computed Shapes: Add + Concat + Reshape

Gallery generated by Sphinx-Gallery