yobx.xoptim.patterns.onnx_reshape#

class yobx.xoptim.patterns.onnx_reshape.ConcatReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Tries to reduce the number of nodes in the sequence Concat + Reshape by replacing one of the dimension by -1.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 'c', 'd'])
    I1([Input: I1 <br/> int64, 1])
    I2([Input: I2 <br/> int64, 1])
    SH1[[Shape <br/> start=2, end=3]]
    SH2[[Shape <br/> start=3, end=4]]
    CC[[Concat <br/> axis=0]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 'a', 'b', 'd', 'c'])

    %% Flow
    X --> SH1
    X --> SH2
    SH1 --> CC
    SH2 --> CC
    I1 --> CC
    I2 --> CC
    CC --> R
    X --> R
    R --> Y

    %% Styling
    class SH1 opNode
    class SH2 opNode
    class CC opNode
    class R opNode
    class X ioNode
    class I1 ioNode
    class I2 ioNode
    class Y ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 'c', 'd'])
    I1([Input: I1 <br/> int64, 1])
    I2([Input: I2 <br/> int64, 1])
    D1([Input: D1 <br/> int64, 1])
    CM1[Constant <br/> value: -1]
    CC[[Concat <br/> axis=0]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 'a', 'b', 'd', 'c'])

    %% Flow
    I1 --> CC
    I2 --> CC
    D1 --> CC
    CM1 --> CC
    CC --> R
    X --> R
    R --> Y

    %% Styling
    class CM1 constNode
    class CC opNode
    class R opNode
    class X ioNode
    class I1 ioNode
    class I2 ioNode
    class D1 ioNode
    class Y ioNode
    
apply(g: GraphBuilder, concat: NodeProto, reshape: 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_reshape.ReduceReshapePattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Replaces the sequence Reduce* Reshape if reshape is only introduces to deal with a dimension kept because keepdims=1.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 3, 2])
    C[Constant <br/> value: 3]
    RS[[ReduceSum <br/> axes=1, keepdims=1]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 3])

    %% Flow
    X --> RS
    RS --> R
    C -->|shape| R
    R --> Y

    %% Styling
    class C constNode
    class RS opNode
    class R opNode
    class X ioNode
    class Y ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 3, 2])
    RS[[ReduceSum <br/> axes=1, keepdims=0]]
    Y([Output: Y <br/> float, 3])

    %% Flow
    X --> RS
    RS --> Y

    %% Styling
    class RS opNode
    class X ioNode
    class Y ioNode
    
apply(g: GraphBuilder, node: NodeProto, next_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_reshape.Reshape2Of3Pattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#

Replaces the reshapes around element-wise operators. It can be 3 or 2 out of 3.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 4])
    Y([Input: Y <br/> float, 2, 3, 4])
    C1[Constant <br/> value: -1, 8]
    C2[Constant <br/> value: 3, -1]
    C3[Constant <br/> value: 2, 3, 4]
    R1[[Reshape]]
    R2[[Reshape]]
    R3[[Reshape]]
    M[[Mul]]
    xrr([Output: xrr <br/> float, 3, 8])
    Z([Output: Z <br/> float, 2, 3, 4])

    %% Flow
    C1 -->|shape1| R1
    X --> R1
    C2 -->|shape2| R2
    Y --> R2
    R1 --> M
    R2 --> M
    M --> xrr
    M --> R3
    C3 -->|shape3| R3
    R3 --> Z

    %% Styling
    class C1 constNode
    class C2 constNode
    class C3 constNode
    class R1 opNode
    class R2 opNode
    class R3 opNode
    class M opNode
    class X ioNode
    class Y ioNode
    class xrr ioNode
    class Z ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 4])
    Y([Input: Y <br/> float, 2, 3, 4])
    C2[Constant <br/> value: 3, -1]
    M[[Mul]]
    R[[Reshape]]
    xrr([Output: xrr <br/> float, 3, 8])
    Z([Output: Z <br/> float, 2, 3, 4])

    %% Flow
    X --> M
    Y --> M
    M --> Z
    C2 -->|shape2| R
    M --> R
    R --> xrr

    %% Styling
    class C2 constNode
    class M opNode
    class R opNode
    class X ioNode
    class Y ioNode
    class xrr ioNode
    class Z ioNode
    
apply(g: GraphBuilder, node_left: NodeProto, node_right: NodeProto, next_node: 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_reshape.ReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Checks that a Reshape is really needed.

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

Moves two reshape operators beyond a binary operator if it is possible.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 4])
    Y([Input: Y <br/> float, 'a', 4])
    C1[Constant <br/> value: -1, 8]
    C2[Constant <br/> value: -1, 8]
    R1[[Reshape]]
    R2[[Reshape]]
    A[[Add]]
    Z([Output: Z <br/> float, 'b', 8])

    %% Flow
    C1 -->|sh1| R1
    X --> R1
    C2 -->|sh2| R2
    Y --> R2
    R1 --> A
    R2 --> A
    A --> Z

    %% Styling
    class C1 constNode
    class C2 constNode
    class R1 opNode
    class R2 opNode
    class A opNode
    class X ioNode
    class Y ioNode
    class Z ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 4])
    Y([Input: Y <br/> float, 'a', 4])
    C1[Constant <br/> value: -1, 8]
    A[[Add]]
    R[[Reshape]]
    Z([Output: Z <br/> float, 'b', 8])

    %% Flow
    X --> A
    Y --> A
    A --> R
    C1 -->|sh1| R
    R --> Z

    %% Styling
    class C1 constNode
    class A opNode
    class R opNode
    class X ioNode
    class Y ioNode
    class Z ioNode
    
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_reshape.ReshapeReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Replaces the sequence Reshape, Reshape by Reshape.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 128])
    C1[Constant <br/> value: 4096, 7, 7, 128]
    C2[Constant <br/> value: 4096, 49, 128]
    R1[[Reshape]]
    R2[[Reshape]]
    s2([Output: s2 <br/> float, 4096, 49, 128])

    %% Flow
    C1 -->|sh1| R1
    X --> R1
    R1 --> R2
    C2 -->|sh2| R2
    R2 --> s2

    %% Styling
    class C1 constNode
    class C2 constNode
    class R1 opNode
    class R2 opNode
    class X ioNode
    class s2 ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 128])
    C2[Constant <br/> value: 4096, 49, 128]
    R[[Reshape]]
    s2([Output: s2 <br/> float, 4096, 49, 128])

    %% Flow
    C2 -->|sh2| R
    X --> R
    R --> s2

    %% Styling
    class C2 constNode
    class R opNode
    class X ioNode
    class s2 ioNode
    
apply(g: GraphBuilder, node: NodeProto, next_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_reshape.ShapeBasedEditDistanceReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Tries to reduce the number of nodes in the sequence Concat + Reshape by replacing one of the dimension by -1 or 0. The pattern tries to align shape information to infer a static shape.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 'd'])
    I1[Initializer <br/> value: -1]
    SH[[Shape <br/> start=2, end=3]]
    CC[[Concat <br/> axis=0]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 6, 'd'])

    %% Flow
    X --> SH
    SH --> CC
    I1 --> CC
    CC --> R
    X --> R
    R --> Y

    %% Styling
    class I1 constNode
    class SH opNode
    class CC opNode
    class R opNode
    class X ioNode
    class Y ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 'd'])
    C[Constant <br/> value: 6, -1]
    R[[Reshape]]
    Y([Output: Y <br/> float, 6, 'd'])

    %% Flow
    C -->|shape| R
    X --> R
    R --> Y

    %% Styling
    class C constNode
    class R opNode
    class X ioNode
    class Y ioNode
    
apply(g: GraphBuilder, reshape: 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_reshape.ShapeBasedReshapeIsSqueezePattern(verbose: int = 0, priority: int = 0)[source]#

Replaces a replaces by a squeeze or unsqueeze pattern if possible. It is only available for opset >= 18.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 'd'])
    one[Initializer <br/> value: 1]
    SH[[Shape]]
    CC[[Concat <br/> axis=0]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 1, 2, 3, 'd', 1])

    %% Flow
    X --> SH
    SH --> CC
    one --> CC
    one --> CC
    CC --> R
    X --> R
    R --> Y

    %% Styling
    class one constNode
    class SH opNode
    class CC opNode
    class R opNode
    class X ioNode
    class Y ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 'd'])
    C[Constant <br/> value: 0, 4]
    U[[Unsqueeze]]
    Y([Output: Y <br/> float, 1, 2, 3, 'd', 1])

    %% Flow
    C -->|axes| U
    X --> U
    U --> Y

    %% Styling
    class C constNode
    class U opNode
    class X ioNode
    class Y ioNode
    
apply(g: GraphBuilder, reshape: 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_reshape.ShapedBasedReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Checks that a Reshape is really needed based on the input shape.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 'c'])
    C[Constant <br/> value: 0, 0, -1]
    R[[Reshape]]
    xrr([Output: xrr <br/> float, 'a', 'b', 'c'])

    %% Flow
    C -->|shape2| R
    X --> R
    R --> xrr

    %% Styling
    class C constNode
    class R opNode
    class X ioNode
    class xrr ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    X[Input: X] --> ID[Identity]
    S[Input: shape2] -.->|Invalid?| ID
    ID --> xrr[Output: xrr]

    class ID constNode
    
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_reshape.StaticConcatReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Tries to reduce the number of nodes in the sequence Concat + Reshape by replacing one of the dimension by -1.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 'd'])
    I1([Input: I1 <br/> int64, 1])
    SH[[Shape <br/> start=2, end=3]]
    CC[[Concat <br/> axis=0]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 6, 'd'])

    %% Flow
    X --> SH
    SH --> CC
    I1 --> CC
    CC --> R
    X --> R
    R --> Y

    %% Styling
    class SH opNode
    class CC opNode
    class R opNode
    class X ioNode
    class I1 ioNode
    class Y ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 2, 3, 'd'])
    I1([Input: I1 <br/> int64, 1])
    CM1[Constant <br/> value: -1]
    CC[[Concat <br/> axis=0]]
    R[[Reshape]]
    Y([Output: Y <br/> float, 6, 'd'])

    %% Flow
    I1 --> CC
    CM1 --> CC
    CC --> R
    X --> R
    R --> Y

    %% Styling
    class CM1 constNode
    class CC opNode
    class R opNode
    class X ioNode
    class I1 ioNode
    class Y ioNode
    
apply(g: GraphBuilder, concat: NodeProto, reshape: 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_reshape.UnsqueezeOrSqueezeReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Replaces the sequence Unsqueeze or Unsqueeze, Reshape by Reshape.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 8, 16])
    S3([Input: shape3 <br/> int64, 2])
    C[Constant <br/> value: 0]
    U[[Unsqueeze]]
    R[[Reshape]]
    Z([Output: Z <br/> float, '2*a', 64])

    %% Flow
    C -->|axes| U
    X --> U
    U --> R
    S3 -->|shape| R
    R --> Z

    %% Styling
    class C constNode
    class U opNode
    class R opNode
    class X ioNode
    class S3 ioNode
    class Z ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 8, 16])
    S3([Input: shape3 <br/> int64, 2])
    R[[Reshape]]
    Z([Output: Z <br/> float, '2*a', 64])

    %% Flow
    X --> R
    S3 -->|shape| R
    R --> Z

    %% Styling
    class R opNode
    class X ioNode
    class S3 ioNode
    class Z ioNode
    
apply(g: GraphBuilder, node_before: NodeProto, reshape_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_reshape.UnsqueezeReshapePattern(verbose: int = 0, priority: int = 0)[source]#

Unsqueeze + Reshape into Unsqueeze at a different place if possible.

Model with nodes to be fused:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 'c'])
    C1[Constant <br/> value: 2]
    C2[Constant <br/> value: 0, 1, -1, 0]
    U[[Unsqueeze]]
    R[[Reshape]]
    Z([Output: Z <br/> float, 'e', 'f', 'g', 'h'])

    %% Flow
    C1 -->|axes| U
    X --> U
    U --> R
    C2 -->|shape| R
    R --> Z

    %% Styling
    class C1 constNode
    class C2 constNode
    class U opNode
    class R opNode
    class X ioNode
    class Z ioNode
    

Outcome of the fusion:

        graph TD

    classDef constNode fill:#f9f,stroke:#333,stroke-width:2px,color:#333
    classDef opNode fill:#bbf,stroke:#333,stroke-width:2px,color:#333
    classDef ioNode fill:#dfd,stroke:#333,color:#333
    %% Nodes
    X([Input: X <br/> float, 'a', 'b', 'c'])
    C[Constant <br/> value: 1]
    U[[Unsqueeze]]
    Z([Output: Z <br/> float, 'e', 'f', 'g', 'h'])

    %% Flow
    C -->|axes| U
    X --> U
    U --> Z

    %% Styling
    class C constNode
    class U opNode
    class X ioNode
    class Z ioNode
    
apply(g: GraphBuilder, unsqueeze: NodeProto, reshape: 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.