yobx.xshape.cost_inference#
Per-op FLOPs (floating-point operation) estimators for ONNX nodes.
The public entry point is estimate_node_flops(). All per-op helper
functions are private (_flops_*) and dispatched via _OP_HANDLERS.
All estimators use the native symbolic-dimension helpers from
onnx_light.onnx_core.expressions so that symbolic (dynamic) dimensions are
handled correctly. When the shapes are partially or fully unknown the
estimators return None.
Integration with NativeShapeInference:
use NativeShapeInference.estimate_node_flops() to estimate the cost of a
node using the shapes already inferred during a
run_model() call.
- yobx.xshape.cost_inference.estimate_node_flops(node: NodeProto, shape_fn: Callable[[str], Tuple[int | str | None, ...] | None], literal_fn: Callable[[str], Tuple[int | str | None, ...] | None]) int | str | None[source]#
Estimates the number of floating-point operations for a single ONNX node.
Returns
Nonewhen required dimensions are unknown or theop_typeis not covered. Symbolic dimensions produce symbolic costs. Rank-only costs remain available when individual dimensions are unknown.- Parameters:
node – ONNX node
shape_fn – callable mapping tensor name to a shape tuple containing integers, symbolic strings, or
Nonefor unknown dimensionsliteral_fn – callable mapping tensor name → int-value tuple for 1-D integer constant tensors (shape specification tensors); used as a fallback when shape_fn cannot resolve a shape
- Returns:
estimated number of FLOPs, or
None
- yobx.xshape.cost_inference.list_op_cost_formulas() Dict[str, str][source]#
Returns a mapping from each supported ONNX
op_typeto the symbolic FLOPs expression produced byestimate_node_flops()on a representative test case from the ONNX backend test suite.For every single-node model in the native wheel’s backend test inventory the static input dimensions are replaced by symbolic variables (
DIM<n>) usingreplace_static_dimensions_by_strings().NativeShapeInferenceis then run withinference=InferenceMode.COSTto obtain the symbolic FLOPs expression.Only the first passing test case per
op_typeis kept. Operators with no matching backend test case, or whose cost cannot be inferred symbolically, are omitted.- Returns:
{op_type: symbolic_flops_expression}sorted alphabetically.