Efficient Attention Architecture Explained: Sparse Indexing and KV Reuse

    This article breaks down how a modern LLM attention architecture reduces the cost of long-context inference by separating historical content from historical position selection. It walks through the computation path of a single token, covering sliding window attention (SWA), compressed sparse attention (CSA2), the Full/Reindex/Reuse layer types, cross-layer KV reuse, the hierarchical sparse indexer, and compression error control (CED). Readers will find illustrative dimensions, layer allocation examples, execution differences between the three layer modes, and practical engineering notes on memory budget, scheduling, and observability. All dimensions and layer counts are illustrative configurations meant to explain the design rationale rather than product commitments.

    1. Why Efficient Attention Became the Main Line of Architecture Evolution

    As context lengths keep growing, the memory footprint and memory-access overhead of attention scale quadratically. The industry has largely converged on one optimization direction: reducing repeated computation and repeated storage of historical information. In conventional designs, every layer independently generates and stores a complete key-value cache of history, and every layer repeats a global position selection step. This creates two forms of redundancy: redundant caching of historical content, and redundant caching of historical position-selection results.

    This article analyzes a next-generation LLM architecture by following the actual computation path of a token from input to output. It focuses on sliding window attention (SWA), compressed sparse attention (CSA2), the three layer types (Full/Reindex/Reuse), cross-layer KV reuse, the hierarchical sparse indexer, and compression error control (CED). Note that the dimensions and layer counts used here are illustrative configurations for explaining the design ideas; they do not represent any commitment about a specific product.

    2. Overall Architecture: Splitting “Content” and “Selection” into Two Shared States

    The most notable design in this architecture is that the historical information each layer used to maintain redundantly is split into two shared states that can be updated independently:

    • Historical content cache (global KV): stores the compressed global representation of history;
    • Historical position selection (Top-K index): stores the result of “which global positions the current query should attend to.”

    On top of this, layers are divided into three types:

    • F (Full): generates new global KV and computes a new Top-K index;
    • I (Reindex): reuses existing global KV but recomputes the Top-K index;
    • R (Reuse): reuses both the existing global KV and the Top-K index.

    A typical configuration is that in a 40-layer depth, only a few layers (for example, around layers 2, 8, and 14) actually generate new global KV, and these layers correspond to hidden states at different depths, so historical semantic information at different levels is preserved. Only a smaller number of layers perform a new index selection (Full and Reindex together are about 8 layers), while the remaining roughly 30 layers directly reuse existing index results. Meanwhile, every layer keeps its own SWA cache and main query.

    The benefit of this design is direct: the number of global KV generations drops from “once per layer” to “a few times,” and the number of global index computations drops substantially as well, while local window attention remains independent per layer. This balances expressive power and computational efficiency in long-context scenarios.

    A schematic layer-type allocation can be understood like this:

    1. layer 0 : R (reuse KV + reuse Top-K)
    2. layer 1 : I (reuse KV + recompute Top-K)
    3. layer 2 : F (new global KV + new Top-K)
    4. ...
    5. layer 8 : F
    6. layer 14: F
    7. other layers: alternating R / I

    3. Basic Dimensions and Input Shape

    Before understanding the computation path, fix a set of illustrative dimensions:

    • Hidden dimension d_model = 5120
    • Main attention query heads = 64, per-head dimension = 512
    • Query low-rank dimension = 1280
    • Indexer query heads = 32, per-head dimension = 128
    • Sparse attention Top-K = 512
    • SWA window length = 128
    • CSA2 encoder compression ratio = 2, decoder compression ratio = 1

    Assume batch size B=1 and sequence length S=1024, so the input entering attention is X ∈ R^{1×1024×5120}. If the model enables multiple residual streams (for example, 4), the residual stream shape is X_mHC ∈ R^{1×1024×4×5120}, but after mixing the dimension stays the same as on entry, so downstream modules do not need to be aware of the number of residual streams.

    4. The Low-Rank Query Generation Path

    Query generation happens in two steps, compress first then expand, to reduce parameter count and computation:

    Step one, low-rank compression:

    1. Q_latent = X · W_q^A
    2. [1, 1024, 5120] -> [1, 1024, 1280]

    Step two, project to 64 attention heads:

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

    This “reduce to low rank first, then raise the dimension” approach is consistent with the idea of low-rank key-value caches: compress information shared across tokens into a smaller latent space, then expand it per head, reducing cache and memory-access pressure while preserving multi-head expressiveness.

    5. SWA: The Role of Local Window Attention

    Sliding window attention handles short-range dependencies. A window length of 128 means each token interacts only with the most recent 128 positions, and its cache is maintained independently per layer, so it is unaffected by cross-layer reuse. The value of SWA is that:

    • Computation and memory overhead grow linearly with the window and are decoupled from total sequence length;
    • It preserves local syntax and short-range semantics, providing stable local context for the global sparse path;
    • It complements the global KV mechanism, forming a dual-channel structure of “fine local + sparse global.”

    6. CSA2 and the Hierarchical Sparse Indexer

    The core of CSA2 is “compression + sparse selection.” On the encoder side, history is compressed at a ratio of 2 to produce global KV; on the decoder side the compression ratio is 1, preserving a fine-grained representation of the current token. The indexer uses 32 heads with a per-head dimension of 128 to compute a match score between each query and the global positions, then selects the Top-512 positions to participate in the main attention computation.

    The point of the hierarchical sparse indexer is that global KV generated at different depths corresponds to hidden states at different levels: shallow layers lean toward surface form and local patterns, while deep layers lean toward abstract semantics. Organizing multiple global KVs by depth lets upper layers perform index selection at different semantic granularities instead of relying on a single representation.

    7. Execution Differences Between Full / Reindex / Reuse

    • Full: writes new global KV and computes a new Top-K, suitable for depths where semantics shift noticeably;
    • Reindex: global KV stays unchanged, but the query change means the attended positions need updating, so only the index is recomputed;
    • Reuse: both the query and history are relatively stable, so the existing index is reused directly at almost zero extra cost.

    In engineering terms, these three modes can be controlled through a unified schedule table, avoiding separate branch logic written for each layer:

    1. def layer_mode(idx, full_layers={2, 8, 14}, reindex_layers={1, 5, 9, 13}):
    2. if idx in full_layers: return "F"
    3. if idx in reindex_layers: return "I"
    4. return "R"

    8. CED: Controlling Compression Error

    Any compression introduces error. The idea behind CED is to add an error constraint between compression and sparse selection, preventing key historical information from being dropped during compression or Top-K truncation. In practice, two things are usually watched: first, that representation similarity is preserved before and after compression; second, that the contribution deviation of selected positions to the output distribution stays controllable. Without this kind of control, long-context settings tend to show the problem of “information in middle passages being diluted.”

    9. Engineering Recommendations

    • Memory budget: generating global KV only in a few layers can significantly reduce long-context cache usage, but space still needs to be reserved for each layer’s SWA cache;
    • Scheduling complexity: the layer-type allocation must stay consistent with the training strategy and cannot be changed arbitrarily at inference time;
    • Observability: it is advisable to monitor the actual Top-K hit distribution per layer and compression error metrics, making it easier to locate long-context degradation;
    • Platform side: inference services can be hosted on a container platform, with a logging service and monitoring alerts tracking cache hit rate and latency percentiles.

    10. Summary

    The key insight of this architecture is decoupling “historical content” and “historical position selection” into two shared states that can be updated independently: a few Full layers establish multi-depth global KV, Reindex layers update the index to follow query changes, and a large number of Reuse layers eliminate repeated computation. Together with SWA for local precision, CSA2 and the hierarchical indexer for global coverage, and CED for compression error control, this dual-cache approach of “content cache + index cache” is more worth borrowing than simply stacking sparse patterns.