Understanding Efficient Attention Architectures: CSA2 and Cross-Layer KV Reuse
This article breaks down how an efficient attention architecture organizes computation across layers, using a 40-layer model as a reference. It explains the CSA2 design, where historical content (key-value caches) and historical position selection (Top-K indices) are separated into two independently updated shared states. You will learn how the encoder and decoder segments differ, how low-rank query projection and asymmetric compression rates work, and what Full, Reindex, and Reuse modes mean for inference cost. The article also includes dimension conventions, a tuning checklist, and practical notes on cache hit rate and index recomputation.
1. Why Efficient Attention Became the Main Axis of Architecture Evolution
As context lengths keep growing, the memory footprint and memory-access cost of attention scale quadratically, making attention the dominant source of inference cost. The prevailing technical trend is to preserve long-range modeling ability while dismantling a redundant structure: generating and storing a full set of historical keys and values at every layer.
This article uses a next-generation large language model architecture as its analysis target and walks through the actual computation path of a single token from input to output, layer by layer. Note that the discussion covers a general architectural paradigm, not the proprietary implementation of any specific product.
2. Overall Architecture: Division of Labor Between the Encoder and Decoder Segments
The model has 40 layers, split into a 20-layer encoder segment and a 20-layer decoder segment. The attention role of each layer is determined by one of four modes:
- S (sliding window only): computes only within a local window and produces no global key-value cache.
- F (full generation): generates new global keys and values and recomputes the Top-K index.
- R (reuse): directly reuses the existing global keys, values, and Top-K index.
- I (reindex): reuses the existing global keys and values but recomputes the Top-K index.
In the encoder segment, layers 2, 8, and 14 each generate an independent set of global keys and values. Because these correspond to hidden states at different depths, they retain historical information from different levels and avoid the information loss caused by deeper layers overwriting shallow semantics.
The decoder segment is dominated by reindexing. Reindexing means re-deciding which positions in the global key-value cache to use, rather than regenerating the keys and values themselves.
This yields the following resource distribution across the network:
- Only 4 of the 40 layers actually generate new global keys and values;
- Only 8 layers perform a new index selection (4 full + 4 reindex);
- The remaining 30 layers directly reuse existing index results;
- All 40 layers still keep their own sliding-window keys and values and main queries.
This is the core idea of the architecture: the historical information that was previously regenerated at every layer is split into two independently updatable shared states — the historical content cache (keys and values) and the historical position selection (Top-K). Decoupling the two is the key to CSA2.
3. Key Dimensions and Shape Conventions
Before tracing the computation path, fix a typical set of dimensions:
| Item | 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 |
| Sliding window | 128 |
| Encoder segment compression rate | 2 |
| Decoder segment compression rate | 1 |
Assuming a batch size of 1 and a sequence length of 1024, the tensor entering attention is X ∈ R^{1×1024×5120}. If multiple residual streams are enabled, its shape is X_mHC ∈ R^{1×1024×4×5120}, and the mixed dimension stays consistent with the input, so the shape contract of subsequent layers is not broken.
4. Query Generation: Low-Rank Compression Then Expansion
The query side uses a two-stage projection that compresses first and expands second:
# Step 1: low-rank compressionQ_latent = X @ Wq_A # [1,1024,5120] -> [1,1024,1280]# Step 2: expand to multiple headsQ = Q_latent @ Wq_B # [1,1024,1280] -> [1,1024,64,512]
This design has three benefits: it splits one large matrix multiplication into two smaller ones, reducing parameter count; the latent variable becomes a reusable shared representation that is easy to align with key-value compression; and the 1280 bottleneck dimension significantly reduces the memory residency of activations.
5. The Key-Value Path and the Role of Compression Rates
The key-value side is likewise compressed into a latent representation first, then aggregated according to the compression rate. A compression rate of 2 in the encoder segment means every two adjacent tokens are merged into one cache slot; a compression rate of 1 in the decoder segment means no merging, preserving per-token granularity. The logic behind this asymmetric setup is that the encoder segment handles historical information that can tolerate stronger aggregation, while the decoder segment needs finer positional resolution.
The sliding-window portion is maintained independently, with a window length of 128, and handles precise modeling over short distances; the global portion is governed by Top-K=512 and handles long-range dependencies. Combined, a single layer has both local sharpness and global coverage.
6. How the Hierarchical Sparse Indexer Works
The indexer uses 32 heads of 128 dimensions each, a clear asymmetry against the main attention’s 64 heads of 512 dimensions. The reason is that the indexing task only needs to decide whether a position is worth attending to — a low-precision ranking problem that does not require the representational capacity of main attention.
Its flow can be summarized as:
- Use the indexer query to lightly score against the compressed keys;
- Select the Top-K positions;
- Pass the selection result to main attention as a sparse mask.
Because the indexer is far cheaper than main attention, separating “choosing positions” from “computing content” is the main lever for reducing overall cost.
7. Engineering Implications of Full / Reindex / Reuse
The three modes correspond to completely different execution branches at inference time:
- Full: needs to write new global cache entries and trigger a full index computation, the highest cost;
- Reindex: reads the global cache only and recomputes the index, moderate cost;
- Reuse: both cache and index are hits, with almost no extra cost.
In batch inference, the mode distribution determines actual throughput. Spreading Full layers evenly causes memory peak jitter; concentrating them makes it easier to allocate cache blocks in one pass. In engineering practice, cache paging and pre-allocation strategies are usually combined to reduce fragmentation.
8. Practical Recommendations and Tuning Checklist
- Compression rate: when context is extremely long and information is highly redundant, the encoder segment compression rate can be raised, at the cost of detail-localization ability;
- Top-K: strongly task-dependent; retrieval-style tasks usually need a larger K;
- Window length: too short weakens local syntactic modeling, too long weakens the benefit of sparsification;
- Layer layout: the number of Full layers determines cache write bandwidth and should be evaluated against available memory;
- Observability: tracking metrics such as cache hit rate and index recomputation time through monitoring alerts and logging services is recommended.
9. Summary
The most valuable takeaway from this architecture is not any single-point optimization, but treating “historical content” and “historical position” as two independently evolving states. Understanding this lets you decide, in your own model or inference optimization work, which layers need new information and which only need to re-locate, achieving a controllable balance among memory, bandwidth, and accuracy.
