Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Running models efficiently

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:

Follow the data through the mechanismAn ordinary full-prompt passEarlier context is dimmed; outlined columns are current.Current token computation.5,1204,096 prompt positionsFirst half of the stack20 layers5,120Second half of the stack20 layers5,120Earlier context is dimmed; outlined columns are current.Current token computation.5,120Earlier context is dimmed; outlined columns are current.Current token computation.512P cached positions / layerEarlier context is dimmed; outlined columns are current.Current token computation.512P cached positions / layer40 × 4,096 = 163,840 layer–token visits
A conventional 40-layer stack processes the full prompt in every layer. Layer–token visits count positions passing through layers; they do not measure wall-clock latency.

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:

Follow the data through the mechanismPrepare the prompt history onceEarlier context is dimmed; outlined columns are current.Current token computation.5,1204,096 prompt positionsCausal encoder20 layers5,120Earlier context is dimmed; outlined columns are current.Current token computation.5,120Final encoder outputsDecoder global KV5,120512Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512Prepared global historyEncoded query positionEarlier context is dimmed; outlined columns are current.Current token computation.5,120Causal decoder20 layers5,120Earlier context is dimmed; outlined columns are current.Current token computation.5,120
DeepSeek-V4.1 projects the decoder’s global KV from the encoder’s final outputs. Decoder layers share that prepared global history while making their own queries. The current-position branch is shown separately from full-prompt preparation.

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:

Follow the data through the mechanismRead the promptReplay its local tailProcess a new tokenEarlier context is dimmed; outlined columns are current.Current token computation.5,1204,096 positionsEncoder20 layers5,120Global KV projection5,120512Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512P global positionsLast encoder outputsEarlier context is dimmed; outlined columns are current.Current token computation.5,120128 positionsDecoder20 layers5,120Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512128 local positions / layerLocal buffers readyEarlier context is dimmed; outlined columns are current.Current token computation.5,120Encoder + decoder40 layers5,120Earlier context is dimmed; outlined columns are current.Current token computation.5,120Vocabulary projection5,120129,280Softmax → next tokenPrompt work: 20 × 4,096 + 20 × 128 = 84,480 layer–token visits
DeepSeek-V4.1: all prompt positions run through 20 encoder layers. Replaying the final 128 encoder outputs through the decoder rebuilds its per-layer local buffers. New generated positions subsequently pass through both halves.

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

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.

← Browse all subjectsBack to the beginning ↑