Status

Dynamic causal conv1d

Triton kernel for short causal convolutions whose taps change per token, as in the key/value convolutions of Inkling 2:

y[t, c] = sum over lag of (static[lag, c] + dynamic[t, head(c), lag]) * x[t - lag, c]

The PyTorch version materializes the windows and the summed taps as two big float32 tensors; this kernel reads them in place. Forward and backward are both there, so it's for training as well as prefill and decoding, and it's an order of magnitude faster.

Peak memory of one forward and backward at Inkling 2's size (8 heads of 128 channels, 4 taps, batch 1):

tokens PyTorch this kernel saving
2k 176 MiB 17 MiB 10.2x
8k 544 MiB 69 MiB 7.9x
32k 2,176 MiB 404 MiB 5.4x
128k 8,704 MiB 1,232 MiB 7.1x

That's per call, and Inkling 2 makes two (keys and values) in every full-attention layer.

Usage

Like causal-conv1d's causal_conv1d_fn and causal_conv1d_update, there are two layers:

  • dynamic_causal_conv1d_fn for prefill and training (forward and backward). It takes the inputs channels-first, optionally preceded by a cached conv state, and with seq_idx (e.g. from DataCollatorWithFlattening(return_seq_idx=True)) taps don't cross packed sequences.
  • dynamic_causal_conv1d_update for decoding one token, forward only. It takes the conv state with the token appended and leaves updating the state to the caller.
from kernels import get_kernel

layers = get_kernel("kernels-community/dynamic-conv1d", version=2).layers
fn, update = layers.dynamic_causal_conv1d_fn(), layers.dynamic_causal_conv1d_update()

# x: (B, C, T), static: (W, C), dynamic: (B, T, H, W) -> (B, T, C)
y = fn(x, static, dynamic, seq_idx=seq_idx)
conv_state = x[..., -W:]  # (B, C, state_len)
# context: (B, C, state_len + 1), dynamic_t: (B, 1, H, W) -> (B, 1, C)
context = torch.cat([conv_state, token[..., None]], -1)
y_t = update(context, static, dynamic_t)
conv_state = context[..., 1:]

Outputs are float32, like the transformers reference.

Credits

Built on the causal_conv1d kernels of flash-linear-attention (MIT), with the per-token tap sum of vLLM's Inkling 2 short convolution (vLLM, Apache-2.0).

Downloads last month
-
kernel
apache-2.0