Sparse Attention Architecture Explained: CSA2 and Cross-Layer KV Reuse
This article breaks down a sparse attention architecture built around CSA2 and cross-layer key-value reuse. It explains why efficient attention has become a central architectural concern as context lengths grow, then walks through the full computation path of a token: sliding-window local attention, compressed sparse attention, the Full/Reindex/Reuse layer roles, hierarchical sparse indexing, and compression error control. Readers will find the overall 40-layer layout, key dimensions and tensor shapes, query generation via low-rank compression, scheduling pseudocode, and practical engineering notes for managing memory and retrieval quality.
1. Why Efficient Attention Became the Main Line of Architecture Evolution
As context lengths keep growing, the cost of attention scales with the square of the sequence length, and both memory footprint and memory-access pressure grow alongside it. The common industry approach is to strike a balance between “global visibility” and “computational cost”: preserve the ability to model long-range dependencies while avoiding the need to regenerate and store a complete key-value cache at every layer. A class of newer sparse attention designs offers one answer: split historical information into two independently updatable states—a history content cache and history position selection—so that most layers only reuse existing state, while a few layers take on generation and re-indexing duties.
This article follows “the real computation path of a token through the model” as its main thread, unpacking sliding-window local attention, compressed sparse attention, the three layer types (full / reindex / reuse), cross-layer key-value reuse, the hierarchical sparse indexer, and compression error control one by one.
2. Overall Architecture: Only 4 Global Generations Across 40 Layers
The overall structure can be understood as a two-part layout of “front-end encoding + back-end decoding,” where each layer plays one of four roles:
- S (sliding window only): keeps only the keys and values within the local window and produces no global cache;
- F (full): generates new global keys and values and computes a new Top-K index;
- R (reuse): directly reuses the existing global keys, values, and index;
- I (reindex): reuses the existing global keys and values but recomputes the Top-K selection.
Under a typical configuration, the first 20 layers form the encoding segment, where layers 2, 8, and 14 each generate an independent global key-value set; the last 20 layers form the decoding segment, dominated by re-indexing. The result: only 4 of the 40 layers actually generate new global keys and values, 8 layers perform a new index selection (4 full + 4 reindex), and the remaining 30 layers directly reuse the existing index—yet all 40 layers keep their own sliding-window keys, values, and main query.
The value of this design is that global caches generated at different depths correspond to hidden states at different levels, naturally preserving multi-scale historical information; separating index updates from content updates avoids the waste of “recomputing all keys and values just to swap in a new set of positions.”
3. Core Dimensions and Input Shapes
The key dimension configuration typically includes: main model hidden dimension 5120; 64 main attention query heads with a per-head dimension of 512; query low-rank compression dimension 1280; 32 indexer query heads with a per-head dimension of 128; sparse attention Top-K of 512; sliding window size 128; encoding-segment compression rate 2; decoding-segment compression rate 1.
Assuming batch size 1 and sequence length 1024, the tensor entering attention is [1, 1024, 5120]. If a multi-residual-stream design is present (for example, 4 residual streams with shape [1, 1024, 4, 5120]), the dimension after mixing still matches the input, so subsequent modules can be stacked without any special handling.
4. Query Generation: Low-Rank Compression Plus Multi-Head Projection
The query path has two steps—compress first, then expand:
Q_latent = X @ Wq_A # [1,1024,5120] -> [1,1024,1280]Q = Q_latent @ Wq_B # [1,1024,1280] -> [1,1024,64,512]
Low-rank compression first reduces the 5120-dimensional hidden state to a 1280-dimensional latent space, then projects it to 64 query heads. This both cuts the parameter count and lets the indexer share the same latent representation, reducing redundant computation.
5. Sliding-Window Local Attention (SWA)
The sliding-window branch interacts only with the most recent 128 positions, reducing complexity from quadratic to linear. In practice, the window is usually implemented as a chunked ring buffer: new tokens are written at the tail and evicted from the head, avoiding the need to re-concatenate history for every token. The local branch handles short-range syntax and local coherence, while the global branch handles long-range topics and coreference; the two outputs are merged with per-head or per-layer weighting.
6. Compressed Sparse Attention and the Hierarchical Indexer
The global branch does not score every historical position. Instead, a hierarchical sparse indexer first scores the compressed representations and selects the Top-512 positions, after which exact attention is computed only over those positions. The indexer uses a smaller head dimension (128) and fewer heads (32), making it far cheaper than the main attention. The compression rate determines index granularity: a compression rate of 2 in the encoding segment means two adjacent tokens are merged into one index unit, while a rate of 1 in the decoding segment means per-token indexing—a segment-wise trade-off between “retrieval precision” and “indexing overhead.”
7. Scheduling Logic for Full / Reindex / Reuse
The decision among the three roles can be written as concise pseudocode:
if layer in GLOBAL_KV_LAYERS: # e.g., 2/8/14 and the corresponding decoding layerskv_global, topk = build_kv_and_index(x)elif layer in REINDEX_LAYERS:topk = indexer.recompute(kv_global) # swap positions only, not contentelse:topk = cached_topk # fully reusedout = swa(x, kv_local) + sparse_attn(q, kv_global, topk)
There are three scheduling principles: rebuild keys and values only when the content changes; reindex only when the semantics drift; and add zero extra global overhead for all other layers.
8. Compression Error Control and Engineering Notes
Both compression and sparsity introduce error. Common control measures include: keeping learnable aggregation weights for compression units; using a low-resolution fallback for positions outside the Top-K; adding a “full-attention distillation” constraint during training; and monitoring index hit rate and attention entropy to prevent the index from collapsing onto a few popular positions. On the deployment side, it is advisable to manage the global cache and the local window in separate pools, and to combine chunked prefill with incremental decoding to reduce peak memory usage under long contexts.
9. Conclusion
The core of efficient attention is not simply “computing less,” but splitting history into two states—content and position—and updating them separately. Understanding these four things—layer role assignment, segment-wise compression rates, indexer cost, and error control—lets you quickly judge the benefit boundaries of a sparse scheme when building your own model or evaluating options. Combining this with general capabilities such as the container platform, object storage, and monitoring and alerting offered by Baidu AI Cloud can further turn the tuning process for this kind of architecture into a repeatable engineering workflow.
