Encoder-decoder prefill: must every layer read the whole prompt?
Prepare a shared history in the encoder, let the decoder read it, and discover why a short local replay is still needed before generation.
Before generating a response, a language model processes the prompt and prepares its KV cache. This is prefill. A long prompt can make us wait even when the answer will be short.
We have already considered sharing cached history between layers. Could a model go further and avoid running some layers over the entire prompt in the first place?
Find the repeated preparation
In a conventional transformer, every prompt position passes through every layer. Each layer needs that work to prepare its own keys and values. Processing the positions together makes the computation efficient, but does not remove the layer-by-layer dependency.
For a 4,096-position prompt and 40 layers, that is 163,840 layer–token visits:
Simply skipping the later layers would leave their caches missing. We need to change where those layers get their history, rather than omit work they still depend on.
Let one part prepare the history
Split the stack into a causal encoder and a causal decoder. The encoder processes the prompt. Instead of requiring a separate full-prompt calculation inside each decoder layer, project the decoder’s global keys and values from the encoder’s final outputs.
The decoder layers can then share that prepared history. They still calculate different queries and attention results, but their global cache no longer depends on running every decoder layer at every earlier prompt position:
This is the idea behind decoder–decoder designs such as YOCO and the worked DeepSeek-V4.1 model. “Encoder” here means the causal layers preparing reusable history. It does not mean a bidirectional text encoder or an image encoder.
DeepSeek-V4.1 has 20 encoder layers and 20 decoder layers, with 5,120-coordinate representations and a 512-coordinate shared global K/V entry. The encoder’s final output supplies that decoder history.
Keep the decoder’s recent window correct
There is a catch. The decoder also uses layer-local 128-token windows. Preparing its global history does not populate those local buffers.
We can rebuild them by replaying the last 128 encoder outputs through the decoder. That gives each decoder layer its own recent history without processing the entire prompt again. For a shorter prompt, replay the available tail. The last replayed position also supplies the prediction for the first new token.
Now the path through preparation and generation becomes:
For our 4,096-position example, the prompt work is 20 × 4,096 + 20 × 128 = 84,480 layer–token visits, compared with 163,840 in the ordinary full-stack pass. This count illustrates the work avoided; actual latency also depends on the layers’ operations, batching and memory traffic.
Generate with the complete stack
After that preparation, new generated positions pass through both halves. The encoder produces a new representation and extends the shared global history; the decoder uses its own queries, the prepared history and its local buffers to produce the next prediction.
The saving is in preparing a long prompt. It does not turn a 40-layer model into a 20-layer model for generation. Nor does it make the history constant-size: compression, selection and cache precision remain separate choices.
The DeepSeek-V4.1 architecture shows how this split fits alongside CSA2, indexer reuse and the model’s other components. Here, the key change is the dependency: the decoder’s global history is available before the decoder has processed the full prompt.
Sources and worked example
- You Only Cache Once (YOCO), Sun et al., 2024.
- DeepSeek-V4.1 model and technical report.
- DeepSeek-V4.1 reference implementation.
- Worked architecture and sourced layer schedule.
The 40-layer comparison counts layer–token visits, not FLOPs or time. It isolates full-prompt preparation and the 128-position decoder replay. The drawings show the main global 512-coordinate K/V representation; indexer state, compression details and FP4 storage are described on the model page. Shared global history is compatible with distinct per-layer local buffers. Both stages remain causal, and both run on generated positions.