Efficient Attention Architecture Explained: CSA2 and Cross-Layer KV Reuse
This article breaks down CSA2, a hierarchical compressed sparse attention architecture that separates historical content from historical position selection. It explains the encoder and decoder layer types, key dimension settings, the step-by-step computation path for a single token, the engineering benefits of a hierarchical sparse indexer, and the practical caveats around compression error and implementation. Readers will gain a clear basis for choosing window width, Top-K size, and layer type placement in long-context system design.
Why Efficient Attention Became the Main Line of Architecture Evolution
As context length grows from thousands of tokens to hundreds of thousands or more, the complexity of the attention mechanism becomes the dominant source of inference cost. Standard full attention scales quadratically with sequence length $S$, while the KV cache grows linearly with both the number of layers and the sequence length. As a result, long-context workloads are usually constrained by memory bandwidth rather than compute. The industry has converged on four broad strategies: restrict attention to a local window so tokens only attend to nearby content; compress history by aggregating distant tokens into fewer representations; select sparsely by computing only the few historical positions most relevant to the current query; and share state across layers to avoid rebuilding equivalent history at every layer.
The hierarchical compressed sparse attention architecture discussed here (referred to as CSA2 for this class of designs) combines these ideas. It splits “historical content” and “historical position selection” into two shared states that can be updated independently, so that in a forty-layer network only a small number of layers actually produce new global representations.
Overall Layered Structure: Encoder and Decoder Segments
This class of architecture typically treats the front of the network as an encoder segment and the back as a decoder segment, with the two differing in how they generate global representations and refresh indices. For clarity, define four layer types:
- S (local only): Uses sliding window attention (SWA) only, produces no global KV, and does not participate in the global index.
- F (full generation): Produces new global KV and recomputes the Top-K index.
- R (reuse): Reuses existing global KV and the existing Top-K index directly, with almost no additional cost.
- I (re-index): Reuses existing global KV but recomputes the Top-K index, updating only “where to look.”
In the first twenty layers of the encoder segment, local layers and full layers alternate, with three independent global KV caches generated at layers 2, 8, and 14. These three caches correspond to hidden states at different depths and therefore preserve historical information at different levels of abstraction: shallow layers lean toward lexical and local collocations, middle layers toward syntactic structure, and deep layers toward semantics and task-relevant information. Merging them into one would cause information at different levels to overwrite each other, while keeping them separate lets later layers draw on them as needed.
The decoder segment is dominated by re-indexing: no new global content is added, and the model only re-evaluates “which historical positions to attend to” based on the current hidden state. Overall, only four of the forty layers produce new global KV, eight perform new index selection (four full plus four re-index), and the remaining thirty reuse the existing index. All forty layers, however, keep their own local window KV and primary query. This is the core of CSA2: the historical information that was previously rebuilt at every layer is decoupled into a content cache and a position selection that can be updated separately.
Key Dimension Configuration and Meaning
A typical dimension configuration is shown below (used to illustrate the order of magnitude, not specific product parameters):
hidden_size = 5120query_heads = 64head_dim = 512query_latent_dim = 1280indexer_heads = 32indexer_head_dim = 128sparse_top_k = 512swa_window = 128encoder_compress_ratio = 2decoder_compress_ratio = 1
The hidden size of 5120 sets the width of the residual stream. The main attention uses 64 heads of 512 dimensions each, together with a 1280-dimensional low-rank query latent, which substantially compresses both query-side parameters and activations. The indexer uses a lightweight structure of 32 heads of 128 dimensions each, responsible only for scoring and ranking rather than content aggregation. A sparse Top-K of 512 and a local window of 128 mean that the effective receptive field of a single token is roughly all positions within the window plus 512 selected distant positions. If multiple residual streams are present (for example, a 4-way mixture), the dimension after mixing still matches the input, keeping the module stackable.
Step-by-Step Breakdown of the Single-Token Computation Path
Assume batch size $B=1$, sequence length $S=1024$, and input tensor $X \in \mathbb{R}^{1\times1024\times5120}$.
Step 1: Low-rank generation of the query. Compress first, then expand, to reduce parameters and memory usage:
# X: [B, S, 5120]Q_latent = X @ Wq_A # [B, S, 1280]Q = Q_latent @ Wq_B # [B, S, 64*512] -> reshape into multiple heads
Step 2: Local window attention. Each query interacts only with the 128 positions inside the window, so complexity is nearly linear in sequence length, and the window KV is stored independently per layer with no cross-layer synchronization.
Step 3: Global index scoring. The indexer uses a lightweight 32-head, 128-dimension query/key pair to score candidate positions and select the Top-512. This step produces only a “set of positions,” not content.
Step 4: Aggregate global content by index. Behavior depends on the layer type: F layers produce new global KV and compute a new index; I layers keep the old KV and refresh the index; R layers reuse both; S layers skip this step.
Step 5: Merge and output. The local attention result and the global sparse attention result are combined via gating or weighting, then projected back to the 5120-dimensional residual stream.
Engineering Value of the Hierarchical Sparse Indexer
The hierarchical sparse indexer completely separates scoring from value retrieval, yielding three benefits. First, memory savings: global KV is materialized in only four layers and shared by the rest, reducing KV usage in long-context settings to roughly one tenth of the original. Second, bandwidth savings: the indexer has small dimensions and is reusable, and Top-K results are inherited at zero cost across thirty layers, cutting repeated sorting and gather overhead. Third, extensibility: Top-K and window width become two independently tunable knobs that can trade off accuracy against throughput without changing the main model structure.
Compression Error and Implementation Notes
Compression is not lossless, and three points deserve attention. First, balancing compression ratio against semantic fidelity: a higher compression ratio in the encoder segment and a lower one in the decoder segment is a common compromise. Second, consistency between index and content: if re-indexing layers refresh too often, the selection can jitter, while refreshing too rarely may select stale positions; in practice this usually stays aligned with the layer type mask used during training. Third, on the engineering side, the non-contiguous memory access of sparse gather can easily become a bottleneck, so indices typically need blocking and alignment, combined with monitoring, alerting, and logging services to observe hit rates and latency distributions. For cloud deployment, container platforms can host elastic inference, object storage can hold long-context caches, and message queues can smooth request spikes, forming a complete long-text service capability.
Summary
The essence of CSA2-style architectures is turning “what to remember” and “where to look” into two independently evolving states: the content side maintains multi-depth global caches through a few full layers, while the position side is refreshed dynamically by a lightweight indexer and re-indexing layers. Understanding this computation path gives you a grounded basis for choosing window width, Top-K size, and layer type placement when designing long-context systems, and general-purpose capabilities from Baidu AI Cloud can host deployment and operations, turning efficient attention into a measurable cost advantage.
