Hybrid LLM Inference Acceleration: Optimizing the Prefill and Decode Stages Separately

    This article examines why the fixed-size recurrent state in hybrid LLM architectures is a hidden memory bottleneck, and presents two optimizations: layered truncation with lightweight memory writing during prefill, and low-rank decomposition of recurrent state access during decode. It includes pseudocode, measured throughput gains, quality evaluation methodology, and practical deployment recommendations for teams running hybrid models in production.

    1. The Background: Hybrid Architectures Carry an Underrated Memory Cost

    Mainstream LLM architectures have been shifting away from pure softmax attention toward a hybrid shape: a small number of attention layers combined with a large number of linear attention or state space layers. The motivation is straightforward. The KV cache of an attention layer grows linearly with context length, while linear attention and state space layers maintain a fixed-size recurrent state. In theory, that means less GPU memory and better long-context behavior.

    In practice, however, there is a counterintuitive finding: a fixed-size recurrent state is not actually small. In one class of hybrid models, the recurrent state for a single request occupies roughly the same space as the attention cache for 4,500 tokens. So even when the context is only a few thousand tokens, the recurrent state is already one of the main consumers of GPU memory.

    Its lifecycle characteristics make things worse:

    • The recurrent state is read and written once per generated token, which makes it a high-frequency memory access pattern.
    • Its size is fixed, so it cannot be released as the context shrinks.
    • It competes for GPU memory with active requests and the prefix cache, directly pushing down the batch size you can run concurrently.

    The conclusion follows: inference optimization for hybrid architectures cannot focus on the KV cache alone. The costs of the prefill and decode stages have to be handled separately.

    2. The Core Insight: Writing Memory Is Cheaper Than Making Predictions

    The conventional assumption is that processing an input requires every token to pass through all layers, because only the output of the final layer can predict the next token. But if you split the objective apart, the compute requirements of the two parts turn out to be asymmetric:

    1. Predicting the next token requires deep semantic abstraction and must go through the full network.
    2. Writing memory for subsequent generation (the KV cache and the recurrent state) needs significantly less deep computation.

    This asymmetry is measurable. Across five trained hybrid LLMs, the difference between full deep computation and lightweight memory writing was measured separately, and that difference is the entire basis for the first stage of optimization. It requires no retraining of the original model and does not change the semantic path of the generation stage.

    3. Approach One: Layered Truncation in Prefill with Lightweight Memory Writing

    The method can be summarized as “compute the front layers fully, backfill the rest”:

    • Except for the last token, all other tokens run only the first 31 layers (for a 48-layer model).
    • A lightweight memory writing component generates the KV cache and recurrent state for the remaining 17 layers.
    • During generation, every token still runs all 48 layers, so the output quality path is unchanged.

    Illustrative pseudocode:

    1. def prefill_seeded(hidden, layers, writer, cut=31):
    2. # Front section: full computation for high-quality representations
    3. for i in range(cut):
    4. hidden = layers[i](hidden)
    5. # Back section: no full forward pass, the lightweight module writes memory only
    6. for i in range(cut, len(layers)):
    7. writer.write(i, hidden) # write KV cache / recurrent state
    8. return hidden # the last token takes the full path for the first prediction step
    9. def decode_step(token, layers, writer):
    10. hidden = token
    11. for layer in layers: # generation stage keeps full depth
    12. hidden = layer(hidden)
    13. return hidden

    Three engineering points matter here. First, the truncation point has to be chosen from measurements on the specific model, since the optimal split differs across models. Second, the memory writing component must be lightweight enough, or it will consume the compute you just saved. Third, the special handling of the first and last tokens cannot be skipped, or the quality of the first prediction step drops.

    4. Approach Two: Low-Rank Decomposition for Recurrent State Access

    The decode-side optimization targets memory access and memory footprint. The observation is that the recurrent state read and written at each step usually has a dominant component close to a fixed direction, with the remainder forming a low-rank structure. Given that structure, there is no reason to move the state around repeatedly in dense form.

    The approach keeps the state in decomposed form and lets the model read and update it directly in that form, instead of reconstructing a dense matrix, computing, and compressing it back afterward. The gains are quantifiable: for one hybrid model, the per-request recurrent state dropped from 113.2MB to 29.2MB right after compression, averaging 43.4MB over the full compression cycle.

    A structural sketch (expressing the decomposition idea only, not a concrete interface):

    1. state_t ≈ u * v_t^T + R_t
    2. # u: approximately fixed dominant direction (can be cached and reused)
    3. # v_t: low-dimensional coefficients, updated each step
    4. # R_t: low-rank residual, compressed periodically

    The direct effects are lower GPU memory usage, more concurrent requests, and reduced bandwidth pressure for state reads and writes at each step. For a memory-access-bound stage like decode, this often works better than reducing floating-point operations.

    5. Measured Gains and Evaluation Dimensions

    In a test on a single card (a new-generation accelerator) paired with a mainstream inference serving framework, using real session traces from an AI coding tool as the workload, the approach raised peak throughput per GPU by 16% to 42% for one hybrid model. At the comparison point of “the same P90 user end-to-end output rate,” the throughput gain was 23% to 42%.

    The difference between these two numbers is worth emphasizing. The first is an ideal peak; the second is the usable gain under a latency constraint. For engineering decisions, the P90-constrained number is the one to use, because it corresponds to a real SLA.

    On the quality side, the evaluation matrix covered five models and six checkpoints. The core constraint was that the original model weights stayed frozen throughout, while the added components were trained through distillation. This brings two benefits: there is no second base weight set to maintain, and changes stay contained when the base model is upgraded. It also means the accuracy cost can be quantified point by point, rather than described vaguely as “almost lossless.”

    6. Deployment Recommendations

    1. Measure before optimizing: track the GPU memory share of the recurrent state and the KV cache to confirm the bottleneck is decode rather than prefill.
    2. Run a grid search over the truncation point, with P90 latency and quality as dual constraints.
    3. Tune how often the dominant direction of the state decomposition is updated; updating too frequently cancels out the compression gains.
    4. If you use a container platform with monitoring and alerting, put “per-request state size,” “batch size,” and “P90 output rate” on the same dashboard so a local optimization does not hide a global regression.

    Overall, the room for inference optimization in hybrid architectures sits in two long-neglected places: writing memory and reading state.