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#
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 |
Makes a pattern available for selection. The standard ONNX patterns are registered globally when this module is imported. |
|
Builder |
Overrides a global pattern for optimizers built over that builder. |
|
Graph |
|
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 |
|---|---|
|
All available device-independent patterns, even if |
|
None, including builder registrations. The optimizer’s ordinary cleanup and constant-folding passes still run. |
A concrete |
All device-independent patterns plus those targeting that exact device.
Also sets |
A regex string or compiled |
Available names accepted by |
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 |
|---|---|---|---|
|
2 |
Fuses grouped-query attention cache handling. |
|
|
0 |
|
Fuses an inference batch-normalization subgraph. |
|
0 |
|
Fuses a training batch-normalization subgraph. |
|
0 |
|
Replaces a type-preserving |
|
1 |
|
Collapses two consecutive compatible Cast nodes. |
|
1 |
|
Moves matching floating-point input Cast nodes after a binary operation. |
|
1 |
|
Removes redundant casts surrounding LayerNormalization. |
|
1 |
|
Moves a unary or binary operation to the result Cast type. |
|
1 |
|
Merges two consecutive Clip nodes with complementary bounds. |
|
0 |
|
Drops empty inputs from a Concat node, reducing it to an Identity when a single input remains. |
|
0 |
|
Rewrites a Gather reading a single Concat input into a Gather on that input directly. |
|
0 |
|
Simplifies concatenations that construct reshape shapes. |
|
0 |
|
Eliminates a Concat followed by exact slices recovering all inputs. |
|
0 |
|
Pushes a shape-preserving unary op ahead of a |
|
1 |
|
Replaces a Constant node by an initializer and an Identity node. |
|
0 |
|
Folds a channel-wise constant Add following a Conv into the Conv bias. |
|
0 |
|
Folds inference BatchNormalization following a Conv into its weights and bias. |
|
0 |
|
Removes a null (all-zero) bias input from a Conv node. |
|
0 |
|
Folds a scalar or channel-wise constant Mul following a Conv into its constants. |
|
0 |
|
Fuses multiplication by a reciprocal into one division. |
|
1 |
|
Replaces an inference Dropout by an Identity node when its mask output is unused and training mode is disabled. |
|
0 |
|
Replaces |
|
0 |
|
Drops an |
|
0 |
|
Moves an |
|
0 |
|
Fuses |
|
0 |
|
Replaces a scaled dot-product attention subgraph. |
|
1 |
Replaces grouped-query attention expressed with local functions. |
|
|
1 |
|
Replaces a causal-mask subgraph with a local function. |
|
1 |
|
Fuses scaling and offset operations into a causal-mask function. |
|
1 |
|
Replaces cosine and sine cache construction with a local function. |
|
1 |
|
Replaces half-rotary embedding construction with a local function. |
|
0 |
|
Merges a Concat of single-index Gather nodes on a shared input into one Gather node. |
|
0 |
|
Collapses two consecutive scalar Gather nodes into a single Gather node. |
|
0 |
|
Rewrites a Gather of a scalar index over a Shape node into a narrowed Shape node. |
|
0 |
|
Fuses compatible sibling Gather and Slice ranges into one Split. |
|
0 |
|
Rewrites a Gather selecting a constant scalar, singleton, or arithmetic-progression index into a Slice (plus Squeeze for a scalar index). |
|
0 |
|
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. |
|
0 |
|
Replaces sibling Gather nodes selecting contiguous single indices of a shared input by a single Split node. |
|
0 |
|
Fuses a GELU activation subgraph. |
|
4 |
|
Fuses a two-input Sum into an unbiased Gemm bias input. |
|
1 |
|
Folds input transposes into a Gemm operation. |
|
0 |
|
Replaces no-op arithmetic and layout operations by an Identity node. |
|
1 |
|
Folds an initializer’s Unsqueeze and Cast into the initializer consumed by Add. |
|
0 |
|
Composes consecutive ai.onnx.ml LabelEncoder mappings. |
|
1 |
|
Fuses a layer-normalization subgraph. |
|
1 |
|
Fuses layer normalization with its scale. |
|
0 |
|
Fuses a LeakyRelu activation subgraph. |
|
2 |
|
Fuses a single-token linear-attention recurrence. |
|
3 |
|
Replaces a compatible MatMul and Add with Gemm. |
|
5 |
|
Folds constant inference BatchNormalization parameters into a rank-two MatMul. |
|
1 |
|
Simplifies compatible reshapes around MatMul. |
|
4 |
|
Absorbs one safe scalar Mul or Div adjacent to a rank-two MatMul. |
|
1 |
|
Replaces a compatible maximum with Relu. |
|
1 |
|
Moves compatible scalar multiplications across MatMul. |
|
0 |
|
Combines scalar factors in multiplication chains. |
|
0 |
|
Simplifies multiplication of unsqueezed inputs. |
|
1 |
|
Fuses two consecutive Not nodes into an Identity node. |
|
0 |
|
Rewrites |
|
0 |
|
Folds a Pad node into the |
|
1 |
|
Merges adjacent constant-mode Pad nodes with equal values by summing their pads. |
|
0 |
|
Removes a Cast whose output only feeds Shape nodes, redirecting each Shape node directly to the Cast’s input. |
|
1 |
|
Fuses an RMS-normalization subgraph. |
|
1 |
|
Fuses RMS normalization with a following scale. |
|
0 |
|
Simplifies compatible reduction, arg, and TopK operations. |
|
0 |
|
Simplifies reshape operations around reductions. |
|
0 |
|
Simplifies reduce-sum normalization subgraphs. |
|
1 |
|
Removes Relu before Clip when the Clip minimum is constant and non-negative. |
|
0 |
|
Removes or simplifies redundant reshape operations. |
|
0 |
|
Simplifies two compatible reshapes among three branches. |
|
1 |
|
Simplifies reshape, MatMul, and reshape sequences. |
|
0 |
|
Collapses consecutive compatible reshapes. |
|
0 |
|
Moves compatible reshapes across binary operations. |
|
0 |
|
Simplifies a reshape followed by squeeze. |
|
1 |
|
Simplifies padded rotary concatenation subgraphs. |
|
1 |
|
Fuses a complete rotary-embedding subgraph. |
|
0 |
|
Fuses a canonical convolution-based DFT subgraph into one standard ONNX STFT. |
|
0 |
Eliminates equivalent child computations. |
|
|
0 |
Eliminates equivalent computations from one input. |
|
|
0 |
|
Replaces a SequenceAt reading a constant index of a SequenceConstruct by the corresponding input tensor. |
|
0 |
|
Simplifies a dynamic |
|
0 |
|
Rewrites reshapes according to the distance between known shapes. |
|
0 |
|
Removes dynamic |
|
0 |
|
Removes dynamic |
|
0 |
|
Moves an |
|
0 |
|
Moves input |
|
0 |
|
Eliminates shape-proven identity operations. |
|
1 |
|
Replaces shape-proven scalar MatMul with Mul. |
|
0 |
|
Replaces eligible reshapes with squeeze. |
|
0 |
|
Eliminates shape-equivalent child computations. |
|
0 |
|
Exposes the upstream placeholder for additions of two Shape outputs. |
|
0 |
|
Replaces a dynamic |
|
0 |
|
Rewrites |
|
0 |
|
Simplifies reshapes using inferred input and output shapes. |
|
0 |
|
Fuses canonical rank-4 phase slices and channel Concat into SpaceToDepth. |
|
1 |
|
Replaces an identity full-range Slice with Identity. |
|
0 |
|
Merges two consecutive Slice nodes on distinct axes into one Slice. |
|
0 |
|
Replaces sibling Slice nodes cutting a shared input into contiguous chunks along one axis by a single Split node. |
|
0 |
|
Moves a compatible label cast into SoftmaxCrossEntropyLoss. |
|
0 |
|
Replaces a Split immediately followed by a Concat that restores the original tensor with an Identity node. |
|
0 |
|
Replaces a SequenceAt reading a constant index of a SplitToSequence by a single Split output. |
|
0 |
|
Moves compatible squeeze operations across addition. |
|
0 |
|
Simplifies squeeze, binary operation, and unsqueeze sequences. |
|
0 |
|
Simplifies a |
|
0 |
|
Folds static concatenated reshape shapes. |
|
0 |
|
Simplifies multiplication involving one minus a value. |
|
0 |
|
Swaps a supported |
|
0 |
|
Swaps |
|
0 |
|
Moves scalar addition into compatible range operations. |
|
0 |
|
Swaps compatible unary operations. |
|
0 |
|
Swaps compatible unsqueeze and transpose operations. |
|
0 |
|
Reorders compatible consecutive binary operations. |
|
1 |
|
Moves compatible activations before Reshape. |
|
0 |
|
Replaces shape-equivalent transposes with reshapes. |
|
0 |
|
Removes or reorders a |
|
1 |
|
Folds compatible transposes into MatMul. |
|
1 |
|
Simplifies transpose and reshape inputs to MatMul. |
|
0 |
|
Simplifies transpose, reshape, and transpose sequences. |
|
1 |
|
Folds a |
|
0 |
|
Merges two consecutive |
|
1 |
|
Replaces a classic tree ensemble with the unified |
|
0 |
|
Rewrites |
|
0 |
|
Simplifies reshape operations adjacent to squeeze or unsqueeze. |
|
0 |
|
Simplifies an unsqueeze followed by reshape. |
|
0 |
|
Rewrites |
|
0 |
|
Merges two consecutive |
|
0 |
|
Factors a common additive term from Where branches built with Add. |
The runtime list is available through standard_pattern_names().
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):
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) -> BinarybecomesBinary(x, y) -> Castwhen 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:Cbecomes one safeCast(C)orIdentity.
- 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)withIdentity.x:T -> Cast(to=T) -> y:Tbecomesx: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 oneClip(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 onx.
- 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
Expandfeeding 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)withIdentity(x)when the target shape equals the input shape.
- class onnx_light.onnx_core.optimization.ExpandSwapPattern(*args, **kwargs)#
Moves an
Expandpast a following unary-like operator so the operator runs on the smaller tensor.
- class onnx_light.onnx_core.optimization.ExpandUnsqueezeExpandPattern(*args, **kwargs)#
Fuses
Expand,UnsqueezeandExpandinto a singleUnsqueezefollowed by oneExpand.
- 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.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.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>)#
Incrementally builds an ONNX graph, model or function.
See
onnx_light.onnx_core.graph_builderfor details. By default the builder validates nodes and resolves opsets using the built-in ONNX operator schemas; passschema_lookup=Noneto disable this, or a customop_type -> list[LightOpSchema]callable to use different schemas.- 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=Truefor 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.
- 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=Noneselects all registered device-independent patterns.Falseselects none. A concreteDeviceincludes independent patterns and patterns targeting that exact device, and setsbuilder.device; a conflicting builder device raisesValueError.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.
- 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.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.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.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)intoWhere(c, y, x).
- class onnx_light.onnx_core.optimization.PadConvPattern(*args, **kwargs)#
Folds a Pad node into the
padsattribute 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.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.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.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
Concattarget whenExpandchanges 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
Expandnodes from the batch dimensions ofMatMul.
- class onnx_light.onnx_core.optimization.ShapeBasedExpandBroadcastPattern(*args, **kwargs)#
Removes dynamic
Expandnodes before a broadcasting binary operator.
- class onnx_light.onnx_core.optimization.ShapeBasedExpandCastWhereSwapPattern(*args, **kwargs)#
Moves an
Expandafter a compatibleCastandWherechain.
- class onnx_light.onnx_core.optimization.ShapeBasedExpandSwapPattern(*args, **kwargs)#
Moves input
Expandnodes 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
Expandtarget with an equivalent constant target.
- class onnx_light.onnx_core.optimization.ShapeTransposePattern(*args, **kwargs)#
Rewrites
Shape(Transpose(X, perm))intoGather(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/Unsqueezepair into anIdentityor a singleSqueeze.
- 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
Expandand constant-shapeReshapepair.
- class onnx_light.onnx_core.optimization.SwapExpandUnsqueezePattern(*args, **kwargs)#
Swaps
Expandand a followingUnsqueezeso theUnsqueezeruns 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
Transposefeeding aGatherwith 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.TransposeToInitializerPattern(*args, **kwargs)#
Folds a
Transposeapplied to an initializer into a transposed initializer.
- class onnx_light.onnx_core.optimization.TransposeTransposePattern(*args, **kwargs)#
Merges two consecutive
Transposenodes into a singleTransposeor anIdentitywhen the permutations cancel out.
- class onnx_light.onnx_core.optimization.TreeEnsemblePattern(*args, **kwargs)#
Replaces a classic tree ensemble with the unified
TreeEnsembleoperator.
- class onnx_light.onnx_core.optimization.UnsqueezeEqualPattern(*args, **kwargs)#
Rewrites
Equal(Unsqueeze(x), Unsqueeze(y))intoEqual(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 aConcatof rangedShapeslices interleaved with constant[1]tensors.
- class onnx_light.onnx_core.optimization.UnsqueezeUnsqueezePattern(*args, **kwargs)#
Merges two consecutive
Unsqueezenodes into a singleUnsqueeze.
- 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-tabledirective 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.