yobx.xoptim.patterns_exp.binary_operators#

class yobx.xoptim.patterns_exp.binary_operators.AddAddMulMulBroadcastPattern(verbose: int = 0, priority: int = 4, broadcast: bool = True)[source]#

Replaces Add + Add by AddAdd or Mul + Mul by MulMul if they operate on the same shape.

Parameters:

broadcast – allow broadcast on the first dimensions.

class yobx.xoptim.patterns_exp.binary_operators.AddAddMulMulPattern(verbose: int = 0, priority: int = 3, broadcast: bool = False)[source]#

Replaces Add + Add by AddAdd or Mul + Mul by MulMul if they operate on the same shape.

Parameters:

broadcast – allow broadcast on the first dimensions.

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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    Add_0[["Add(., .)"]]
    Add_1[["Add(., .)"]]

    I_X -->|"FLOAT(d)"| Add_0
    I_Y -->|"FLOAT(d)"| Add_0
    Add_0 -->|"FLOAT(d)"| Add_1
    I_Z -->|"FLOAT(d)"| Add_1

    O_F(["F FLOAT(d)"])
    Add_1 --> O_F

    class I_Z,I_Y,I_X,O_F ioNode
    class Add_0,Add_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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    AddAdd_0[["onnx_extended.ortops.optim.cuda.AddAdd(., ., .)"]]

    I_X -->|"FLOAT(d)"| AddAdd_0
    I_Y -->|"FLOAT(d)"| AddAdd_0
    I_Z -->|"FLOAT(d)"| AddAdd_0

    O_F(["F FLOAT(d)"])
    AddAdd_0 --> O_F

    class I_Z,I_Y,I_X,O_F ioNode
    class AddAdd_0 opNode
    
apply(g: GraphBuilder, node_left: NodeProto, node_right: NodeProto, 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_exp.binary_operators.AddMulBroadcastPattern(verbose: int = 0, priority: int = 4, broadcast: bool = True)[source]#

Replaces Add + Mul by AddMul or Mul + Add by MulAdd if they operate on the same shape.

Parameters:

broadcast – allow broadcast on the first dimensions.

class yobx.xoptim.patterns_exp.binary_operators.AddMulPattern(verbose: int = 0, priority: int = 3, broadcast: bool = False)[source]#

Replaces Add + Mul by AddMul or Mul + Add by MulAdd if they operate on the same shape.

Parameters:

broadcast – allow broadcast on the first dimensions.

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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    Add_0[["Add(., .)"]]
    Mul_1[["Mul(., .)"]]

    I_X -->|"FLOAT(d)"| Add_0
    I_Y -->|"FLOAT(d)"| Add_0
    Add_0 -->|"FLOAT(d)"| Mul_1
    I_Z -->|"FLOAT(d)"| Mul_1

    O_F(["F FLOAT(d)"])
    Mul_1 --> O_F

    class I_Z,I_Y,I_X,O_F ioNode
    class Add_0,Mul_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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    AddMul_0[["onnx_extended.ortops.optim.cuda.AddMul(., ., .)"]]

    I_X -->|"FLOAT(d)"| AddMul_0
    I_Y -->|"FLOAT(d)"| AddMul_0
    I_Z -->|"FLOAT(d)"| AddMul_0

    O_F(["F FLOAT(d)"])
    AddMul_0 --> O_F

    class I_Z,I_Y,I_X,O_F ioNode
    class AddMul_0 opNode
    
apply(g: GraphBuilder, node_left: NodeProto, node_right: NodeProto, 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_exp.binary_operators.AddMulSharedInputBroadcastPattern(verbose: int = 0, priority: int = 4, broadcast: bool = True)[source]#

Replaces Add(A, B) and Add(A, C) by AddSharedInput(A, B, C) if they operate on the same shape. Does the same for operator Mul.

Parameters:

broadcast – allow broadcast on the first dimensions.

class yobx.xoptim.patterns_exp.binary_operators.AddMulSharedInputPattern(verbose: int = 0, priority: int = 3, broadcast: bool = False)[source]#

Replaces Add(A, B) and Add(A, C) by AddSharedInput(A, B, C) if they operate on the same shape. Does the same for operator Mul.

Parameters:

broadcast – allow broadcast on the first dimensions.

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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    Add_0[["Add(., .)"]]
    Add_1[["Add(., .)"]]

    I_X -->|"FLOAT(d)"| Add_0
    I_Y -->|"FLOAT(d)"| Add_0
    I_X -->|"FLOAT(d)"| Add_1
    I_Z -->|"FLOAT(d)"| Add_1

    O_F1(["F1 FLOAT(d)"])
    Add_0 --> O_F1
    O_F2(["F2 FLOAT(d)"])
    Add_1 --> O_F2

    class I_Z,I_Y,I_X,O_F1,O_F2 ioNode
    class Add_0,Add_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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    AddSharedInput_0[["onnx_extended.ortops.optim.cuda.AddSharedInput(., ., .)"]]

    I_X -->|"FLOAT(d)"| AddSharedInput_0
    I_Y -->|"FLOAT(d)"| AddSharedInput_0
    I_Z -->|"FLOAT(d)"| AddSharedInput_0

    O_F1(["F1 FLOAT(d)"])
    AddSharedInput_0 --> O_F1
    O_F2(["F2 FLOAT(d)"])
    AddSharedInput_0 --> O_F2

    class I_Z,I_Y,I_X,O_F1,O_F2 ioNode
    class AddSharedInput_0 opNode
    
apply(g: GraphBuilder, *nodes: 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.

can_fuse(g: GraphBuilder, nodes: List[NodeProto]) bool[source]#

Checks that one node if not using the output of another.

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_exp.binary_operators.AddMulTransposePattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Replaces (AddMul|MulAdd) + Transpose by (AddMul|MulAdd)(., transposeMiddle=1) if it is possible.

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_Z(["Z FLOAT(a, b, c, d)"])
    I_Y(["Y FLOAT(a, b, c, d)"])
    I_X(["X FLOAT(a, b, c, d)"])

    AddMul_0[["onnx_extended.ortops.optim.cuda.AddMul(., ., .)"]]
    Transpose_1[["Transpose(., perm=[0, 2, 1, 3])"]]

    I_X -->|"FLOAT(a, b, c, d)"| AddMul_0
    I_Y -->|"FLOAT(a, b, c, d)"| AddMul_0
    I_Z -->|"FLOAT(a, b, c, d)"| AddMul_0
    AddMul_0 -->|"FLOAT(a, b, c, d)"| Transpose_1

    O_final(["final FLOAT(a, b, c, d)"])
    Transpose_1 --> O_final

    class I_Z,I_Y,I_X,O_final ioNode
    class AddMul_0,Transpose_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_Z(["Z FLOAT(a, b, c, d)"])
    I_Y(["Y FLOAT(a, b, c, d)"])
    I_X(["X FLOAT(a, b, c, d)"])

    AddMul_0[["onnx_extended.ortops.optim.cuda.AddMul(., ., .)"]]

    I_X -->|"FLOAT(a, b, c, d)"| AddMul_0
    I_Y -->|"FLOAT(a, b, c, d)"| AddMul_0
    I_Z -->|"FLOAT(a, b, c, d)"| AddMul_0

    O_final(["final FLOAT(a, b, c, d)"])
    AddMul_0 --> O_final

    class I_Z,I_Y,I_X,O_final ioNode
    class AddMul_0 opNode
    
apply(g: GraphBuilder, node: NodeProto, transpose_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_exp.binary_operators.MulSigmoidPattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Replaces Mul + Sigmoid by MulSigmoid if they operate on the same input.

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(UNKNOWNDIM, UNKNOWNDIM1)"])

    Sigmoid_0[["Sigmoid(.)"]]
    Mul_1[["Mul(., .)"]]

    I_X -->|"FLOAT(UNKNOWNDIM, UNKNOWNDIM1)"| Sigmoid_0
    I_X -->|"FLOAT(UNKNOWNDIM, UNKNOWNDIM1)"| Mul_1
    Sigmoid_0 -->|"FLOAT(UNKNOWNDIM, UNKNOWNDIM1)"| Mul_1

    O_Y(["Y FLOAT(UNKNOWNDIM2, UNKNOWNDIM3)"])
    Mul_1 --> O_Y

    class I_X,O_Y ioNode
    class Sigmoid_0,Mul_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(UNKNOWNDIM, UNKNOWNDIM1)"])

    MulSigmoid_0[["onnx_extended.ortops.optim.cuda.MulSigmoid(.)"]]

    I_X -->|"FLOAT(UNKNOWNDIM, UNKNOWNDIM1)"| MulSigmoid_0

    O_Y(["Y FLOAT(UNKNOWNDIM2, UNKNOWNDIM3)"])
    MulSigmoid_0 --> O_Y

    class I_X,O_Y ioNode
    class MulSigmoid_0 opNode
    
apply(g: GraphBuilder, node_sigmoid: NodeProto, node_mul: 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_exp.binary_operators.NegXplus1Pattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Replaces 1 - X by NegXplus1 if they operate on the same input.

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(UNKNOWNDIM, UNKNOWNDIM1)"])

    Sub_0[["Sub([1.0], .)"]]

    I_X -->|"FLOAT(UNKNOWNDIM, UNKNOWNDIM1)"| Sub_0

    O_Y(["Y FLOAT(UNKNOWNDIM2, UNKNOWNDIM3)"])
    Sub_0 --> O_Y

    class I_X,O_Y ioNode
    class Sub_0 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(UNKNOWNDIM, UNKNOWNDIM1)"])

    NegXplus1_0[["onnx_extended.ortops.optim.cuda.NegXplus1(.)"]]

    I_X -->|"FLOAT(UNKNOWNDIM, UNKNOWNDIM1)"| NegXplus1_0

    O_Y(["Y FLOAT(UNKNOWNDIM2, UNKNOWNDIM3)"])
    NegXplus1_0 --> O_Y

    class I_X,O_Y ioNode
    class NegXplus1_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_exp.binary_operators.SubMulBroadcastPattern(verbose: int = 0, priority: int = 4, broadcast: bool = True)[source]#

Replaces Add + Mul by AddMul or Mul + Add by MulAdd if they operate on the same shape.

Parameters:

broadcast – allow broadcast on the first dimensions.

class yobx.xoptim.patterns_exp.binary_operators.SubMulPattern(verbose: int = 0, priority: int = 3, broadcast: bool = False)[source]#

Replaces Sub + Mul by AddMul or Mul + Add by MulAdd if they operate on the same shape.

Parameters:

broadcast – allow broadcast on the first dimensions.

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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    Sub_0[["Sub(., .)"]]
    Mul_1[["Mul(., .)"]]

    I_X -->|"FLOAT(d)"| Sub_0
    I_Y -->|"FLOAT(d)"| Sub_0
    Sub_0 -->|"FLOAT(d)"| Mul_1
    I_Z -->|"FLOAT(d)"| Mul_1

    O_F(["F FLOAT(d)"])
    Mul_1 --> O_F

    class I_Z,I_Y,I_X,O_F ioNode
    class Sub_0,Mul_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_Z(["Z FLOAT(d)"])
    I_Y(["Y FLOAT(d)"])
    I_X(["X FLOAT(d)"])

    SubMul_0[["onnx_extended.ortops.optim.cuda.SubMul(., ., .)"]]

    I_X -->|"FLOAT(d)"| SubMul_0
    I_Y -->|"FLOAT(d)"| SubMul_0
    I_Z -->|"FLOAT(d)"| SubMul_0

    O_F(["F FLOAT(d)"])
    SubMul_0 --> O_F

    class I_Z,I_Y,I_X,O_F ioNode
    class SubMul_0 opNode
    
apply(g: GraphBuilder, node_left: NodeProto, node_right: NodeProto, 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.