Efficient Sparse Attention: From Sliding-Window Attention to Cross-Layer KV Reuse

    This article examines a 40-layer sparse attention architecture that separates global KV content from the indexes used to select it. It explains how Sliding Window Attention, full-attention layers, KV reuse, and reindexing divide responsibilities across layers, and reviews the key dimensions, query-generation process, and efficiency trade-offs.

    1. Why Attention Needs to Be Slimmed Down Across Layers

    A growing number of model architectures avoid generating and storing a complete global key-value (KV) cache at every attention layer. Instead, they maintain two independently managed forms of historical state: the cached content itself (the KV values) and the positions selected from that history (sparse Top-K indexes).

    The motivation is straightforward. As context length grows, maintaining a global KV cache at every layer increases memory and bandwidth costs linearly, even though much of the stored information is redundant.

    The architecture discussed here follows a simple principle: most layers perform local attention, a small number generate and extend global memory, and other layers use sparse indexes to retrieve from that shared memory as needed.

    2. Layer Roles in a 40-Layer Architecture

    The 40 layers can be grouped into four roles:

    • S (SWA layer): Performs only Sliding Window Attention with a 128-token window. It does not generate global KV.
    • F (Full layer): Performs full attention, generates new global KV, and computes new Top-K indexes.
    • R (Reuse layer): Reuses existing global KV and Top-K indexes without recomputing them.
    • I (Reindex layer): Reuses existing global KV but recomputes Top-K indexes from the current layer’s hidden states.

    The division of work is highly sparse:

    1. Only 4 of the 40 layers generate new global KV.
    2. Only 8 layers select indexes: 4 Full layers and 4 Reindex layers.
    3. The remaining 30 layers reuse existing index results.
    4. All 40 layers maintain their own local SWA KV and main attention queries.

    The first three Full layers—for example, layers 2, 8, and 14—each generate an independent set of global KV based on hidden states at a different depth. This preserves historical information at multiple levels of abstraction: earlier layers tend to capture more surface-level features, while deeper layers capture more semantic features. Keeping multiple global memories at different depths allows later layers to draw on information with different receptive fields.

    On the decoder side, the architecture mainly uses reindexing. Generating new global KV during decoding would be expensive, so the model instead uses the current decoding state to choose which positions to retrieve from shared memory. This is a form of content-addressed sparse retrieval.

    3. Key Dimensions and Parameters

    The technical report specifies the following dimensions:

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

    For a single sequence of 1,024 tokens, the input to attention is X ∈ R^(1×1024×5120). The model also maintains a multi-head context residual stream, X_mHC ∈ R^(1×1024×4×5120). The mixed output retains the same dimension as the input, preserving the residual pathway.

    4. Query Generation Through Two-Stage Low-Rank Projection

    Query generation uses two projection stages, following the general approach of latent attention.

    Stage 1: Low-rank compression

    1. Q_latent = X @ W_q_a
    2. # [1, 1024, 5120] -> [1, 1024, 1280]

    This compresses the 5,120-dimensional hidden state into a 1,280-dimensional latent space, reducing the size of the intermediate representation that needs to be cached. Only the compressed latent vectors are cached, which is central to reducing KV memory use.

    Stage 2: Projection into attention heads

    1. Q = Q_latent @ W_q_b
    2. # [1, 1024, 1280] -> [1, 1024, 64*512]

    The result is a full query with 64 attention heads, each 512 dimensions wide. Using the 1,280-dimensional latent space as an intermediate separates low-rank storage from higher-dimensional computation.

    5. The Sparse Indexer: Choosing Where to Look

    The Indexer acts as the coordinator for sparse attention. It uses a separate, lightweight set of queries—32 heads with 128 dimensions per head, considerably smaller than the main attention’s 64 × 512 configuration—to score candidates in the global KV store. It then selects the Top 512 positions for the layer’s attention operation.

    Even when the sequence reaches tens of thousands of tokens, the main attention at each layer is limited to:

    • 128 positions in the SWA window, preserving local continuity;
    • 512 positions selected through sparse retrieval, keeping global information accessible.

    Together, these positions determine what the layer can attend to. On the encoder side, indexes are compressed by a factor of 2. On the decoder side, the compression ratio is 1, retaining more complete index information during generation.

    6. Benefits and Trade-Offs of Cross-Layer KV Reuse

    The architecture’s benefits can be considered across three dimensions:

    1. Memory: Global KV is generated in only about 10% of the layers, substantially reducing KV-cache use in long-context scenarios.
    2. Bandwidth: Thirty layers reuse existing index results, avoiding repeated global KV reads and writes at every layer.
    3. Quality: Multiple global KV sets from different depths, together with reindexing, avoid a one-size-fits-all reuse strategy. Deeper layers can retrieve information again based on their semantic needs.

    Reuse is a trade-off, not compression without cost. Reindex layers provide a way to select different historical positions when hidden states have evolved and the existing indexes no longer match the current semantic requirements. This creates a configurable middle ground between cache reuse and retrieval precision.

    7. Summary

    The architecture’s central idea can be expressed as two separations: global KV generation is decoupled from global KV consumption, and KV content is decoupled from position selection. The first is achieved through the division of responsibilities among Full, Reuse, and Reindex layers; the second through a dedicated sparse Indexer.

    For engineers working on efficient attention, this shared-state design—combining cached content with position selection—offers a way to balance inference cost and model expressiveness in long-context architectures. It can also serve as a useful reference when evaluating designs for custom models.

    In practice, training and deploying models at this scale may involve general infrastructure such as cloud GPU platforms, high-performance object storage, and monitoring and alerting systems to support the training and inference pipeline.