Inference Acceleration with Asymmetric Encoder-Decoder Design and KV Cache Compression
This article explains how to speed up large language model inference by treating the prefill and decode phases as two distinct workloads. It covers three complementary techniques: an asymmetric causal encoder-decoder structure that cuts prefill compute, cross-layer sharing of sparse attention indexes and cache to reduce memory traffic, and low-bit KV cache quantization to shrink memory footprint. It also includes an honest assessment of tradeoffs and a practical validation checklist for production rollout.
1. The Root Problem: Prefill and Decode Are Two Different Workloads
Large model inference is split into two phases, and this split is the starting point for all optimization.
- Prefill phase: processes the entire prompt in one pass, with all tokens running forward in parallel. It is compute-bound, achieves high compute utilization, but the number of activated parameters scales with sequence length.
- Decode phase: generates tokens autoregressively, one token per step. It is memory-bound, and the bottleneck is the bandwidth for moving weights and the KV cache, not compute.
A typical online-serving cost structure looks like this: prompts keep getting longer (RAG injection, code repository context, multi-turn history), so the prefill share keeps rising; meanwhile the time-to-first-token (TTFT) that users perceive is determined almost entirely by prefill. As a result, decode-only optimizations yield limited gains in long-context scenarios, and the two phases must be treated differently at the level of the computation graph.
As a reference point drawn from commonly cited figures in public technical reports: for a sparse Mixture-of-Experts (MoE) model with a 552B-parameter backbone, the decode phase activates about 16B parameters per token, while a structural redesign can compress the effective prefill activation to 8B, nearly halving prefill compute. The following sections break down how this is achieved.
2. Technique One: Asymmetric Causal Encoder-Decoder Structure
2.1 Core Idea
A traditional decoder-only structure treats all tokens equally: every position passes through all layers. But causal attention has a long-underestimated property—earlier tokens do not need to see later content, and their representations are repeatedly “reused” in subsequent layers without updating their information source.
This makes it possible to split the network into two segments, forming an asymmetric causal encoder-decoder structure:
- Front segment (encoder side): builds representations for the vast majority of prompt tokens. It has more layers and greater width, but runs only once.
- Back segment (decoder side): has fewer layers and narrower parameters, and handles interaction with already-generated tokens and the final output.
The key constraint is asymmetric, not acausal: the front segment still strictly maintains a causal mask internally, and the back segment reads the front segment’s output through a cross-segment connection. This introduces no future-information leakage, and the numerical path stays consistent between training and inference.
2.2 Benefits and Costs
The benefits are twofold: on the prefill side, most tokens pass only through the front segment, halving effective activation; on the decode side, the narrower back segment reduces per-step memory access.
The costs must be assessed honestly:
- Training requires an additional cross-segment alignment objective, otherwise the back segment struggles to converge;
- The front segment becomes a non-splittable serial dependency, fixing the pipeline-parallel (PP) split points and reducing flexibility;
- For very short prompts, the front segment’s fixed overhead dominates and the gains are diluted.
2.3 Illustrative Implementation
# Pseudocode: segmented forward pass with the causal mask unchangeddef forward(prompt_ids, gen_len, cfg):# Front segment: processes the prompt only, causal mask, output is cross-segment memoryenc_hidden, enc_kv = encoder_backbone(prompt_ids, causal=True)# Back segment: narrow network, generates token by token, reads front-segment memorykv = enc_kvfor _ in range(gen_len):h = decoder_backbone.step(last_token, memory=enc_hidden, kv=kv)kv = kv.append(h)last_token = sample(h)return tokens
3. Technique Two: Cross-Layer Shared Sparse Attention and Cache Reuse
3.1 Division of Labor Between Sparse Retrieval and the Main Cache
Long-context attention commonly uses a two-tier “retrieval + main cache” structure:
- Main KV: keeps the full key-values for all layers, serving as the accuracy fallback;
- Indexer K: low-dimensional/low-precision index keys used for scoring;
- Top-K selection result: the few important position indexes picked out per layer.
In a naive implementation, these three things are computed and stored separately for each layer, with high redundancy.
3.2 Three Reductions from Cross-Layer Reuse
Sharing the index and selection results across several adjacent layers can:
- Reduce duplicate caching: index keys no longer need to be stored once per layer;
- Reduce index computation: the compute cost of scoring and sorting is amortized over the reuse group;
- Improve memory locality: layers in the same group read the same index, raising the cache hit rate.
# Pseudocode: share the index and Top-K within a reuse groupfor group in layer_groups: # e.g., every 4 layers form a groupidx_k = indexer(group[0], hidden) # computed once per grouptopk = topk_select(idx_k, k=group_k) # selection result shared within the groupfor layer in group:out = layer.attend(main_kv, topk) # each layer reuses the same topk
Note: too large a reuse group will hurt accuracy, so a sensitivity sweep over group size on a validation set is usually needed rather than setting it by intuition.
4. Technique Three: Low-Bit KV Cache Quantization
The KV cache is the single largest consumer of memory in long context. Dropping the main cache from high-precision floating point to the 4-bit range is the steepest single cut in compression ratio.
Example figures: the global KV cache drops from 3514 bytes/token to 890 bytes/token, roughly one quarter of the original.
Translated into perceivable capacity:
Single sequence, 128K context:3514 B/token × 131072 ≈ 460 MB890 B/token × 131072 ≈ 117 MBSingle 80GB card, reserving 20GB for weights and activations:60GB available → concurrent sequences rise from ~130 to ~510
Engineering considerations:
- Grouped quantization: maintain scale/zero-point per head or per channel group to avoid a global dynamic range being wrecked by outliers;
- Outlier channel protection: keep higher precision for the few high-magnitude channels;
- K-only vs. V-only quantization comparison: the two have different sensitivity to accuracy and must be measured separately;
- Fuse dequantization into the attention kernel to avoid extra memory round-trips eating up the bandwidth gains.
5. Validation Checklist for Production
- Per-phase timing: instrument prefill and decode separately to confirm the gains land in the expected phase;
- Dual metrics TTFT and TPOT: reporting throughput alone hides first-token latency regressions;
- Long-text accuracy regression: run needle-in-a-haystack, multi-hop QA, and long-document summarization once each;
- Measured peak memory: align theoretical estimates with
torch.cuda.max_memory_allocated; - In-batch length variance: when the length distribution is highly uneven, bucketing and structural optimizations can interfere with each other.
6. Summary
The three techniques address three different bottlenecks: the asymmetric encoder-decoder structure cuts prefill compute, cross-layer cache reuse cuts memory traffic and index overhead, and low-bit KV quantization cuts memory capacity. They are orthogonal and stackable, but each brings its own training complexity, parallelism flexibility, and accuracy risk. In production, a container platform can support canary rollout, and logging and monitoring/alerting systems can provide per-phase metric observation—letting data, not intuition, decide the reuse group size and quantization bit width.
