.. _l-api-pattern-catalog: Patterns ======== This table is generated from the patterns registered by the installed ``onnx_light`` package. Summaries come from the C++ Doxygen comments; follow the C++ class links to see the rewrite graphs. .. list-table:: :header-rows: 1 :widths: 5 30 65 :class: sphinx-datatable pattern-catalog * - # - Pattern - Summary * - 1 - :cpp:class:`AttentionGQA ` (``attention``) - :class:`Python ` — Converts cached grouped-query attention into ONNX Attention cache inputs and outputs. * - 2 - :cpp:class:`BatchNormalization ` (``normalization``) - :class:`Python ` — Removes an identity BatchNormalization in inference mode. * - 3 - :cpp:class:`BatchNormalizationTraining ` (``normalization``) - :class:`Python ` — Expands training BatchNormalization into primitive operators. * - 4 - :cpp:class:`Cast ` (``canonicalization``) - :class:`Python ` — Canonicalizes a Cast to Identity when its input already has the target type. * - 5 - :cpp:class:`CastCast ` (``canonicalization``) - :class:`Python ` — Canonicalizes consecutive Cast nodes when a single conversion is equivalent. * - 6 - :cpp:class:`CastCastBinary ` (``canonicalization``) - :class:`Python ` — Moves matching floating-point Cast nodes after a binary arithmetic operation. * - 7 - :cpp:class:`CastLayerNormalizationCast ` (``normalization``) - :class:`Python ` — Removes FP32 promotion around eligible standard FP16 normalization. * - 8 - :cpp:class:`CastOpCast ` (``canonicalization``) - :class:`Python ` — Moves an operation from a temporary floating-point type to its result type. * - 9 - :cpp:class:`ClipClip ` (``canonicalization``) - :class:`Python ` — Merges two consecutive Clip nodes when one defines the minimum and the other the maximum. * - 10 - :cpp:class:`ConcatEmpty ` (``collections``) - :class:`Python ` — Removes ``Concat`` inputs that are empty along the concatenation axis. * - 11 - :cpp:class:`ConcatGather ` (``collections``) - :class:`Python ` — Simplifies ``Gather(Concat(...), cst_index)`` when the index is a constant single-element ``int64`` tensor and every ``Concat`` input is a 1-D tensor of a statically known size. * - 12 - :cpp:class:`ConcatReshape ` (``reshape``) - :class:`Python ` — Replaces dynamic dimensions in a concatenated Reshape target with one inferred dimension. * - 13 - :cpp:class:`ConcatSliceElimination ` (``collections``) - :class:`Python ` — Removes a ``Concat`` whose consumers are exact, non-overlapping ``Slice`` nodes that recover every input in order along the concatenation axis. * - 14 - :cpp:class:`ConcatTwiceUnary ` (``collections``) - :class:`Python ` — Pushes a shape-preserving unary operator through a ``Concat(x, x)`` so that ``Unary(Concat(x, x))`` becomes ``Concat(Unary(x), Unary(x))``, exposing the shared ``Unary(x)`` for common-subexpression elimination. * - 15 - :cpp:class:`ConstantToInitializer ` (``canonicalization``) - :class:`Python ` — Replaces a Constant node by an initializer and an Identity node. * - 16 - :cpp:class:`ConvAddFusion ` (``canonicalization``) - :class:`Python ` — Fuses a channel-wise Add following a Conv into the Conv bias. * - 17 - :cpp:class:`ConvBatchNormalizationFusion ` (``canonicalization``) - :class:`Python ` — Fuses inference BatchNormalization following a Conv into the Conv weights and bias. * - 18 - :cpp:class:`ConvBiasNull ` (``canonicalization``) - :class:`Python ` — Removes a null (all-zero) bias input from a Conv node. * - 19 - :cpp:class:`ConvMulFusion ` (``canonicalization``) - :class:`Python ` — Fuses a scalar or channel-wise Mul following a Conv into its weights and optional bias. * - 20 - :cpp:class:`DivMul ` (``algebra``) - :class:`Python ` — Fuses a reciprocal followed by multiplication into one division. * - 21 - :cpp:class:`Dropout ` (``canonicalization``) - :class:`Python ` — Replaces an eligible Dropout by an Identity node. * - 22 - :cpp:class:`Expand ` (``expand``) - :class:`Python ` — Removes an ``Expand`` that does not change the shape of its input. * - 23 - :cpp:class:`ExpandBroadcast ` (``expand``) - :class:`Python ` — Drops an ``Expand`` feeding an element-wise binary operator. * - 24 - :cpp:class:`ExpandSwap ` (``expand``) - :class:`Python ` — Moves an ``Expand`` past a following unary-like operator. * - 25 - :cpp:class:`ExpandUnsqueezeExpand ` (``expand``) - :class:`Python ` — Fuses ``Expand``, ``Unsqueeze`` and ``Expand`` into ``Unsqueeze`` then ``Expand``. * - 26 - :cpp:class:`FunctionAttention ` (``attention``) - :class:`Python ` — Fuses a masked scaled-dot-product attention graph into a local function. * - 27 - :cpp:class:`FunctionAttentionGQA ` (``attention``) - :class:`Python ` — Moves matching GQA repeat-interleave branches into a LocalAttention function. * - 28 - :cpp:class:`FunctionCausalMask ` (``attention``) - :class:`Python ` — Fuses a causal-mask index construction into a local function. * - 29 - :cpp:class:`FunctionCausalMaskMulAdd ` (``attention``) - :class:`Python ` — Fuses an additive causal-mask index construction into a local function. * - 30 - :cpp:class:`FunctionCosSinCache ` (``attention``) - :class:`Python ` — Fuses cosine and sine rotary-cache generation into a local function. * - 31 - :cpp:class:`FunctionHalfRotaryEmbedding ` (``attention``) - :class:`Python ` — Fuses the canonical half-rotary decomposition into a local function. * - 32 - :cpp:class:`GatherConcat ` (``collections``) - :class:`Python ` — Simplifies ``Gather(Concat(..., X, ..., axis=0), cst)`` into ``Gather(X, cst - offset)`` when exactly one ``Concat`` input ``X`` is non-constant, all others are constants of known size, ``X`` is 1-D, and every requested index falls inside ``X``'s slice of the concatenation. * - 33 - :cpp:class:`GatherGather ` (``collections``) - :class:`Python ` — Composes two consecutive ``axis=0`` ``Gather`` nodes with constant indices into a single ``Gather``. * - 34 - :cpp:class:`GatherShape ` (``collections``) - :class:`Python ` — Simplifies ``Gather(Shape(X), indices)`` into ``Shape(X, start, end)`` when ``indices`` is a constant ``int64`` scalar or a contiguous ascending 1-D range. * - 35 - :cpp:class:`GatherSliceToSplit ` (``collections``) - :class:`Python ` — Replaces compatible sibling scalar ``Gather`` and contiguous ``Slice`` ranges covering a complete static axis with one ``Split``. Scalar Gather outputs are restored with ``Squeeze``. * - 36 - :cpp:class:`GatherToSlice ` (``collections``) - :class:`Python ` — Replaces a ``Gather`` selecting a constant scalar index, a constant single-element vector index, or a constant arithmetic-progression vector of indices (a "range" with a fixed, strictly ascending step) by an equivalent ``Slice``. ``Slice`` is generally cheaper to execute than ``Gather`` and, unlike ``Gather``, it never triggers a data-dependent gather kernel. * - 37 - :cpp:class:`GatherUpstreamPropagation ` (``collections``) - :class:`Python ` — Moves a scalar or vector constant-index ``Gather`` upstream across one compatible producer node, so that the producer runs on the smaller (already gathered) tensor(s) instead of the original full-size ones. * - 38 - :cpp:class:`GathersSplit ` (``collections``) - :class:`Python ` — Merges several sibling ``Gather`` nodes reading consecutive constant indices ``0, 1, ..., n-1`` on the same axis of a common input into a single ``Split`` (followed by ``Squeeze`` nodes when the indices are scalars). * - 39 - :cpp:class:`Gelu ` (``normalization``) - :class:`Python ` — Fuses exact Erf-based Gelu and its tanh approximation. * - 40 - :cpp:class:`GemmOutputTranspose ` (``matmul``) - :class:`Python ` — Absorbs a matrix output Transpose into an unbiased Gemm. * - 41 - :cpp:class:`GemmSumFusion ` (``matmul``) - :class:`Python ` — Fuses a two-input Sum following an unbiased Gemm into the Gemm bias input. * - 42 - :cpp:class:`GemmTranspose ` (``matmul``) - :class:`Python ` — Absorbs one or both matrix input Transposes into Gemm transpose flags. * - 43 - :cpp:class:`GroupedMatMul ` (``matmul``) - :class:`Python ` — Fuses per-token expert selection into an opset-29 GroupedMatMul. * - 44 - :cpp:class:`Identity ` (``canonicalization``) - :class:`Python ` — Replaces no-op operations by an Identity node. * - 45 - :cpp:class:`IfIdenticalBranches ` (``canonicalization``) - :class:`Python ` — Inlines an If whose branches are structurally identical up to local names. * - 46 - :cpp:class:`InitializerUnsqueezeCast ` (``canonicalization``) - :class:`Python ` — Folds an initializer's Unsqueeze and Cast into the initializer consumed by Add. * - 47 - :cpp:class:`LabelEncoderFusion ` (``traditionalml``) - :class:`Python ` — Composes two consecutive ``ai.onnx.ml::LabelEncoder`` mappings, including propagation of the first encoder's default through the second encoder. * - 48 - :cpp:class:`LayerNormalization ` (``normalization``) - :class:`Python ` — Fuses an explicit layer-normalization decomposition. * - 49 - :cpp:class:`LayerNormalizationScale ` (``normalization``) - :class:`Python ` — Folds a following affine transform into LayerNormalization parameters. * - 50 - :cpp:class:`LeakyRelu ` (``normalization``) - :class:`Python ` — Fuses a Where-based LeakyRelu decomposition. * - 51 - :cpp:class:`LinearAttention ` (``attention``) - :class:`Python ` — Fuses a single-token linear-attention recurrence into ``LinearAttention``. * - 52 - :cpp:class:`MatMulAdd ` (``matmul``) - :class:`Python ` — Fuses an Add bias into a MatMul or Gemm. * - 53 - :cpp:class:`MatMulBatchNormalizationFusion ` (``matmul``) - :class:`Python ` — Folds inference BatchNormalization parameters into a constant MatMul weight. * - 54 - :cpp:class:`MatMulReshape2Of3 ` (``matmul``) - :class:`Python ` — Normalizes adjacent Reshapes around a MatMul to the final batch shape. * - 55 - :cpp:class:`MatMulScaleFusion ` (``matmul``) - :class:`Python ` — Absorbs one scalar Mul or safe Div adjacent to a rank-two MatMul. * - 56 - :cpp:class:`MaxRelu ` (``normalization``) - :class:`Python ` — Replaces a maximum with scalar zero by Relu. * - 57 - :cpp:class:`MulMulMatMul ` (``matmul``) - :class:`Python ` — Moves scalar factors from both MatMul inputs to one multiplication after the MatMul. * - 58 - :cpp:class:`MulMulMulScalar ` (``algebra``) - :class:`Python ` — Combines the scalar constants from three nested Mul or Div nodes. * - 59 - :cpp:class:`MulUnsqueezeUnsqueeze ` (``layout``) - :class:`Python ` — Moves equal, unshared ``Unsqueeze`` operations after a ``Mul``. * - 60 - :cpp:class:`NotNot ` (``canonicalization``) - :class:`Python ` — Fuses two consecutive Not nodes into an Identity. * - 61 - :cpp:class:`NotWhere ` (``expand``) - :class:`Python ` — Rewrites ``Where(Not(c), x, y)`` into ``Where(c, y, x)``. * - 62 - :cpp:class:`PadConv ` (``canonicalization``) - :class:`Python ` — Fuses a Pad node followed by a Conv node into a single Conv node whose ``pads`` attribute absorbs the spatial padding. * - 63 - :cpp:class:`PadPadFusion ` (``canonicalization``) - :class:`Python ` — Merges two adjacent constant-mode Pad nodes with equal constant values. * - 64 - :cpp:class:`PadPool ` (``canonicalization``) - :class:`Python ` — Fuses zero-valued spatial Pad into AveragePool or UINT8 MaxPool. * - 65 - :cpp:class:`PreShapeNodeElimination ` (``collections``) - :class:`Python ` — Removes a ``Cast`` node whose output only feeds ``Shape`` consumers, redirecting each such ``Shape`` node directly to the ``Cast``'s input. * - 66 - :cpp:class:`RMSNormalization ` (``normalization``) - :class:`Python ` — Fuses an explicit root-mean-square normalization decomposition. * - 67 - :cpp:class:`RMSNormalizationMul ` (``normalization``) - :class:`Python ` — Folds a constant post-scale into RMSNormalization. * - 68 - :cpp:class:`ReduceArgTopK ` (``algebra``) - :class:`Python ` — Fuses matching ReduceMin/ReduceMax and ArgMin/ArgMax nodes into TopK with K=1. * - 69 - :cpp:class:`ReduceReshape ` (``reshape``) - :class:`Python ` — Fuses a dimension-removing Reshape into a reduction. * - 70 - :cpp:class:`ReduceSumNormalize ` (``algebra``) - :class:`Python ` — Moves a Cast-ReduceSum-Mul-Sub-Cast normalization chain to the result type. * - 71 - :cpp:class:`ReluClipFusion ` (``canonicalization``) - :class:`Python ` — Removes a Relu immediately followed by a Clip whose effective minimum is non-negative. * - 72 - :cpp:class:`Reshape ` (``reshape``) - :class:`Python ` — Removes a Reshape whose constant target already equals its input shape. * - 73 - :cpp:class:`Reshape2Of3 ` (``reshape``) - :class:`Python ` — Moves a binary operation to the common outer shape represented by surrounding Reshapes. * - 74 - :cpp:class:`ReshapeMatMulReshape ` (``matmul``) - :class:`Python ` — Removes batch-flattening Reshapes around a MatMul. * - 75 - :cpp:class:`ReshapeReshape ` (``reshape``) - :class:`Python ` — Composes two consecutive Reshapes into one. * - 76 - :cpp:class:`ReshapeReshapeBinary ` (``reshape``) - :class:`Python ` — Moves equal input Reshapes after an element-wise binary operation. * - 77 - :cpp:class:`ReshapeSqueeze ` (``reshape``) - :class:`Python ` — Fuses a Squeeze into the constant target of a preceding Reshape. * - 78 - :cpp:class:`RotaryConcatPart ` (``attention``) - :class:`Python ` — Collapses two zero-padded rotary branches into Split, Neg, and Concat. * - 79 - :cpp:class:`RotaryEmbedding ` (``attention``) - :class:`Python ` — Replaces a half-rotary local function and doubled caches with RotaryEmbedding. * - 80 - :cpp:class:`STFTFusion ` (``canonicalization``) - :class:`Python ` — Fuses a canonical convolution-based DFT into a standard ONNX STFT. * - 81 - :cpp:class:`SameChildren ` (``algebra``) - :class:`Python ` — Merges pairs of identical sibling nodes, and any identical descendant chains. * - 82 - :cpp:class:`SameChildrenFromInput ` (``algebra``) - :class:`Python ` — Merges identical nodes that consume the same graph input. * - 83 - :cpp:class:`SequenceConstructAt ` (``collections``) - :class:`Python ` — Replaces ``SequenceConstruct(x0, x1, ...)`` followed by the matching ``SequenceAt(seq, 0)``, ``SequenceAt(seq, 1)``, ... with one ``Identity`` per ``SequenceAt``, forwarding the original tensor directly. * - 84 - :cpp:class:`ShapeBasedConcatExpand ` (``expand``) - :class:`Python ` — Simplifies a dynamic ``Concat`` used as an ``Expand`` target shape. * - 85 - :cpp:class:`ShapeBasedEditDistanceReshape ` (``reshape``) - :class:`Python ` — Materializes a Concat-built Reshape target from inferred input and output shapes. * - 86 - :cpp:class:`ShapeBasedExpandBroadcast ` (``expand``) - :class:`Python ` — Removes dynamic ``Expand`` nodes before a broadcasting binary operator. * - 87 - :cpp:class:`ShapeBasedExpandBroadcastMatMul ` (``expand``) - :class:`Python ` — Removes dynamic ``Expand`` nodes before ``MatMul``. * - 88 - :cpp:class:`ShapeBasedExpandCastWhereSwap ` (``expand``) - :class:`Python ` — Moves an ``Expand`` after ``Cast`` and ``Where``. * - 89 - :cpp:class:`ShapeBasedExpandSwap ` (``expand``) - :class:`Python ` — Moves input ``Expand`` nodes after a broadcasting binary operator. * - 90 - :cpp:class:`ShapeBasedIdentity ` (``algebra``) - :class:`Python ` — Replaces a shape-preserving Slice by an Identity. * - 91 - :cpp:class:`ShapeBasedMatMulToMul ` (``matmul``) - :class:`Python ` — Replaces a MatMul with unit reduction dimensions by element-wise Mul. * - 92 - :cpp:class:`ShapeBasedReshapeIsSqueeze ` (``reshape``) - :class:`Python ` — Replaces a Reshape or all-one Expand that only inserts or removes unit dimensions. * - 93 - :cpp:class:`ShapeBasedSameChildren ` (``algebra``) - :class:`Python ` — Merges Expand or Reshape siblings whose inferred output shapes are equal. * - 94 - :cpp:class:`ShapeBasedShapeShapeAdd ` (``algebra``) - :class:`Python ` — Recognizes an Add fed by two Shape nodes but performs no rewrite. * - 95 - :cpp:class:`ShapeBasedStaticExpand ` (``expand``) - :class:`Python ` — Replaces a dynamic ``Expand`` target with an equivalent constant target. * - 96 - :cpp:class:`ShapeTranspose ` (``collections``) - :class:`Python ` — Replaces ``Shape(Transpose(X, perm))`` by ``Gather(Shape(X), perm)`` so the expensive ``Transpose`` on the full data tensor is avoided. * - 97 - :cpp:class:`ShapedBasedReshape ` (``reshape``) - :class:`Python ` — Removes a same-rank Reshape whose target copies every dimension but the last. * - 98 - :cpp:class:`SliceConcatToSpaceToDepth ` (``collections``) - :class:`Python ` — Replaces the canonical rank-4 four-phase slicing and channel concatenation used by focus layers with the standard ONNX ``SpaceToDepth(blocksize=2)``. * - 99 - :cpp:class:`SliceElimination ` (``collections``) - :class:`Python ` — Replaces a full-range Slice by an Identity. * - 100 - :cpp:class:`SliceSlice ` (``collections``) - :class:`Python ` — Merges two consecutive ``Slice`` nodes acting on disjoint axes into a single ``Slice`` whose ``starts`` / ``ends`` / ``axes`` (and ``steps`` when present) are the concatenation of the two operand vectors. * - 101 - :cpp:class:`SlicesSplit ` (``collections``) - :class:`Python ` — Merges several sibling ``Slice`` nodes that partition a common input along a single axis into contiguous, non-overlapping ranges into one ``Split``. * - 102 - :cpp:class:`SoftmaxCrossEntropyLossCast ` (``normalization``) - :class:`Python ` — Fuses a float16 mean cross-entropy loss decomposition. * - 103 - :cpp:class:`SplitConcat ` (``collections``) - :class:`Python ` — Replaces a ``Split`` immediately followed by a ``Concat`` that re-joins all of its outputs, in order and on the same axis, with a single ``Identity``. * - 104 - :cpp:class:`SplitToSequenceSequenceAt ` (``collections``) - :class:`Python ` — Replaces ``SplitToSequence(x, [split], axis=a)`` followed by the matching ``SequenceAt(seq, 0)``, ``SequenceAt(seq, 1)``, ... with a single ``Split``. * - 105 - :cpp:class:`SqueezeAdd ` (``layout``) - :class:`Python ` — Moves equal ``Squeeze`` operations after an ``Add``. * - 106 - :cpp:class:`SqueezeBinaryUnsqueeze ` (``layout``) - :class:`Python ` — Cancels a scalar-producing ``Squeeze``/binary/``Unsqueeze`` chain by expanding the scalar right operand instead. * - 107 - :cpp:class:`SqueezeUnsqueeze ` (``unsqueeze``) - :class:`Python ` — Simplifies a ``Squeeze``/``Unsqueeze`` pair into ``Identity`` or ``Squeeze``. * - 108 - :cpp:class:`StaticConcatReshape ` (``reshape``) - :class:`Python ` — Replaces the sole dynamic element of a concatenated Reshape target with ``[-1]``. * - 109 - :cpp:class:`Sub1Mul ` (``algebra``) - :class:`Python ` — Rewrites multiplication by ``1 - x`` into a product followed by subtraction. * - 110 - :cpp:class:`SwapExpandReshape ` (``expand``) - :class:`Python ` — Swaps an ``Expand`` with a following constant-shape ``Reshape``. * - 111 - :cpp:class:`SwapExpandUnsqueeze ` (``expand``) - :class:`Python ` — Swaps an ``Expand`` and a following ``Unsqueeze``. * - 112 - :cpp:class:`SwapRangeAddScalar ` (``algebra``) - :class:`Python ` — Moves a shape-[1] second Add input from after Range into its start and limit inputs. * - 113 - :cpp:class:`SwapUnary ` (``algebra``) - :class:`Python ` — Moves a rank-preserving layout operation after a compatible following operation. * - 114 - :cpp:class:`SwapUnsqueezeTranspose ` (``layout``) - :class:`Python ` — Swaps an unshared ``Unsqueeze`` and ``Transpose``, remapping both axes and permutation. * - 115 - :cpp:class:`SwitchOrderBinary ` (``algebra``) - :class:`Python ` — Reassociates nested Add or Mul nodes to reduce broadcasting rank. * - 116 - :cpp:class:`SwitchReshapeActivation ` (``matmul``) - :class:`Python ` — Moves an element-wise activation before a Reshape or Transpose. * - 117 - :cpp:class:`TransposeEqualReshape ` (``layout``) - :class:`Python ` — Replaces a ``Transpose`` that only repositions size-one axes around at most one other dimension with a ``Reshape``. * - 118 - :cpp:class:`TransposeGather ` (``transpose``) - :class:`Python ` — Removes an unnecessary ``Transpose`` feeding a ``Gather`` with a scalar index. * - 119 - :cpp:class:`TransposeMatMul ` (``matmul``) - :class:`Python ` — Absorbs rank-two input Transposes into Gemm transpose attributes. * - 120 - :cpp:class:`TransposeReshapeMatMul ` (``matmul``) - :class:`Python ` — Swaps a last-two-dimension Transpose with a following Reshape on one MatMul input. * - 121 - :cpp:class:`TransposeReshapeTranspose ` (``layout``) - :class:`Python ` — Moves a constant ``Reshape`` across one of two adjacent ``Transpose`` nodes when the input and target dimensions can be aligned by merging or splitting contiguous dimensions. * - 122 - :cpp:class:`TransposeTranspose ` (``transpose``) - :class:`Python ` — Merges two ``Transpose`` nodes, optionally separated by ``Identity`` nodes. * - 123 - :cpp:class:`TreeEnsemble ` (``traditionalml``) - :class:`Python ` — Replaces classic TreeEnsembleRegressor and TreeEnsembleClassifier nodes with the unified ai.onnx.ml TreeEnsemble operator from opset 5. * - 124 - :cpp:class:`UnsqueezeEqual ` (``expand``) - :class:`Python ` — Moves ``Equal(x, c)`` after a compatible sibling ``Unsqueeze(x, a)`` and preserves the rank of ``Equal(Unsqueeze(x, a), Unsqueeze(y, a))``. * - 125 - :cpp:class:`UnsqueezeOrSqueezeReshape ` (``reshape``) - :class:`Python ` — Removes a Squeeze or Unsqueeze immediately before a Reshape. * - 126 - :cpp:class:`UnsqueezeReshape ` (``reshape``) - :class:`Python ` — Collapses a specific Unsqueeze-Reshape sequence into one Unsqueeze. * - 127 - :cpp:class:`UnsqueezeShape ` (``collections``) - :class:`Python ` — Replaces ``Shape(Unsqueeze(X, axes))`` by a ``Concat`` of ``Shape(X)`` slices interleaved with constant ``[1]`` tensors at the inserted axis positions. * - 128 - :cpp:class:`UnsqueezeUnsqueeze ` (``unsqueeze``) - :class:`Python ` — Merges two consecutive ``Unsqueeze`` nodes into a single one. * - 129 - :cpp:class:`WhereAdd ` (``expand``) - :class:`Python ` — Rewrites an additive mask or factors a common term from ``Where`` branches.