yobx.xoptim.patterns_ort.causal_conv#

class yobx.xoptim.patterns_ort.causal_conv.CausalConvWithStatePattern(verbose: int = 0, priority: int = 2)[source]#

Fuses Concat + Conv (+ Slice) into com.microsoft.CausalConvWithState.

The operator performs a stateful causal depthwise 1-D convolution and replaces the streaming pattern that concatenates a past-state buffer with the current input, runs a depthwise Conv, and optionally slices the last K-1 frames back out as the next state.

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_input(["input FLOAT(N, C, L)"])
    I_weight(["weight FLOAT(C, 1, K)"])
    I_bias(["bias FLOAT(C)"])
    I_state(["past_state FLOAT(N, C, K-1)"])

    Concat_0[["Concat(., ., axis=2)"]]
    Conv_1[["Conv(., ., ., groups=C)"]]
    Slice_2[["Slice(., ., ., [2])"]]

    I_state -->|"FLOAT(N, C, K-1)"| Concat_0
    I_input -->|"FLOAT(N, C, L)"| Concat_0
    Concat_0 -->|"FLOAT(N, C, K-1+L)"| Conv_1
    I_weight -->|"FLOAT(C, 1, K)"| Conv_1
    I_bias -->|"FLOAT(C)"| Conv_1
    Concat_0 -->|"FLOAT(N, C, K-1+L)"| Slice_2

    O_output(["output FLOAT(N, C, L)"])
    Conv_1 --> O_output
    O_state(["present_state FLOAT(N, C, K-1)"])
    Slice_2 --> O_state

    class I_input,I_weight,I_bias,I_state,O_output,O_state ioNode
    class Concat_0,Conv_1,Slice_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_input(["input FLOAT(N, C, L)"])
    I_weight(["weight FLOAT(C, 1, K)"])
    I_bias(["bias FLOAT(C)"])
    I_state(["past_state FLOAT(N, C, K-1)"])

    CausalConvWithState_0[["com.microsoft.CausalConvWithState(., ., ., .)"]]

    I_input -->|"FLOAT(N, C, L)"| CausalConvWithState_0
    I_weight -->|"FLOAT(C, 1, K)"| CausalConvWithState_0
    I_bias -->|"FLOAT(C)"| CausalConvWithState_0
    I_state -->|"FLOAT(N, C, K-1)"| CausalConvWithState_0

    O_output(["output FLOAT(N, C, L)"])
    CausalConvWithState_0 --> O_output
    O_state(["present_state FLOAT(N, C, K-1)"])
    CausalConvWithState_0 --> O_state

    class I_input,I_weight,I_bias,I_state,O_output,O_state ioNode
    class CausalConvWithState_0 opNode
    
apply(g: GraphBuilder, concat_node: NodeProto, conv_node: NodeProto, slice_node: NodeProto | None = None) 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.