A Deep Dive into Efficient Sparse Attention Architectures: The Complete Computation Path of a Single Token

    This article explains a modern sparse attention architecture designed for long-context LLM inference. It covers why efficient attention matters, how the architecture splits layers into SWA, Full, Reuse, and Reindex roles, the core hyperparameters, the two-step low-rank Query generation, the full computation path of a single token, and the lightweight hierarchical sparse indexer. Readers will learn how decoupling KV storage from position selection dramatically reduces both memory and compute costs.

    1. Background: Why Efficient Attention Is the Focus of Architecture Competition

    As context windows of large models keep growing, the complexity of standard attention mechanisms becomes a major bottleneck: both attention compute and KV cache memory grow linearly or even quadratically with sequence length, directly driving up inference costs. Mainstream solutions share a common trend—minimizing the number of historical tokens that participate in attention computation while preserving model quality, and sharing a compressed KV cache across layers.

    This article follows the actual computation path of a single token through the model, focusing on these core mechanisms:

    • SWA (Sliding Window Attention): low-cost modeling of local context;
    • Full / Reindex / Reuse layer behaviors: generation, reuse, and re-indexing of the global KV cache;
    • Cross-layer KV reuse and compression: sharing the global historical cache across different depths;
    • Hierarchical sparse indexer: lightweight Top-K position selection;
    • Low-rank Query compression: reducing projection overhead at the attention entry point.

    2. Overall Architecture: Splitting 40 Layers into “Generate, Select, Reuse” Roles

    The core abstraction of this architecture replaces the traditional Transformer approach—where every layer independently generates and maintains its own global KV—with two separable shared states:

    1. Historical content cache (KV): the state tensors that actually carry historical information;
    2. Historical position selection (Top-K indices): deciding which historical positions deserve the current Query’s attention.

    These two states can be updated independently, which is the key to the entire design. To achieve this, layers are assigned four different roles:

    Role Meaning Global KV Top-K Index
    S (SWA) Pure sliding-window layer Not generated Not computed
    F (Full) Full layer Generates new global KV Computes new Top-K
    R (Reuse) Full-reuse layer Reuses existing KV Reuses existing indices
    I (Reindex) Re-index layer Reuses existing KV Recomputes Top-K

    Encoder-Side Layout

    Among the first 20 layers, positions such as L2, L8, and L14 each generate an independent global KV. These KVs come from hidden states at different depths, carrying historical information at different levels of abstraction—shallow layers lean toward lexical and local semantics, deeper layers toward global semantics and reasoning state. Multiple global KVs coexisting is equivalent to giving the model multiple “memory banks” with different temporal and semantic granularities.

    Decoder-Side Layout

    The decoder side primarily uses the Reindex mechanism: no new global KV is generated; the model only re-decides which positions to select from the existing global KV for attention. In other words, the historical content during decoding is frozen and reused, while only the selection logic changes dynamically.

    Global Statistics: A Remarkable Reduction Ratio

    Taking a 40-layer model as an example:

    • Only 4 layers actually generate new global KV (Full layers);
    • Only 8 layers perform new index selection (4 Full + 4 Reindex);
    • As many as 30 layers directly reuse previous index results;
    • Yet all 40 layers retain their own SWA KV and main Query.

    In short, the “writing” of global historical information is compressed into a tiny number of layers, and the “read path selection” is refreshed only in a subset of layers—everything else stands on the shoulders of earlier layers. Compared to the traditional approach where every layer independently maintains a full global KV, both KV cache memory and indexing compute are dramatically amortized.

    3. Core Dimensions: Design Trade-offs from the Configuration

    Before tracing the computation path, note the core hyperparameters:

    Parameter Value
    Hidden Dimension (d_model) 5120
    Main Attention Query Heads 64
    Main 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-Side Compression Ratio 2
    Decoder-Side Compression Ratio 1

    A few notable observations:

    • Head Dimension is 512, far above the common 64/128, meaning each head carries stronger expressive capacity while the number of heads stays restrained;
    • Top-K = 512 and SWA Window = 128 mean that the historical tokens actually participating in computation at any layer are hard-capped at a few hundred, decoupled from total sequence length;
    • The indexer’s dimensions are much smaller than the main attention (32 heads × 128 dims vs. 64 heads × 512 dims), because the indexer only needs to “judge positional importance,” not full expressiveness—a classic lightweight design;
    • The compression ratio is asymmetric between encoder and decoder (2 vs. 1), matching the different needs of offline batch compression during prefill versus low-latency responses during decoding.

    4. Core Components: Two-Step Query Generation

    Assume batch size B=1 and sequence length S=1024. A token entering the attention module has the shape:

    1. X ∈ R^{1 × 1024 × 5120}

    (The model also uses a 4-way residual stream mixing design; the mixed dimension matches the entry dimension, which we won’t expand on here.)

    Query generation happens in two steps:

    Step 1, low-rank compression:

    1. Q_latent = X · W_q_A # [1, 1024, 5120] → [1, 1024, 1280]

    The 5120-dim hidden state is first projected into a 1280-dim latent space. This significantly shrinks all subsequent Query-related intermediate tensors—the symmetric application of low-rank key-value compression on the Query side.

    Step 2, up-projection to multi-head form:

    1. Q = Q_latent · W_q_B # [1, 1024, 1280] → [1, 1024, 64 × 512]

    From the latent space, the final Query is projected into 64 attention heads with 512 dims each. Both parameter count and activation memory are noticeably lower than a dense projection directly from 5120 dims to 64 × 512.

    5. The Complete Computation Path of a Single Token

    Putting the components together, a token’s journey through a layer looks like this:

    1. Enter the layer: receive the mixed hidden state X (5120 dims);
    2. Generate Query: compress to 1280 dims via low-rank projection, then expand to 64 heads × 512 dims;
    3. Determine the layer’s role:
      • S layer: perform local attention only within the sliding window of length 128;
      • F layer: write new global KV and use the indexer to score historical positions, selecting Top-512;
      • I layer: reuse the global KV, but re-score and select Top-512 with this layer’s lightweight indexer;
      • R layer: reuse both KV and indices from upstream and compute directly;
    4. Sparse attention: the Query attends only to the union of “SWA window + Top-K global positions,” keeping compute nearly decoupled from sequence length;
    5. Output and residual: write back to the residual stream and proceed to the next layer.

    6. The Hierarchical Sparse Indexer: Making “Selection” Lightweight

    The indexer’s input dimensions are small (32 heads × 128 dims). It does not participate in actual attention computation; it answers one question: which historical positions should the current Query attend to?

    This design offers two engineering benefits:

    • Low scoring cost: index scoring can run in the compressed latent space, far cheaper than full attention;
    • Cross-layer index inheritance: because attention patterns are highly correlated across adjacent layers, Top-K computed by one layer can be directly reused by several subsequent layers, refreshed only at Reindex layers—avoiding redundant per-layer scoring.

    7. Summary and Takeaways

    The essential contribution of this architecture can be condensed into one sentence: decouple the “storage of history” from the “retrieval of history,” and let both be shared across layers at different frequencies. The key takeaways:

    1. Global KV does not need to be rewritten at every layer—a few snapshots at different depths suffice to cover multi-granularity historical information;
    2. Attention position selection exhibits strong cross-layer correlation, making index reuse a low-cost, high-reward lever;
    3. The combination of local (SWA) and global (sparse Top-K) pathways is complementary and represents the mainstream paradigm for sparse attention today;
    4. The combination of low-rank compression at the entry point and a lightweight indexer keeps the extra cost paid for sparsity to a minimum.

    For teams looking to deploy long-context capabilities in high-performance inference scenarios, this approach—separating content caching from position selection, with per-layer tiered reuse—offers directly applicable reference value.