Token-Level Computation Path Analysis of a Hierarchical Sparse Attention Architecture

    This article breaks down how a hierarchical sparse attention architecture processes tokens layer by layer, covering its core design of decoupling global KV content caching from Top-K position selection. It explains the four layer roles (S, F, R, I), cross-layer KV reuse, the hierarchical sparse indexer, CSA2 compression ratios, and provides pseudo-code plus engineering metrics for evaluating this long-context design.

    Token-Level Computation Path Analysis of a Hierarchical Sparse Attention Architecture

    1. Design Motivation: Separating “Historical Content” from “Historical Position Selection”

    Common optimization routes for long-context attention include local window attention, sparse indexing, KV compression, and cross-layer KV reuse. One often-overlooked problem is that repeating the full historical KV storage and Top-K index computation at every layer introduces substantial redundant storage and indexing overhead. The core innovation of the architecture under discussion is to split the historical information that would otherwise be regenerated at every layer into two independently updatable shared states:

    1. Historical content cache (global KV): stores historical token representations that later layers can reuse.
    2. Historical position selection (Top-K indices): records which global historical positions the current query should attend to.

    Once these two are decoupled, some layers can reuse the global KV but recompute Top-K selection, some layers can reuse both the global KV and the Top-K indices, and others only run local SWA. This preserves fine-grained local modeling while reducing repeated generation of global historical states and repeated index computation. According to the publicly available breakdown, in a 40-layer configuration only 4 layers actually generate new global KV, only 8 layers perform new index selection (4 Full layers plus 4 Reindex layers), and 30 layers directly reuse previous index results; yet all 40 layers keep their own SWA KV and main Query. In other words, local window states remain layer-independent, while global historical content and global position selection become cross-layer shared resources.

    2. Token-Level Computation Path Overview

    Assume batch size B=1 and sequence length S=1024, with input representation:

    1. X ∈ R^{[1,1024,5120]}

    The residual stream can be represented as a multi-channel mixed state:

    1. X_mHC ∈ R^{[1,1024,4,5120]}

    After mixing, the dimensionality remains the same as the input. Before entering attention, each token passes through Query generation, layer-type routing, SWA computation, global sparse attention computation, and fusion. Key base dimensions include: main model 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; Sparse Attention Top-K 512; SWA Window 128; CSA2 encoding compression ratio 2 and decoding compression ratio 1. Together these dimensions determine the scale of Query projection, indexer computation, and global KV reads.

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

    1. Q_latent = X W_q^A
    2. [1,1024,5120] -> [1,1024,1280]
    3. Q = Q_latent W_q^B

    The first step compresses the high-dimensional hidden state into a 1280-dimensional latent space; the second projects into 64 main attention heads. The indexer side uses 32 Indexer Query Heads with a head dimension of 128 to produce Top-K selection signals in the hierarchical sparse indexer. This division of labor lets the main Query handle content aggregation while the index Query handles position filtering.

    3. Layer Role State Machine: S, F, R, I

    The public breakdown describes layer behavior with four roles:

    • S: pure SWA layer. Only performs local attention with a window of 128 and produces no global KV. It guarantees every layer retains fine-grained local modeling capability.
    • F: Full layer. Generates new global KV and computes new Top-K indices. It is the “producer” of global historical state.
    • R: Reuse layer. Reuses existing global KV and existing Top-K indices, regenerating neither.
    • I: Reindex layer. Reuses existing global KV but recomputes the Top-K indices. It generates no new global KV; it only re-decides which global positions the current layer should read.

    In the public example of the first 20 encoder layers, layers 2, 8, and 14 each generate an independent copy of global KV. These correspond to hidden states at different depths, so they preserve historical information at different levels. The decoder mainly uses the Reindex mechanism: it recomputes which global KV configurations are needed rather than regenerating global KV. In this way, the “content” of global KV is supplied by a few Full layers, subsequent layers adjust the “read positions” via Reindex, and Reuse directly inherits index results.

    4. How Cross-Layer KV Reuse Coexists with SWA

    The key to cross-layer KV reuse is not “all layers share a single KV copy,” but rather “a few layers generate multiple copies of global KV at different depths, and other layers reuse them via routing.” This brings two benefits: first, it avoids regenerating global historical representations at every layer, reducing global KV cache write and storage pressure; second, by keeping Full layers at different depths, it retains multi-granularity historical information, mitigating the mismatch between a single shared KV and the representation spaces of different layers.

    At the same time, all 40 layers keep their own SWA KV and main Query. Local window KV is not shared across layers, because local neighborhoods are highly sensitive to each layer’s current representation; the main Query is also generated per layer so that each layer’s query fits its own hidden state. The architecture is therefore a hybrid model: local states are layer-independent, global content is shared across layers, and global indices can be either shared or recomputed.

    5. Hierarchical Sparse Indexer and Top-K Selection

    Top-K is 512, meaning the global sparse attention selects 512 historical positions each time. Scoring all historical positions finely would make indexing cost grow with sequence length. The Hierarchical Sparse Indexer adopts a multi-stage filtering approach: it first narrows the candidate set at a coarse granularity, then performs finer Top-K selection within the candidates. The indexer uses 32 Query Heads with a head dimension of 128 to produce selection signals, after which the main attention aggregates content over the selected global KV.

    The value of Reindex layers is this: query distributions differ across layers, and directly reusing Top-K may cause position selection bias. Recomputing the index lets the current layer choose more suitable global historical positions without increasing global KV generation. Reuse layers further save index computation and suit layers whose query distributions align well with the existing index. Full layers generate both global KV and new indices, making them the key nodes of state update.

    6. CSA2 Compression Ratios and Encoding/Decoding Differences

    CSA2 has an encoding compression ratio of 2 and a decoding compression ratio of 1. The encoder, facing longer input, can use stronger compression to reduce global state size; the decoder keeps a compression ratio of 1, preserving higher-resolution historical representations to support sensitivity to contextual detail during generation. The encoder generates multiple independent global KV copies via Full layers such as layers 2, 8, and 14, while the decoder mostly reuses existing global KV through Reindex and reselects indices. This asymmetry reflects the trade-off of “compression-heavy encoding, selection-heavy decoding.”

    CED, related to compression and redundancy removal, has limited detail in public materials; this article does not speculate on its interface or parameters. From the computation-path perspective, it affects state representation and index quality; its exact definition should follow the formal technical report.

    7. Illustrative Pseudo-code

    The following is illustrative layer execution logic, not an actual interface:

    1. for layer in range(40):
    2. h = hidden_states[layer]
    3. q = project_query(h) # low-rank compression, then project to 64 main heads
    4. if layer in FULL_LAYERS:
    5. gkv = generate_global_kv(h)
    6. topk = hierarchical_indexer(q, gkv, k=512)
    7. mode = "F"
    8. elif layer in REINDEX_LAYERS:
    9. gkv = shared_gkv[route(layer)]
    10. topk = hierarchical_indexer(q, gkv, k=512)
    11. mode = "I"
    12. elif layer in REUSE_LAYERS:
    13. gkv = shared_gkv[route(layer)]
    14. topk = shared_topk[route(layer)]
    15. mode = "R"
    16. else:
    17. mode = "S"
    18. swa_out = swa_attention(q, swa_kv[layer], window=128)
    19. if mode != "S":
    20. global_out = sparse_attention(q, gkv, topk)
    21. out = fuse(swa_out, global_out)
    22. else:
    23. out = swa_out
    24. hidden_states[layer + 1] = out

    8. Engineering Analysis Metrics and Practical Takeaways

    When analyzing this type of architecture, it is advisable to track per-layer Full/Reindex/Reuse/SWA ratios, the number of global KV generations, Top-K recomputation counts, SWA KV footprint, global KV read/write bandwidth, and index hit rate. Long-context evaluation should focus on recall at different positions, cross-layer index drift, shared KV capacity bottlenecks, and training stability. On the engineering side, monitor read/write hotspots on the shared global KV, and pipeline overlap between Reindex-layer index computation and main attention aggregation.

    Overall, this design achieves a structured trade-off in long-context sparse attention by “decoupling content caching from position selection,” but its benefits depend on sequence length, layer configuration, batch size, and index quality, and require end-to-end evaluation against specific workloads.