onnx_light.onnx_core.optimization#

Optimization workflow#

Optimization always operates on a GraphBuilder through GraphGraph:

from onnx_light.onnx_core.optimization import GraphBuilder, GraphGraph

builder = GraphBuilder(model)
graph = GraphGraph(builder)
rewrites, report = graph.optimize(report=True)
optimized_model = builder.to_onnx("model")

Pattern registration#

Patterns use the same global-plus-local model as shape functions. Registries are merged by the stable PatternOptimization.name; a more local entry replaces an entry with the same name:

Scope

Registration

Selection

Global

register_pattern()

Used by every new GraphGraph. The standard ONNX patterns are registered globally when this module is imported.

Builder

GraphBuilder.register_pattern()

Overrides a global pattern for optimizers built over that builder.

Graph

GraphGraph(builder, patterns=[...])

Has the highest precedence and is retained for that optimizer, including recursive subgraphs.

Pass use_global_patterns=False to GraphGraph to use only builder and graph registrations. clear_registered_patterns() clears the global registry; reset_registered_patterns() restores the standard patterns.

Registered standard patterns#

The following table lists the standard patterns registered when this module is imported. It is generated from the live registry, so it always reflects the currently available patterns.

Class / registered name

Priority

Candidate roots

Transformation

AttentionGQAPattern / AttentionGQA

2

Fuses grouped-query attention cache handling.

BatchNormalizationPattern / BatchNormalization

0

BatchNormalization

Fuses an inference batch-normalization subgraph.

BatchNormalizationTrainingPattern / BatchNormalizationTraining

0

BatchNormalization

Fuses a training batch-normalization subgraph.

CastPattern / Cast

0

Cast

Replaces a type-preserving Cast(to=T) with Identity.

CastCastPattern / CastCast

1

Cast

Collapses two consecutive compatible Cast nodes.

CastCastBinaryPattern / CastCastBinary

1

Add, Div, Mul, Sub

Moves matching floating-point input Cast nodes after a binary operation.

CastLayerNormalizationCastPattern / CastLayerNormalizationCast

1

GroupNormalization, LayerNormalization, RMSNormalization, SimplifiedLayerNormalization

Removes redundant casts surrounding LayerNormalization.

CastOpCastPattern / CastOpCast

1

Add, Div, Mul, MulSigmoid, Neg, Sigmoid, Softmax, Sub

Moves a unary or binary operation to the result Cast type.

ClipClipPattern / ClipClip

1

Clip

Merges two consecutive Clip nodes with complementary bounds.

ConcatEmptyPattern / ConcatEmpty

0

Concat

Drops empty inputs from a Concat node, reducing it to an Identity when a single input remains.

ConcatGatherPattern / ConcatGather

0

Gather

Rewrites a Gather reading a single Concat input into a Gather on that input directly.

ConcatReshapePattern / ConcatReshape

0

Reshape

Simplifies concatenations that construct reshape shapes.

ConcatTwiceUnaryPattern / ConcatTwiceUnary

0

Concat

Pushes a shape-preserving unary op ahead of a Concat(x, x) so the unary op runs once on x.

ConstantToInitializerPattern / ConstantToInitializer

1

Constant

Replaces a Constant node by an initializer and an Identity node.

ConvBiasNullPattern / ConvBiasNull

0

Conv

Removes a null (all-zero) bias input from a Conv node.

DropoutPattern / Dropout

1

Dropout

Replaces an inference Dropout by an Identity node when its mask output is unused and training mode is disabled.

ExpandPattern / Expand

0

Expand

Replaces Expand(x, shape) with Identity(x) when the target shape equals the input shape.

ExpandBroadcastPattern / ExpandBroadcast

0

Expand

Drops an Expand feeding an element-wise binary operator that already broadcasts the pre-expanded input.

ExpandSwapPattern / ExpandSwap

0

Expand

Moves an Expand past a following unary-like operator so the operator runs on the smaller tensor.

ExpandUnsqueezeExpandPattern / ExpandUnsqueezeExpand

0

Expand

Fuses Expand, Unsqueeze and Expand into a single Unsqueeze followed by one Expand.

FunctionAttentionPattern / FunctionAttention

0

Softmax

Replaces a scaled dot-product attention subgraph.

FunctionAttentionGQAPattern / FunctionAttentionGQA

1

Replaces grouped-query attention expressed with local functions.

FunctionCausalMaskPattern / FunctionCausalMask

1

Greater, LessOrEqual

Replaces a causal-mask subgraph with a local function.

FunctionCausalMaskMulAddPattern / FunctionCausalMaskMulAdd

1

Add

Fuses scaling and offset operations into a causal-mask function.

FunctionCosSinCachePattern / FunctionCosSinCache

1

Cos

Replaces cosine and sine cache construction with a local function.

FunctionHalfRotaryEmbeddingPattern / FunctionHalfRotaryEmbedding

1

Split

Replaces half-rotary embedding construction with a local function.

GatherConcatPattern / GatherConcat

0

Gather

Merges a Concat of single-index Gather nodes on a shared input into one Gather node.

GatherGatherPattern / GatherGather

0

Gather

Collapses two consecutive scalar Gather nodes into a single Gather node.

GatherShapePattern / GatherShape

0

Gather

Rewrites a Gather of a scalar index over a Shape node into a narrowed Shape node.

GathersSplitPattern / GathersSplit

0

Gather

Replaces sibling Gather nodes selecting contiguous single indices of a shared input by a single Split node.

GeluPattern / Gelu

0

Mul

Fuses a GELU activation subgraph.

GemmTransposePattern / GemmTranspose

1

Gemm

Folds input transposes into a Gemm operation.

IdentityPattern / Identity

0

Add, And, BatchNormalization, Div, Expand, Mul, Or, Reshape, Slice, Sub, Transpose

Replaces no-op arithmetic and layout operations by an Identity node.

LayerNormalizationPattern / LayerNormalization

1

ReduceMean

Fuses a layer-normalization subgraph.

LayerNormalizationScalePattern / LayerNormalizationScale

1

LayerNormalization

Fuses layer normalization with its scale.

LeakyReluPattern / LeakyRelu

0

Where

Fuses a LeakyRelu activation subgraph.

MatMulAddPattern / MatMulAdd

3

Gemm, MatMul

Replaces a compatible MatMul and Add with Gemm.

MatMulReshape2Of3Pattern / MatMulReshape2Of3

1

FusedMatMul, MatMul

Simplifies compatible reshapes around MatMul.

MaxReluPattern / MaxRelu

1

Max

Replaces a compatible maximum with Relu.

MulMulMatMulPattern / MulMulMatMul

1

MatMul

Moves compatible scalar multiplications across MatMul.

MulMulMulScalarPattern / MulMulMulScalar

0

Div, Mul

Combines scalar factors in multiplication chains.

MulUnsqueezeUnsqueezePattern / MulUnsqueezeUnsqueeze

0

Mul

Simplifies multiplication of unsqueezed inputs.

NotNotPattern / NotNot

1

Not

Fuses two consecutive Not nodes into an Identity node.

NotWherePattern / NotWhere

0

Where

Rewrites Where(Not(c), x, y) into Where(c, y, x).

PadConvPattern / PadConv

0

Conv

Folds a Pad node into the pads attribute of a following Conv node.

RMSNormalizationPattern / RMSNormalization

1

ReduceMean

Fuses an RMS-normalization subgraph.

RMSNormalizationMulPattern / RMSNormalizationMul

1

RMSNormalization

Fuses RMS normalization with a following scale.

ReduceArgTopKPattern / ReduceArgTopK

0

ArgMax, ArgMin

Simplifies compatible reduction, arg, and TopK operations.

ReduceReshapePattern / ReduceReshape

0

ReduceL1, ReduceL2, ReduceLogSum, ReduceLogSumExp, ReduceMax, ReduceMean, ReduceMin, ReduceProd, ReduceSum, ReduceSumSquare

Simplifies reshape operations around reductions.

ReduceSumNormalizePattern / ReduceSumNormalize

0

ReduceSum

Simplifies reduce-sum normalization subgraphs.

ReshapePattern / Reshape

0

Reshape

Removes or simplifies redundant reshape operations.

Reshape2Of3Pattern / Reshape2Of3

0

Add, And, BitwiseAnd, BitwiseOr, BitwiseXor, Div, Max, Mean, Min, Mod, Mul, Or, Sub, Sum, Xor

Simplifies two compatible reshapes among three branches.

ReshapeMatMulReshapePattern / ReshapeMatMulReshape

1

MatMul

Simplifies reshape, MatMul, and reshape sequences.

ReshapeReshapePattern / ReshapeReshape

0

Reshape

Collapses consecutive compatible reshapes.

ReshapeReshapeBinaryPattern / ReshapeReshapeBinary

0

Add, And, BitwiseAnd, BitwiseOr, BitwiseXor, Div, Max, Mean, Min, Mod, Mul, Or, Sub, Sum, Xor

Moves compatible reshapes across binary operations.

ReshapeSqueezePattern / ReshapeSqueeze

0

Squeeze

Simplifies a reshape followed by squeeze.

RotaryConcatPartPattern / RotaryConcatPart

1

Add

Simplifies padded rotary concatenation subgraphs.

RotaryEmbeddingPattern / RotaryEmbedding

1

HalfRotaryEmbedding

Fuses a complete rotary-embedding subgraph.

SameChildrenPattern / SameChildren

0

Eliminates equivalent child computations.

SameChildrenFromInputPattern / SameChildrenFromInput

0

Eliminates equivalent computations from one input.

SequenceConstructAtPattern / SequenceConstructAt

0

SequenceConstruct

Replaces a SequenceAt reading a constant index of a SequenceConstruct by the corresponding input tensor.

ShapeBasedConcatExpandPattern / ShapeBasedConcatExpand

0

Expand

Simplifies a dynamic Concat target when Expand changes one dimension.

ShapeBasedEditDistanceReshapePattern / ShapeBasedEditDistanceReshape

0

Reshape

Rewrites reshapes according to the distance between known shapes.

ShapeBasedExpandBroadcastPattern / ShapeBasedExpandBroadcast

0

Add, And, Div, Equal, Greater, GreaterOrEqual, Less, LessOrEqual, Mod, Mul, Or, Sub, Xor

Removes dynamic Expand nodes before a broadcasting binary operator.

ShapeBasedExpandBroadcastMatMulPattern / ShapeBasedExpandBroadcastMatMul

0

MatMul

Removes dynamic Expand nodes from the batch dimensions of MatMul.

ShapeBasedExpandCastWhereSwapPattern / ShapeBasedExpandCastWhereSwap

0

Where

Moves an Expand after a compatible Cast and Where chain.

ShapeBasedExpandSwapPattern / ShapeBasedExpandSwap

0

Add, And, Div, Equal, Greater, GreaterOrEqual, Less, LessOrEqual, Mod, Mul, Or, Sub, Xor

Moves input Expand nodes after a broadcasting binary operator.

ShapeBasedIdentityPattern / ShapeBasedIdentity

0

Slice, Transpose

Eliminates shape-proven identity operations.

ShapeBasedMatMulToMulPattern / ShapeBasedMatMulToMul

1

MatMul

Replaces shape-proven scalar MatMul with Mul.

ShapeBasedReshapeIsSqueezePattern / ShapeBasedReshapeIsSqueeze

0

Expand, Reshape

Replaces eligible reshapes with squeeze.

ShapeBasedSameChildrenPattern / ShapeBasedSameChildren

0

Expand, Reshape

Eliminates shape-equivalent child computations.

ShapeBasedShapeShapeAddPattern / ShapeBasedShapeShapeAdd

0

Add

Exposes the upstream placeholder for additions of two Shape outputs.

ShapeBasedStaticExpandPattern / ShapeBasedStaticExpand

0

Expand

Replaces a dynamic Expand target with an equivalent constant target.

ShapeTransposePattern / ShapeTranspose

0

Shape

Rewrites Shape(Transpose(X, perm)) into Gather(Shape(X), perm).

ShapedBasedReshapePattern / ShapedBasedReshape

0

Reshape

Simplifies reshapes using inferred input and output shapes.

SliceSlicePattern / SliceSlice

0

Slice

Merges two consecutive Slice nodes on distinct axes into one Slice.

SlicesSplitPattern / SlicesSplit

0

Slice

Replaces sibling Slice nodes cutting a shared input into contiguous chunks along one axis by a single Split node.

SoftmaxCrossEntropyLossCastPattern / SoftmaxCrossEntropyLossCast

0

Div

Moves a compatible label cast into SoftmaxCrossEntropyLoss.

SplitConcatPattern / SplitConcat

0

Split

Replaces a Split immediately followed by a Concat that restores the original tensor with an Identity node.

SplitToSequenceSequenceAtPattern / SplitToSequenceSequenceAt

0

SplitToSequence

Replaces a SequenceAt reading a constant index of a SplitToSequence by a single Split output.

SqueezeAddPattern / SqueezeAdd

0

Add

Moves compatible squeeze operations across addition.

SqueezeBinaryUnsqueezePattern / SqueezeBinaryUnsqueeze

0

Unsqueeze

Simplifies squeeze, binary operation, and unsqueeze sequences.

SqueezeUnsqueezePattern / SqueezeUnsqueeze

0

Squeeze, Unsqueeze

Simplifies a Squeeze/Unsqueeze pair into an Identity or a single Squeeze.

StaticConcatReshapePattern / StaticConcatReshape

0

Reshape

Folds static concatenated reshape shapes.

Sub1MulPattern / Sub1Mul

0

Mul

Simplifies multiplication involving one minus a value.

SwapExpandReshapePattern / SwapExpandReshape

0

Reshape

Swaps a supported Expand and constant-shape Reshape pair.

SwapExpandUnsqueezePattern / SwapExpandUnsqueeze

0

Expand

Swaps Expand and a following Unsqueeze so the Unsqueeze runs on the smaller, pre-expansion tensor.

SwapRangeAddScalarPattern / SwapRangeAddScalar

0

Range

Moves scalar addition into compatible range operations.

SwapUnaryPattern / SwapUnary

0

Reshape, Squeeze, Transpose, Unsqueeze

Swaps compatible unary operations.

SwapUnsqueezeTransposePattern / SwapUnsqueezeTranspose

0

Transpose

Swaps compatible unsqueeze and transpose operations.

SwitchOrderBinaryPattern / SwitchOrderBinary

0

Add, Mul

Reorders compatible consecutive binary operations.

SwitchReshapeActivationPattern / SwitchReshapeActivation

1

Cos, Cosh, Elu, Erf, Exp, Gelu, LeakyRelu, Relu, Selu, Sin, Sinh, Softplus, Tan, Tanh

Moves compatible activations before Reshape.

TransposeEqualReshapePattern / TransposeEqualReshape

0

Transpose

Replaces shape-equivalent transposes with reshapes.

TransposeGatherPattern / TransposeGather

0

Gather

Removes or reorders a Transpose feeding a Gather with a scalar index.

TransposeMatMulPattern / TransposeMatMul

1

Gemm, MatMul

Folds compatible transposes into MatMul.

TransposeReshapeMatMulPattern / TransposeReshapeMatMul

1

MatMul

Simplifies transpose and reshape inputs to MatMul.

TransposeReshapeTransposePattern / TransposeReshapeTranspose

0

Transpose

Simplifies transpose, reshape, and transpose sequences.

TransposeTransposePattern / TransposeTranspose

0

Transpose

Merges two consecutive Transpose nodes into a single Transpose or an Identity when the permutations cancel out.

UnsqueezeEqualPattern / UnsqueezeEqual

0

Equal

Rewrites Equal(Unsqueeze(x), Unsqueeze(y)) into Equal(x, y) when both Unsqueeze nodes use matching constant axes.

UnsqueezeOrSqueezeReshapePattern / UnsqueezeOrSqueezeReshape

0

Reshape

Simplifies reshape operations adjacent to squeeze or unsqueeze.

UnsqueezeReshapePattern / UnsqueezeReshape

0

Reshape

Simplifies an unsqueeze followed by reshape.

UnsqueezeShapePattern / UnsqueezeShape

0

Unsqueeze

Rewrites Shape(Unsqueeze(X, axes)) into a Concat of ranged Shape slices interleaved with constant [1] tensors.

UnsqueezeUnsqueezePattern / UnsqueezeUnsqueeze

0

Unsqueeze

Merges two consecutive Unsqueeze nodes into a single Unsqueeze.

WhereAddPattern / WhereAdd

0

Where

Factors a common additive term from Where branches built with Add.

The runtime list is available through standard_pattern_names().

See How to add a custom graph-rewriting pattern and set its priority for a Python/C++ how-to on writing a custom pattern and choosing its priority, and Optimizing a model with graph-rewriting patterns for a runnable example covering statistics and replay.

Custom Python pattern#

import onnx_light.onnx.helper as oh
from onnx_light.onnx_core.optimization import (
    GraphBuilder,
    GraphGraph,
    PatternOptimization,
)

class NegNegPattern(PatternOptimization):
    def __init__(self):
        super().__init__(priority=1, name="NegNeg")

    def fast_op_type(self):
        return {"Neg"}

    def match(self, graph, node):
        previous = graph.node_before(node.input[0])
        if previous is None or previous.op_type != "Neg":
            return self.no_match(node, "input is not produced by Neg")
        return self.result([previous, node], insert_at=node)

    def apply(self, graph, nodes):
        previous, node = nodes
        return [
            oh.make_node(
                "Identity", [previous.input[0]], list(node.output)
            )
        ]

builder = GraphBuilder(model)
builder.register_pattern(NegNegPattern())
graph = GraphGraph(builder)
rewrites = graph.optimize()

API#

Graph-pattern optimization with standard and Python-defined patterns.

class onnx_light.onnx_core.optimization.AttentionGQAPattern(*args, **kwargs)#

Fuses grouped-query attention cache handling.

class onnx_light.onnx_core.optimization.BatchNormalizationPattern(*args, **kwargs)#

Fuses an inference batch-normalization subgraph.

class onnx_light.onnx_core.optimization.BatchNormalizationTrainingPattern(*args, **kwargs)#

Fuses a training batch-normalization subgraph.

class onnx_light.onnx_core.optimization.CastCastBinaryPattern(*args, **kwargs)#

Moves matching floating-point input Cast nodes after a binary operation.

Cast(x), Cast(y) -> Binary becomes Binary(x, y) -> Cast when precision and use guards allow it.

class onnx_light.onnx_core.optimization.CastCastPattern(*args, **kwargs)#

Collapses two consecutive compatible Cast nodes.

x:A -> Cast(B) -> Cast(C) -> y:C becomes one safe Cast(C) or Identity.

class onnx_light.onnx_core.optimization.CastLayerNormalizationCastPattern(*args, **kwargs)#

Removes redundant casts surrounding LayerNormalization.

class onnx_light.onnx_core.optimization.CastOpCastPattern(*args, **kwargs)#

Moves a unary or binary operation to the result Cast type.

Compatible input Cast nodes and the trailing result Cast are removed or relocated while preserving shared outputs.

class onnx_light.onnx_core.optimization.CastPattern(*args, **kwargs)#

Replaces a type-preserving Cast(to=T) with Identity.

x:T -> Cast(to=T) -> y:T becomes x:T -> Identity -> y:T.

class onnx_light.onnx_core.optimization.ClipClipPattern(*args, **kwargs)#

Merges two consecutive Clip nodes with complementary bounds.

Clip(x, min) -> Clip(x1, , max) becomes one Clip(x, min, max) when one Clip defines the minimum and the other the maximum.

class onnx_light.onnx_core.optimization.ConcatEmptyPattern(*args, **kwargs)#

Drops empty inputs from a Concat node, reducing it to an Identity when a single input remains.

class onnx_light.onnx_core.optimization.ConcatGatherPattern(*args, **kwargs)#

Rewrites a Gather reading a single Concat input into a Gather on that input directly.

class onnx_light.onnx_core.optimization.ConcatReshapePattern(*args, **kwargs)#

Simplifies concatenations that construct reshape shapes.

class onnx_light.onnx_core.optimization.ConcatTwiceUnaryPattern(*args, **kwargs)#

Pushes a shape-preserving unary op ahead of a Concat(x, x) so the unary op runs once on x.

class onnx_light.onnx_core.optimization.ConstantToInitializerPattern(*args, **kwargs)#

Replaces a Constant node by an initializer and an Identity node.

class onnx_light.onnx_core.optimization.ConvBiasNullPattern(*args, **kwargs)#

Removes a null (all-zero) bias input from a Conv node.

class onnx_light.onnx_core.optimization.DropoutPattern(*args, **kwargs)#

Replaces an inference Dropout by an Identity node when its mask output is unused and training mode is disabled.

class onnx_light.onnx_core.optimization.FunctionAttentionGQAPattern(*args, **kwargs)#

Replaces grouped-query attention expressed with local functions.

class onnx_light.onnx_core.optimization.FunctionAttentionPattern(*args, **kwargs)#

Replaces a scaled dot-product attention subgraph.

class onnx_light.onnx_core.optimization.FunctionCausalMaskMulAddPattern(*args, **kwargs)#

Fuses scaling and offset operations into a causal-mask function.

class onnx_light.onnx_core.optimization.FunctionCausalMaskPattern(*args, **kwargs)#

Replaces a causal-mask subgraph with a local function.

class onnx_light.onnx_core.optimization.FunctionCosSinCachePattern(*args, **kwargs)#

Replaces cosine and sine cache construction with a local function.

class onnx_light.onnx_core.optimization.FunctionHalfRotaryEmbeddingPattern(*args, **kwargs)#

Replaces half-rotary embedding construction with a local function.

class onnx_light.onnx_core.optimization.GatherConcatPattern(*args, **kwargs)#

Merges a Concat of single-index Gather nodes on a shared input into one Gather node.

class onnx_light.onnx_core.optimization.GatherGatherPattern(*args, **kwargs)#

Collapses two consecutive scalar Gather nodes into a single Gather node.

class onnx_light.onnx_core.optimization.GatherShapePattern(*args, **kwargs)#

Rewrites a Gather of a scalar index over a Shape node into a narrowed Shape node.

class onnx_light.onnx_core.optimization.GathersSplitPattern(*args, **kwargs)#

Replaces sibling Gather nodes selecting contiguous single indices of a shared input by a single Split node.

class onnx_light.onnx_core.optimization.GeluPattern(*args, **kwargs)#

Fuses a GELU activation subgraph.

class onnx_light.onnx_core.optimization.GemmTransposePattern(*args, **kwargs)#

Folds input transposes into a Gemm operation.

class onnx_light.onnx_core.optimization.GraphBuilder(name: str = 'graph', schema_lookup: ~collections.abc.Callable[[str], list[~onnx_light.onnx_py._onnxpyprotoop.onnx_op.LightOpSchema]] | None = <function _default_schema_lookup>)#

Incrementally builds an ONNX graph, model or function.

See onnx_light.onnx_core.graph_builder for details. By default the builder validates nodes and resolves opsets using the built-in ONNX operator schemas; pass schema_lookup=None to disable this, or a custom op_type -> list[LightOpSchema] callable to use different schemas.

clear_registered_patterns() None#

Removes every builder-local pattern.

register_pattern(pattern: PatternOptimization) None#

Registers or replaces a pattern for this builder.

registered_pattern_names() tuple[str, ...]#

Returns builder-local pattern names in registration order.

registered_patterns() tuple[PatternOptimization, ...]#

Returns builder-local patterns in registration order.

unregister_pattern(name: str) bool#

Removes a builder-local pattern and returns whether it existed.

class onnx_light.onnx_core.optimization.GraphGraph(builder: GraphBuilder, patterns: Iterable[str | PatternOptimization] | None = None, *, use_global_patterns: bool = True)#

Indexes a builder and runs globally or locally registered patterns.

Global patterns are applied first. Patterns registered on builder then replace global patterns sharing their name, and patterns passed here have the highest precedence. Set use_global_patterns=False to start from an empty registry.

class onnx_light.onnx_core.optimization.IdentityPattern(*args, **kwargs)#

Replaces no-op arithmetic and layout operations by an Identity node.

class onnx_light.onnx_core.optimization.LayerNormalizationPattern(*args, **kwargs)#

Fuses a layer-normalization subgraph.

class onnx_light.onnx_core.optimization.LayerNormalizationScalePattern(*args, **kwargs)#

Fuses layer normalization with its scale.

class onnx_light.onnx_core.optimization.LeakyReluPattern(*args, **kwargs)#

Fuses a LeakyRelu activation subgraph.

class onnx_light.onnx_core.optimization.MatMulAddPattern(*args, **kwargs)#

Replaces a compatible MatMul and Add with Gemm.

class onnx_light.onnx_core.optimization.MatMulReshape2Of3Pattern(*args, **kwargs)#

Simplifies compatible reshapes around MatMul.

class onnx_light.onnx_core.optimization.MaxReluPattern(*args, **kwargs)#

Replaces a compatible maximum with Relu.

class onnx_light.onnx_core.optimization.MulMulMatMulPattern(*args, **kwargs)#

Moves compatible scalar multiplications across MatMul.

class onnx_light.onnx_core.optimization.MulMulMulScalarPattern(*args, **kwargs)#

Combines scalar factors in multiplication chains.

class onnx_light.onnx_core.optimization.MulUnsqueezeUnsqueezePattern(*args, **kwargs)#

Simplifies multiplication of unsqueezed inputs.

class onnx_light.onnx_core.optimization.NotNotPattern(*args, **kwargs)#

Fuses two consecutive Not nodes into an Identity node.

class onnx_light.onnx_core.optimization.NotWherePattern(*args, **kwargs)#

Rewrites Where(Not(c), x, y) into Where(c, y, x).

class onnx_light.onnx_core.optimization.PadConvPattern(*args, **kwargs)#

Folds a Pad node into the pads attribute of a following Conv node.

class onnx_light.onnx_core.optimization.RMSNormalizationMulPattern(*args, **kwargs)#

Fuses RMS normalization with a following scale.

class onnx_light.onnx_core.optimization.RMSNormalizationPattern(*args, **kwargs)#

Fuses an RMS-normalization subgraph.

class onnx_light.onnx_core.optimization.ReduceArgTopKPattern(*args, **kwargs)#

Simplifies compatible reduction, arg, and TopK operations.

class onnx_light.onnx_core.optimization.ReduceReshapePattern(*args, **kwargs)#

Simplifies reshape operations around reductions.

class onnx_light.onnx_core.optimization.ReduceSumNormalizePattern(*args, **kwargs)#

Simplifies reduce-sum normalization subgraphs.

class onnx_light.onnx_core.optimization.Reshape2Of3Pattern(*args, **kwargs)#

Simplifies two compatible reshapes among three branches.

class onnx_light.onnx_core.optimization.ReshapeMatMulReshapePattern(*args, **kwargs)#

Simplifies reshape, MatMul, and reshape sequences.

class onnx_light.onnx_core.optimization.ReshapePattern(*args, **kwargs)#

Removes or simplifies redundant reshape operations.

class onnx_light.onnx_core.optimization.ReshapeReshapeBinaryPattern(*args, **kwargs)#

Moves compatible reshapes across binary operations.

class onnx_light.onnx_core.optimization.ReshapeReshapePattern(*args, **kwargs)#

Collapses consecutive compatible reshapes.

class onnx_light.onnx_core.optimization.ReshapeSqueezePattern(*args, **kwargs)#

Simplifies a reshape followed by squeeze.

class onnx_light.onnx_core.optimization.RotaryConcatPartPattern(*args, **kwargs)#

Simplifies padded rotary concatenation subgraphs.

class onnx_light.onnx_core.optimization.RotaryEmbeddingPattern(*args, **kwargs)#

Fuses a complete rotary-embedding subgraph.

class onnx_light.onnx_core.optimization.SameChildrenFromInputPattern(*args, **kwargs)#

Eliminates equivalent computations from one input.

class onnx_light.onnx_core.optimization.SameChildrenPattern(*args, **kwargs)#

Eliminates equivalent child computations.

class onnx_light.onnx_core.optimization.SequenceConstructAtPattern(*args, **kwargs)#

Replaces a SequenceAt reading a constant index of a SequenceConstruct by the corresponding input tensor.

class onnx_light.onnx_core.optimization.ShapeBasedEditDistanceReshapePattern(*args, **kwargs)#

Rewrites reshapes according to the distance between known shapes.

class onnx_light.onnx_core.optimization.ShapeBasedIdentityPattern(*args, **kwargs)#

Eliminates shape-proven identity operations.

class onnx_light.onnx_core.optimization.ShapeBasedMatMulToMulPattern(*args, **kwargs)#

Replaces shape-proven scalar MatMul with Mul.

class onnx_light.onnx_core.optimization.ShapeBasedReshapeIsSqueezePattern(*args, **kwargs)#

Replaces eligible reshapes with squeeze.

class onnx_light.onnx_core.optimization.ShapeBasedSameChildrenPattern(*args, **kwargs)#

Eliminates shape-equivalent child computations.

class onnx_light.onnx_core.optimization.ShapeBasedShapeShapeAddPattern(*args, **kwargs)#

Exposes the upstream placeholder for additions of two Shape outputs.

class onnx_light.onnx_core.optimization.ShapedBasedReshapePattern(*args, **kwargs)#

Simplifies reshapes using inferred input and output shapes.

class onnx_light.onnx_core.optimization.SliceSlicePattern(*args, **kwargs)#

Merges two consecutive Slice nodes on distinct axes into one Slice.

class onnx_light.onnx_core.optimization.SlicesSplitPattern(*args, **kwargs)#

Replaces sibling Slice nodes cutting a shared input into contiguous chunks along one axis by a single Split node.

class onnx_light.onnx_core.optimization.SoftmaxCrossEntropyLossCastPattern(*args, **kwargs)#

Moves a compatible label cast into SoftmaxCrossEntropyLoss.

class onnx_light.onnx_core.optimization.SplitConcatPattern(*args, **kwargs)#

Replaces a Split immediately followed by a Concat that restores the original tensor with an Identity node.

class onnx_light.onnx_core.optimization.SplitToSequenceSequenceAtPattern(*args, **kwargs)#

Replaces a SequenceAt reading a constant index of a SplitToSequence by a single Split output.

class onnx_light.onnx_core.optimization.SqueezeAddPattern(*args, **kwargs)#

Moves compatible squeeze operations across addition.

class onnx_light.onnx_core.optimization.SqueezeBinaryUnsqueezePattern(*args, **kwargs)#

Simplifies squeeze, binary operation, and unsqueeze sequences.

class onnx_light.onnx_core.optimization.StaticConcatReshapePattern(*args, **kwargs)#

Folds static concatenated reshape shapes.

class onnx_light.onnx_core.optimization.Sub1MulPattern(*args, **kwargs)#

Simplifies multiplication involving one minus a value.

class onnx_light.onnx_core.optimization.SwapRangeAddScalarPattern(*args, **kwargs)#

Moves scalar addition into compatible range operations.

class onnx_light.onnx_core.optimization.SwapUnaryPattern(*args, **kwargs)#

Swaps compatible unary operations.

class onnx_light.onnx_core.optimization.SwapUnsqueezeTransposePattern(*args, **kwargs)#

Swaps compatible unsqueeze and transpose operations.

class onnx_light.onnx_core.optimization.SwitchOrderBinaryPattern(*args, **kwargs)#

Reorders compatible consecutive binary operations.

class onnx_light.onnx_core.optimization.SwitchReshapeActivationPattern(*args, **kwargs)#

Moves compatible activations before Reshape.

class onnx_light.onnx_core.optimization.TransposeEqualReshapePattern(*args, **kwargs)#

Replaces shape-equivalent transposes with reshapes.

class onnx_light.onnx_core.optimization.TransposeMatMulPattern(*args, **kwargs)#

Folds compatible transposes into MatMul.

class onnx_light.onnx_core.optimization.TransposeReshapeMatMulPattern(*args, **kwargs)#

Simplifies transpose and reshape inputs to MatMul.

class onnx_light.onnx_core.optimization.TransposeReshapeTransposePattern(*args, **kwargs)#

Simplifies transpose, reshape, and transpose sequences.

class onnx_light.onnx_core.optimization.UnsqueezeEqualPattern(*args, **kwargs)#

Rewrites Equal(Unsqueeze(x), Unsqueeze(y)) into Equal(x, y) when both Unsqueeze nodes use matching constant axes.

class onnx_light.onnx_core.optimization.UnsqueezeOrSqueezeReshapePattern(*args, **kwargs)#

Simplifies reshape operations adjacent to squeeze or unsqueeze.

class onnx_light.onnx_core.optimization.UnsqueezeReshapePattern(*args, **kwargs)#

Simplifies an unsqueeze followed by reshape.

class onnx_light.onnx_core.optimization.WhereAddPattern(*args, **kwargs)#

Factors a common additive term from Where branches built with Add.

onnx_light.onnx_core.optimization.clear_registered_patterns() None#

Removes every globally registered pattern, including standard patterns.

onnx_light.onnx_core.optimization.register_pattern(pattern: PatternOptimization) None#

Registers or replaces a process-global pattern.

onnx_light.onnx_core.optimization.registered_pattern_names() tuple[str, ...]#

Returns global pattern names in registration order.

onnx_light.onnx_core.optimization.registered_patterns() tuple[PatternOptimization, ...]#

Returns global patterns in registration order.

onnx_light.onnx_core.optimization.render_rst_standard_patterns_table() str#

Renders the standard ONNX patterns as a reST list-table.

The table is generated from the patterns returned by standard_patterns(), so it stays in sync with the registered patterns without any manual maintenance.

Returns:

The list-table directive as a reST string.

onnx_light.onnx_core.optimization.replay(model: ModelProto, rewrites: Iterable[LocalRewriting], schema_lookup: SchemaLookup | None = <function _default_schema_lookup>) GraphProto#

Replays captured rewrites and returns the resulting graph.

onnx_light.onnx_core.optimization.reset_registered_patterns() None#

Restores the global registry to the standard ONNX patterns.

onnx_light.onnx_core.optimization.standard_pattern_names() list[str]#

Returns the standard ONNX pattern names.

onnx_light.onnx_core.optimization.standard_patterns(names: Iterable[str] | None = None) list[PatternOptimization]#

Creates the selected standard ONNX patterns.

onnx_light.onnx_core.optimization.unregister_pattern(name: str) bool#

Removes a global pattern and returns whether it existed.