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

Independent model deltas#

diff_models(canonical, target, base_dir=..., target_dir=...) constructs a RewritingProto from independently produced models. Comparison and replay run in C++ (GraphBuilder::DiffRewritings); consumers do not implement a graph diff or a replay engine. The returned plan already contains validated native hash_before and hash_after compatibility hashes and can be passed directly to apply_rewritings_to_model().

source_builder.diff_rewritings(target) also accepts a ModelProto or a root GraphBuilder. The native builder overload compares both existing builders directly without exporting and reimporting the target, and preserves both builders.

Nested rewrites and reference attributes.

Nested inference must observe enclosing target values and descriptors together. The previously fixed parent-initializer/If case remains covered by test_nested_edits_use_replaced_parent_initializer_values. The additional test_deep_captures_use_replaced_values_and_descriptors regression covers two If levels, multiple captures, graph-input defaults, unchanged consumers, and rewritten enclosing producers. Constant replacement and Constant-to-initializer replacement reproduced stale captured shapes; affected enclosing nodes are now replaced atomically, and nested builders are imported after the existing incremental refresh publishes the target producer state. Loop body inputs retain their inferred descriptors as well. Source and target builders remain unchanged.

Only resolved internal subgraph references are normalized during matching. Declared semantic STRING/STRINGS attributes remain exact, including Quantize’s parameter_ref; generated internal aliases avoid semantic attribute names. Harmless GRAPH/GRAPHS renaming is still supported. Structural compatibility hashes intentionally omit ordinary attribute payloads, so equal hashes do not establish semantic equivalence. Final verification compares materialized node attributes independently of the matching skeleton. The numerical test_model_diff_preserves_semantic_parameter_reference regression uses two valid parameter sets with different scales: the previously empty diff must now reconstruct the target reference and its different numerical result.

Named plans use the existing ModelProto.rewritings representation:

import onnx_light.onnx as onnx
from onnx_light.onnx_core.optimization import (
    apply_rewritings_to_model,
    diff_models,
)

canonical.add_rewriting("cpu", diff_models(canonical, cpu_model))
canonical.add_rewriting("cuda", diff_models(canonical, cuda_model))
onnx.save(canonical, "artifacts/model.onnx")
loaded = onnx.load("artifacts/model.onnx")
cpu = apply_rewritings_to_model(loaded, loaded.rewritings["cpu"])
cuda = apply_rewritings_to_model(loaded, loaded.rewritings["cuda"])

No proto fields or separate reference-file format are introduced. A target is reconstructed from its source model and a list of native rewrites, with normal pre/post structural-hash validation and no fallback.

Dense initializer additions, removals, same-name replacements and ordering, node ordering, empty optional output slots, nested graph edits, and local function additions are supported. Initializer permutations are ordinary rewrites whose initializer_calls invoke a local forwarding function and whose computed_initializer_positions specify the resulting positions. Forwarding functions with no nodes and outputs referring directly to their inputs reuse the tensor storage and external byte ranges without materialization. New forwarding functions needed only during initializer preparation remain in the rewriting plan, not in the reconstructed model. Functions also used by runtime nodes are retained.

Two different definitions of the same local-function identity (domain, name, overload) are unsupported and raise an error. Removing local functions or reordering graph-input defaults is also unsupported.

Input/output names, shapes, dtypes, effective value tags, persistent bindings, sparse/extended initializer declarations, model declarations and user metadata must remain compatible. Existing opset versions cannot be removed or changed; external sparse, encoded and paged-cache declarations additionally require matching source storage directories during diff construction. New required domains are added to the plan. Unsupported differences raise explicit errors rather than adapting interfaces (in particular, no implicit FP32/FP16 conversion). Model-declaration errors list every differing declaration and opset with its source and target values; interface errors name each mismatched input or output with both tensor types. Producer strings, graph/node names and documentation, inferred intermediate value-info shapes, and regenerated lifetime, constant, in-place and peak-memory annotations are nonsemantic diagnostics. Effective value tags are compared, not ignored. Changes to other intermediate value metadata are rejected.

Comparison indexes nodes and initializers by identity rather than performing quadratic sequence alignment. Equal external references with equal metadata reuse the source storage without opening payload files. base_dir and target_dir locate the respective external payloads when comparing different references, including identical filenames in different directories. Such comparisons materialize the two compared tensors, not the complete checkpoint. This storage-aware comparison also covers tensor attributes, local-function bodies and defaults. Sparse initializer contents must also match; changes are rejected. Deferred tensors reuse a fast path when their backing-file snapshots and byte ranges match, even across independent loads. Copied deferred payloads are loaded and detached so the plan can be serialized independently. Changed or new dense initializers, tensor attributes and nested graph weights from another directory are embedded in the plan so replay cannot accidentally read the source model’s file with the same name. Retained external references preserve their locations, offsets and lengths; callers must keep their backing files available. Replay performs the usual graph import, shape inference and validation; constants needed for inference may therefore require materialization.

Optimization callback#

Pass callback(graph, match) to optimize to report eligible pattern matches or reject individual matches before their nodes are reserved:

def on_match(graph, match):
    print(graph.name, match.pattern.name, len(match.nodes))
    return match.pattern.name != "TransposeTranspose"

rewrites = graph.optimize(callback=on_match)

Returning False rejects the match; True or None accepts it. Rejected matches do not block other patterns from matching the same nodes. The callback also runs in nested subgraphs, receiving the corresponding graph. It observes matches, not completed rewrites: accepted matches are revalidated before application and matches may be reconsidered in subsequent iterations. Cleanup passes are unaffected, and no callback runs when there are no eligible matches. Callback exceptions propagate.

The callback must not mutate the graph, patterns, or match, or retain borrowed graph or node references beyond the call. C++ callers can pass an OptimizationCallback as the fifth argument to GraphGraph::Optimize; its boolean result accepts or rejects the match.

Parallel matching#

Pass parallel=True to match explicitly opted-in native patterns concurrently when at least 32 candidates are eligible. Match acceptance, optimization callbacks and rewrite application remain sequential and preserve candidate order. Python-defined patterns are always matched sequentially on the calling thread. Mixing native and Python-defined patterns does not disable native parallel matching. The C++ parallel argument follows the optional optimization callback.

Native patterns are sequential by default. CanMatchInParallel may return true only after auditing the matcher’s state and every graph query it uses for concurrent access. Matchers must not mutate the pattern, builder, graph, or shared caches. TransposeTranspose opts in because it reads only immutable nodes and the frozen structural index; unaudited patterns remain sequential.

Pattern registration#

The available patterns are merged by their stable PatternOptimization.name. Builder registrations replace global entries with the same name before selection:

Scope

Registration

Selection

Global

register_pattern()

Makes a pattern available for selection. 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=[...])

Selects only the given names and instances. Explicit instances override earlier entries with the same name and are retained for that optimizer, including recursive subgraphs.

clear_registered_patterns() clears the global registry; reset_registered_patterns() restores the standard patterns.

Pattern selection#

GraphGraph(builder, patterns=...) accepts one selector:

Selector

Selected patterns

None (default)

All available device-independent patterns, even if builder.device is already set.

False or []

None, including builder registrations. The optimizer’s ordinary cleanup and constant-folding passes still run.

A concrete Device

All device-independent patterns plus those targeting that exact device. Also sets builder.device and subgraph targets. A different, already-defined device in any of those builders raises ValueError before changing any target.

A regex string or compiled re.Pattern[str]

Available names accepted by fullmatch, including device-specific patterns. Use ".*MatMul.*" to select names containing MatMul.

An iterable of names and/or pattern instances

Only those patterns, including device-specific ones. Names are exact, not regexes. Standard names may also be instantiated when absent from the global registry.

Regexes and explicit lists do not modify builder.device or automatically add global or builder patterns. A regex matching nothing selects no patterns; invalid regexes and invalid selector types raise exceptions. True is not a selector, and Device.kUndefined is not a concrete device: use None for the default selection. During recursive optimization, a subgraph with an undefined device inherits its parent builder’s device; explicitly defined subgraph devices are preserved.

import re
from onnx_light.onnx_core.shape_inference import Device

graph = GraphGraph(builder)                         # device-independent defaults
graph = GraphGraph(builder, patterns=False)         # cleanup only
graph = GraphGraph(builder, patterns=Device.kCPU)    # defaults + CPU patterns
graph = GraphGraph(builder, patterns=r".*MatMul.*")  # full name regex
graph = GraphGraph(builder, patterns=re.compile(r"Cast.*"))
graph = GraphGraph(builder, patterns=["Cast", "TransposeMatMul"])

A pattern’s device defaults to Device.kUndefined, meaning device-independent. Custom Python patterns declare a target with super().__init__(name="MyCPUFusion", device=Device.kCPU); native patterns use the corresponding third PatternOptimization constructor argument. Device equality is exact, not a GPU-family or execution-provider capability query. Existing standard patterns remain device-independent, including patterns whose matching heuristics inspect builder.device.

The former use_global_patterns argument is removed. Replace an explicit list plus use_global_patterns=False with just that list, use False for no patterns, or pass builder.registered_patterns() for builder-only patterns. To combine defaults with explicit patterns, supply the combined list explicitly, for example [*standard_patterns(), custom_pattern] (which intentionally includes every standard pattern, regardless of device).

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

LayerNormalization, RMSNormalization

Removes redundant casts surrounding LayerNormalization.

CastOpCastPattern / CastOpCast

1

Add, Div, Mul, 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.

ConcatSliceEliminationPattern / ConcatSliceElimination

0

Concat

Eliminates a Concat followed by exact slices recovering all inputs.

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.

ConvAddFusionPattern / ConvAddFusion

0

Conv

Folds a channel-wise constant Add following a Conv into the Conv bias.

ConvBatchNormalizationFusionPattern / ConvBatchNormalizationFusion

0

Conv

Folds inference BatchNormalization following a Conv into its weights and bias.

ConvBiasNullPattern / ConvBiasNull

0

Conv

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

ConvMulFusionPattern / ConvMulFusion

0

Conv

Folds a scalar or channel-wise constant Mul following a Conv into its constants.

DivMulPattern / DivMul

0

Div

Fuses multiplication by a reciprocal into one division.

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

Slice, 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.

GatherSliceToSplitPattern / GatherSliceToSplit

0

Gather, Slice

Fuses compatible sibling Gather and Slice ranges into one Split.

GatherToSlicePattern / GatherToSlice

0

Gather

Rewrites a Gather selecting a constant scalar, singleton, or arithmetic-progression index into a Slice (plus Squeeze for a scalar index).

GatherUpstreamPropagationPattern / GatherUpstreamPropagation

0

Gather

Moves a scalar or vector constant-index Gather upstream across one compatible producer (pointwise ops, Transpose, Reshape, MatMul batch dims, Softmax, LayerNormalization) so it runs on smaller tensors.

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.

GemmOutputTransposePattern / GemmOutputTranspose

1

Transpose

Folds an output transpose into an unbiased Gemm.

GemmSumFusionPattern / GemmSumFusion

2

Sum

Fuses a two-input Sum into an unbiased Gemm bias input.

GemmTransposePattern / GemmTranspose

1

Gemm

Folds input transposes into a Gemm operation.

GroupedMatMulPattern / GroupedMatMul

2

Concat, Loop, Squeeze

Fuses gathered weights, unrolled MatMuls, or a per-token Loop into GroupedMatMul.

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.

IfIdenticalBranchesPattern / IfIdenticalBranches

1

If

Inlines an If with alpha-equivalent branches.

InitializerUnsqueezeCastPattern / InitializerUnsqueezeCast

1

Cast

Folds an initializer’s Unsqueeze and Cast into the initializer consumed by Add.

LabelEncoderFusionPattern / LabelEncoderFusion

0

LabelEncoder

Composes consecutive ai.onnx.ml LabelEncoder mappings.

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.

LinearAttentionPattern / LinearAttention

2

Mul

Fuses a single-token linear-attention recurrence.

MatMulAddPattern / MatMulAdd

2

Gemm, MatMul

Replaces a compatible MatMul and Add with Gemm.

MatMulBatchNormalizationFusionPattern / MatMulBatchNormalizationFusion

2

BatchNormalization

Folds constant inference BatchNormalization parameters into a rank-two MatMul.

MatMulReshape2Of3Pattern / MatMulReshape2Of3

1

FusedMatMul, MatMul

Simplifies compatible reshapes around MatMul.

MatMulScaleFusionPattern / MatMulScaleFusion

2

MatMul

Absorbs one safe scalar Mul or Div adjacent to a rank-two 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.

PadPadFusionPattern / PadPadFusion

1

Pad

Merges adjacent constant-mode Pad nodes with equal values by summing their pads.

PadPoolPattern / PadPool

0

AveragePool, MaxPool

Folds zero-valued spatial Pad into safe AveragePool or UINT8 MaxPool.

PreShapeNodeEliminationPattern / PreShapeNodeElimination

0

Cast

Removes a Cast whose output only feeds Shape nodes, redirecting each Shape node directly to the Cast’s input.

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.

ReluClipFusionPattern / ReluClipFusion

1

Clip

Removes Relu before Clip when the Clip minimum is constant and non-negative.

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.

STFTFusionPattern / STFTFusion

0

Transpose

Fuses a canonical convolution-based DFT subgraph into one standard ONNX STFT.

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.

SliceConcatToSpaceToDepthPattern / SliceConcatToSpaceToDepth

0

Concat

Fuses canonical rank-4 phase slices and channel Concat into SpaceToDepth.

SliceEliminationPattern / SliceElimination

1

Slice

Replaces an identity full-range Slice with Identity.

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.

TreeEnsemblePattern / TreeEnsemble

1

TreeEnsembleClassifier, TreeEnsembleRegressor

Replaces a classic tree ensemble with the unified TreeEnsemble operator.

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(). Use standard_pattern_sets() to select the named all, L1, L2, and Layer groups registered by the C++ pattern registry and exposed through Python. L1 lists the fusion patterns shared with ONNX Runtime; L2 lists onnx-light-only standard patterns, and Layer selects the opt-in repeated-layer pattern. all contains the union of these groups. Add another group with register_pattern_set(), add members to any built-in group with extend_pattern_set(), and remove custom groups with unregister_pattern_set(). Groups can overlap. Members may be standard patterns or patterns previously added with register_pattern(). standard_patterns(pattern_sets["custom"]) creates the selected instances. The complete pattern catalogue in the ByOp catalogue adds the C++ documentation link and the Before/After rewrite graph for every entry.

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 optimization statistics. Replay is demonstrated separately in Replaying graph-rewriting patterns.

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):
        if node.domain not in ("", "ai.onnx"):
            return self.no_match(node, "the candidate is not in the default ONNX domain")
        previous = graph.node_before(node.input[0])
        if previous is None or previous.op_type != "Neg" or previous.domain not in ("", "ai.onnx"):
            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.ConcatSliceEliminationPattern(*args, **kwargs)#

Eliminates a Concat followed by exact slices recovering all inputs.

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.ConvAddFusionPattern(*args, **kwargs)#

Folds a channel-wise constant Add following a Conv into the Conv bias.

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

Folds inference BatchNormalization following a Conv into its weights and bias.

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.ConvMulFusionPattern(*args, **kwargs)#

Folds a scalar or channel-wise constant Mul following a Conv into its constants.

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

Fuses multiplication by a reciprocal into one division.

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.ExpandBroadcastPattern(*args, **kwargs)#

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

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

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

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

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

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

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

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.FunctionPattern(*args, **kwargs)#

Creates a fixed-arity rewrite from template and replacement ONNX functions.

apply(self, graph: onnx_light.onnx_py._onnxpycore.builder.GraphGraph, nodes: collections.abc.Sequence[onnx_light.onnx_py._onnxpyprotoop.NodeProto]) → onnx_light.onnx_py._onnxpyprotoop.RepeatedProtoFieldNodeProto#
match(self, graph: onnx_light.onnx_py._onnxpycore.builder.GraphGraph, candidate: onnx_light.onnx_py._onnxpyprotoop.NodeProto) → onnx_light.onnx_py._onnxpycore.builder.MatchResult#
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.GatherSliceToSplitPattern(*args, **kwargs)#

Fuses compatible sibling Gather and Slice ranges into one Split.

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

Rewrites a Gather selecting a constant scalar, singleton, or arithmetic-progression index into a Slice (plus Squeeze for a scalar index).

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

Moves a scalar or vector constant-index Gather upstream across one compatible producer (pointwise ops, Transpose, Reshape, MatMul batch dims, Softmax, LayerNormalization) so it runs on smaller tensors.

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.GemmOutputTransposePattern(*args, **kwargs)#

Folds an output transpose into an unbiased Gemm.

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

Fuses a two-input Sum into an unbiased Gemm bias input.

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>, *, opsets: int | dict[str, int] | None = None, local_registry: ~onnx_light.onnx_py._onnxpycore.builder.OperatorRegistry | None = None)#

Incrementally builds an ONNX graph, model or function.

See onnx_light.onnx_core.graph_builder for details. By default the builder validates nodes at selected 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. For an empty builder, opsets selects the default ONNX version (an integer) or explicit domain-to-version imports (a dictionary). The default None selects {"": onnx_opset_version()}; an empty dictionary selects no imports. When importing a model, None preserves its declared imports. local_registry snapshots operator schemas and shape callbacks before importing any node. Model-local functions take precedence over local registrations, which take precedence over global registrations. Nested builders and optimization/replay copies retain this snapshot.

clear_registered_patterns() → None#

Removes every builder-local pattern.

init(value: ndarray, name: str | None = None, *, copy: bool = True) → str#

Adds a NumPy initializer and returns its final name.

By default, copies the payload. With copy=False, borrows C-contiguous, dtype-aligned, little-endian storage without conversion. Supports bool, 8/16/32/64-bit integers, float16/32/64 and complex64/128; rejects other dtypes and layouts. Retains the array until the last borrowed payload owner releases it, including models exported from this builder.

Writable arrays remain writable and mutations are visible to all owners. The caller must finish mutations before optimization or creating an execution session, which may cache derived values, and must not resize or reallocate storage while borrowed. Use copy=True for an independent snapshot; setting an array read-only does not freeze its aliases.

inp(name: str, elem_type: int, shape: list[str | int | None]) → str#

Declares and returns a compact graph input.

property op: _OperatorProxy#

Returns the cached compact operator proxy.

out(name: str, elem_type: int | None = None, shape: list[str | int | None] | None = None) → str#

Declares and returns a compact graph output.

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.

to_onnx(kind: ProtoKind = ProtoKind.kModel, ir_version: int = 0, domain: str = '', *, optimize: OptimizationMode = OptimizationMode.kNone, canonicalize: bool = False, move_shape_and_size_nodes: bool = True) → ModelProto | GraphProto | FunctionProto#

Exports the current graph, an optimized copy, or captured rewritings.

With OptimizationMode.kNone (the default), preserves the existing export behavior. With OptimizationMode.kOptimize, optimizes an independent builder using the global and builder-local pattern registrations, then exports the result without attached plans. Model export requires standard ONNX constructs.

With OptimizationMode.kRewritings, returns the finalized reference ModelProto and attaches the captured plan as rewritings["optimized"]. canonicalize=True explicitly cleans this reference before capture; it is disabled by default. Shape/Size hoisting is enabled by default and can be disabled with move_shape_and_size_nodes=False. Requires kind=ProtoKind.kModel and rejects an existing plan with that name. Other named plans are retained unless explicit canonicalization changes their reference. Optimization never edits the source builder; export never reorders the graph.

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] | str | Pattern[str] | Device | Literal[False] | None = None)#

Indexes a builder and selects the patterns used for rewriting.

patterns=None selects all registered device-independent patterns. False selects none. A concrete Device includes independent patterns and patterns targeting that exact device, and sets builder.device; a conflicting builder device raises ValueError.

A string or compiled regex selects registered names using fullmatch. An iterable selects only its exact names and pattern instances. These explicit selections can include device-specific patterns without changing the builder device. Builder registrations override global registrations before selection; repeated explicit names keep the last instance. Disabling patterns does not disable the optimizer’s cleanup passes. Pass remove_unused=False to optimize() to disable only the unused-node cleanup pass; other cleanup passes may still remove nodes or initializers.

optimize(max_iter: int = -1, *, report: Literal[False] = False, folding_options: ConstantFoldingOptions | None = None, remove_unused: bool = True, callback: Callable[[GraphGraph, MatchResult], bool | None] | None = None, parallel: bool = False, canonicalize: bool = False, move_shape_and_size_nodes: bool = True) → RewritingProto#
optimize(max_iter: int = -1, *, report: Literal[True], folding_options: ConstantFoldingOptions | None = None, remove_unused: bool = True, callback: Callable[[GraphGraph, MatchResult], bool | None] | None = None, parallel: bool = False, canonicalize: bool = False, move_shape_and_size_nodes: bool = True) → tuple[RewritingProto, OptimizationReport]
optimize(max_iter: int = -1, *, report: bool, folding_options: ConstantFoldingOptions | None = None, remove_unused: bool = True, callback: Callable[[GraphGraph, MatchResult], bool | None] | None = None, parallel: bool = False, canonicalize: bool = False, move_shape_and_size_nodes: bool = True) → RewritingProto | tuple[RewritingProto, OptimizationReport]

Returns sequential proto edits and optionally their optimization report.

canonicalize=True explicitly cleans the reference before capture. It is disabled by default. Undo restores that reference, which retains the original nodes when canonicalization is disabled. move_shape_and_size_nodes=True records Shape/Size hoisting after optimization and reports its timing and moved-node count. Disable it explicitly to retain the existing metadata-reader placement.

Calls callback(graph, match) for each eligible, disjoint pattern match, including matches in nested subgraphs, before reserving its nodes. Returning False rejects that match without blocking other patterns; True or None accepts it, so reporting-only callbacks need no return. Cleanup passes are not intercepted. Matches may be reconsidered in later iterations and accepted matches are revalidated before application.

The callback must not mutate the graph, patterns, or match, or retain references to the graph or matched nodes beyond the call. Exceptions propagate to the caller.

parallel=True matches explicitly opted-in native patterns concurrently. Other patterns, including Python-defined patterns requiring the GIL, remain sequential. The optimization callback always runs sequentially on the calling thread.

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

Fuses gathered weights, unrolled MatMuls, or a per-token Loop into GroupedMatMul.

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.IfIdenticalBranchesPattern(*args, **kwargs)#

Inlines an If with alpha-equivalent branches.

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

Folds an initializer’s Unsqueeze and Cast into the initializer consumed by Add.

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

Composes consecutive ai.onnx.ml LabelEncoder mappings.

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.LinearAttentionPattern(*args, **kwargs)#

Fuses a single-token linear-attention recurrence.

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

Describes one sequential, materialized graph edit.

ByteSize(self) → int#

Returns the serialized size in bytes, following the protobuf API.

Clear(self) → None#

Clears the object.

ClearField(self, field_name: str) → None#

Clears the field field_name, following the protobuf API.

CopyFrom(self, arg: onnx_light.onnx_py._onnxpyprotoop.LocalRewritingProto, /) → None#

Copies one instance into this one.

Equals(self, other: onnx_light.onnx_py._onnxpyprotoop.LocalRewritingProto) → bool#

Compares message fields directly without serialization.

HasField(self, field_name: str) → bool#
ParseFromFile(self, name: str, options: object | None = None, external_data_file: str = '') → None#

Parses a binary file to fill this instance.

ParseFromString(self, data: bytes, options: object | None = None) → None#
ParseFromString(self, data: str, options: object | None = None) → None

Overloaded function.

  1. ParseFromString(self, data: bytes, options: object | None = None) -> None

Parses a sequence of bytes to fill this instance.

  1. ParseFromString(self, data: str, options: object | None = None) -> None

Parses a string to fill this instance.

SerializeSize(self, options: object | None = None) → onnx_light.onnx_py._onnxpyprotoop.SerializeSizeResult#

Returns the size once serialized without serializing.

SerializeToFile(self, name: str, options: object | None = None, external_data_file: str = '') → None#

Serializes this instance into a file. Standalone messages require single-file ONNX output. If external_data_file is not empty, big weights are stored in this (depending on options.raw_data_threshold). When writing to two files, temporary external-data metadata is cleared so the model’s values and metadata stay unchanged. Borrowed buffers not proven independent of files are first snapshotted in the source, even if a later write fails; their addresses and ownership may change.

SerializeToFileDescriptor(self, fd: int, options: object | None = None) → None#

Serializes this instance into an open file descriptor without closing it.

SerializeToOStream(self, output: object) → None#

Alias for SerializeToOstream (capital S) for protobuf API compatibility. Serializes this instance to a Python file-like object that has a write() method.

SerializeToOstream(self, output: object) → None#

Serializes this instance to a Python file-like object that has a write() method.

SerializeToString(self, options: object | None = None) → bytes#

Serializes this instance into a sequence of bytes.

property added_initializer_positions#

Initializer positions after this edit.

property added_initializers#

Materialized replacement initializers.

property added_nodes#

Replacement nodes.

property added_nodes_positions#

Node positions after this edit.

property computed_initializer_positions#

Reserved for prepared initializer positions.

property graph_path#

Subgraph path before this edit.

has_added_initializer_positions(self) → bool#

Tells if ‘added_initializer_positions’ has a value.

has_added_initializers(self) → bool#

Tells if ‘added_initializers’ has a value.

has_added_nodes(self) → bool#

Tells if ‘added_nodes’ has a value.

has_added_nodes_positions(self) → bool#

Tells if ‘added_nodes_positions’ has a value.

has_computed_initializer_positions(self) → bool#

Tells if ‘computed_initializer_positions’ has a value.

has_graph_path(self) → bool#

Tells if ‘graph_path’ has a value.

has_initializer_calls(self) → bool#

Tells if ‘initializer_calls’ has a value.

has_matched_nodes(self) → bool#

Tells if ‘matched_nodes’ has a value.

has_metadata_props(self) → bool#

Tells if ‘metadata_props’ has a value.

has_pattern_name(self) → bool#

Tells if ‘pattern_name’ has a value

has_removed_extended_initializers(self) → bool#

Tells if ‘removed_extended_initializers’ has a value.

has_removed_initializers(self) → bool#

Tells if ‘removed_initializers’ has a value.

property initializer_calls#

Reserved for initializer preparation calls.

property matched_nodes#

Removed node positions before this edit.

property metadata_props#

Descriptive metadata.

property pattern_name#

Diagnostic name of the pattern.

property removed_extended_initializers#

Removed extended initializer positions before this edit.

property removed_initializers#

Removed dense initializer positions before this edit.

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

Replaces a compatible MatMul and Add with Gemm.

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

Folds constant inference BatchNormalization parameters into a rank-two MatMul.

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

Simplifies compatible reshapes around MatMul.

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

Absorbs one safe scalar Mul or Div adjacent to a rank-two MatMul.

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

Replaces a compatible maximum with Relu.

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

Moves one node in the final simultaneous relocation stage.

ByteSize(self) → int#

Returns the serialized size in bytes, following the protobuf API.

Clear(self) → None#

Clears the object.

ClearField(self, field_name: str) → None#

Clears the field field_name, following the protobuf API.

CopyFrom(self, arg: onnx_light.onnx_py._onnxpyprotoop.MoveNodeProto, /) → None#

Copies one instance into this one.

Equals(self, other: onnx_light.onnx_py._onnxpyprotoop.MoveNodeProto) → bool#

Compares message fields directly without serialization.

HasField(self, field_name: str) → bool#
ParseFromFile(self, name: str, options: object | None = None, external_data_file: str = '') → None#

Parses a binary file to fill this instance.

ParseFromString(self, data: bytes, options: object | None = None) → None#
ParseFromString(self, data: str, options: object | None = None) → None

Overloaded function.

  1. ParseFromString(self, data: bytes, options: object | None = None) -> None

Parses a sequence of bytes to fill this instance.

  1. ParseFromString(self, data: str, options: object | None = None) -> None

Parses a string to fill this instance.

SerializeSize(self, options: object | None = None) → onnx_light.onnx_py._onnxpyprotoop.SerializeSizeResult#

Returns the size once serialized without serializing.

SerializeToFile(self, name: str, options: object | None = None, external_data_file: str = '') → None#

Serializes this instance into a file. Standalone messages require single-file ONNX output. If external_data_file is not empty, big weights are stored in this (depending on options.raw_data_threshold). When writing to two files, temporary external-data metadata is cleared so the model’s values and metadata stay unchanged. Borrowed buffers not proven independent of files are first snapshotted in the source, even if a later write fails; their addresses and ownership may change.

SerializeToFileDescriptor(self, fd: int, options: object | None = None) → None#

Serializes this instance into an open file descriptor without closing it.

SerializeToOStream(self, output: object) → None#

Alias for SerializeToOstream (capital S) for protobuf API compatibility. Serializes this instance to a Python file-like object that has a write() method.

SerializeToOstream(self, output: object) → None#

Serializes this instance to a Python file-like object that has a write() method.

SerializeToString(self, options: object | None = None) → bytes#

Serializes this instance into a sequence of bytes.

property from_position#

Node position before the relocation stage.

property function_id#

overload] local-function identity.

Type:

Optional escaped domain

Type:

name[

property graph_path#

Subgraph path after the ordinary edits.

has_from_position(self) → bool#

Tells if ‘from_position’ has a value.

has_function_id(self) → bool#

Tells if ‘function_id’ has a value

has_graph_path(self) → bool#

Tells if ‘graph_path’ has a value.

has_to_position(self) → bool#

Tells if ‘to_position’ has a value.

property to_position#

Node position after the relocation stage.

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.PadPadFusionPattern(*args, **kwargs)#

Merges adjacent constant-mode Pad nodes with equal values by summing their pads.

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

Folds zero-valued spatial Pad into safe AveragePool or UINT8 MaxPool.

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

Extracts low-transfer groups selected by the core partition algorithm (opt-in).

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

Removes a Cast whose output only feeds Shape nodes, redirecting each Shape node directly to the Cast’s input.

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.ReluClipFusionPattern(*args, **kwargs)#

Removes Relu before Clip when the Clip minimum is constant and non-negative.

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

Encapsulates structurally repeated regions as model-local functions (opt-in).

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.RewritingProto(*args, **kwargs)#

Stores ordered edits followed by simultaneous node moves.

ByteSize(self) → int#

Returns the serialized size in bytes, following the protobuf API.

Clear(self) → None#

Clears the object.

ClearField(self, field_name: str) → None#

Clears the field field_name, following the protobuf API.

CopyFrom(self, arg: onnx_light.onnx_py._onnxpyprotoop.RewritingProto, /) → None#

Copies one instance into this one.

Equals(self, other: onnx_light.onnx_py._onnxpyprotoop.RewritingProto) → bool#

Compares message fields directly without serialization.

HasField(self, field_name: str) → bool#
ParseFromFile(self, name: str, options: object | None = None, external_data_file: str = '') → None#

Parses a binary file to fill this instance.

ParseFromString(self, data: bytes, options: object | None = None) → None#
ParseFromString(self, data: str, options: object | None = None) → None

Overloaded function.

  1. ParseFromString(self, data: bytes, options: object | None = None) -> None

Parses a sequence of bytes to fill this instance.

  1. ParseFromString(self, data: str, options: object | None = None) -> None

Parses a string to fill this instance.

SerializeSize(self, options: object | None = None) → onnx_light.onnx_py._onnxpyprotoop.SerializeSizeResult#

Returns the size once serialized without serializing.

SerializeToFile(self, name: str, options: object | None = None, external_data_file: str = '') → None#

Serializes this instance into a file. Standalone messages require single-file ONNX output. If external_data_file is not empty, big weights are stored in this (depending on options.raw_data_threshold). When writing to two files, temporary external-data metadata is cleared so the model’s values and metadata stay unchanged. Borrowed buffers not proven independent of files are first snapshotted in the source, even if a later write fails; their addresses and ownership may change.

SerializeToFileDescriptor(self, fd: int, options: object | None = None) → None#

Serializes this instance into an open file descriptor without closing it.

SerializeToOStream(self, output: object) → None#

Alias for SerializeToOstream (capital S) for protobuf API compatibility. Serializes this instance to a Python file-like object that has a write() method.

SerializeToOstream(self, output: object) → None#

Serializes this instance to a Python file-like object that has a write() method.

SerializeToString(self, options: object | None = None) → bytes#

Serializes this instance into a sequence of bytes.

property added_functions#

Shared additional functions.

has_added_functions(self) → bool#

Tells if ‘added_functions’ has a value.

has_hash_after(self) → bool#

Tells if ‘hash_after’ has a value.

has_hash_before(self) → bool#

Tells if ‘hash_before’ has a value.

has_metadata_props(self) → bool#

Tells if ‘metadata_props’ has a value.

has_moved_nodes(self) → bool#

Tells if ‘moved_nodes’ has a value.

has_replaced_function_ids(self) → bool#

Tells if ‘replaced_function_ids’ has a value.

has_required_opset_imports(self) → bool#

Tells if ‘required_opset_imports’ has a value.

has_rewrites(self) → bool#

Tells if ‘rewrites’ has a value.

property hash_after#

Structural hash after all edits and node moves.

property hash_before#

Structural hash before applying this complete plan.

property metadata_props#

Descriptive metadata.

property moved_nodes#

Simultaneous moves after all ordinary edits.

property replaced_function_ids#

overload] identities explicitly replaced by added_functions.

Type:

Escaped domain

Type:

name[

property required_opset_imports#

Required additional imports.

property rewrites#

Edits in application order.

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.STFTFusionPattern(*args, **kwargs)#

Fuses a canonical convolution-based DFT subgraph into one standard ONNX STFT.

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.ShapeBasedConcatExpandPattern(*args, **kwargs)#

Simplifies a dynamic Concat target when Expand changes one dimension.

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

Rewrites reshapes according to the distance between known shapes.

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

Removes dynamic Expand nodes from the batch dimensions of MatMul.

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

Removes dynamic Expand nodes before a broadcasting binary operator.

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

Moves an Expand after a compatible Cast and Where chain.

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

Moves input Expand nodes after a broadcasting binary operator.

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.ShapeBasedStaticExpandPattern(*args, **kwargs)#

Replaces a dynamic Expand target with an equivalent constant target.

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

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

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

Simplifies reshapes using inferred input and output shapes.

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

Fuses canonical rank-4 phase slices and channel Concat into SpaceToDepth.

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

Replaces an identity full-range Slice with Identity.

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.SqueezeUnsqueezePattern(*args, **kwargs)#

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

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.SwapExpandReshapePattern(*args, **kwargs)#

Swaps a supported Expand and constant-shape Reshape pair.

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

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

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.TransposeGatherPattern(*args, **kwargs)#

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

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.TransposeTransposePattern(*args, **kwargs)#

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

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

Replaces a classic tree ensemble with the unified TreeEnsemble operator.

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.UnsqueezeShapePattern(*args, **kwargs)#

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

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

Merges two consecutive Unsqueeze nodes into a single Unsqueeze.

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.apply_rewritings(model: ModelProto, rewrites: RewritingProto, schema_lookup: SchemaLookup | None = <function _default_schema_lookup>) → GraphProto#

Applies captured rewrites and returns the resulting graph.

onnx_light.onnx_core.optimization.apply_rewritings_to_model(model: ModelProto, rewrites: RewritingProto, schema_lookup: SchemaLookup | None = <function _default_schema_lookup>) → ModelProto#

Applies captured rewrites to a model, preserving functions and imports.

onnx_light.onnx_core.optimization.clear_registered_patterns() → None#

Removes every globally registered pattern, including standard patterns.

onnx_light.onnx_core.optimization.diff_models(model: ModelProto, target: ModelProto, *, base_dir: str = '', target_dir: str = '', schema_lookup: SchemaLookup | None = <function _default_schema_lookup>) → RewritingProto#

Constructs a replay-validated native delta to an independent target model.

External references are interpreted relative to their respective directories. Identical references reuse storage without reading payloads; different references are compared by content. Interface and persistent declarations must match. See the model-delta documentation for supported differences.

onnx_light.onnx_core.optimization.extend_pattern_set(name: str, pattern_names: Iterable[str]) → None#

Adds names to an existing pattern set in the C++ registry.

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

Registers or replaces a process-global pattern.

onnx_light.onnx_core.optimization.register_pattern_set(name: str, pattern_names: Iterable[str]) → None#

Creates a named pattern set in the C++ pattern registry.

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.reset_registered_patterns() → None#

Restores the global registry to the standard ONNX patterns.

onnx_light.onnx_core.optimization.standard_pattern_names(*, include_opt_in: bool = False) → list[str]#

Returns standard pattern names, excluding opt-in patterns unless requested.

onnx_light.onnx_core.optimization.standard_pattern_set_names() → list[str]#

Returns the names of registered standard pattern sets.

onnx_light.onnx_core.optimization.standard_pattern_sets() → dict[str, tuple[str, ...]]#

Returns named, overlapping C++-registered groups of 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.

onnx_light.onnx_core.optimization.unregister_pattern_set(name: str) → bool#

Removes a custom pattern set from the C++ registry.