Hierarchical Sparse Attention: Computation Paths and Cross-Layer Cache Reuse

    This article explains a long-context model architecture that combines hierarchical sparse attention with cross-layer cache sharing. It separates historical content caching from historical position selection, using shared global KV caches and Top-K indices that can be updated independently. The design, layer-state transitions, query-generation path, SWA and global retrieval workflow, hierarchical indexing, and key engineering trade-offs are covered.

    Hierarchical Sparse Attention: Computation Paths and Cross-Layer Cache Reuse

    This article examines a class of long-context model architectures that combine hierarchical sparse attention with cross-layer cache sharing. Unlike designs that generate a complete global key-value cache at every layer, this architecture separates two types of shared state that can be updated independently:

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

    Understanding this separation is essential for analyzing the computation path, memory footprint, and engineering trade-offs of the architecture.

    1. Design Goals: Controlling Computation and Cache Size for Long Contexts

    The main costs of long-context attention come from two sources: the scale of Query-to-history interactions and the memory and bandwidth required to store KV caches at every layer. Common approaches include sliding-window attention, global sparse retrieval, low-rank compression, and cross-layer cache reuse.

    This architecture combines these ideas in the following way:

    • Every layer retains its own sliding-window KV cache and primary Query to preserve local modeling and layer-specific representations.
    • Global KV caches are generated only at selected layers.
    • Top-K indices are recomputed only at selected layers.
    • Other layers directly reuse existing global KV caches and index results.

    The immediate benefit is reduced redundant generation of global KV caches and fewer repeated index computations. At the same time, multiple global KV caches from different depths preserve historical information at different representation levels.

    The trade-off is increased complexity in cache-version management, index consistency, and cross-layer scheduling. Ablation studies and runtime monitoring are therefore needed to verify the actual accuracy and latency benefits.

    2. Layer Types: S, F, R, and I as a State Machine

    Based on the actual path of a token through the model, layers can be divided into four types:

    • S: Sliding-window-only attention layer
      Generates and uses only its own SWA KV cache. It does not generate global KV or participate in global Top-K index selection.

    • F: Full layer
      Generates a new global KV cache for the current layer and computes new Top-K indices from the current Query.

    • R: Reuse layer
      Reuses an existing global KV cache and existing Top-K indices. It neither generates a new global KV cache nor recomputes the indices.

    • I: Reindex layer
      Reuses an existing global KV cache but recomputes the Top-K indices required by the current layer.

    In an example encoder configuration, L2, L8, and L14 each generate an independent global KV cache. Because these caches are derived from hidden states at different depths, they preserve different levels of historical information rather than simply duplicating the same cache.

    The decoder relies more heavily on reindexing: it reuses existing global KV caches while recalculating which entries should be selected. Across the full model, only 4 of 40 layers generate new global KV caches, while 8 layers perform new index selection. These consist of 4 Full layers and 4 Reindex layers. The remaining 30 layers directly reuse previously computed indices. However, all 40 layers retain their own SWA KV cache and primary Query.

    The scheduling logic can be represented with the following pseudocode:

    1. for layer in layers:
    2. q = project_query(x)
    3. kv_swa = build_swa_kv(x, window=128)
    4. if layer.type == "S":
    5. out = swa_attend(q, kv_swa)
    6. elif layer.type == "F":
    7. kv_global = build_global_kv(x)
    8. index = topk_index(q, kv_global, K=512)
    9. out = attend(q, kv_swa, kv_global, index)
    10. elif layer.type == "R":
    11. out = attend(q, kv_swa, global_cache[depth], cached_index)
    12. elif layer.type == "I":
    13. index = topk_index(q, global_cache[depth], K=512)
    14. out = attend(q, kv_swa, global_cache[depth], index)

    The key point is not the exact interface design, but the state transition:

    • S layers maintain only local-window state.
    • F layers produce both global content and global selection.
    • R layers reuse both content and selection.
    • I layers reuse content while refreshing selection.

    The primary Query is generated independently at every layer, allowing each layer to perform attention according to the needs of its own representation.

    3. Dimensions and the Query Generation Path

    The following values represent an example configuration:

    • Hidden dimension: 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 representation entering the attention module is:

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

    If multiple mixed residual streams are used, the representation may take the form:

    1. X_mHC ∈ R^{1×1024×4×5120}

    After mixing, the dimension returns to 5120. This keeps the input and output dimensions of the attention module stable, which facilitates cross-layer reuse and residual connections.

    Query generation typically uses low-rank compression followed by projection:

    1. Q_latent = X W_q^A
    2. Dimension change: [1,1024,5120] -> [1,1024,1280]
    3. Q = Q_latent W_q^B
    4. Reshape to [1,1024,64,512]

    The representation is first compressed to 1280 dimensions and then projected into 64 primary attention heads, each with a dimension of 512.

    The indexer Query is generated separately using 32 heads with a head dimension of 128. It is used for Top-K index selection rather than for direct primary-attention weighting.

    Separating the primary Query from the indexer Query allows content interaction and position selection to operate at different granularities:

    • The primary Query determines how local-window and global content should be combined.
    • The indexer Query determines which positions should be selected from the global cache.

    4. Coordinating SWA, Global KV, and Index Reuse

    With an SWA window of 128, each layer retains nearby local information. This reduces the risk that important short-range dependencies will be missed by global sparse selection.

    With Top-K set to 512, the model selects a larger set of potentially distant historical positions from the global KV cache. The local window and global Top-K retrieval therefore serve complementary roles:

    • The sliding window provides stable, continuous local modeling.
    • Top-K retrieval provides sparse long-range access.

    The key principle of cross-layer KV reuse is not that every layer shares one identical KV cache. Instead, shared state is divided into two categories:

    1. Historical content cache
      Generated by a small number of Full layers. Multiple global KV caches may exist at different depths, each preserving historical information from a different representation level.

    2. Historical position selection
      Represented by Top-K indices. These indices may be recomputed by Full or Reindex layers, or directly reused by Reuse layers.

    The two components can be updated independently. For example:

    • A Reindex layer refreshes the indices without regenerating global KV.
    • A Reuse layer reuses both the global KV cache and the indices.

    This reduces the number of global KV-generation operations while allowing reindexing to improve retrieval when the Query distribution changes significantly.

    In the example configuration, the encoder compression ratio is 2 and the decoder compression ratio is 1. These values generally indicate different levels of compression applied to key-value representations in different stages. Stronger compression reduces cache usage, but its effects on index recall and final accuracy must be evaluated separately.

    5. Hierarchical Sparse Indexing and the Role of CED

    A hierarchical sparse indexer divides exact Top-K selection over the full global KV cache into multiple filtering stages. A coarse-grained index or bucket assignment first narrows the candidate range, after which exact selection is performed within the candidate set.

    This prevents indexing cost from increasing linearly with the size of the global KV cache, making the approach more suitable for long-context workloads.

    Engineering evaluation should focus on:

    • Top-K recall
    • Candidate-bucket coverage
    • Index refresh frequency
    • Selection strategies for global KV caches at different depths

    The CED-related condition-enhancement path is better understood as an auxiliary route for enhancing representations or conditioning signals, rather than as a replacement for the global KV cache. It should remain decoupled from the primary attention path so that it does not alter the lifecycle of cross-layer shared state.

    During evaluation, the impact of CED on Top-K recall, sliding-window coverage, and long-context accuracy should be measured through separate ablations. This avoids incorrectly attributing its benefits to cache reuse.

    6. Engineering Trade-offs and Evaluation Recommendations

    The main benefits of this architecture come from reducing the number of layers that generate global KV caches and the number of layers that perform index computation. However, part of the complexity is shifted to cache management and scheduling.

    In production implementations, the following metrics should be monitored:

    • Global KV cache hit rate
    • Per-layer index reuse rate
    • Top-K recall
    • SWA window coverage
    • Memory consumption
    • Time to first token
    • Decoding throughput

    Ablation studies should separately evaluate the effects of:

    • The number of Full layers
    • Reindex frequency
    • SWA window size
    • Top-K value
    • Compression ratio

    Overall, this type of hierarchical sparse attention architecture balances computation, memory usage, and representational capacity for long-context modeling through the following strategy:

    • Every layer retains its own primary Query and SWA KV cache.
    • Only selected layers generate global KV caches.
    • Some layers refresh the Top-K indices.
    • Most layers reuse existing index results.

    Its central design principles are to decouple content caching from position selection, combine local-window attention with global sparse retrieval, minimize updates to shared state, and preserve historical information from multiple representation depths.