Hybrid Attention and State Space Models › Prefill and Decode Are Different Regimes

Michael BrenndoerferNovember 17, 20263 min read
Language AI HandbookAll chapters
Part 19 of 67Alternative Sequence and Generative Architectures
Chapter 134Hybrid Attention and State Space Models › Prefill and Decode Are Different Regimes
Reading section 5 of 12 · Prefill and Decode Are Different Regimes · 3 minSection 5 of 12 · 3 min

Section 5 of 12 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

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 LL 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 LL at fixed width. Full causal attention has L(L+1)/2L(L+1)/2 accessible query–key pairs, because position tt has tt 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 LL 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 LL has 2Lnkvdhead2L n_{\text{kv}}d_{\text{head}} stored scalars per layer and sequence. Grouped-query attention shares each key and value head across several query heads, which reduces nkvn_{\text{kv}} relative to using one key and value head per query head.

For the Mamba-1-style layer modeled here, the recurrent state holds dinner×dstated_{\text{inner}}\times d_{\text{state}} scalars. The assumed convolution cache holds dinner×dconvd_{\text{inner}}\times d_{\text{conv}} scalars. Both shapes are fixed as LL 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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy