CausalConvWithState

CausalConvWithState - 29

Version

This version of the operator has been available since version 29.

Summary

Stateful causal one-dimensional depthwise convolution.

input has shape (B, C, L), weight has shape (C, 1, K), and optional bias has shape (C): B is batch size, C is channel count, L is input length, and K is kernel size. Let d = dilation (default 1) and H = (K - 1) * d. Both K and d must be at least 1, and the effective kernel size H + 1 must fit in a signed 64-bit integer.

Both state tensors have shape (B, C, H) and store consecutive raw samples in chronological order. Form P by concatenating past_state and input along axis 2; use a zero tensor of shape (B, C, H) when past_state is absent. For each batch b, channel c, and position 0 <= t < L, compute z[b,c,t] = sum_{j=0}^{K-1} weight[c,0,j] * P[b,c,t+j*d] + bias[c], with omitted bias treated as zero. The output has shape (B, C, L) and equals z for activation ‘none’ (the default), or z / (1 + exp(-z)) elementwise for ‘silu’ and its alias ‘swish’.

The next state is present_state = P[:, :, L:L+H], independent of activation. When L < H, it retains the required past samples or initial zeros. Passing this state to the next call preserves convolution across chunk boundaries. For K = 1, state has shape (B, C, 0). For L = 0, output has shape (B, C, 0) and state is unchanged (all zeros if past_state is absent).

Attributes

  • activation - STRING (default is none):

    Fused activation function. One of: ‘silu’, ‘swish’, ‘none’. Default is ‘none’.

  • dilation - INT (default is 1):

    Spacing between kernel samples. Must be >= 1.

Inputs

Between 2 and 4 inputs.

  • input (heterogeneous) - T:

    Input tensor with shape (batch_size, channels, length). Channels-first layout.

  • weight (heterogeneous) - T:

    Depthwise convolution kernel with shape (channels, 1, k), where k must be >= 1. Each channel is convolved independently with its corresponding kernel.

  • bias (optional, heterogeneous) - T:

    Optional per-channel bias with shape (channels).

  • past_state (optional, heterogeneous) - T:

    Raw chronological history with shape (batch_size, channels, (k - 1) * dilation). If not provided, history is zero.

Outputs

  • output (heterogeneous) - T:

    Convolution output with same shape as input.

  • present_state (heterogeneous) - T:

    The last (k - 1) * dilation raw samples of the concatenated history and input, with shape (batch_size, channels, (k - 1) * dilation).

Type Constraints

  • T in ( tensor(bfloat16), tensor(float), tensor(float16) ):

    Constrain input and output types to float tensors.

Examples

_dilated

import numpy as np
import onnx

for dilation, length, k, with_state, activation in (
    (2, 7, 3, False, "none"),
    (3, 1, 4, True, "silu"),
    (2, 0, 3, True, "none"),
    (3, 2, 1, False, "swish"),
):
    input_ = np.arange(2 * length, dtype=np.float32).reshape(1, 2, length)
    weight = np.arange(2 * k, dtype=np.float32).reshape(2, 1, k) / 10
    bias = np.array([0.25, -0.5], dtype=np.float32)
    history = (k - 1) * dilation
    past = np.arange(2 * history, dtype=np.float32).reshape(1, 2, history)
    node = onnx.helper.make_node(
        "CausalConvWithState",
        ["input", "weight", "bias", "past_state"]
        if with_state
        else ["input", "weight", "bias"],
        ["output", "present_state"],
        dilation=dilation,
        activation=activation,
    )
    expected = _compute(
        input_, weight, bias, past if with_state else None, activation, dilation
    )
    expect(
        node,
        inputs=[input_, weight, bias, past]
        if with_state
        else [input_, weight, bias],
        outputs=list(expected),
        name=f"test_causal_conv_with_state_dilated_d{dilation}_l{length}_k{k}",
        opset_imports=[onnx.helper.make_opsetid("", 29)],
    )

_basic

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)

output, present_state = _compute(input_, weight)

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_basic",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_with_bias

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight", "bias"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)
bias = np.random.randn(channels).astype(np.float32)

output, present_state = _compute(input_, weight, bias=bias)

expect(
    node,
    inputs=[input_, weight, bias],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_with_bias",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_with_past_state

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight", "", "past_state"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)
past_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)

output, present_state = _compute(input_, weight, past_state=past_state)

expect(
    node,
    inputs=[input_, weight, past_state],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_with_past_state",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_silu

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
    activation="silu",
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)

output, present_state = _compute(input_, weight, activation="silu")

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_silu",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_swish_alias

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
    activation="swish",
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)

output, present_state = _compute(input_, weight, activation="swish")

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_swish_alias",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_decode_step

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight", "bias", "past_state"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 1, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)
bias = np.random.randn(channels).astype(np.float32)
past_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)

output, present_state = _compute(
    input_, weight, bias=bias, past_state=past_state
)

expect(
    node,
    inputs=[input_, weight, bias, past_state],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_decode_step",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_kernel_size_one

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 8, 1
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)

output, present_state = _compute(input_, weight)

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_kernel_size_one",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_with_bias_and_past_state

import numpy as np
import onnx

# Multi-token (T>1) path through Concat(past, input) -> Conv(+bias).
node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight", "bias", "past_state"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)
bias = np.random.randn(channels).astype(np.float32)
past_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)

output, present_state = _compute(
    input_, weight, bias=bias, past_state=past_state
)

expect(
    node,
    inputs=[input_, weight, bias, past_state],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_with_bias_and_past_state",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_silu_with_past_state

import numpy as np
import onnx

# Fused activation combined with concat-from-past variant of PaddedInput.
node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight", "", "past_state"],
    outputs=["output", "present_state"],
    activation="silu",
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)
past_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)

output, present_state = _compute(
    input_, weight, past_state=past_state, activation="silu"
)

expect(
    node,
    inputs=[input_, weight, past_state],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_silu_with_past_state",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_b1_c1_degenerate

import numpy as np
import onnx

# Mamba/GDN inner-head edge case: B=1, C=1.
node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 1, 1, 6, 4
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)

output, present_state = _compute(input_, weight)

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_b1_c1_degenerate",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_short_input_no_past_state

import numpy as np
import onnx

# L < k-1 with no past_state: zero-pad is wider than the input.
node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 2, 5
input_ = np.random.randn(batch_size, channels, length).astype(np.float32)
weight = np.random.randn(channels, 1, k).astype(np.float32)

output, present_state = _compute(input_, weight)

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_short_input_no_past_state",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_fp16

import numpy as np
import onnx

node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.rand(batch_size, channels, length).astype(np.float16)
weight = np.random.rand(channels, 1, k).astype(np.float16)

output, present_state = _compute(input_, weight)

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_fp16",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

_silu_fp16

import numpy as np
import onnx

# fp16 + SiLU: the reference upcasts Sigmoid/Mul to float32, so the
# function-body expansion must do the same to stay numerically faithful.
node = onnx.helper.make_node(
    "CausalConvWithState",
    inputs=["input", "weight"],
    outputs=["output", "present_state"],
    activation="silu",
)

batch_size, channels, length, k = 2, 4, 8, 4
input_ = np.random.rand(batch_size, channels, length).astype(np.float16)
weight = np.random.rand(channels, 1, k).astype(np.float16)

output, present_state = _compute(input_, weight, activation="silu")

expect(
    node,
    inputs=[input_, weight],
    outputs=[output, present_state],
    name="test_causal_conv_with_state_silu_fp16",
    opset_imports=[onnx.helper.make_opsetid("", 27)],
)

CausalConvWithState - 27

Version

This version of the operator has been available since version 27.

Summary

Stateful causal 1D depthwise convolution.

Used by Gated DeltaNet (Qwen3.5) and Mamba (Jamba, FalconMamba) as a preprocessing step. Replaces the 3-op pattern (Concat + Conv + Slice) with a single fused operation.

The convolution is causal (looks only at current and past positions) and depthwise (each channel is convolved independently with its own kernel).

The input, weight, past_state, output, and present_state tensors are rank-3 with shape (batch_size, channels, length). The optional bias input is rank-1 with shape (channels). For higher-dimensional data, use Reshape nodes before and after this operator to pack extra dimensions into the batch or channel axis.

Weight layout: (channels, 1, k) for depthwise convolution. The carry state stores the last (k-1) positions for incremental decode.

The optional activation attribute supports fused SiLU/Swish activation.

Attributes

  • activation - STRING (default is none):

    Fused activation function. One of: ‘silu’, ‘swish’, ‘none’. Default is ‘none’.

Inputs

Between 2 and 4 inputs.

  • input (heterogeneous) - T:

    Input tensor with shape (batch_size, channels, length). Channels-first layout.

  • weight (heterogeneous) - T:

    Depthwise convolution kernel with shape (channels, 1, k) where k is the kernel size. The middle dim of size 1 follows the ONNX Conv weight layout (M, C/group, k1, ..., kn): since this op is always depthwise, group = channels, so C/group = 1. Keeping this layout makes the weight tensor a drop-in for a depthwise Conv(group=channels) weight, so Conv <-> CausalConvWithState rewrites require no reshape.

  • bias (optional, heterogeneous) - T:

    Optional per-channel bias with shape (channels).

  • past_state (optional, heterogeneous) - T:

    Carry state from previous step with shape (batch_size, channels, k - 1). If not provided, padding is zero.

Outputs

  • output (heterogeneous) - T:

    Convolution output with same shape as input.

  • present_state (heterogeneous) - T:

    Updated carry state with shape (batch_size, channels, k - 1). Contains the last (k - 1) values of the effective padded/concatenated sequence along the causal axis, including any values from past_state or zero-padding when the current input is shorter than k - 1.

Type Constraints

  • T in ( tensor(bfloat16), tensor(float), tensor(float16) ):

    Constrain input and output types to float tensors.