Hybrid Attention and State Space Models › Prefill and Decode Are Different Regimes
Language AI HandbookAll chapters Prefill processes the prompt and supplies the next-token distribution at the last prompt position. The decoder uses that distribution to choose the first output token. Incremental decode then consumes that token and extends the saved state to predict the next one. Profile the two phases separately to find which operations or memory transfers limit the workload. Sarathi-Serve
Prefill
During prefill, each layer processes a prompt of tokens. Attention can build queries, keys and values for the prompt in parallel. A selective state-space layer can use a parallel scan to evaluate the recurrence over the prompt. Each approach still needs a way to pass the resulting state to decode.
Consider the operation counts of one attention mixer and one Mamba-1-style mixer. The attention projections are linear in at fixed width. Full causal attention has accessible query–key pairs, because position has available keys. Counting fixed work per pair gives a term that grows quadratically. A hybrid with some full attention retains that quadratic term, with fewer layers contributing to it.
The two large projections in the state-space mixer also grow linearly with at fixed width. At fixed state dimension and convolution width, its convolution and discrete state update do fixed work at each position. The prefill example below compares just the projections and attention pair products, with the terms left out listed alongside the count. For a long prompt, a smaller count of full-attention layers can substantially reduce the mixer arithmetic.
Decode
During incremental decoding, each sequence contributes one new position at a time. In a memory-bandwidth-limited workload, the cache and model weights contribute to the bytes a device must move. Batching can reuse weights across sequences. Kernel layout and on-chip reuse determine how persistent state translates to traffic.
For full-context attention, each step scores available prefix keys and combines their associated values. A prefix of length has stored scalars per layer and sequence. Grouped-query attention shares each key and value head across several query heads, which reduces relative to using one key and value head per query head.
For the Mamba-1-style layer modeled here, the recurrent state holds scalars. The assumed convolution cache holds scalars. Both shapes are fixed as changes. The two states can have distinct storage precision in a real implementation.
For each full-context attention layer, the persistent KV cache grows with the number of retained positions. Each modeled Mamba-1 layer adds a fixed recurrent state and convolution buffer. The cache example adds these terms across the stack.
The number of allocated bytes does not specify how often those bytes are transferred from device memory. The accounting below computes capacity. Runtime bandwidth needs a workload and a measurement.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!