yobx.xoptim.patterns.onnx_cast#

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

Moves two cast operators beyond a binary operator The cast must cast from a float type to another float type.

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

    Cast_0[["Cast(., to=FLOAT16)"]]
    Cast_1[["Cast(., to=FLOAT16)"]]
    Add_2[["Add(., .)"]]

    I_X -->|"FLOAT(a, 4)"| Cast_0
    I_Y -->|"FLOAT(a, 4)"| Cast_1
    Cast_0 -->|"FLOAT16(a, 4)"| Add_2
    Cast_1 -->|"FLOAT16(a, 4)"| Add_2

    O_Z(["Z FLOAT16(a, 4)"])
    Add_2 --> O_Z

    class I_Y,I_X,O_Z ioNode
    class Cast_0,Cast_1,Add_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, 4)"])
    I_X(["X FLOAT(a, 4)"])

    Add_0[["Add(., .)"]]
    Cast_1[["Cast(., to=FLOAT16)"]]

    I_X -->|"FLOAT(a, 4)"| Add_0
    I_Y -->|"FLOAT(a, 4)"| Add_0
    Add_0 -->|"FLOAT(a, 4)"| Cast_1

    O_Z(["Z FLOAT16(a, 4)"])
    Cast_1 --> O_Z

    class I_Y,I_X,O_Z ioNode
    class Add_0,Cast_1 opNode
    
apply(g: GraphBuilder, left: NodeProto, 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.onnx_cast.CastCastPattern(verbose: int = 0, priority: int = 0)[source]#

Checks that two consecutive cast can be avoided.

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 FLOAT16(b, c)"])

    Cast_0[["Cast(., to=FLOAT)"]]
    Cast_1[["Cast(., to=FLOAT)"]]

    I_x1 -->|"FLOAT16(b, c)"| Cast_0
    Cast_0 -->|"FLOAT(b, c)"| Cast_1

    O_Y(["Y FLOAT(b, c)"])
    Cast_1 --> O_Y

    class I_x1,O_Y ioNode
    class Cast_0,Cast_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_x1(["x1 FLOAT16(b, c)"])

    Cast_0[["Cast(., to=FLOAT)"]]

    I_x1 -->|"FLOAT16(b, c)"| Cast_0

    O_Y(["Y FLOAT(b, c)"])
    Cast_0 --> O_Y

    class I_x1,O_Y ioNode
    class Cast_0 opNode
    
apply(g: GraphBuilder, cast1: NodeProto, cast2: 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_cast.CastOpCastPattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Removes two cast surrounding another operator.

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

    Cast_0[["Cast(., to=FLOAT)"]]
    Add_1[["Add(., .)"]]
    Cast_2[["Cast(., to=FLOAT16)"]]

    I_Y -->|"FLOAT16(a, b)"| Cast_0
    I_X -->|"FLOAT(a, b)"| Add_1
    Cast_0 -->|"FLOAT(a, b)"| Add_1
    Add_1 -->|"FLOAT(a, b)"| Cast_2

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

    class I_Y,I_X,O_Z ioNode
    class Cast_0,Add_1,Cast_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 FLOAT16(a, b)"])
    I_X(["X FLOAT(a, b)"])

    Cast_0[["Cast(., to=FLOAT16)"]]
    Add_1[["Add(., .)"]]

    I_X -->|"FLOAT(a, b)"| Cast_0
    Cast_0 -->|"FLOAT16(a, b)"| Add_1
    I_Y -->|"FLOAT16(a, b)"| Add_1

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

    class I_Y,I_X,O_Z ioNode
    class Cast_0,Add_1 opNode
    
apply(g: GraphBuilder, cast_in_left: NodeProto, cast_in_right: NodeProto, node: NodeProto, cast_out_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_cast.CastPattern(verbose: int = 0, priority: int = 0)[source]#

Checks that a Cast is really needed.

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__onx_mul045(["_onx_mul045 FLOAT16(4, 512, 16384)"])

    Cast_0[["Cast(., to=FLOAT16)"]]

    I__onx_mul045 -->|"FLOAT16(4, 512, 16384)"| Cast_0

    O_mul_34(["mul_34 FLOAT16(4, 512, 16384)"])
    Cast_0 --> O_mul_34

    class I__onx_mul045,O_mul_34 ioNode
    class Cast_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__onx_mul045(["_onx_mul045 FLOAT16(4, 512, 16384)"])

    Identity_0[["Identity(.)"]]

    I__onx_mul045 -->|"FLOAT16(4, 512, 16384)"| Identity_0

    O_mul_34(["mul_34 FLOAT16(4, 512, 16384)"])
    Identity_0 --> O_mul_34

    class I__onx_mul045,O_mul_34 ioNode
    class Identity_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_cast.ComputationCastOpCastPattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Changes the computation type to make it faster if one of the inputs was just casted before.

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

    Cast_0[["Cast(., to=FLOAT)"]]
    Add_1[["Add(., .)"]]

    I_Y -->|"FLOAT16(a, b)"| Cast_0
    I_X -->|"FLOAT(a, b)"| Add_1
    Cast_0 -->|"FLOAT(a, b)"| Add_1

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

    class I_Y,I_X,O_Z ioNode
    class Cast_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 FLOAT16(a, b)"])
    I_X(["X FLOAT(a, b)"])

    Cast_0[["Cast(., to=FLOAT16)"]]
    Add_1[["Add(., .)"]]
    Cast_2[["Cast(., to=FLOAT)"]]

    I_X -->|"FLOAT(a, b)"| Cast_0
    Cast_0 -->|"FLOAT16(a, b)"| Add_1
    I_Y -->|"FLOAT16(a, b)"| Add_1
    Add_1 -->|"FLOAT16(a, b)"| Cast_2

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

    class I_Y,I_X,O_Z ioNode
    class Cast_0,Add_1,Cast_2 opNode
    
apply(g: GraphBuilder, node_left: NodeProto | None, node_right: NodeProto | None, 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.