Inside a Mamba block the input branch passes through a causal depthwise convolution of width before the SSM. During training it runs over the whole sequence; during generation it must run one token at a time from a small cache. Implement both and the hand-off between them.
Convention. x has shape (ED, L) (channels by time), w has shape (ED, K) and b has shape (ED,). Each channel is convolved with its own kernel (depthwise), and inputs before time 0 are zeros (left padding ):
So w[:, K-1] multiplies the current input and w[:, 0] the oldest one. This is what torch.nn.Conv1d(ED, ED, K, groups=ED, padding=K-1) computes after keeping the first L outputs.
causal_conv1d(x, w, b)
(ED, L) output above.conv_step(conv_state, x_t, w, b)
conv_state has shape (ED, K-1) and holds the previous K-1 inputs, oldest in column 0. x_t has shape (ED,).(y_t, new_state): the output for this time step, shape (ED,), and the state after appending x_t and dropping the oldest column, shape (ED, K-1).conv_state in place; return a new array. Starting from a zero state and stepping through x column by column must reproduce causal_conv1d(x, w, b).conv_state_from_prefix(x, K)
x of shape (ED, L) has been processed: its last K-1 columns, left-padded with zeros when L < K-1. Continuing with conv_step from this state must match the full convolution of the concatenated sequence.is valid: the state then has zero columns and every function must still return the shapes above.