Mamba's selective SSM runs, for every batch element and each of the ED inner channels, an N-dimensional linear recurrence whose parameters change at every step. With the state before the first step equal to zero:
Here and are scalars for channel at step , is the channel's diagonal of , and are -vectors shared by all channels. Write a fused Triton kernel that computes y and never writes any state to global memory.
Required kernel and wrapper:
@triton.jit
def selective_scan_kernel(u_ptr, delta_ptr, A_ptr, B_ptr, C_ptr, D_ptr, y_ptr, L, ED, N, BLOCK_N: tl.constexpr):
def selective_scan_triton(u, delta, A, B, C, D) -> torch.Tensor:
u and delta are (batch, L, ED), A is (ED, N) with negative entries, B and C are (batch, L, N), and D is (ED,). delta is the step size after the softplus, so it is already positive.(batch element, channel) on the grid (batch, ED). Each program keeps its state as a BLOCK_N = triton.next_power_of_2(N) vector in registers, masks the lanes >= N, walks t = 0, 1, ..., L - 1 in order and stores only y_t.y of shape (batch, L, ED), float32. Any L >= 1 and N >= 1 must work. Nothing of size batch * L * ED * N may be allocated: a test measures peak GPU memory.A and its D once before the loop; inside the loop each step needs two scalars and two N-vectors.A loads as 0 and B, C load as 0 there: such a lane starts at zero and stays at zero.C_t . h_t is a tl.sum over the state axis..contiguous() in the wrapper lets the kernel use flat offsets such as (b * L + t) * ED + d.