Efficient Attention Architecture Explained: Sparse Indexing and Cross-Layer KV Reuse in Practice

    This article breaks down a modern efficient attention architecture by following a single token through the full computation path. It explains how sliding window attention (SWA), sparse compressed attention (CSA2), global KV, and Top-K indexing work together through a Full/Reindex/Reuse scheduling scheme, along with cross-layer KV reuse, a hierarchical sparse indexer, and compression error control (CED). Readers will find the layer structure, key dimensions, Query generation, scheduling logic, and engineering evaluation metrics needed to understand how content caching and position selection are decoupled for long-context models.

    1. Why Efficient Attention Has Become the Main Line of Architecture Evolution

    As context lengths keep growing, the memory footprint and memory-access overhead of attention computation rise nonlinearly. The prevailing industry trend is to stop having every layer independently maintain the full history. Instead, “historical content” and “historical position selection” are split into two types of state that can be updated independently. This article uses a next-generation large model’s attention architecture as an abstract sample and follows the real computation path of a single token to explain sliding window attention (SWA), sparse compressed attention (CSA2), the Full/Reindex/Reuse three-state scheduling of global KV and Top-K indexing, cross-layer KV reuse, the hierarchical sparse indexer, and compression error control (CED).

    2. Overall Layered Structure: Division of Responsibilities Between Encoder and Decoder

    The first 20 layers can be viewed as the encoder segment, using three layer types:

    • S: Pure sliding window, which keeps only the local window KV and produces no global KV;
    • F: Full, which generates new global KV and recomputes the Top-K index;
    • I: Reindex, which reuses existing global KV but recomputes the Top-K index.

    Layers 2, 8, and 14 each generate a separate global KV, corresponding to hidden states at different depths, thereby preserving multi-level historical semantics. The last 20 layers form the decoder segment, which is dominated by Reindex. It does not regenerate global KV but only re-decides “which historical positions to attend to.” The resulting statistics are: among 40 layers, only 4 generate new global KV, 8 perform new index selection (4 Full + 4 Reindex), and 30 directly reuse existing indices; however, all layers keep their own SWA KV and main Query. This is the core idea of CSA2: decoupling content caching from position selection and updating them separately.

    3. Key Dimensions and the Shape of a Token Entering Attention

    The core configuration includes: hidden dimension 5120, 64 main attention Query heads, per-head dimension 512, Query low-rank dimension 1280, 32 indexer Query heads, indexer per-head dimension 128, sparse Top-K of 512, SWA window 128, encoder compression ratio 2, and decoder compression ratio 1. With batch size 1 and sequence length 1024, the input tensor is X ∈ R^{1×1024×5120}. If there are multiple residual streams, for example 4 in parallel, the residual tensor is R^{1×1024×4×5120}, and after mixing it returns to a unified hidden dimension.

    4. Query Generation: Low-Rank Compression Followed by Projection

    The Query is generated in two stages, compressing first and then expanding, to reduce parameters and computation:

    1. Q_latent = X @ Wq_A # [1,1024,5120] -> [1,1024,1280]
    2. Q = Q_latent @ Wq_B # -> [1,1024,64,512]

    The low-rank bottleneck lets different heads share the compressed semantic basis and then projects it to 64 heads, balancing expressiveness and efficiency.

    5. Hierarchical Sparse Indexer and Three-State Scheduling

    The indexer uses a lightweight Query with 32 heads and 128 dimensions to score candidate historical positions and select the Top-512. Full layers update both content and index; Reindex layers keep content fixed and only refresh the index; Reuse layers keep both. The logic is illustrated below:

    1. if layer.type == FULL:
    2. kv_global, topk = build_kv_and_index(X)
    3. elif layer.type == REINDEX:
    4. topk = reindex(kv_global, X)
    5. else: # REUSE
    6. topk = cached_topk
    7. out = SWA(local_kv) + SparseAttn(kv_global, topk)

    The benefit of this scheduling is that global content is refreshed only in a few layers, while the index can adapt more frequently to semantic drift, balancing accuracy and overhead under long sequences.

    6. Compression Error Control and Engineering Evaluation

    CSA2 uses an encoder compression ratio of 2 and a decoder compression ratio of 1, which means cross-layer reuse introduces information loss and requires a compression error control mechanism for monitoring and compensation. In engineering practice, three types of metrics are worth watching: index hit rate and position drift, the difference in attention distribution before and after compression, and downstream quality regression on long-context tasks. On the cloud, inference services can be deployed with a container platform, while a logging service and a monitoring and alerting system collect the layer-type distribution, Top-K overlap rate, and latency percentiles, forming a closed loop for continuous tuning. Overall, the key to efficient attention is not single-point operator optimization, but the coordinated design of the three elements: content caching, position selection, and error control.