.xoptim.patterns¶
modules
- .xoptim.patterns.onnx_any
- .xoptim.patterns.onnx_cast
- .xoptim.patterns.onnx_clip
- .xoptim.patterns.onnx_constants
- .xoptim.patterns.onnx_conv
- .xoptim.patterns.onnx_dropout
- .xoptim.patterns.onnx_equal
- .xoptim.patterns.onnx_expand
- .xoptim.patterns.onnx_functions
- .xoptim.patterns.onnx_layer_normalization
- .xoptim.patterns.onnx_matmul
- .xoptim.patterns.onnx_mul
- .xoptim.patterns.onnx_reduce
- .xoptim.patterns.onnx_reshape
- .xoptim.patterns.onnx_rotary
- .xoptim.patterns.onnx_slice
- .xoptim.patterns.onnx_split
- .xoptim.patterns.onnx_sub
- .xoptim.patterns.onnx_sequence
- .xoptim.patterns.onnx_transpose
- .xoptim.patterns.onnx_unsqueeze
- class experimental_experiment.xoptim.patterns.AlmostDoNothingPattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]¶
Checks that a Expand is really needed.
- apply(g: GraphBuilder, node: NodeProto) List[NodeProto] [source]¶
The method does the rewriting. It assumes it can happen. It takes a list of nodes impacted by the rewriting assumes no other pattern optimizer will be modify them. It receives the list of nodes returned by method apply. Since it is a list of argument, method match can include None values. The method returns the new nodes. The optimizer considers that any node given to this function is removed from the graph, and any node returned by it are added. If a received node must be kept, it must be added to the list of returned node.
- Parameters:
nodes – nodes returned by method match, there are then removed
- Returns:
nodes to add to graph.
- match(g: GraphBuilderPatternOptimization, node: NodeProto, matched: List[MatchResult]) MatchResult | None [source]¶
Determines nodes around node which can be rewritten.
- Parameters:
g – is a
GraphBuilderPatternOptimization
, it holds all the existing nodes, is able to return any information about type, shape, the node before, the node after another one.node – the matching must determine if some nodes around this one are part of set of nodes this pattern optmizer can rewrite. From there, the function explores wherever it needs, checking any condition it needs.
matched – usually unused, it returns of nodes already matching a pattern
The method must not modify the graph. The method returns None if no match is found or an instance of class
MatchResult
. It must contain:a list of nodes involved in the rewriting. It does not mean all of them will be removed but all of them are needed to do the rewriting and must not be impacted by other pattern optimizer.
A function doing the rewriting (usually method apply of the pattern class).
An existing node where the rewritten nodes can be inserted. Knowing it makes it faster to rewriter. If not specified, the optimizer will automatically determine the position of the new nodes.
- experimental_experiment.xoptim.patterns.get_default_patterns(verbose: int = 0) List[PatternOptimization] [source]¶
Returns a default list of optimization patterns. It is equal to the following list.
<<<
from experimental_experiment.xoptim.patterns_api import pattern_table_doc from experimental_experiment.xoptim.patterns import get_default_patterns print(pattern_table_doc(get_default_patterns(), as_rst=True))
>>>
name
short_name
priority
doc
0
BatchNormalizationPattern
BatchNormalization
0
Checks that a BatchNormalization is really needed.
1
BatchNormalizationTrainingPattern
BatchNormalizationTraining
0
Checks that a BatchNormalization in training mode can be avoided.
2
CastLayerNormalizationCastPattern
CastLayerNormalizationCast
1
Checks that a Cast is really needed around LayerNormalization
3
CastPattern
Cast
0
Checks that a Cast is really needed.
4
CastCastBinaryPattern
CastCastBinary
1
Moves two cast operators beyond a binary operator The cast must cast from a float type to another float type.
5
CastOpCastPattern
CastOpCast
1
Removes two cast surrounding another operator.
6
ClipClipPattern
ClipClip
1
Merges consecutive clips if one is defining min and the other max.
7
ComputationCastOpCastPattern
ComputationCastOpCast
1
Changes the computation type to make it faster if one of the inputs was just casted before.
8
ConvBiasNullPattern
ConvBiasNull
0
Checks that a Conv has a null bias.
9
DropoutPattern
Dropout
1
Checks that a Cast is really needed.
10
ExpandPattern
Expand
0
Checks that a Expand is really needed.
11
ExpandBroadcastPattern
ExpandBroadcast
1
Checks that a Expand is really needed before an element wise operator. The objective is to save one allocation and let the next operator do the expansion by broadcasting one input.
12
ExpandSwapPattern
ExpandSwap
1
Tries to move a node Expand forward in the graph. Expand + Exp can be changed into Exp + Expand. Then Exp applies on a tensor of a smaller or equal size.
13
GeluPattern
Gelu
0
Detects the decomposed version of Gelu with Tanh .. math
14
IdentityPattern
Identity
0
Replaces operator such as Div(X, 1), Mul(X, 1), Add(X, 0), Sub(X, 0), Transpose(X, [0, 1, 2, …]) into identity nodes.
15
LayerNormalizationPattern
LayerNormalization
1
Fuses node of a LayerNormalization.
16
LayerNormalizationScalePattern
LayerNormalizationScale
1
Fused LayerNormalization, scale, bias just after.
17
LeakyReluPattern
LeakyRelu
0
Detects the decomposed version of LeakyRelu.
18
MulMulMulScalarPattern
MulMulMulScalar
1
Replaces the sequence {Div | Mul} and {Div | Mul} + {Div | Mul} with {Div | Mul} Mul.
19
ReduceReshapePattern
ReduceReshape
1
Replaces the sequence Reduce* Reshape if reshape is only introduces to deal with a dimension kept because keepdim=1.
20
ReduceSumNormalizePattern
ReduceSumNormalize
1
Nodes equivalent to a reduction.
21
ReshapePattern
Reshape
0
Checks that a Reshape is really needed.
22
ReshapeMatMulReshapePattern
ReshapeMatMulReshape
1
Replaces the sequence Reshape, Matmul, Reshape by Matmul.
23
Reshape2Of3Pattern
Reshape2Of3
1
Replaces the reshapes around element-wise operators. It can be 3 or 2 out of 3.
24
ReshapeReshapeBinaryPattern
ReshapeReshapeBinary
1
Moves two reshape operators beyond a binary operator if it is possible.
25
MatMulAddPattern
MatMulAdd
1
Replaces the sequence MatMul, Add into Gemm
26
GemmTransposePattern
GemmTranspose
1
Replaces Gemm (., constant) by Gemm(., constant’, transB=1)
27
MatMulReshape2Of3Pattern
MatMulReshape2Of3
1
Replaces the reshapes around a matmul It can be 3 or 2 out of 3. It is similar to
experimental_experiment.xoptim.patterns.onnx_reshape.Reshape2Of3Pattern
.28
MulMulMatMulPattern
MulMulMatMul
1
Replaces
MatMul(a*c, b*d)
where c and d are constant scalar byMatMul(a,b) * (c,d)
.29
ReshapeReshapePattern
ReshapeReshape
0
Replaces the sequence Reshape, Reshape by Reshape.
30
RotaryConcatPartPattern
RotaryConcatPart
1
Optimizes the following pattern .. plot
31
SameChildrenPattern
SameChildren
0
Checks that a Cast is really needed.
32
SequenceConstructAtPattern
SequenceConstructAt
1
Replaces the sequence
SequenceConstruct(x1, x2, ...)
followed bySequenceAt(seq, 0)
,SequenceAt(seq, 1)
, …33
SliceSlicePattern
SliceSlice
1
Merges consecutive slices if axis are disjoints.
34
SlicesSplitPattern
SlicesSplit
1
Merges multiple parallel slices into a split.
35
SoftmaxCrossEntropyLossCastPattern
SoftmaxCrossEntropyLossCast
0
Detects one decomposed version of SoftmaxCrossEntropyLoss.
36
SplitConcatPattern
SplitConcat
1
Replaces Split + Concat into identity if this is equivalent.
37
SqueezeUnsqueezePattern
SqueezeUnsqueeze
0
Replaces the sequence Squeeze, Unsqueeze by Identity.
38
Sub1MulPattern
Sub1Mul
1
Replaces the sequence (1 - X) x Y by Y - X x Y to avoid the creation of a constant in the graph. x means element wise multiplication.
39
SwitchOrderBinaryPattern
SwitchOrderBinary
1
If it makes sense, switches the order of two multiplications or two addtions if the broadcasting reduces one operator to a an insignificant number.
40
SwitchReshapeActivationPattern
SwitchReshapeActivation
1
Swiches Gelu and Reshape after a Gemm or a MatMul. Gelu can also be Gelu, Exp, Elu, Relu, Tan, Tanh, Cos, Cosh, Sin, Sinh, Erf, LeakyRelu, PRelu, Selu, Softmax, Softplus. Reshape can also be Transpose.
41
TransposeEqualReshapePattern
TransposeEqualReshape
1
Replaces a Transpose by a Reshape when switched dimensions are all equal to 1 but one.
42
TransposeMatMulPattern
TransposeMatMul
1
Replaces the sequence Transpose, Matmul or Gemm into Gemm
43
TransposeReshapeMatMulPattern
TransposeReshapeMatMul
1
Replaces the sequence Transpose, Reshape, Matmul into Reshape, Transpose, Matmul if possible. Another optimizer will optimizes this sequence by using Gemm or better.
44
TransposeReshapeTransposePattern
TransposeReshapeTranspose
0
Swaps Reshape and Transpose in a sequence such as this one:
45
TransposeTransposePattern
TransposeTranspose
0
Removes two consecutive transpose if the second one put the tensor in origin shape.
46
UnsqueezeEqualPattern
UnsqueezeEqual
1
Replaces the sequence R -> Equal -> Unsqueeze, R -> Unsqueeze, into R -> Unsqueeze -> Equal.
47
UnsqueezeUnsqueezePattern
UnsqueezeUnsqueeze
0
Replaces the sequence Unsqueeze, Unsqueeze by Unsqueeze.