Asymmetric Computation and KV Cache Compression for Long-Context LLM Inference

    This article explains why long-context LLM inference becomes increasingly expensive and how three techniques address the cost imbalance between prefill and decode: asymmetric computation, cross-layer reuse, and low-bit KV cache quantization. It covers the core ideas, quality trade-offs, engineering considerations, and a combined evaluation framework based on prefill compute, decode memory, long-context quality, and end-to-end latency.

    1. Background: Why Long-Context Inference Gets More Expensive

    LLM inference is typically split into two stages: prefill and decode. Prefill processes all prompt tokens at once; it is compute-intensive and highly parallel. Decode generates tokens one by one and is constrained by memory bandwidth and cache capacity. As the context grows from a few thousand tokens to tens of thousands or more, the cost structure of the two stages quickly becomes unbalanced: prefill compute grows roughly linearly with the number of tokens, while the memory footprint of the KV cache expands with both token count and layer count.

    There are three common optimization directions in the industry: assigning different compute intensity to different stages (asymmetric computation), reducing repeated computation and storage (cross-layer reuse), and lowering the bit width of each cache element (low-bit cache). Each is discussed below.

    2. Asymmetric Computation: Doing Less Work in Prefill

    2.1 Core Idea

    In a conventional implementation, the same token passes through roughly the same network depth and activation scale in both prefill and decode. Asymmetric computation breaks this symmetry: the network is treated as a “first half” and a “second half,” so most prompt tokens only pass through the first half during prefill, while a small number of key tokens continue through the full network.

    The benefit is quantifiable. Assume the backbone is a Mixture-of-Experts (MoE) model that activates about 16B parameters per token during decode. If most prompt tokens only traverse the first half during prefill, their activation drops to about 8B, cutting overall prefill compute close to half.

    2.2 Why Halving Does Not Noticeably Hurt Quality

    The key is the global visibility of the attention mechanism. The first half of the network has already encoded the entire context, so the second half only needs to refine a small set of “selected” tokens. As long as the selection strategy covers the information that truly affects generation (instructions, constraints, recent dialogue), the quality loss stays controlled.

    2.3 Engineering Considerations

    • Make the stage split configurable: the boundary between the first and second half should be a tunable hyperparameter, so quality-cost trade-offs can be made per task.
    • Keep the selection strategy stable: if Top-K selection fluctuates too much, generation becomes unstable; deterministic ordering and a minimum retained set are recommended.
    • Add observability: log the actual prefill activation volume and the selection hit rate to avoid “nominal halving with no real reduction.”

    Illustrative pseudocode:

    1. def prefill(tokens, net, split_layer, keep_ratio=0.1):
    2. # First half: all tokens go through
    3. h = net.forward_layers(tokens, 0, split_layer)
    4. # Select a few key tokens for the second half
    5. idx = select_topk(h, ratio=keep_ratio)
    6. h[idx] = net.forward_layers(h[idx], split_layer, net.num_layers)
    7. return h, idx

    3. Cross-Layer Reuse: Doing Repeated Work Only Once

    3.1 Where the Repeated Overhead Comes From

    In deep networks, adjacent layers often need similar context indexing information. If every layer independently computes and caches the main state, index keys, and Top-K results, it produces substantial duplicated storage and duplicated index computation.

    3.2 The Three Reuse Targets

    • Reuse the main state across layers: the main KV is shared between layers, reducing duplicated caching.
    • Reuse index keys: only one index structure needs to be maintained, avoiding per-layer rebuilding.
    • Reuse selection results: Top-K selection results are passed between layers, eliminating repeated sorting.

    3.3 Benefits and Risks

    The benefit is lower memory and bandwidth usage at the same time. The risk is that inter-layer differences get flattened, which may weaken the expressive power of deeper layers. In practice, reuse only among adjacent layers with high similarity, and keep a few layers computing independently as compensation.

    4. Low-Bit Cache: From 3514 to 890

    Cache compression is the most direct memory optimization. With FP4 quantization of the main cache, for example, the per-token global cache footprint drops from about 3514 bytes to about 890 bytes, roughly a quarter of the original.

    Points to note:

    • Quantization error accumulates; keep higher precision for critical layers.
    • Dequantization adds extra computation, so the balance between bandwidth and compute must be evaluated.
    • The compression ratio should be evaluated together with context length; the gains are more obvious for long contexts.

    5. Combined Effect and Evaluation

    The three techniques do not simply stack; they amplify each other: asymmetric computation reduces prefill compute, cross-layer reuse reduces cache and index overhead, and low-bit caching further compresses memory. For evaluation, four metrics are recommended: prefill compute per token, decode memory footprint, long-context quality degradation, and end-to-end latency.

    A simplified evaluation framework:

    1. def evaluate(cfg, prompts):
    2. cost_prefill = estimate_flops(cfg, prompts) # prefill compute
    3. mem = estimate_kv_bytes(cfg, max_len) # peak memory
    4. quality = run_eval_set(cfg, prompts) # quality regression
    5. latency = measure_latency(cfg, prompts) # end-to-end latency
    6. return dict(prefill=cost_prefill, mem=mem, quality=quality, latency=latency)

    6. Deployment in the Cloud: Combining General Capabilities

    To deploy this kind of optimization in a cloud environment, general capabilities can be combined: a container platform to host inference services and elastic scaling, object storage to hold models and evaluation sets, and logging plus monitoring and alerting to observe activation volume, cache hit rate, and latency jitter. Baidu AI Cloud can provide support in these general categories, making it easier for teams to fold the optimizations above into a sustainable engineering iteration loop.

    7. Conclusion

    Asymmetric computation, cross-layer reuse, and low-bit caching are essentially about trading cost for “structured trade-offs”: letting most tokens compute less, storing repeated information only once, and making each cache element take less space. Combined, they bring prefill compute close to half and cache footprint down to about a quarter, offering a repeatable cost-reduction path for long-context inference. In practice, always drive tuning with observable metrics and avoid “paper optimizations.”