Long Context in LLMs: The Evolution of Attention Mechanisms and Million-Token Practices

    This article explains why million-token context is a system-level engineering problem rather than a simple configuration change. It compares multi-query attention, grouped-query attention, low-rank KV compression, and residual information pathways, then shows how to estimate KV cache size and attention compute costs. It also covers practical techniques such as cache tiering, prefix reuse, chunked prefilling, hybrid sparse attention, scheduling isolation, and observability, along with an evaluation checklist for real-world long-context deployments.

    1. The Starting Point: Long Context Is Not Just a Bigger Window

    Over the past year, the open-source LLM ecosystem has iterated quickly. One notable trend is that the industry once assumed mainstream model architectures had converged, with only local fine-tuning left to do. Yet newer models have returned to the older multi-query attention route, showing that there is still meaningful room for architectural improvement. This back-and-forth reminds developers that context length is not a configuration item. It is a system-level outcome determined jointly by GPU memory, bandwidth, operator implementation, and training strategy.

    Pushing the context window from a few thousand tokens to one million puts pressure on three areas:

    1. The KV cache grows linearly with sequence length, which directly limits concurrency.
    2. Attention computation grows quadratically with sequence length, which determines single-request latency.
    3. Memory bandwidth becomes the bottleneck, while compute utilization actually drops.

    Because of this, the claim “supports million-token context” must be broken down into verifiable engineering metrics. Otherwise, real-world usability cannot be assessed.

    2. Trade-offs Among Three Attention Approaches

    1. Multi-Query Attention: Trading Sharing for Bandwidth

    All query heads share the same set of keys and values. This decouples KV cache size from the number of heads, significantly reducing memory usage and memory access. The cost is limited representational capacity, which must be compensated through training strategy.

    2. Grouped-Query Attention: A Middle Ground

    Grouping sits between full sharing and full independence, balancing quality and overhead. It has been a relatively safe default choice for quite some time.

    3. Low-Rank KV Compression: Projecting KV into a Lower-Dimensional Space

    Keys and values are projected into a low-dimensional latent representation and reconstructed during inference. This saves cache space but introduces extra projection operators, raising the requirements for operator implementation and numerical stability.

    4. Residual Information Pathways: Reusing Historical Representations Across Layers

    Historical representations flow across layers in a residual manner, reducing repeated computation and repeated storage. This is an optimization dimension orthogonal to attention compression.

    It is worth emphasizing that system-level coupled optimization is harder than isolated innovation, but also more effective. Any single change must pass four checks at the same time: operators, parallel partitioning, cache management, and training objectives.

    3. Quantitative Estimation: Do the Math Before Choosing a Design

    Let L be the number of layers, H the number of KV heads, D the head dimension, S the sequence length, and b the number of bytes per value. The KV cache is approximately:

    1. KV_bytes ≈ 2 × L × H × D × S × b

    With L=64, H=8, D=128, S=1,000,000, and b=2, a single request needs roughly 256 GB of cache. If H is reduced to 1, it drops to about 32 GB. This order-of-magnitude difference directly determines whether a single machine can serve the request and how much concurrency is possible.

    The main attention computation is approximately:

    1. FLOPs ≈ 4 × L × H × S² × D

    Doubling the sequence length quadruples the computation, while the cache only doubles. This explains why long-context scenarios often hit compute and latency limits first, rather than memory capacity.

    4. Engineering Techniques That Make Million-Token Context Usable

    • Cache tiering: keep hot data in GPU memory, push cold data to object storage or block storage, and use local NVMe as a second-level cache.
    • Prefix reuse: cache fixed prefixes such as system prompts, tool definitions, and long documents so that cached portions are not recomputed.
    • Chunked prefilling: split long sequences into chunks and prefill them in parallel to control peak memory usage and avoid long requests blocking the queue.
    • Hybrid sparse and sliding-window attention: keep nearby tokens dense and distant tokens sparse, combined with position-encoding extrapolation strategies.
    • Scheduling isolation: place long-context requests and short requests in separate pools to avoid head-of-line blocking.
    • Observability: use logging services and monitoring alerts to track cache hit rate, time to first token, per-token output latency, and peak memory usage.

    Illustrative service-layer pseudocode:

    1. def handle(req, cache, pool):
    2. prefix = normalize(req.system + req.tools)
    3. kv = cache.get(prefix) or prefill_chunked(prefix, pool)
    4. for chunk in split(req.body, size=8192):
    5. kv = prefill_chunked(chunk, pool, kv=kv)
    6. return decode(kv, max_new_tokens=req.max_tokens)

    5. Evaluation Checklist: Avoid “It Runs but It Is Not Usable”

    1. Retrieval tasks: locating information in long text, multi-hop question answering, and distractor exclusion.
    2. Aggregation tasks: cross-section summarization and structured extraction.
    3. Reasoning tasks: math and code, checking whether performance degrades under long context.
    4. Stability: hallucination rate, instruction-following accuracy, and format compliance rate.
    5. Cost: cache hit rate, cost per request, and P95 latency.

    6. Conclusion

    There is no final architecture, only the optimal solution under given constraints. For developers, the practical path is to first estimate memory and bandwidth requirements quantitatively, then choose an attention approach, and finally use cache reuse, chunked prefilling, and observability to turn theoretical capability into a stable service. The combination of general capabilities from Baidu AI Cloud, such as container platforms, object storage, and monitoring alerts, can serve as an infrastructure reference for deploying long-context inference.