yobx.xoptim.patterns.onnx_functions#

class yobx.xoptim.patterns.onnx_functions.GeluPattern(verbose: int = 0, priority: int = 0, min_opset: int = 20, domain: str = '')[source]#

Detects the decomposed version of Gelu with Tanh

y = \frac{x}{2}
\left(1 + \tanh\left(\sqrt{\frac{2}{\pi}} (x + 0.044715 * x^3)\right)\right)

Model with nodes to be fused:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_linear_5(["linear_5 FLOAT16(4, 512, 16384)"])

    Pow_0[["Pow(., [3.0])"]]
    Mul_1[["Mul(., 0.0447)"]]
    Add_2[["Add(., .)"]]
    Mul_3[["Mul(., 0.798)"]]
    Tanh_4[["Tanh(.)"]]
    Add_5[["Add(., 1.0)"]]
    Mul_6[["Mul(., 0.5)"]]
    Mul_7[["Mul(., .)"]]

    I_linear_5 -->|"FLOAT16(4, 512, 16384)"| Pow_0
    Pow_0 -->|"FLOAT16(4, 512, 16384)"| Mul_1
    I_linear_5 -->|"FLOAT16(4, 512, 16384)"| Add_2
    Mul_1 -->|"FLOAT16(4, 512, 16384)"| Add_2
    Add_2 -->|"FLOAT16(4, 512, 16384)"| Mul_3
    Mul_3 -->|"FLOAT16(4, 512, 16384)"| Tanh_4
    Tanh_4 -->|"FLOAT16(4, 512, 16384)"| Add_5
    I_linear_5 -->|"FLOAT16(4, 512, 16384)"| Mul_6
    Mul_6 -->|"FLOAT16(4, 512, 16384)"| Mul_7
    Add_5 -->|"FLOAT16(4, 512, 16384)"| Mul_7

    O_mul_4(["mul_4 FLOAT16(4, 512, 16384)"])
    Mul_7 --> O_mul_4

    class I_linear_5,O_mul_4 ioNode
    class Pow_0,Mul_1,Add_2,Mul_3,Tanh_4,Add_5,Mul_6,Mul_7 opNode
    

Outcome of the fusion:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_linear_5(["linear_5 FLOAT16(4, 512, 16384)"])

    Gelu_0[["Gelu(.)"]]

    I_linear_5 -->|"FLOAT16(4, 512, 16384)"| Gelu_0

    O_mul_4(["mul_4 FLOAT16(4, 512, 16384)"])
    Gelu_0 --> O_mul_4

    class I_linear_5,O_mul_4 ioNode
    class Gelu_0 opNode
    
apply_pattern(g: GraphBuilder, x, c3, c04, cpi, one, c2)[source]#

Applies the replacement.

match_pattern(g: GraphBuilder, x, c3, c04, cpi, one, c2)[source]#

Builds the pattern to match.

validate_mapping(g: GraphBuilderPatternOptimization, deleted_nodes: List[NodeProto], pattern_nodes: List[NodeProto] | None = None) bool[source]#

Validates the mapping.

Parameters:
  • g – GraphBuilder

  • deleted_nodes – matched nodes from the model (to be deleted)

  • pattern_nodes – matched nodes coming from the pattern

Returns:

validate the mapping or not, default is True

class yobx.xoptim.patterns.onnx_functions.LeakyReluPattern(verbose: int = 0, priority: int = 0, min_opset: int = 6)[source]#

Detects the decomposed version of LeakyRelu.

Model with nodes to be fused:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_X1(["X1 FLOAT(3, 3)"])

    Greater_0[["Greater(., [0.0])"]]
    Mul_1[["Mul(., [-0.33])"]]
    Where_2[["Where(., ., .)"]]

    I_X1 -->|"FLOAT(3, 3)"| Greater_0
    I_X1 -->|"FLOAT(3, 3)"| Mul_1
    Greater_0 -->|"BOOL(3, 3)"| Where_2
    I_X1 -->|"FLOAT(3, 3)"| Where_2
    Mul_1 -->|"FLOAT(3, 3)"| Where_2

    O_Y(["Y FLOAT(3, 3)"])
    Where_2 --> O_Y

    class I_X1,O_Y ioNode
    class Greater_0,Mul_1,Where_2 opNode
    

Outcome of the fusion:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_X1(["X1 FLOAT(3, 3)"])

    LeakyRelu_0[["LeakyRelu(.)"]]

    I_X1 -->|"FLOAT(3, 3)"| LeakyRelu_0

    O_Y(["Y FLOAT(3, 3)"])
    LeakyRelu_0 --> O_Y

    class I_X1,O_Y ioNode
    class LeakyRelu_0 opNode
    
apply_pattern(g: GraphBuilder, x, zero, slope)[source]#

Applies the replacement.

match_pattern(g: GraphBuilder, x, zero, slope)[source]#

Builds the pattern to match.

validate_mapping(g: GraphBuilderPatternOptimization, deleted_nodes: List[NodeProto], pattern_nodes: List[NodeProto] | None = None) bool[source]#

Validates the mapping.

Parameters:
  • g – GraphBuilder

  • deleted_nodes – matched nodes from the model (to be deleted)

  • pattern_nodes – matched nodes coming from the pattern

Returns:

validate the mapping or not, default is True

class yobx.xoptim.patterns.onnx_functions.MaxReluPattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Replaces Max(x, 0) or Max(0, x) with Relu(x).

Model with nodes to be fused:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_X(["X FLOAT(a, b)"])
    I_zero(["zero FLOAT(1)"])

    Constant_0[["Constant() -#gt; zero"]]
    Max_1[["Max(., .)"]]

    I_X -->|"FLOAT(a, b)"| Max_1
    Constant_0 -->|"FLOAT(1)"| Max_1

    O_Y(["Y FLOAT(a, b)"])
    Max_1 --> O_Y

    class I_X,O_Y ioNode
    class Constant_0 constNode
    class Max_1 opNode
    

Outcome of the fusion:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_X(["X FLOAT(a, b)"])

    Relu_0[["Relu(.)"]]

    I_X -->|"FLOAT(a, b)"| Relu_0

    O_Y(["Y FLOAT(a, b)"])
    Relu_0 --> O_Y

    class I_X,O_Y ioNode
    class Relu_0 opNode
    
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 optimizer 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.

class yobx.xoptim.patterns.onnx_functions.SoftmaxCrossEntropyLossCastPattern(verbose: int = 0, priority: int = 0, min_opset: int = 14, domain: str = '')[source]#

Detects one decomposed version of SoftmaxCrossEntropyLoss.

Model with nodes to be fused:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_I(["I INT64(A)"])
    I_X(["X FLOAT16(A, B)"])

    Equal_0[["Equal(., [-100])"]]
    Not_1[["Not(.)"]]
    Where_2[["Where(., ., [0])"]]
    Unsqueeze_3[["Unsqueeze(., [1])"]]
    LogSoftmax_4[["LogSoftmax(., axis=1)"]]
    GatherElements_5[["GatherElements(., ., axis=1)"]]
    Squeeze_6[["Squeeze(., [1])"]]
    Neg_7[["Neg(.)"]]
    Where_8[["Where(., ., [0.0])"]]
    Cast_9[["Cast(., to=FLOAT)"]]
    ReduceSum_10[["ReduceSum(.)"]]
    Cast_11[["Cast(., to=FLOAT16)"]]
    Cast_12[["Cast(., to=FLOAT)"]]
    ReduceSum_13[["ReduceSum(.)"]]
    Cast_14[["Cast(., to=FLOAT16)"]]
    Div_15[["Div(., .)"]]

    I_I -->|"INT64(A)"| Equal_0
    Equal_0 -->|"BOOL(A)"| Not_1
    Not_1 -->|"BOOL(A)"| Where_2
    I_I -->|"INT64(A)"| Where_2
    Where_2 -->|"INT64(A)"| Unsqueeze_3
    I_X -->|"FLOAT16(A, B)"| LogSoftmax_4
    LogSoftmax_4 -->|"FLOAT16(A, B)"| GatherElements_5
    Unsqueeze_3 -->|"INT64(A, 1)"| GatherElements_5
    GatherElements_5 -->|"FLOAT16(A, 1)"| Squeeze_6
    Squeeze_6 -->|"FLOAT16(A)"| Neg_7
    Not_1 -->|"BOOL(A)"| Where_8
    Neg_7 -->|"FLOAT16(A)"| Where_8
    Not_1 -->|"BOOL(A)"| Cast_9
    Cast_9 -->|"FLOAT(A)"| ReduceSum_10
    ReduceSum_10 -->|"FLOAT()"| Cast_11
    Where_8 -->|"FLOAT16(A)"| Cast_12
    Cast_12 -->|"FLOAT(A)"| ReduceSum_13
    ReduceSum_13 -->|"FLOAT()"| Cast_14
    Cast_14 -->|"FLOAT16()"| Div_15
    Cast_11 -->|"FLOAT16()"| Div_15

    O_Y(["Y FLOAT16()"])
    Div_15 --> O_Y

    class I_I,I_X,O_Y ioNode
    class Equal_0,Not_1,Where_2,Unsqueeze_3,LogSoftmax_4,GatherElements_5,Squeeze_6 opNode
    class Neg_7,Where_8,Cast_9,ReduceSum_10,Cast_11,Cast_12,ReduceSum_13 opNode
    class Cast_14,Div_15 opNode
    

Outcome of the fusion:

        graph TD

    classDef ioNode fill:#dfd,stroke:#333,color:#333
    classDef initNode fill:#cccc00,stroke:#333,color:#333
    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333

    I_I(["I INT64(A)"])
    I_X(["X FLOAT16(A, B)"])

    SoftmaxCrossEntropyLoss_0[["SoftmaxCrossEntropyLoss(., .)"]]

    I_X -->|"FLOAT16(A, B)"| SoftmaxCrossEntropyLoss_0
    I_I -->|"INT64(A)"| SoftmaxCrossEntropyLoss_0

    O_Y(["Y FLOAT16()"])
    SoftmaxCrossEntropyLoss_0 --> O_Y

    class I_I,I_X,O_Y ioNode
    class SoftmaxCrossEntropyLoss_0 opNode
    
classmethod apply_pattern(g: GraphBuilder, X, indices, axis, zerof, zeroi, b)[source]#

Applies the replacement.

match_pattern(g: GraphBuilder, X, indices, axis, zerof, zeroi, b)[source]#

Builds the pattern to match.

validate_mapping(g: GraphBuilderPatternOptimization, deleted_nodes: List[NodeProto], pattern_nodes: List[NodeProto] | None = None) bool[source]#

Validates the mapping.

Parameters:
  • g – GraphBuilder

  • deleted_nodes – matched nodes from the model (to be deleted)

  • pattern_nodes – matched nodes coming from the pattern

Returns:

validate the mapping or not, default is True