yobx.xoptim.patterns.onnx_reduce#

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

Fuses ReduceMin(X, axis), ArgMin(X, axis) into TopK(, k=1).

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_one(["one INT64(1)"])

    Constant_0[["Constant() -#gt; one"]]
    ReduceMin_1[["ReduceMin(., .)"]]
    ArgMin_2[["ArgMin(., axis=1)"]]

    I_X -->|"FLOAT(a, b)"| ReduceMin_1
    Constant_0 -->|"INT64(1)"| ReduceMin_1
    I_X -->|"FLOAT(a, b)"| ArgMin_2

    O_Y2(["Y2 INT64(a)"])
    ArgMin_2 --> O_Y2
    O_Y1(["Y1 FLOAT(a)"])
    ReduceMin_1 --> O_Y1

    class I_X,I_one,O_Y2,O_Y1 ioNode
    class Constant_0 constNode
    class ReduceMin_1,ArgMin_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_X(["X FLOAT(a, b)"])
    I_one(["one INT64(1)"])

    TopK_0[["TopK(., ., axis=1)"]]
    Squeeze_1[["Squeeze(., .)"]]
    Squeeze_2[["Squeeze(., .)"]]

    I_X -->|"FLOAT(a, b)"| TopK_0
    I_one -->|"INT64(1)"| TopK_0
    TopK_0 --> Squeeze_1
    I_one -->|"INT64(1)"| Squeeze_1
    TopK_0 --> Squeeze_2
    I_one -->|"INT64(1)"| Squeeze_2

    O_Y2(["Y2 INT64(a)"])
    Squeeze_2 --> O_Y2
    O_Y1(["Y1 FLOAT(a)"])
    Squeeze_1 --> O_Y1

    class I_X,I_one,O_Y2,O_Y1 ioNode
    class TopK_0,Squeeze_1,Squeeze_2 opNode
    
apply(g: GraphBuilder, reduce_node: NodeProto, arg_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_reduce.ReduceSumNormalizePattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Nodes equivalent to a reduction.

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 FLOAT16(a, b)"])
    I_axis(["axis INT64()"])

    Constant_0[["Constant() -#gt; axis"]]
    Cast_1[["Cast(., to=FLOAT)"]]
    ReduceSum_2[["ReduceSum(., .)"]]
    Mul_3[["Mul(., .)"]]
    Sub_4[["Sub(., .)"]]
    Cast_5[["Cast(., to=FLOAT16)"]]

    I_X -->|"FLOAT16(a, b)"| Cast_1
    Cast_1 -->|"FLOAT(a, b)"| ReduceSum_2
    Constant_0 -->|"INT64()"| ReduceSum_2
    ReduceSum_2 -->|"FLOAT(a, 1)"| Mul_3
    I_Y -->|"FLOAT(a, b)"| Mul_3
    Cast_1 -->|"FLOAT(a, b)"| Sub_4
    Mul_3 -->|"FLOAT(a, b)"| Sub_4
    Sub_4 -->|"FLOAT(a, b)"| Cast_5

    O_Z(["Z FLOAT16(a, b)"])
    Cast_5 --> O_Z

    class I_Y,I_X,I_axis,O_Z ioNode
    class Constant_0 constNode
    class Cast_1,ReduceSum_2,Mul_3,Sub_4,Cast_5 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 FLOAT16(a, b)"])
    I_axis(["axis INT64()"])

    ReduceSum_0[["ReduceSum(., .)"]]
    Cast_1[["Cast(., to=FLOAT16)"]]
    Mul_2[["Mul(., .)"]]
    Sub_3[["Sub(., .)"]]

    I_X -->|"FLOAT16(a, b)"| ReduceSum_0
    I_axis -->|"INT64()"| ReduceSum_0
    I_Y -->|"FLOAT(a, b)"| Cast_1
    ReduceSum_0 --> Mul_2
    Cast_1 -->|"FLOAT16(a, b)"| Mul_2
    I_X -->|"FLOAT16(a, b)"| Sub_3
    Mul_2 --> Sub_3

    O_Z(["Z FLOAT16(a, b)"])
    Sub_3 --> O_Z

    class I_Y,I_X,I_axis,O_Z ioNode
    class ReduceSum_0,Cast_1,Mul_2,Sub_3 opNode
    
apply(g: GraphBuilder, cast_node: NodeProto, node: NodeProto, mul_node: NodeProto, sub_node: NodeProto, cast2_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.