Hybrid Architecture Inference Optimization: Writing Memory Instead of Full Computation

    This article breaks down inference cost into prefill computation and decode-time state occupancy, then examines two optimizations for hybrid architecture models: using a lightweight writer to generate deep-layer memory for non-final tokens instead of running full depth during prefill, and storing recurrent state in a principal-component-plus-low-rank form during decoding. It also covers writer training via distillation, throughput and quality evaluation, controlled experiments, engineering deployment considerations, and applicability boundaries.

    1. Starting Point: What Inference Actually Pays For

    The cost of large model inference can be divided into two parts. The first is computation, especially when processing long inputs; this stage is called prefill. The second is state occupancy, meaning the intermediate data the model must store and repeatedly read and write during generation. These two parts fall into different stages, so their optimization strategies should also be designed separately.

    During prefill, the model reads the entire prompt at once, computes the hidden state of each token layer by layer, and writes out the KV cache or recurrent state. During decoding, the model generates only one token at a time, but it must read all previously stored state and update part of it.

    2. State Structure of Hybrid Architectures

    Hybrid architectures combine two types of layers. A small number of softmax attention layers store KV cache, whose size grows linearly with context length and is suitable for precise retrieval of historical information. A large number of linear attention layers or state-space layers maintain recurrent state, whose size is independent of context length, although the state footprint of a single request is not small. One study measured a hybrid model and found that the recurrent state footprint per request was equivalent to the attention cache of about 4,500 tokens.

    This means that even if KV cache is controlled, recurrent state still consumes considerable GPU memory. The state is updated for every generated token and also competes with active requests and prefix cache for the same memory, so it directly limits the concurrency ceiling.

    3. Optimization Idea 1: Deep Computation Does Not Need to Serve Every Token

    During prefill, only the final output of the last input token is used to predict the first response token. The outputs of the other tokens do not directly participate in prediction; their role is to provide memory for subsequent generation, namely the KV cache and recurrent state written by each layer.

    This leads to a judgment: the computation depth required to write memory for subsequent generation may be much smaller than the depth required to predict the next token. Specifically, non-final tokens can run only the first several layers, and then a lightweight writer component uses the shallow hidden states to generate the corresponding caches or states for the remaining deep layers. The final token still runs the full network to ensure the quality of the first output.

    The decoding stage still runs the full number of layers, because every step must output a real prediction and deep computation cannot be omitted.

    How to Train the Writer Component

    The base model weights remain frozen, and only the newly added writer component is trained. The training objective is to make the caches and states produced by the writer as close as possible to the results of the full network. A common method is distillation: the full model is the teacher, the lightweight writer is the student, and outputs are aligned layer by layer.

    The following code is only for illustrating the data flow. The functions are abstract placeholders and do not correspond to any specific library or interface.

    1. def prefill(tokens, layers, writer, shallow_k=31):
    2. hidden = embed(tokens)
    3. # Non-final tokens run only the first shallow_k layers
    4. for layer in layers[:shallow_k]:
    5. hidden = layer(hidden)
    6. # The writer generates memory (cache or state) for deep layers
    7. memory = writer(hidden[:-1])
    8. # The final token runs the full network to predict the first output
    9. last = hidden[-1:]
    10. for layer in layers[shallow_k:]:
    11. last = layer(last, memory)
    12. return memory, last

    This design has a clear boundary. The writer replaces the generation of memory, not the generation of predictions. If deep computation is critical for certain fine-grained information, the writer’s error will accumulate along the context, so it must be validated through task-level evaluation.

    4. Optimization Idea 2: Store Recurrent State in Low-Rank Form

    The second idea targets state occupancy during decoding. Research has observed that the recurrent state read and written at each step often contains a dominant component with a nearly fixed direction, while the remaining content has a low-rank structure. The state can therefore be stored as a “principal component plus low-rank residual” form, and the model can read and update directly in this form without restoring the full matrix at every step.

    Taking numbers reported in one study as an example, the recurrent state per request was compressed from about 113.2 MB to about 29.2 MB immediately after compression, and the average occupancy during the compression period was about 43.4 MB. Peak occupancy dropped by about 74%; the average occupancy lies between the two because the state grows gradually during generation and is then periodically compressed.

    The compression period is a clear trade-off. More frequent compression lowers memory occupancy, but each compression brings more computation overhead and approximation error.

    The following table compares the focus of the two types of optimization:

    Dimension Prefill optimization Decoding optimization
    Cost targeted Deep-layer computation Recurrent state memory
    Modification point Deep-layer path for non-final tokens Storage form of state
    Changes single-step output? No, requires quality validation No, requires quality validation
    Main risk Accumulated memory-writing error Low-rank approximation error

    5. How to Evaluate Such Optimizations

    Definitions of Throughput Metrics

    Throughput must be distinguished by two definitions. Peak throughput reflects how many outputs the system can process at optimal concurrency, but this number may come at the cost of worse single-user experience. Therefore, throughput at the same tail latency should also be compared, for example by fixing the P90 end-to-end output rate as the comparison point.

    One study reported on session traces of coding agents that peak throughput per GPU improved by about 16% to 42%, and at the fixed P90 output rate comparison point improved by about 23% to 42%. These numbers depend on hardware, scheduler, workload shape, and the model itself, and should not be directly extrapolated to other scenarios.

    Coverage of Quality Evaluation

    The accuracy of the optimized model should be validated across multiple models and multiple checkpoints. At minimum, the following categories of tasks should be covered:

    • General benchmarks: check whether overall capability degrades.
    • Long-context retrieval: check whether deep memory writing loses critical information.
    • Business-related tasks: for example, code generation, multi-turn dialogue, and tool calling.
    • Generation length and repetition rate: observe whether low-rank approximation introduces degradation, such as shorter outputs or repetitive loops.

    Controlled Variables in Comparative Experiments

    When comparing performance before and after optimization, hardware, scheduling policy, and request distribution should be fixed. Otherwise, throughput differences may come from scheduling changes rather than model changes. Each experiment should record memory watermark, batch size, and request length distribution to facilitate reproduction and attribution.

    6. Engineering Deployment Considerations

    1. Memory budget: first measure the respective shares of recurrent state, KV cache, and prefix cache, then decide the optimization priority.
    2. Concurrency and scheduling: the memory saved by state compression translates into throughput gains only when memory is the bottleneck.
    3. Version management: the writer is bound to the base model, and after the base model is updated, it needs to be re-distilled and evaluated.
    4. Fallback path: keep the full-network inference path so that it can be switched quickly if quality degrades.
    5. Monitoring metrics: record first-token latency, output rate percentiles, memory watermark, and compression trigger count.

    7. Applicability Boundaries and Feasibility Checks

    These methods rely on the memory structure already present in the model.