Efficient Attention Architectures in LLMs: CSA2 and Cross-Layer KV Reuse

    This article breaks down how modern long-context attention architectures manage the trade-off between historical information and memory bandwidth. It explains CSA2's core idea of separating shared state into historical content (KV cache) and historical position selection (Top-K), then walks through sliding window attention (SWA), the Full/Reuse/Reindex update modes for global KV, cross-layer KV reuse, the hierarchical sparse indexer, and Compression Error Control (CED). Readers will find the dimension settings, query generation flow, resource distribution across layers, and practical engineering guidance for memory accounting, bandwidth optimization, observability, and elastic deployment.

    Efficient Attention Architectures in LLMs: CSA2 and Cross-Layer KV Reuse

    Why Attention Needs to Be Decomposed

    The core tension in long-context inference is that historical information provides value, but it also consumes memory and bandwidth. In standard full attention, every layer generates and stores its own key-value cache for all historical positions. As the number of layers grows, cache size and read bandwidth quickly become the bottleneck. The recent industry trend is to stop making every layer “record history on its own” and instead introduce mechanisms such as window attention, sparse selection, and cross-layer sharing.

    This article follows the actual computation path of a single token through the model. It focuses on sliding window attention (SWA), compressed sparse attention (CSA2), the three update modes for global KV, cross-layer KV reuse, the hierarchical sparse indexer, and Compression Error Control (CED).

    Overall Architecture: Splitting “History” into Two Shared States

    The overall structure can be understood as a two-stage stack: an encoder segment followed by a decoder segment. The encoder segment generates and refreshes the global historical representation, while the decoder segment performs position selection on top of the existing history. Updates to the global KV are grouped into three modes:

    • F (Full): Generate new global KV and compute a new Top-K index.
    • R (Reuse): Directly reuse the existing global KV and Top-K index.
    • I (Reindex): Reuse the existing global KV but recompute the Top-K index.

    A typical configuration has a few layers in the encoder segment (for example, layers 2, 8, and 14) each producing an independent global KV. Because they correspond to hidden states at different depths, they retain semantics and historical information from different levels rather than simply duplicating the same content. The decoder segment is mostly Reindex: it re-decides which positions of the global KV to attend to, without regenerating the KV content.

    The resulting resource distribution is clear. At a scale of 40 layers, only 4 layers actually generate new global KV; only 8 layers perform a new index selection (4 Full + 4 Reindex); the remaining 30 layers directly reuse existing index results. However, all 40 layers keep their own SWA KV and main Query.

    This is the core idea of CSA2: split the history that each layer used to regenerate into two shared states, the historical content cache (KV) and the historical position selection (Top-K). The two can be updated, invalidated, and reused separately.

    Basic Dimensions and Input Shapes

    A common set of dimensions is:

    Item Value
    Hidden dimension d_model 5120
    Main attention Query heads 64
    Main attention per-head dimension 512
    Query latent dimension 1280
    Indexer Query heads 32
    Indexer per-head dimension 128
    Sparse attention Top-K 512
    SWA window 128
    Encoder compression ratio 2
    Decoder compression ratio 1

    Assume batch size B=1 and sequence length S=1024. The tensor entering attention is X ∈ R^{1×1024×5120}. If there are multiple residual streams (for example, 4), it is denoted X_mHC ∈ R^{1×1024×4×5120}. After mixing, the dimension stays the same as the input, so the input dimension of all subsequent projection matrices is unchanged.

    Two-Stage Query Generation

    The Query uses low-rank decomposition, compressing first and then expanding:

    1. Q_latent = X · W_q^A # [1,1024,5120] -> [1,1024,1280]
    2. Q = Q_latent · W_q^B # -> [1,1024,64,512]

    The first stage compresses the 5120-dimensional hidden state into a 1280-dimensional latent space. The second stage expands it into 64 heads of 512 dimensions. The benefit is that the object shared across layers and cached becomes a low-dimensional latent variable, reducing both memory usage and projection computation, while expressive power is restored through the second projection.

    Hierarchical Sparse Indexer: How Top-K Is Selected

    The indexer is a lightweight scorer. It uses fewer heads (32) and a smaller head dimension (128) to estimate the availability of historical positions for each Query, then takes the Top-K (512). It has three key points:

    1. Decoupled from main attention: The indexer has far fewer parameters than main attention, keeping scoring overhead controllable.
    2. Granularity is position blocks, not single points: The hierarchical structure usually filters at a coarse granularity first, then refines within the candidate set, avoiding exact scoring over the full sequence.
    3. Indexes are reusable: This is the premise for Reindex and Reuse to exist. An index is an independent state and does not have to live and die with the KV.

    Illustrative logic:

    1. # Pseudocode: hierarchical sparse indexing
    2. score = indexer_q @ indexer_k.T # coarse-grained scoring
    3. coarse = topk(score, k=coarse_k) # coarse filtering
    4. fine = refine(score, coarse) # refinement within candidates
    5. idx = topk(fine, k=512) # final Top-K

    How SWA, Compression, and Full/Reindex/Reuse Work Together

    In a single forward pass, the Query faces three information sources at once: recent tokens within the window (SWA, window 128, independent per layer, highest precision), compressed global history (encoder compression ratio 2, decoder compression ratio 1), and the 512 global positions selected by the indexer. Their division of labor is: SWA preserves local detail, compressed global preserves long-range overview, and Top-K preserves key jump access.

    Mode selection follows an “update on demand” principle. Use Full when content changes significantly; use Reindex when content is stable but the query distribution drifts; use Reuse when both are stable. In engineering, a lightweight criterion (such as hidden state drift magnitude or index overlap rate) can drive the mode decision, avoiding wasted work at every layer.

    Compression Error Control (CED)

    Compression inevitably causes information loss. CED constrains that loss to an acceptable range in three ways: it aligns key statistics before and after compression; it protects high-norm and abnormal activation positions so they are not averaged away; and it keeps the decoder compression ratio at 1, compressing less or not at all at the positions closest to the output. The practical rule of thumb is: the closer a layer is to the output, the more conservative compression should be.

    Engineering Implementation Notes

    • Memory accounting: Count SWA KV, global KV, and index cache separately. Because global KV is shared across layers, its total size depends roughly on the number of layers that generate it, not the total number of layers.
    • Bandwidth first: The bottleneck in the decoding stage is usually reading rather than computing. Reusing indexes significantly reduces random access.
    • Observability: Add instrumentation in logging services and monitoring alerts to record the layer distribution of Full/Reindex/Reuse, index overlap rate, and compression error, making it easier to locate quality regressions.
    • Elastic deployment: Combine with container platforms for elastic scaling, routing long-context requests and short requests to different instances to avoid interference.

    Summary

    The key to efficient attention is not “less computation” but a more reasonable division of state: decouple historical content from historical position selection and update them at different frequencies; carry the shared cache with low-rank latent variables; use a lightweight hierarchical indexer to decide the access range; and use error control to hold the compression lower bound. Understanding this path makes it easier to balance quality, memory, and throughput in long-context scenarios.