Memory Writing Instead of Full-Depth Computation: Optimizing Hybrid Model Inference
This article explores an inference optimization approach for hybrid Large Language Models (LLMs) that addresses the high cost of the prefill phase during long-input processing. By recognizing that the computational depth required to write to memory differs from predicting the next token, the proposed method introduces a layer-wise prefill and lightweight memory writing scheme. This approach keeps original model weights frozen while training a small writing component via distillation, significantly reducing deep forward computations without compromising generation quality.
1. Background: Long Inputs Concentrate Inference Costs in the Prefill Phase
Inference in Large Language Models (LLMs) typically occurs in two phases. The first is the prefill phase, where the model reads all user-provided inputs at once to compute the key-value cache (KV Cache) or recurrent states for each layer, preparing them for subsequent generation. The second is the decode phase, where the model generates output tokens one by one, reading the accumulated cache for each new token generated.
Longer inputs mean higher prefill latency. In scenarios such as coding assistants, document question-answering, and multi-turn conversations, inputs often reach tens of thousands of tokens or more, making prefill the dominant contributor to the first-token latency. Additionally, the cache consumes significant GPU memory; as concurrent requests increase, the memory pressure rises, which subsequently limits decoding throughput.
Recently, a class of hybrid architecture models has gained attention. In these models, some layers utilize attention mechanisms, while others employ recurrent states or linear attention structures. Although the size of recurrent states does not grow with sequence length, a separate copy must be saved for each request, meaning it still consumes GPU memory.
2. Core Observation: Writing to Memory Requires Less Computational Depth Than Predicting the Next Token
Deep computation is primarily used to generate the high-level semantic representations necessary for predicting the next token. What the generation phase actually reads is the key-value or state stored in the cache. This content only needs to retain information readable for subsequent steps; it does not necessarily have to be computed layer-by-layer through the entire deep network.
Shallow representations already contain a wealth of lexical and local grammatical information, while deep representations perform more abstract integration based on these. If a shallower representation, combined with a mapping of very few parameters, can generate the cache required by subsequent layers, most of the deep forward computation can be bypassed. There are two prerequisites for this approach to work: the mapping must be sufficiently accurate, and the error must not be amplified during subsequent generation.
The original research measured the gap between the “computational depth required to write to memory” and the “computational depth required to predict the next token” across five pre-trained hybrid models, and designed a prefill acceleration scheme based on these measurements. Specific measurement values should be referenced in the original research report.
3. Solution Breakdown: Layer-Wise Prefill and Lightweight Memory Writing
This scheme divides the model’s layers into two segments. Assuming the model has a total of N layers, the first k layers are called compute layers, and the remaining N-k layers are called write layers. The prefill process is as follows:
- Except for the last token, all other tokens only pass through the compute layers.
- A lightweight writing component reads the hidden states output by the compute layers and generates the KV cache and recurrent states required by the write layers.
- The last token passes through all N layers completely to output the prediction distribution for the next token.
- After entering the decode phase, each new token is still computed through all N layers, keeping the main path for generation quality unchanged.
The last token must undergo full computation because its output directly determines the first generated token, making it the most sensitive to accuracy. The deep cache for the remaining tokens is approximated by the writing component.
The pseudo-code below illustrates the process and does not represent a real framework interface:
# Pseudo-code: illustrating the execution flow of layer-wise prefilldef prefill(tokens, compute_layers, write_layers, memory_writer):h = embed(tokens)for layer in compute_layers: # Compute layers: all tokens participateh = layer(h)# The cache required by write layers is generated by a lightweight component, except for the last tokendeep_cache = memory_writer(h[:-1])# The last token goes through the full network to get the next token predictionlast = h[-1:]for layer in write_layers:last = layer(last, cache=deep_cache)return last, deep_cache
Training Method. The original model weights remain frozen throughout the training process, and the newly added writing component is trained via distillation. The teacher signal comes from the deep layer outputs of the original model, and the student component learns to map equivalent caches from shallow states. Freezing the original weights offers two main benefits:
