Sparse Attention Architecture Design for Next-Generation Large Language Models

    This article examines an efficient large language model attention architecture built around sliding-window attention, compressed sparse attention, cross-layer KV reuse, and hierarchical sparse indexing. By following the computation path of a token, it explains how Full, Reuse, and Reindex layers separate historical content caching from position selection to reduce global attention cost while preserving local-context modeling.

    In recent years, attention mechanisms in mainstream large language models have followed a common direction: pure full attention is increasingly being replaced by hybrid designs that combine local windows with sparse global attention.

    This article analyzes a representative architecture by tracing the actual computation path of a token through the model. It examines key design elements, including Sliding Window Attention (SWA), Compressed Sparse Attention (CSA2), the Full/Reindex/Reuse layer roles, cross-layer KV reuse, and the Hierarchical Sparse Indexer.

    1. Architecture Overview: Separating Repeated Historical Computation into Two Shared States

    The first step in understanding this architecture is recognizing how it divides responsibilities across Transformer layers.

    In a conventional dense Transformer, every layer generates KV caches for all previous tokens and performs full attention computation. This creates substantial memory and compute overhead. The central insight of this architecture is that historical information content and the selection of historical positions can be treated as separate concerns.

    The architecture defines four layer roles:

    • S (SWA): Uses only sliding-window attention over the most recent local context. It does not generate global KV.
    • F (Full): Generates new global KV and computes new Top-K sparse indices.
    • R (Reuse): Directly reuses existing global KV and previously computed Top-K indices. No new index computation is performed.
    • I (Reindex): Reuses existing global KV content but recomputes Top-K indices based on the current layer’s hidden states.

    Consider a typical 40-layer configuration, with the first 20 layers forming an encoder segment and the remaining 20 layers forming a decoder segment. The layer-role distribution leads to a clear conclusion:

    • All 40 layers retain their own local SWA KV and primary queries.
    • Only four layers—the F layers, positioned at layers such as L2, L8, and L14—actually generate new global KV.
    • Only eight layers perform index selection: four Full layers and four Reindex layers.
    • The remaining 30 layers fully reuse previously computed index results.

    An important detail is that the F layers at L2, L8, and L14 each generate an independent global KV set. Because they are derived from hidden states at different depths, they naturally preserve historical information at different abstraction levels: shallower layers tend to capture surface-level features, while deeper layers encode more semantic information.

    The decoder segment primarily relies on the Reindex mechanism. It does not regenerate global KV. Instead, it uses the current layer representation to decide which historical positions should be attended to.

    This is the core idea behind Compressed Sparse Attention (CSA2): rather than regenerating historical information at every layer, the model separates it into two independently shared states:

    • Historical content cache: KV
    • Historical position selection: Top-K indices

    These states can be updated at different frequencies, substantially reducing both computation and cache costs.

    2. Key Dimension Configuration

    Before examining the computation path in detail, the model’s main hyperparameters are summarized below.

    Parameter Value
    Hidden Dimension (d_model) 5120
    Primary Attention Query Heads 64
    Primary Attention Head Dimension 512
    Query Latent Dimension 1280
    Indexer Query Heads 32
    Indexer Head Dimension 128
    Sparse Attention Top-K 512
    SWA Window 128
    Encoder Compression Ratio 2
    Decoder Compression Ratio 1

    Assume a batch size of B=1 and a sequence length of S=1024. The attention input tensor is:

    X ∈ R^(1×1024×5120)

    The model also maintains a multi-path residual stream:

    X_mHC ∈ R^(1×1024×4×5120)

    After mixing, the tensor passed into attention retains the same dimensionality as the input.

    3. Core Component: Query Generation

    The primary attention mechanism uses low-rank factorization to compress Query generation. This follows the same general principle as low-rank compression on the KV side: reducing projection parameters and intermediate activation memory.

    First, the input is projected down into a lower-dimensional latent representation:

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

    The latent representation is then projected back up and split across 64 attention heads:

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

    Compared with directly projecting from 5120 dimensions to 64 × 512 dimensions, the 1280-dimensional bottleneck significantly reduces the parameter size of the projection matrices. At the same time, this low-rank factorization is designed to preserve model quality with minimal loss.

    4. Sparse Indexer and Hierarchical Indexing

    With Top-K=512, sparse attention must determine which historical KV positions are most relevant to the current Query. This task is handled by a lightweight Indexer.

    The Indexer uses a smaller attention structure with 32 heads and 128 dimensions per head. At a much lower cost than the primary attention mechanism, it computes importance scores for historical positions for each Query. It then selects the Top-K positions to construct a sparse attention mask.

    The hierarchical design means that indices do not need to be recomputed in every layer:

    • Indices calculated by a shallow Full layer can be directly reused by multiple later Reuse layers.
    • A Reindex layer recalculates indices only when representations have changed significantly.
    • Local window attention remains independent in every layer.

    This update schedule—less frequent content updates, moderately frequent index updates, and per-layer local-window computation—is the key mechanism that balances efficiency and accuracy.

    5. Differences Between the Encoder and Decoder Segments

    The encoder segment processes the fully visible input sequence. It can apply a compression ratio of 2 to global KV, further reducing cache requirements.

    The decoder segment handles autoregressive generation. To preserve token-by-token generation quality, its compression ratio is set to 1, meaning no compression is applied. The decoder also relies more heavily on Reindex operations to adapt to the continuously changing generation context.

    This asymmetric design balances throughput during the prefill stage with output quality during the decoding stage.

    6. Summary

    The architecture can be summarized through three core principles:

    1. Decoupling: Separate KV content from index selection so that what to attend to and the content being attended to can be updated at different frequencies.
    2. Hierarchical sharing: Generate global KV in only a small number of layers and recompute indices in only a limited set of layers, while the remaining layers reuse them. This keeps the growth of global attention overhead close to constant.
    3. Local coverage: Preserve independent SWA and Query computation in every layer, ensuring that detailed modeling of recent context is not compromised by sparsification.

    For engineers focused on long-context inference efficiency, the design pattern of local attention, sparse global attention, and cross-layer reuse represents a mature direction in efficient attention architectures. It is also an important consideration for model evaluation and inference deployment.