LLM Inference Acceleration: Memory Writes and State Compression in Practice

    This article explains a general approach to speeding up large language model inference by separating memory writes from prediction and compressing recurrent states. It covers a layered execution strategy that runs shallow layers during prefill, a low-rank plus principal-direction state representation, measured throughput and memory results, evaluation methodology, and practical deployment advice—without retraining the original model.

    1. Background: The Compute Mismatch Between Prefill and Decode

    In LLM inference serving, a request typically goes through two phases: prefill and decode. Prefill processes all input positions at once; it is compute-intensive and highly parallel. Decode generates tokens one by one and is constrained by memory bandwidth and the volume of state reads and writes.

    A common pain point in practice is that, to “write” the context into the model’s internal state, the server has to run the full network depth for every input position—even though only the generation phase actually needs high-quality token-by-token prediction.

    This raises a fair question: does preparing memory for future generation really require the same depth of computation as “predicting the next token”? Usually, it does not. This article discusses a general optimization idea (referred to below as the “layered memory write and state compression approach”) built around this mismatch.

    2. Core Idea 1: Separate “Writing Memory” from “Making Predictions”

    2.1 Layered Execution Strategy

    Take a 48-layer hybrid large model as an example. When processing the input, all positions except the last one can run only the first 31 layers. A lightweight memory-writing component then generates the key-value cache and recurrent state needed for the remaining 17 layers. During generation, every token still goes through the full 48 layers.

    The scheduling logic is illustrated below (pseudocode, expressing the scheduling idea only):

    1. # Prefill: mostly shallow layers + lightweight write
    2. h = embed(input_tokens)
    3. h = backbone.forward_layers(h, upto=31) # most positions run shallow layers only
    4. kv_cache, recurrent_state = memory_writer(h) # lightweight component fills in deep memory
    5. # The last position still runs full depth to preserve first-token prediction quality
    6. last = backbone.forward_full(input_tokens[-1:])
    7. # Decode: full-depth token-by-token generation
    8. for step in range(max_new_tokens):
    9. last = backbone.forward_full(last, kv_cache, recurrent_state)
    10. yield sample(last)

    2.2 Why It Works

    The key is that the two tasks differ in their need for deep computation: predicting the next token requires full semantic integration, while “writing memory” for subsequent generation can get by with significantly less deep computation. This approach measured that difference on five trained hybrid large models and used the measurements to cut prefill compute. Three engineering points to note: first, the shallow truncation position should be determined through offline calibration; second, the memory-writing component must stay lightweight so it does not offset the gains; third, first-token quality and long-range consistency need regression evaluation as a safeguard.

    3. Core Idea 2: Low-Rank and Principal-Direction Compression of Recurrent States

    3.1 Observation

    The recurrent state read and written at each decode step often has a dominant component close to a fixed direction, with the remainder forming a low-rank structure. This means the state is not “full-rank and isotropic” but highly compressible.

    3.2 Approach

    Store the state in a decomposed form of “principal direction + low-rank residual,” and let the model read and update that form directly, avoiding a return to a dense large matrix at every step. For a 48-layer hybrid model, a single request’s recurrent state drops from 113.2MB to 29.2MB right after compression, averaging about 43.4MB over the full compression cycle. Lower memory usage directly increases the number of concurrent requests and per-GPU throughput.

    3.3 Implementation Notes

    • Maintain the principal direction with a moving average or a few steps of power iteration to avoid high-cost online decomposition;
    • Choose a residual rank between 2 and 8 to trade off quality and memory, adjusting dynamically with load;
    • During updates, perform addition and orthogonalization only within the low-rank subspace to keep numerical stability;
    • Combine with paged cache management to reduce fragmentation and copying.

    4. Results and Evaluation Methodology

    On a single accelerator card, a mainstream inference framework, and long-context workloads such as “AI coding tool” session traces, this approach can raise the target model’s peak per-GPU throughput by 16%–42%; at comparison points with the same P90 user end-to-end output rate, throughput improves by 23%–42%. On the quality side, accuracy was evaluated across five models and six checkpoints, with the original model weights frozen throughout and the added components trained via distillation, keeping the accuracy cost within an acceptable range.

    Recommended evaluation criteria:

    • Throughput: fix the input/output length distribution and report peak per-GPU throughput and equal-comparison points such as P90 rate;
    • Memory: report both before and after compression and the cycle average;
    • Quality: cover three task types—short Q&A, long-document summarization, and code completion—with comparisons under frozen weights;
    • Stability: observe state drift and first-token quality regression over long sessions.

    5. Deployment Advice

    To evaluate a similar approach in your own inference service, start with an offline script that plots the “shallow truncation position vs. quality loss” curve, then gradually enable state compression. For monitoring, integrate logging and alerting services and build dashboards for throughput, memory, and P90 rate. A cloud implementation can rely on container platforms and object storage for elastic scaling and model artifact management, without depending on any proprietary interfaces. Overall, decoupling “writing memory” from “making predictions” and compressing recurrent states is a general path to improving inference economics without training the original model.