Decoding Mixed Attention Architectures: Hierarchical Design for Million-Token Contexts
This article explores hierarchical attention architectures designed to handle million-token contexts in large language models. It examines the lifecycle costs of KV states, the role of KV sharing mechanisms, and three complementary attention structures—Compressed Sparse Attention (CSA), Heavy Compression Attention (HCA), and Sliding Window Attention (SWA). Additionally, it covers practical engineering considerations such as low-precision indexers, cache management, and training-inference consistency, providing a comprehensive guide to scaling context windows efficiently.
1. Why Million-Token Contexts Require Attention Redesign
When context length scales from tens of thousands to the million-token magnitude, the true challenge is no longer the single attention computation itself, but the sustained overhead of historical states throughout their lifecycle. For every new token generated, the model must:
- Store: The KV states of all historical tokens must be persisted.
- Read: Attention computation requires high-frequency read-back of these states.
- Retrieve: Sparsification schemes must first locate relevant fragments, and retrieval itself incurs costs.
- Cache and Reuse: The inference system’s cache hit rate and eviction policies directly determine latency.
- Recover: In ultra-long sessions, compressed states may need to be decompressed and rebuilt.
The costs of these stages grow linearly or even super-linearly with length $L$. Optimizing only one stage (e.g., implementing sparse retrieval) will still leave other stages as bottlenecks. Therefore, advanced architectures choose to process historical states hierarchically based on context distance and information resolution: preserving details for nearby context, using retrieval for mid-range context, and summarizing for distant context, thereby reallocating resources holistically.
2. KV Sharing Mechanism: The Starting Point for All Savings
Before discussing hierarchy, a fundamental question must be addressed: whether KV still needs to be stored repeatedly per head. Three common industry approaches are compared below:
| Type | Q and KV Relationship | State Cost |
|---|---|---|
| MHA | Each query head has independent K/V | High expressiveness, but KV cache scales with head count |
| GQA | A group of query heads shares one K/V set | Trade-off between expressiveness and cache size |
| MQA | All query heads share the same K/V set | Smallest cache, expressiveness relies on output projection compensation |
Low-rank compression schemes (e.g., compressing KV into a low-rank latent representation and expanding it to each head during use) still require caching a low-rank state per token. In a million-token context scenario, a more aggressive approach is taken: retain only a single shared KV on the main path, where perspective differences across heads are primarily compensated by query heads and output projections. This step eliminates the per-token, per-head constant factor, freeing up budget space for subsequent distance-based hierarchical design.
3. Three Core Structures of Mixed Attention
A typical implementation of hierarchical design consists of three complementary components:
3.1 Compressed Sparse Attention (CSA)
The concept involves two-level reduction: the first level compresses historical KV into fewer compressed entries (similar to creating aggregated representations of continuous fragments); the second level uses a lightweight indexer to select blocks relevant to the current query from the compressed entries, applying fine-grained attention only to the selected blocks.
The key point is that compression and retrieval are native training behaviors, not inference-time post-processing. The indexer is jointly trained with the main attention, and retrieval errors can be corrected via gradients, thereby avoiding the capability mismatch caused by “full training but sparse inference.”
3.2 Heavy Compression Attention (HCA)
CSA retrieves relevant mid-range fragments, but for extremely distant history (hundreds of thousands of tokens ago), block-by-block retrieval is less meaningful—such content serves more as “background knowledge” than “precise citations.” HCA heavily compresses ultra-long history at a resolution lower than the token level, forming a low-cost global memory. It sacrifices detail for coverage, ensuring the model “remembers the event,” while specific details are retrieved on demand by CSA.
3.3 Sliding Window Attention (SWA)
No matter how refined the compression scheme is, high-resolution information for recent tokens (anaphora resolution, code indentation, poetic meter and rhyme) cannot tolerate lossy compression. SWA retains uncompressed token-level KV within a recent window, serving as a fallback for the insufficient resolution of CSA/HCA.
3.4 Division of Labor
Distance from the queryNear ---------------------------------------------- Far| SWA: Token-level, uncompressed, sliding window || CSA: Compressed entries + indexed retrieval || HCA: Ultra-low resolution, global summary memory |
Furthermore, these structures can be interleaved at the layer level: shallow layers focus on local context (primarily SWA), while deeper layers handle long-range aggregation (with higher HCA weights), further matching the degree of information abstraction at different depths.
4. Low-Precision Bypass for the Indexer
In sparse architectures, although the indexer only performs coarse screening, its own cost of reading all compressed entries under a million-token context cannot be ignored. A natural optimization is to decouple the indexer from the main attention: the index path runs at lower numerical precision or even discrete representations, and only the selected blocks enter the high-precision main path. This ensures that retrieval overhead no longer scales proportionally with the main computation, forming a dual-path structure of “low-precision coarse screening + high-precision fine calculation.”
5. Engineering Considerations
For architectural design to be practically deployed, the inference system must adapt synchronously:
- Cache Partition Management: The SWA window acts as a ring buffer, CSA entries can be statically laid out, and HCA summaries are nearly permanent; these three are suited for different storage media and eviction strategies.
- Bandwidth Over Compute: Under long contexts, attention is often memory-bound. Every increment in compression ratio allows the servable context length to step up a tier.
- Training-Inference Consistency: Sparse branches participate in the forward pass during training to avoid distribution drift during deployment.
- Scalability Validation: Evaluating long-range capabilities requires covering needle-in-a-haystack retrieval tasks and summarization tasks to verify whether CSA and HCA are each performing their designated roles.
6. Conclusion
The mixed attention design for million-token contexts can be summarized by a clear principle: allocate resolution based on information value—preserve details for recent context, rely on retrieval for relevant context, keep summaries for ancient context, and share states for commonalities. KV sharing cuts constant factors, while compression and sparsification transform length-dependent growth into a controllable hierarchical structure, and native training ensures that every compression step does not deviate from the model’s actual capabilities. For enterprise deployment, this type of architecture means a significant increase in the context length that can be supported under the same hardware budget, opening up practical engineering spaces for scenarios like long-document analysis and large-scale code understanding.
