yobx.xoptim.patterns_ort.complex_mul#
- class yobx.xoptim.patterns_ort.complex_mul.ComplexMulConjPattern(verbose: int = 0, priority: int = 2)[source]#
Replaces a decomposed complex multiplication with conjugate by
com.microsoft.ComplexMulConj(A, B).Complex multiplication with conjugate is defined as:
C_r = A_r * B_r + A_i * B_i C_i = A_i * B_r - A_r * B_i
(Equivalent to multiplying A by the complex conjugate of B.)
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_A(["A FLOAT(a, b, 2)"]) I_B(["B FLOAT(a, b, 2)"]) Gather_0[["Gather(., [0], axis=-1)"]] Gather_1[["Gather(., [1], axis=-1)"]] Gather_2[["Gather(., [0], axis=-1)"]] Gather_3[["Gather(., [1], axis=-1)"]] Mul_4[["Mul(., .)"]] Mul_5[["Mul(., .)"]] Mul_6[["Mul(., .)"]] Mul_7[["Mul(., .)"]] Add_8[["Add(., .)"]] Sub_9[["Sub(., .)"]] Unsqueeze_10[["Unsqueeze(., [-1])"]] Unsqueeze_11[["Unsqueeze(., [-1])"]] Concat_12[["Concat(., ., axis=-1)"]] I_A -->|"FLOAT(a, b)"| Gather_0 I_A -->|"FLOAT(a, b)"| Gather_1 I_B -->|"FLOAT(a, b)"| Gather_2 I_B -->|"FLOAT(a, b)"| Gather_3 Gather_0 --> Mul_4 Gather_2 --> Mul_4 Gather_1 --> Mul_5 Gather_3 --> Mul_5 Gather_1 --> Mul_6 Gather_2 --> Mul_6 Gather_0 --> Mul_7 Gather_3 --> Mul_7 Mul_4 --> Add_8 Mul_5 --> Add_8 Mul_6 --> Sub_9 Mul_7 --> Sub_9 Add_8 --> Unsqueeze_10 Sub_9 --> Unsqueeze_11 Unsqueeze_10 --> Concat_12 Unsqueeze_11 --> Concat_12 O_C(["C FLOAT(a, b, 2)"]) Concat_12 --> O_C class I_A,I_B,O_C ioNode class Gather_0,Gather_1,Gather_2,Gather_3 opNode class Mul_4,Mul_5,Mul_6,Mul_7,Add_8,Sub_9,Unsqueeze_10,Unsqueeze_11,Concat_12 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_A(["A FLOAT(a, b, 2)"]) I_B(["B FLOAT(a, b, 2)"]) ComplexMulConj_0[["com.microsoft.ComplexMulConj(., .)"]] I_A -->|"FLOAT(a, b, 2)"| ComplexMulConj_0 I_B -->|"FLOAT(a, b, 2)"| ComplexMulConj_0 O_C(["C FLOAT(a, b, 2)"]) ComplexMulConj_0 --> O_C class I_A,I_B,O_C ioNode class ComplexMulConj_0 opNode- apply(g: GraphBuilder, g_a0: NodeProto, g_a1: NodeProto, g_b0: NodeProto, g_b1: NodeProto, mul_rr: NodeProto, mul_ii: NodeProto, mul_ri: NodeProto, mul_ir: NodeProto, real_node: NodeProto, imag_node: NodeProto, unsq_r: NodeProto, unsq_i: NodeProto, concat_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_ort.complex_mul.ComplexMulPattern(verbose: int = 0, priority: int = 2)[source]#
Replaces a decomposed complex multiplication by
com.microsoft.ComplexMul(A, B).Complex multiplication is defined as:
C_r = A_r * B_r - A_i * B_i C_i = A_r * B_i + A_i * B_r
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_A(["A FLOAT(a, b, 2)"]) I_B(["B FLOAT(a, b, 2)"]) Gather_0[["Gather(., [0], axis=-1)"]] Gather_1[["Gather(., [1], axis=-1)"]] Gather_2[["Gather(., [0], axis=-1)"]] Gather_3[["Gather(., [1], axis=-1)"]] Mul_4[["Mul(., .)"]] Mul_5[["Mul(., .)"]] Mul_6[["Mul(., .)"]] Mul_7[["Mul(., .)"]] Sub_8[["Sub(., .)"]] Add_9[["Add(., .)"]] Unsqueeze_10[["Unsqueeze(., [-1])"]] Unsqueeze_11[["Unsqueeze(., [-1])"]] Concat_12[["Concat(., ., axis=-1)"]] I_A -->|"FLOAT(a, b)"| Gather_0 I_A -->|"FLOAT(a, b)"| Gather_1 I_B -->|"FLOAT(a, b)"| Gather_2 I_B -->|"FLOAT(a, b)"| Gather_3 Gather_0 --> Mul_4 Gather_2 --> Mul_4 Gather_1 --> Mul_5 Gather_3 --> Mul_5 Gather_0 --> Mul_6 Gather_3 --> Mul_6 Gather_1 --> Mul_7 Gather_2 --> Mul_7 Mul_4 --> Sub_8 Mul_5 --> Sub_8 Mul_6 --> Add_9 Mul_7 --> Add_9 Sub_8 --> Unsqueeze_10 Add_9 --> Unsqueeze_11 Unsqueeze_10 --> Concat_12 Unsqueeze_11 --> Concat_12 O_C(["C FLOAT(a, b, 2)"]) Concat_12 --> O_C class I_A,I_B,O_C ioNode class Gather_0,Gather_1,Gather_2,Gather_3 opNode class Mul_4,Mul_5,Mul_6,Mul_7,Sub_8,Add_9,Unsqueeze_10,Unsqueeze_11,Concat_12 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_A(["A FLOAT(a, b, 2)"]) I_B(["B FLOAT(a, b, 2)"]) ComplexMul_0[["com.microsoft.ComplexMul(., .)"]] I_A -->|"FLOAT(a, b, 2)"| ComplexMul_0 I_B -->|"FLOAT(a, b, 2)"| ComplexMul_0 O_C(["C FLOAT(a, b, 2)"]) ComplexMul_0 --> O_C class I_A,I_B,O_C ioNode class ComplexMul_0 opNode- apply(g: GraphBuilder, g_a0: NodeProto, g_a1: NodeProto, g_b0: NodeProto, g_b1: NodeProto, mul_rr: NodeProto, mul_ii: NodeProto, mul_ri: NodeProto, mul_ir: NodeProto, real_node: NodeProto, imag_node: NodeProto, unsq_r: NodeProto, unsq_i: NodeProto, concat_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.