yobx.xoptim.patterns.onnx_unsqueeze#
- class yobx.xoptim.patterns.onnx_unsqueeze.MulUnsqueezeUnsqueezePattern(verbose: int = 0, priority: int = 0)[source]#
Replaces
Mul(Unsqueeze(x, axes), Unsqueeze(y, axes))byUnsqueeze(Mul(x, y), axes)when both inputs are unsqueezed with the same axes.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_Y(["Y FLOAT(a, b)"]) Unsqueeze_0[["Unsqueeze(., [2])"]] Unsqueeze_1[["Unsqueeze(., [2])"]] Mul_2[["Mul(., .)"]] I_X -->|"FLOAT(a, b)"| Unsqueeze_0 I_Y -->|"FLOAT(a, b)"| Unsqueeze_1 Unsqueeze_0 -->|"FLOAT(a, b, 1)"| Mul_2 Unsqueeze_1 -->|"FLOAT(a, b, 1)"| Mul_2 O_Z(["Z FLOAT(a, b, 1)"]) Mul_2 --> O_Z class I_X,I_Y,O_Z ioNode class Unsqueeze_0,Unsqueeze_1,Mul_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)"]) I_Y(["Y FLOAT(a, b)"]) Mul_0[["Mul(., .)"]] Unsqueeze_1[["Unsqueeze(., [2])"]] I_X -->|"FLOAT(a, b)"| Mul_0 I_Y -->|"FLOAT(a, b)"| Mul_0 Mul_0 -->|"FLOAT(a, b)"| Unsqueeze_1 O_Z(["Z FLOAT(a, b, 1)"]) Unsqueeze_1 --> O_Z class I_X,I_Y,O_Z ioNode class Mul_0,Unsqueeze_1 opNode- apply(g: GraphBuilder, unsqueeze1: NodeProto, unsqueeze2: NodeProto, mul: 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_unsqueeze.SqueezeAddPattern(verbose: int = 0, priority: int = 0)[source]#
Replaces the sequence Add(Squeeze, Squeeze) by Squeeze(Add).
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_S2(["S2 INT64(1)"]) I_S1(["S1 INT64(1)"]) Squeeze_0[["Squeeze(.)"]] Squeeze_1[["Squeeze(.)"]] Add_2[["Add(., .)"]] I_S1 -->|"INT64(1)"| Squeeze_0 I_S2 -->|"INT64(1)"| Squeeze_1 Squeeze_0 -->|"INT64()"| Add_2 Squeeze_1 -->|"INT64()"| Add_2 O_s(["s INT64()"]) Add_2 --> O_s class I_S2,I_S1,O_s ioNode class Squeeze_0,Squeeze_1,Add_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_S2(["S2 INT64(1)"]) I_S1(["S1 INT64(1)"]) Add_0[["Add(., .)"]] Squeeze_1[["Squeeze(.)"]] I_S1 -->|"INT64(1)"| Add_0 I_S2 -->|"INT64(1)"| Add_0 Add_0 -->|"INT64(1)"| Squeeze_1 O_s(["s INT64()"]) Squeeze_1 --> O_s class I_S2,I_S1,O_s ioNode class Add_0,Squeeze_1 opNode- apply(g: GraphBuilder, squeeze1: NodeProto, squeeze2: NodeProto, add: 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_unsqueeze.SqueezeBinaryUnsqueezePattern(verbose: int = 0, priority: int = 0)[source]#
Replaces the sequence Squeeze Binary Unsqueeze) by Binary.
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_zero(["zero INT64(1)"]) I_d(["d INT64(1)"]) I_two(["two INT64()"]) Constant_0[["Constant() -#gt; two"]] Constant_1[["Constant() -#gt; zero"]] Squeeze_2[["Squeeze(.)"]] Div_3[["Div(., .)"]] Unsqueeze_4[["Unsqueeze(., .)"]] I_d -->|"INT64(1)"| Squeeze_2 Squeeze_2 -->|"INT64()"| Div_3 Constant_0 -->|"INT64()"| Div_3 Div_3 -->|"INT64()"| Unsqueeze_4 Constant_1 -->|"INT64(1)"| Unsqueeze_4 O_e(["e INT64(1)"]) Unsqueeze_4 --> O_e class I_zero,I_d,I_two,O_e ioNode class Constant_0,Constant_1 constNode class Squeeze_2,Div_3,Unsqueeze_4 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_zero(["zero INT64(1)"]) I_d(["d INT64(1)"]) I_two(["two INT64()"]) Unsqueeze_0[["Unsqueeze(., .)"]] Div_1[["Div(., .)"]] I_two -->|"INT64()"| Unsqueeze_0 I_zero -->|"INT64(1)"| Unsqueeze_0 I_d -->|"INT64(1)"| Div_1 Unsqueeze_0 --> Div_1 O_e(["e INT64(1)"]) Div_1 --> O_e class I_zero,I_d,I_two,O_e ioNode class Unsqueeze_0,Div_1 opNode- apply(g: GraphBuilder, squeeze_node: NodeProto, binary_node: NodeProto, unsqueeze_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_unsqueeze.SqueezeUnsqueezePattern(verbose: int = 0, priority: int = 0)[source]#
Replaces the sequence Squeeze, Unsqueeze by Identity or the other ways around.
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, 1, 1, d)"]) Unsqueeze_0[["Unsqueeze(., [1, 2])"]] Squeeze_1[["Squeeze(., [1, 2])"]] I_X -->|"FLOAT(a, 1, 1, d)"| Unsqueeze_0 Unsqueeze_0 -->|"FLOAT(a, 1, 1, 1, 1, d)"| Squeeze_1 O_Y(["Y FLOAT(a, 1, 1, d)"]) Squeeze_1 --> O_Y class I_X,O_Y ioNode class Unsqueeze_0,Squeeze_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_X(["X FLOAT(a, 1, 1, d)"]) Identity_0[["Identity(.)"]] I_X -->|"FLOAT(a, 1, 1, d)"| Identity_0 O_Y(["Y FLOAT(a, 1, 1, d)"]) Identity_0 --> O_Y class I_X,O_Y ioNode class Identity_0 opNode- apply(g: GraphBuilder, node_first: NodeProto, node_second: 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_unsqueeze.UnsqueezeUnsqueezePattern(verbose: int = 0, priority: int = 0)[source]#
Replaces the sequence Unsqueeze, Unsqueeze by Unsqueeze.
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)"]) Unsqueeze_0[["Unsqueeze(., [2])"]] Unsqueeze_1[["Unsqueeze(., [3])"]] I_X -->|"FLOAT(a, b)"| Unsqueeze_0 Unsqueeze_0 -->|"FLOAT(a, b, 1)"| Unsqueeze_1 O_Y(["Y FLOAT(1, 1, a, b)"]) Unsqueeze_1 --> O_Y class I_X,O_Y ioNode class Unsqueeze_0,Unsqueeze_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_X(["X FLOAT(a, b)"]) Unsqueeze_0[["Unsqueeze(., [2, 3])"]] I_X -->|"FLOAT(a, b)"| Unsqueeze_0 O_Y(["Y FLOAT(1, 1, a, b)"]) Unsqueeze_0 --> O_Y class I_X,O_Y ioNode class Unsqueeze_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.