Optimizing LLM Inference: Shallow Compute in Prefill Phase for Memory Writing

    This article explores an LLM inference optimization method that reduces computational costs by computing only early layers during the prefill phase for memory writing, rather than running full-depth computations. It also introduces a low-rank decomposition technique for recurrent states in hybrid models to further cut memory usage. The article details the implementation approach, performance gains, accuracy guarantees, and practical engineering considerations for deploying these optimizations.

    1. The Starting Point: Does the Prefill Phase Really Need Full Computation?

    In LLM inference services, a request is typically split into two phases:

    • Prefill Phase: Processes all input tokens at once to generate the KV Cache for subsequent generation phases to reuse.
    • Decode Phase: Autoregressively generates output tokens one by one, reading the full context information at each step.

    In traditional implementations, every input token in the prefill phase runs through all model layers. For a 48-layer model processing 1,000 tokens, this means about 48,000 “token×layer” forward computations. For long-context scenarios—such as AI coding assistants handling thousands of lines of code—the prefill computation overhead directly increases Time-To-First-Token (TTFT) and squeezes GPU resources for other requests.

    But a critical question arises: Do input token representations really need to be computed through every layer?

    2. Key Insight: Different Depths for Memory Writing vs. Next Token Prediction

    The core of this optimization stems from an easily overlooked asymmetry:

    Writing memory for subsequent generation requires significantly less deep computation than predicting the next token itself.

    Why? Because input tokens do not need to complete the prediction task themselves—they only need to encode sufficient information into the KV Cache and recurrent state for the decode phase tokens to read. Full comprehension and prediction are tasks for the decode phase, where each token requires the full reasoning depth.

    The specific approach can be summarized as:

    1. Division of Labor: All input tokens except the last one only run through the early layers of the model (e.g., the first 31 layers out of 48).
    2. Memory Writing: A lightweight “memory writing component” takes over to generate the corresponding KV Cache and recurrent state for the remaining deep layers.
    3. Unaffected Decoding: Each token in the generation phase still runs through the full 48 layers, ensuring output quality via complete computation.

    This conclusion is not a hypothetical assumption but can be quantitatively measured on various pre-trained hybrid architecture LLMs: the computational depth required for memory writing is significantly lower than a full forward pass. This is the foundation of the scheme—first verify the difference exists, then exploit it to save compute.

    3. Second Optimization: Low-Rank Decomposition of Recurrent States

    For hybrid architecture models with recurrent states, there is another optimization opportunity on the decoding side.

    Observations indicate that the recurrent state read and written at each step often contains a nearly fixed principal component direction, while the remaining part exhibits a low-rank structure. This means the full state tensor contains massive redundancy.

    Therefore, the state can be saved in a decomposed form:

    • A stable principal component;
    • A low-rank dynamic residual component.

    During inference, the model directly reads and updates this decomposed form instead of manipulating the full dense state. In a measured example, the recurrent state for a single request dropped from 113.2MB to 29.2MB immediately after compression, averaging 43.4MB over the compression cycle—reducing memory footprint to roughly a quarter of the original. This translates directly into two benefits:

    • A single GPU can accommodate more concurrent request contexts, boosting throughput.
    • Reduced data volume for state read/write operations alleviates memory bottlenecks.

    4. Effectiveness Validation and Accuracy Trade-offs

    Any inference optimization must address two questions: How much faster is it? How much does accuracy drop?

    Regarding throughput gains, tested on high-end GPUs, mainstream inference frameworks, and real conversation traces from AI coding tools, this scheme increased peak single-GPU throughput by 16% to 42%. When using the same P90 user-end-to-end output rate as a baseline (i.e., ensuring no degradation in user experience), the throughput improvement was 23% to 42%. This “experience-preserving” comparison metric is more meaningful for engineering—it shows the gains are not achieved by sacrificing response speed.

    Regarding accuracy guarantees, the scheme was evaluated for quality on five models and six checkpoints. Note its training strategy:

    • The original model weights are frozen throughout, avoiding disruption to existing capabilities.
    • The newly added components are trained via distillation, aligning the lightweight component’s output with the full computation results.

    This “freeze base + distill addition” approach is a common best practice in current inference acceleration: the changes are additive, risks are controllable, and rollback is straightforward.

    5. Engineering Implementation Guidelines

    To practice similar approaches in your own inference services, consider the following points:

    1. 1. Depth Split Point Selection
    2. - The number of early layers is not fixed; it must be measured on the target model
    3. by evaluating the "memory writing quality vs. compute depth" curve.
    4. 2. Memory Writing Component Training
    5. - Use KV Cache/recurrent states from full computation as distillation targets.
    6. - Training data distribution should closely match real-world online requests (e.g., code, long documents).
    7. 3. State Compression Scheduling
    8. - The size is smallest right after compression and gradually increases as decoding progresses.
    9. Memory reservation should be planned based on "average value + peak margin".
    10. 4. Gradual Rollout and Evaluation
    11. - Use full computation as a control group, compare on downstream task benchmarks,
    12. and scale up only after confirming acceptable quality regression.

    6. Conclusion

    The value of this work lies not in a specific number, but in the methodology it reveals: inference optimization should first analyze task asymmetries rather than blindly stacking compute. Writing memory during prefill does not require full depth, and recurrent states do not need to be stored at full rank—identifying structural redundancy and spending compute where it matters can significantly reduce inference costs while maintaining output quality. For scenarios with long contexts and high concurrency, such as AI coding assistants and document Q&A, this approach is particularly worth emulating.