Efficient Attention in Large Models: Sparse Attention and KV Reuse Explained
This article explains why efficient attention is essential as context lengths grow, then unpacks a 40-layer architecture that splits shared history into global KV caches and Top-K sparse indices. It covers layer roles (SWA, Full, Reuse, Reindex), parameter layout, two-step low-rank Query projection, the per-token compute path, and deployment implications for KV cache sharing and long-context inference.
Why Efficient Attention Is Inevitable
As context windows keep growing, standard self-attention compute and memory scale quadratically with sequence length. KV cache storage is the main bottleneck for long-context inference. Vendors therefore pursue efficient attention. One representative design no longer lets every layer regenerate full history. Instead it splits “history” into two independently managed shared states:
- Content cache: global KV (key/value vectors) for “what was seen.”
- Position selection: Top-K sparse indices for “which past positions matter.”
They can be updated and reused separately. That idea is compressed shared attention state (CSA²).
Architecture: Only 4 of 40 Layers Fully Write Global State
The model has 40 layers. Every layer keeps its own sliding-window KV (SWA KV) and main Query. For global information, layers play four roles:
| Tag | Meaning | Global KV | Top-K index |
|---|---|---|---|
| S | Pure SWA | Not produced | Not produced |
| F | Full | Newly generated | Newly computed |
| R | Reuse | Reused | Reused |
| I | Reindex | Reused | Recomputed |
Workload split:
- Only 4 layers generate new global KV (Full).
- Only 8 layers run new index selection (4 Full + 4 Reindex).
- The remaining 30 layers reuse prior indices.
- All 40 layers keep private SWA KV and main Query.
Writes of global state are expensive and therefore sparse; reads are shared across layers at near-zero extra cost.
Multiple global KV tensors in the encoder
In the first 20 encoder layers, layers 2, 8, and 14 each produce an independent global KV. They come from different depths of hidden states—shallow features versus higher-level semantics—so later layers can read history from more than one compressed view.
Reindex on the decoder
Decoder layers mainly use Reindex. Reindex does not regenerate global KV; it only recomputes which positions in the shared global KV to attend to. Content is shared; attention patterns can change as new tokens appear. Encoder compression rate is 2; decoder compression rate is 1, reflecting different information-density needs.
Dimensions and Parameter Layout
| Parameter | Value |
|---|---|
| Hidden dimension | 5120 |
| Main attention query heads | 64 |
| Main attention head dimension | 512 |
| Query latent dimension | 1280 |
| Indexer query heads | 32 |
| Indexer head dimension | 128 |
| Sparse attention Top-K | 512 |
| SWA window | 128 |
| CSA² encoder compression | 2 |
| CSA² decoder compression | 1 |
For batch 1 and sequence length 1024, the attention input is X ∈ R^{1×1024×5120}. There is also a residual-style intermediate X_mHC ∈ R^{1×1024×4×5120}; after mixing, the output matches the input so residuals stay aligned.
Design notes:
- Query head dimension is 512, much larger than the usual 64/128, increasing per-head bandwidth while low-rank compression controls parameter count.
- The indexer uses small heads (128-d, 32 heads) as a cheap coarse filter for Top-512, then the large main heads refine—classic coarse-to-fine ranking.
- The SWA window is only 128, so dense local mixing stays in a small neighborhood; global context goes through the sparse index path.
Query Generation: Two-Step Low-Rank Projection
Step 1 — compress:
Q_latent = X @ W_q_a # [1, 1024, 5120] -> [1, 1024, 1280]
W_q_a shrinks from 5120 × (64×512) to 5120 × 1280 (about 25× fewer parameters).
Step 2 — expand to 64 heads:
Q = Q_latent @ W_q_b # [1, 1024, 1280] -> [1, 1024, 64, 512]
Heads share a low-dimensional latent, then expand. That continues an MLA-style design, with a notably larger head dimension in this generation.
Full Compute Path for One Token
- Enter the layer with
X(5120-d). - Build Query via two-step low-rank projection (64 heads).
- Local attention against the layer’s private SWA KV (window 128).
- Indexer (32 heads × 128-d) decides S/F/R/I: reuse index, recompute index, or write new global KV.
- Global sparse attention: Top-512 positions from shared global KV, exact attention with the main Query.
- Cross-layer share of this layer’s global KV / indices by role.
- Mix the two attention paths, align dimensions, add residual, pass to the next layer.
Engineering Takeaways
- Layered KV cache management: global KV is written sparsely and reused widely, so inference engines can use a shared pool instead of per-layer copies.
- Index computation can be decoupled: Reindex separates selection from attention, which helps pipelining.
- Compression rate as a knob: different encoder/decoder rates show information density can be tuned for short chat versus long documents.
For teams serving large models in the cloud, sparse attention plus shared content cache and separate position selection is a practical pattern for cost-effective long-context serving. The split between content cache and position choice matters more than any single hyperparameter.
