Skip to content

Mamba Operators

Every op on this page is used the same way: construct it once, then call it. The constructor takes what the kernel is compiled with; the call takes the tensors. Both are documented under each op — __init__ and forward, where forward is what runs when you call op(...).

Full forward

tileops.mamba.Mamba2FwdOp

Mamba-2 State-Space Dual (SSD) full forward pass operator.

Combines SSDChunkCumsum → SSDChunkCoupling → SSDChunkState → SSDStatePassing → SSDChunkScan into a single callable whose interface matches mamba_chunk_scan_combined from the official mamba_ssm library, except that final_states is always returned.

__init__

__init__(
    chunk_size=256,
    dt_softplus=True,
    *,
    target=None
)

Build the op and its sub-ops. Shapes and dtype are taken from each call.

Parameters:

  • chunk_size (int, default: 256 ) –

    Tokens per chunk (default 256).

  • dt_softplus (bool, default: True ) –

    Apply softplus to (dt + dt_bias) before use.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    dt,
    A,
    B,
    C,
    dt_bias=None,
    initial_states=None,
)

Run the full Mamba-2 SSD forward pass.

Parameters:

  • x (Tensor) –

    (batch, seqlen, n_heads, d_head) dtype

  • dt (Tensor) –

    (batch, seqlen, n_heads) float32

  • A (Tensor) –

    (n_heads,) float32 (log-space, ≤ 0)

  • B (Tensor) –

    (batch, seqlen, n_groups, d_state) dtype

  • C (Tensor) –

    (batch, seqlen, n_groups, d_state) dtype

  • dt_bias (Tensor | None, default: None ) –

    (n_heads,) float32, optional

  • initial_states (Tensor | None, default: None ) –

    (batch, n_heads, d_head, d_state) float32, optional

Returns:

  • y ( Tensor ) –

    (batch, seqlen, n_heads, d_head) float32

  • final_states ( Tensor ) –

    (batch, n_heads, d_head, d_state) float32

Selective scan

tileops.mamba.SelectiveScanFwdOp

Mamba-1 S6 selective scan: a first-order recurrence per channel and state whose decay and input both follow the input.

With dt = softplus(delta + delta_bias) (delta + delta_bias without delta_softplus) and channel d reading group g = d // (Di / G) of B and C::

h[t, n] = exp(dt[d, t] * A[d, n]) * h[t - 1, n] + dt[d, t] * u[d, t] * B[g, n, t]
y[d, t] = (sum_n C[g, n, t] * h[t, n] + D[d] * u[d, t]) * silu(z[d, t])

h starts from initial_states or zeros, and restarts from zeros at every step whose seq_idx differs from the step before; final_states is h after the last step. Every product and sum is float32 and y rounds once to the input dtype. The exponentials and divisions use the hardware's approximate instructions, each within two float32 units in the last place; the softplus is within a few. Where dt * A <= 0 a run of steps decays the state by the product of their decays, and a product below float32's normal range is zero: h[t, n] is off by at most |h[s, n]| * 2**-126 for the state h[s, n] the product scales, and y[d, t] by at most sum_n |C[g, n, t]| * |h[s, n]| * 2**-126, the gate's factor aside. A state that overflows float32 stays infinite or NaN, which of the two unspecified, until the next restart; every step after a restart is exact.

__init__

__init__(
    delta_softplus=True,
    *,
    target=None
)

Build the op. Shapes, presence of the optional inputs and dtype come from each call.

Parameters:

  • delta_softplus (bool, default: True ) –

    Whether dt is the softplus of delta + delta_bias.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    u,
    delta,
    A,
    B,
    C,
    D=None,
    z=None,
    delta_bias=None,
    seq_idx=None,
    initial_states=None,
)

Scan every channel's states along the sequence axis.

Parameters:

  • u (Tensor) –

    (Bsz, Di, S) float16, bfloat16 or float32, the scanned input.

  • delta (Tensor) –

    (Bsz, Di, S) like u, the step before delta_bias and softplus.

  • A (Tensor) –

    float32 (Di, N), the continuous-time rates.

  • B (Tensor) –

    (Bsz, G, N, S) like u, the input gate of each group.

  • C (Tensor) –

    (Bsz, G, N, S) like u, the output read of each group.

  • D (Tensor | None, default: None ) –

    Optional float32 (Di,) skip.

  • z (Tensor | None, default: None ) –

    Optional (Bsz, Di, S) gate like u.

  • delta_bias (Tensor | None, default: None ) –

    Optional float32 (Di,), added to delta.

  • seq_idx (Tensor | None, default: None ) –

    Optional int32 (Bsz, S); the state restarts where it changes.

  • initial_states (Tensor | None, default: None ) –

    Optional float32 (Bsz, Di, N), the state before the first step.

Returns:

  • Tensor –

    New (y, final_states): y (Bsz, Di, S) in u's dtype and

  • Tensor –

    final_states float32 (Bsz, Di, N).

Causal convolution

tileops.mamba.CausalConv1dFwdOp

Causal depthwise convolution (width 2 to 4) with SiLU; writes conv_state in place.

x holds the sequences token-major, cu_seqlens delimiting them. For a sequence on tokens [a, e) and channel c, with d the dilation and h the (W - 1) * d tokens before it (the first (W - 1) * d columns of conv_state[s] where has_initial_state[s], zeros otherwise)::

xp = concat(h[c, :], x[a:e, c])
output[a + i, c] = act(bias[c] + sum_k weight[c, k] * xp[i + k * d])

and those columns of conv_state[s] are overwritten in place with the sequence's last (W - 1) * d tokens of xp; an empty sequence keeps its state. With cache_indices, sequence s's state is conv_state[cache_indices[s]]. The products accumulate in float32, a float16 or bfloat16 weight and bias read as float32, and each output rounds once to the input dtype. SiLU uses the hardware's approximate exponential and reciprocal, each within two float32 units in the last place.

The caller guarantees well-formed cu_seqlens, or passes validate_inputs=True, which checks it on the CPU on every call.

__init__

__init__(
    activation="silu",
    *,
    dilation=1,
    validate_inputs=False,
    target=None
)

Build the op. Shapes, the sequence lengths and dtype are taken from each call.

Parameters:

  • activation (str | None, default: 'silu' ) –

    "silu" to apply SiLU to the convolution, or None for none.

  • dilation (int, default: 1 ) –

    Tokens between adjacent filter taps.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that cu_seqlens starts at 0, never decreases and ends at x's token count, and that cache_indices lies in its manifest range.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    weight,
    conv_state,
    cu_seqlens,
    bias,
    has_initial_state=None,
    *,
    cache_indices=None
)

Convolve every packed sequence and leave its last tokens in its state.

Parameters:

  • x (Tensor) –

    (Tt, D) float16 or bfloat16, the sequences packed token-major. Read in place when its channels are contiguous and its tokens at least D elements apart, as the columns of a wider projection are; copied otherwise.

  • weight (Tensor) –

    (D, W), one filter a channel, in x's dtype or float32.

  • conv_state (Tensor) –

    (B, D, L), or (P, D, L) with cache_indices, contiguous, L >= (W - 1) * dilation; read where has_initial_state is set and overwritten in place.

  • cu_seqlens (Tensor) –

    int32 (B + 1,) packed offsets, from 0 to Tt.

  • bias (Tensor | None) –

    (D,) bias in weight's dtype, or None.

  • has_initial_state (Tensor | None, default: None ) –

    Optional bool (B,); absent, every sequence starts from zeros.

  • cache_indices (Tensor | None, default: None ) –

    Optional int32 (B,), each sequence's distinct row of conv_state, or -1 for a padded sequence, whose state is never written and whose outputs are unspecified.

Returns:

  • Tensor –

    A new (Tt, D) tensor in x's dtype.

SSD stages

tileops.mamba.SSDChunkCumsumFwdOp

Mamba-2 dA_cumsum forward operator.

Applies optional per-head bias, optional softplus activation, and clamping to raw dt values, then computes the chunk-local inclusive prefix sum of dA = dt * A. dA_cumsum is computed from the float32 dt before dt_out is cast to out_dtype.

__init__

__init__(
    chunk_size,
    out_dtype=torch.float32,
    dt_softplus=False,
    dt_min=0.0,
    dt_max=float(
        "inf"
    ),
    *,
    target=None
)

Build the op. Shapes are taken from each call.

Parameters:

  • chunk_size (int) –

    Tokens per chunk.

  • out_dtype (dtype, default: float32 ) –

    Storage dtype of dt_out.

  • dt_softplus (bool, default: False ) –

    Whether to apply softplus (with bypass for dt > 20) to dt.

  • dt_min (float, default: 0.0 ) –

    Lower clamp bound applied after bias and softplus.

  • dt_max (float, default: float('inf') ) –

    Upper clamp bound applied after bias and softplus.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    dt,
    A,
    dt_bias=None,
)

Run the dA_cumsum forward pass.

Parameters:

  • dt (Tensor) –

    (batch, seq_len, n_heads) float32 — raw dt values.

  • A (Tensor) –

    (n_heads,) float32 — SSM decay parameters.

  • dt_bias (Tensor | None, default: None ) –

    (n_heads,) float32, optional — per-head dt bias.

Returns:

  • dt_out ( Tensor ) –

    (batch, n_heads, num_chunks, chunk_size) out_dtype — processed dt.

  • dA_cumsum ( Tensor ) –

    (batch, n_heads, num_chunks, chunk_size) float32 — inclusive prefix sum of dA = dt_val * A, computed from fp32 dt_val before casting dt_out.

tileops.mamba.SSDChunkCouplingFwdOp

CB (C@B) matrix producer operator.

Computes cb[b,c,g,l,s] = sum_n C[b,cQ+l,g,n] * B[b,cQ+s,g,n] with causal masking (cb[l,s] = 0 if s > l).

__init__

__init__(
    chunk_size,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from each call.

Parameters:

  • chunk_size (int) –

    Chunk length (Q).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    C,
    B,
)

Parameters:

  • C (Tensor) –

    [batch, S, G, N] dtype

  • B (Tensor) –

    [batch, S, G, N] dtype

Returns:

  • cb ( Tensor ) –

    [batch, num_chunks, G, Q, Q] dtype

tileops.mamba.SSDChunkStateFwdOp

Mamba-2 State-Space Dual (SSD) chunk state forward operator.

Computes the chunk-end State Space Model (SSM) state for each chunk:

out[b, c, h, p, n] = sum_{l=0}^{Q-1} x[b, cQ+l, h, p] * B[b, cQ+l, g(h), n] * exp(dA_cumsum[b,h,c,Q-1] - dA_cumsum[b,h,c,l]) * dt[b, h, c, l] * (1 if seq_idx is None else (seq_idx[b,cQ+Q-1] >= 0 and seq_idx[b,cQ+l] == seq_idx[b,c*Q+Q-1]))

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    x,
    B,
    dt,
    dA_cumsum,
    seq_idx=None,
)

Run the SSD chunk state forward pass.

Parameters:

  • x (Tensor) –

    (batch, seq_len, n_heads, d_head)

  • B (Tensor) –

    (batch, seq_len, n_groups, d_state)

  • dt (Tensor) –

    (batch, n_heads, num_chunks, chunk_len), x's dtype or float32

  • dA_cumsum (Tensor) –

    (batch, n_heads, num_chunks, chunk_len) float32

  • seq_idx (Tensor | None, default: None ) –

    (batch, seq_len) int32, optional

Returns:

  • states ( Tensor ) –

    (batch, num_chunks, n_heads, d_head, d_state) float32

tileops.mamba.SSDStatePassingFwdOp

Mamba-2 State-Space Dual (SSD) state passing forward operator.

Performs the inter-chunk recurrent scan:

s_c[m] = exp(dA_chunk_cumsum[b, h, c]) * s_{c-1}[m] + states[b, c, h, m]

with s_{-1} = initial_states, or 0 when it is not passed.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    states,
    dA_chunk_cumsum,
    initial_states=None,
)

Run the SSD state passing forward pass.

Parameters:

  • states (Tensor) –

    (batch, num_chunks, n_heads, d_state)

  • dA_chunk_cumsum (Tensor) –

    (batch, n_heads, num_chunks) float32

  • initial_states (Tensor | None, default: None ) –

    (batch, n_heads, d_state) float32, optional

Returns:

  • prev_states ( Tensor ) –

    (batch, num_chunks, n_heads, d_state) float32

  • final_states ( Tensor ) –

    (batch, n_heads, d_state) float32

tileops.mamba.SSDChunkScanFwdOp

Mamba-2 State-Space Dual (SSD) fused chunk output operator.

Fuses the history (prev_states) contribution and intra-chunk causal decay into a single pass, computing:

out[l, p] = exp(dA_cumsum[l]) * (C[l] @ prev_states) + sum_{s <= l} cb[l, s] * exp(dA_cumsum[l] - dA_cumsum[s]) * dt[s] * x[s, p]

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    x,
    cb,
    dA_cumsum,
    C,
    prev_states,
    dt,
)

Run the fused SSD chunk output pass.

Parameters:

  • x (Tensor) –

    (batch, seqlen, n_heads, d_head) dtype

  • cb (Tensor) –

    (batch, num_chunks, n_groups, chunk_len, chunk_len) dtype

  • dA_cumsum (Tensor) –

    (batch, n_heads, num_chunks, chunk_len) float32

  • C (Tensor) –

    (batch, seqlen, n_groups, d_state) dtype

  • prev_states (Tensor) –

    (batch, num_chunks, n_heads, d_head, d_state) float32

  • dt (Tensor) –

    (batch, n_heads, num_chunks, chunk_len) dtype

Returns:

  • y ( Tensor ) –

    (batch, seqlen, n_heads, d_head) float32

Decode

tileops.mamba.CausalConv1dUpdateFwdOp

Decode steps of CausalConv1dFwdOp; advances conv_state in place.

For sequence b and channel c, with s = conv_state[b, c] its last W - 1 tokens::

output[b, c] = act(bias[c] + sum_{k < W-1} weight[c, k] * s[k] + weight[c, W-1] * x[b, c])
conv_state[b, c] <- (s[1], ..., s[W-2], x[b, c])

In general, with d the dilation, a state is the first (W - 1) * d of its L columns and the taps are d tokens apart, as in CausalConv1dFwdOp. conv_state_indices maps sequence b to state conv_state[conv_state_indices[b]]; query_start_loc packs several tokens a sequence in x, each convolved in turn. With num_accepted_tokens, as vLLM's causal_conv1d_update takes it, sequence b with a = num_accepted_tokens[b] and q tokens reads its history from column a - 1, and its first (W - 1) * d + q - 1 columns become its last that many tokens, so the next call resumes after whichever draft was accepted. A sequence of more than L - (W - 1) * d + 1 tokens breaks the call's contract, which no check enforces; the in-tree kernel then leaves the outputs of its tokens past that count unwritten.

The products accumulate in float32 and each output rounds once to the input dtype. SiLU uses the hardware's approximate exponential and reciprocal, each within two float32 units in the last place.

__init__

__init__(
    activation="silu",
    *,
    dilation=1,
    max_query_len=-1,
    validate_inputs=False,
    target=None
)

Build the op. Shapes and dtype are taken from each call.

Parameters:

  • activation (str | None, default: 'silu' ) –

    "silu" to apply SiLU to the convolution, or None for none.

  • dilation (int, default: 1 ) –

    Tokens between adjacent filter taps.

  • max_query_len (int, default: -1 ) –

    The most tokens a sequence holds, with query_start_loc.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that query_start_loc starts at 0, never decreases, ends at x's token count and holds no sequence longer than max_query_len, and that conv_state_indices and num_accepted_tokens lie in their manifest ranges.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    weight,
    conv_state,
    bias=None,
    *,
    conv_state_indices=None,
    num_accepted_tokens=None,
    query_start_loc=None
)

Convolve each sequence's new tokens with its state and shift them into it.

Parameters:

  • x (Tensor) –

    (B, D) float16 or bfloat16, one token a sequence; (Tt, D), the sequences packed, with query_start_loc.

  • weight (Tensor) –

    (D, W), one filter a channel, in x's dtype or float32.

  • conv_state (Tensor) –

    (B, D, L), or (P, D, L) with conv_state_indices, contiguous, advanced in place.

  • bias (Tensor | None, default: None ) –

    Optional (D,) bias, in weight's dtype.

  • conv_state_indices (Tensor | None, default: None ) –

    Optional int32 (B,), each sequence's distinct row of conv_state, or -1 for a padded sequence, whose state is never written and whose outputs are unspecified.

  • num_accepted_tokens (Tensor | None, default: None ) –

    Optional int32 (B,), the drafts of the previous call each sequence accepted, at least one.

  • query_start_loc (Tensor | None, default: None ) –

    Optional int32 (B + 1,) packed offsets, from 0 to Tt.

Returns:

  • Tensor –

    A new tensor shaped like x, in x's dtype.

tileops.mamba.SSDRecurrentFwdOp

Mamba-2 State-Space Dual (SSD) recurrent decode step; updates state in place.

Returns the output y for the current token:

g = h // (n_heads // n_groups) dA[b, h, p, n] = exp(dt[b, h, p] * A[h, p, n]) state[b,h,p,n] <- dA[b,h,p,n] * state[b,h,p,n] + dt[b,h,p] * B[b,g,n] * x[b,h,p] y[b, h, p] = sum_n state[b, h, p, n] * C[b, g, n]

The caller applies the skip connection (D * x) and the output gate (z * silu).

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    A,
    dt,
    x,
    B,
    C,
    state,
)

Run a single Mamba-2 decode step.

Parameters:

  • A (Tensor) –

    (n_heads, d_head, d_state) float32 -- SSM decay parameter (A <= 0)

  • dt (Tensor) –

    (batch, n_heads, d_head) float32 -- discretization step (post-softplus)

  • x (Tensor) –

    (batch, n_heads, d_head) dtype -- input features per head

  • B (Tensor) –

    (batch, n_groups, d_state) dtype -- SSM B matrix (per group)

  • C (Tensor) –

    (batch, n_groups, d_state) dtype -- SSM C matrix (per group)

  • state (Tensor) –

    (batch, n_heads, d_head, d_state) float32 -- recurrent state (mutated in-place)

Returns:

  • y ( Tensor ) –

    (batch, n_heads, d_head) float32