yobx.xoptim.patterns.onnx_transpose#
- class yobx.xoptim.patterns.onnx_transpose.SwapUnsqueezeTransposePattern(verbose: int = 0, priority: int = 0)[source]#
Swaps Unsqueeze and Transpose.
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, c)"]) I_axes(["axes INT64(2)"]) Constant_0[["Constant() -#gt; axes"]] Unsqueeze_1[["Unsqueeze(., .)"]] Transpose_2[["Transpose(., perm=[0, 2, 1, 4, 3])"]] I_X -->|"FLOAT(a, b, c)"| Unsqueeze_1 Constant_0 -->|"INT64(2)"| Unsqueeze_1 Unsqueeze_1 -->|"FLOAT(a, 1, 1, b, c)"| Transpose_2 O_Y(["Y FLOAT(e, f, g, h, i)"]) Transpose_2 --> O_Y class I_X,I_axes,O_Y ioNode class Constant_0 constNode class Unsqueeze_1,Transpose_2 opNodeOutcome 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, c)"]) I_axes(["axes INT64(2)"]) Transpose_0[["Transpose(., perm=[0, 2, 1])"]] Unsqueeze_1[["Unsqueeze(., .)"]] I_X -->|"FLOAT(a, b, c)"| Transpose_0 Transpose_0 -->|"FLOAT(a, c, b)"| Unsqueeze_1 I_axes -->|"INT64(2)"| Unsqueeze_1 O_Y(["Y FLOAT(e, f, g, h, i)"]) Unsqueeze_1 --> O_Y class I_X,I_axes,O_Y ioNode class Transpose_0,Unsqueeze_1 opNode- apply(g: GraphBuilder, unsqueeze_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.onnx_transpose.TransposeEqualReshapePattern(verbose: int = 0, priority: int = 1, min_opset: int = 1)[source]#
Replaces a Transpose by a Reshape when switched dimensions are all equal to 1 but one.
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(3, 2, 1, 5)"]) Transpose_0[["Transpose(., perm=[0, 2, 1, 3])"]] I_X -->|"FLOAT(3, 2, 1, 5)"| Transpose_0 O_Y(["Y FLOAT(a, b, c, d)"]) Transpose_0 --> O_Y class I_X,O_Y ioNode class Transpose_0 opNodeOutcome 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(3, 2, 1, 5)"]) Reshape_0[["Reshape(., [0, 1, -1, 0])"]] I_X -->|"FLOAT(3, 2, 1, 5)"| Reshape_0 O_Y(["Y FLOAT(a, b, c, d)"]) Reshape_0 --> O_Y class I_X,O_Y ioNode class Reshape_0 opNode- apply(g: GraphBuilder, 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.onnx_transpose.TransposeGatherPattern(verbose: int = 0, priority: int = 0)[source]#
Removes one unnecessary transpose followed by Gather with only one index.
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, 16, 80)"]) I_ind(["ind INT64()"]) Constant_0[["Constant() -#gt; ind"]] Transpose_1[["Transpose(., perm=[1, 0, 2, 3])"]] Gather_2[["Gather(., ., axis=0)"]] I_X -->|"FLOAT(a, b, 16, 80)"| Transpose_1 Transpose_1 -->|"FLOAT(b, a, 16, 80)"| Gather_2 Constant_0 -->|"INT64()"| Gather_2 O_Y(["Y FLOAT(a, 16, 80)"]) Gather_2 --> O_Y class I_X,I_ind,O_Y ioNode class Constant_0 constNode class Transpose_1,Gather_2 opNodeOutcome 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, 16, 80)"]) I_ind(["ind INT64()"]) Gather_0[["Gather(., ., axis=1)"]] I_X -->|"FLOAT(a, b, 16, 80)"| Gather_0 I_ind -->|"INT64()"| Gather_0 O_Y(["Y FLOAT(a, 16, 80)"]) Gather_0 --> O_Y class I_X,I_ind,O_Y ioNode class Gather_0 opNode- apply(g: GraphBuilder, transpose_node: NodeProto, gather_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_transpose.TransposeReshapeTransposePattern(verbose: int = 0, priority: int = 0)[source]#
Swaps Reshape and Transpose in a sequence such as this one:
input is 32x4x14x4x14x128 Transpose(., perm=[0, 1, 3, 2, 4, 5]) Reshape(., 32x56x56x128) Transpose(., perm=[0, 3, 1, 2])
By:
Transpose(., perm=[0, 1, 3, 2, 4, 5]) Transpose(., perm=[0, 5, 1, 2, 3, 4]) Reshape(., 32x128x56x56)
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_xts(["xts FLOAT(32, 2, 14, 2, 13, 256)"]) I_X(["X FLOAT(32, 256, 28, 26)"]) Transpose_0[["Transpose(., perm=[0, 2, 3, 1])"]] Reshape_1[["Reshape(., [32, 2, 14, 2, 13, 256])"]] Transpose_2[["Transpose(., perm=[0, 1, 3, 2, 4, 5])"]] I_X -->|"FLOAT(32, 256, 28, 26)"| Transpose_0 Transpose_0 -->|"FLOAT(32, 28, 26, 256)"| Reshape_1 Reshape_1 -->|"FLOAT(32, 2, 14, 2, 13, 256)"| Transpose_2 O_xts(["xts FLOAT(32, 2, 14, 2, 13, 256)"]) Reshape_1 --> O_xts O_Y(["Y FLOAT(32, 2, 2, 14, 13, 256)"]) Transpose_2 --> O_Y class I_xts,I_X,O_xts,O_Y ioNode class Transpose_0,Reshape_1,Transpose_2 opNodeOutcome 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_xts(["xts FLOAT(32, 2, 14, 2, 13, 256)"]) I_X(["X FLOAT(32, 256, 28, 26)"]) Reshape_0[["Reshape(., [32, 256, 2, 14, 2, 13])"]] Transpose_1[["Transpose(., perm=[0, 2, 3, 4, 5, 1])"]] Transpose_2[["Transpose(., perm=[0, 1, 3, 2, 4, 5])"]] I_X -->|"FLOAT(32, 256, 28, 26)"| Reshape_0 Reshape_0 -->|"FLOAT(32, 256, 2, 14, 2, 13)"| Transpose_1 Transpose_1 -->|"FLOAT(32, 2, 14, 2, 13, 256)"| Transpose_2 O_xts(["xts FLOAT(32, 2, 14, 2, 13, 256)"]) Transpose_1 --> O_xts O_Y(["Y FLOAT(32, 2, 2, 14, 13, 256)"]) Transpose_2 --> O_Y class I_xts,I_X,O_xts,O_Y ioNode class Reshape_0,Transpose_1,Transpose_2 opNode- apply(g: GraphBuilder, t1_node: NodeProto, reshape_node: NodeProto, t2_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_transpose.TransposeTransposePattern(verbose: int = 0, priority: int = 0)[source]#
Removes two consecutive transpose if the second one put the tensor in origin shape.
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_xs(["xs FLOAT(1, 1, 32, 128)"]) Transpose_0[["Transpose(., perm=[1, 0, 3, 2])"]] Transpose_1[["Transpose(., perm=[0, 1, 3, 2])"]] I_xs -->|"FLOAT(1, 1, 32, 128)"| Transpose_0 Transpose_0 -->|"FLOAT(1, 1, 128, 32)"| Transpose_1 O_xm1(["xm1 FLOAT(1, 1, 32, 128)"]) Transpose_1 --> O_xm1 class I_xs,O_xm1 ioNode class Transpose_0,Transpose_1 opNodeOutcome 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_xs(["xs FLOAT(1, 1, 32, 128)"]) Transpose_0[["Transpose(., perm=[1, 0, 2, 3])"]] I_xs -->|"FLOAT(1, 1, 32, 128)"| Transpose_0 O_xm1(["xm1 FLOAT(1, 1, 32, 128)"]) Transpose_0 --> O_xm1 class I_xs,O_xm1 ioNode class Transpose_0 opNode- 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.