yobx.xoptim.patterns.onnx_mul#

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

Replaces the sequence {Div | Mul} and {Div | Mul} + {Div | Mul} with {Div | Mul} Mul.

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_Y(["Y FLOAT(a, b)"])
    I_X(["X FLOAT(a, b)"])

    Mul_0[["Mul(., .)"]]
    Div_1[["Div(., [2.0])"]]
    Div_2[["Div(., [3.0])"]]

    Div_1 --> Mul_0
    Div_2 --> Mul_0
    I_X -->|"FLOAT(a, b)"| Div_1
    I_Y -->|"FLOAT(a, b)"| Div_2

    O_Z(["Z FLOAT(a, b)"])
    Mul_0 --> O_Z

    class I_Y,I_X,O_Z ioNode
    class Mul_0,Div_1,Div_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_Y(["Y FLOAT(a, b)"])
    I_X(["X FLOAT(a, b)"])

    Mul_0[["Mul(., .)"]]
    Div_1[["Div(., [6.0])"]]

    I_X -->|"FLOAT(a, b)"| Mul_0
    I_Y -->|"FLOAT(a, b)"| Mul_0
    Mul_0 -->|"FLOAT(a, b)"| Div_1

    O_Z(["Z FLOAT(a, b)"])
    Div_1 --> O_Z

    class I_Y,I_X,O_Z ioNode
    class Mul_0,Div_1 opNode
    
apply(g: GraphBuilder, node: NodeProto, node_left: NodeProto, node_right: 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_mul.SwitchOrderBinaryPattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

If it makes sense, switches the order of two multiplications or two additions if the broadcasting reduces one operator to a an insignificant number.

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_Y(["Y FLOAT(a, 1, 3, 4)"])
    I_X(["X FLOAT(a, 2, 3, 4)"])
    I_Z(["Z FLOAT(a, 1, 3, 4)"])

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

    I_Z -->|"FLOAT(a, 1, 3, 4)"| Add_0
    Add_1 -->|"FLOAT(a, 2, 3, 4)"| Add_0
    I_X -->|"FLOAT(a, 2, 3, 4)"| Add_1
    I_Y -->|"FLOAT(a, 1, 3, 4)"| Add_1

    O_F(["F FLOAT(a, 2, 3, 4)"])
    Add_0 --> O_F

    class I_Y,I_X,I_Z,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_Y(["Y FLOAT(a, 1, 3, 4)"])
    I_X(["X FLOAT(a, 2, 3, 4)"])
    I_Z(["Z FLOAT(a, 1, 3, 4)"])

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

    I_Y -->|"FLOAT(a, 1, 3, 4)"| Add_0
    I_Z -->|"FLOAT(a, 1, 3, 4)"| Add_0
    Add_0 -->|"FLOAT(a, 1, 3, 4)"| Add_1
    I_X -->|"FLOAT(a, 2, 3, 4)"| Add_1

    O_F(["F FLOAT(a, 2, 3, 4)"])
    Add_1 --> O_F

    class I_Y,I_X,I_Z,O_F ioNode
    class Add_0,Add_1 opNode
    
class BroadcastType(*values)[source]#

Kind of broadcast.

apply(g: GraphBuilder, node: NodeProto, node_left: NodeProto, node_right: 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.

switch_order(shape_left: Tuple[int | torch.SymInt | torch.SymFloat | float | str, ...], shape_right: Tuple[int | torch.SymInt | torch.SymFloat | float | str, ...], shape_before_left: Tuple[int | torch.SymInt | torch.SymFloat | float | str, ...], shape_before_right: Tuple[int | torch.SymInt | torch.SymFloat | float | str, ...], side: int) int[source]#

Tells if the order should be switched. side==0 indicates if shape_left comes from Op(shape_before_left, shape_before_mul).

if side == 0:

  • Case 0: (B + C) + A: Op(Op(shape_before_left, shape_before_right), shape_right)

  • Case 1: (B + A) + C: Op(Op(shape_before_left, shape_right), shape_before_right)

  • Case 2: (A + C) + B: Op(Op(shape_right, shape_before_left), shape_before_left)

The function returns the case.