DeepSeek V4.1 Flash, Illustrated: An Inference Perspective
Attention overhead squeezed to the bone, plus an external Engram that frees up GPU memory.
1Overview: far less compute and memory traffic
LLM inference has two phases with different bottlenecks:
- prefill processes the whole input in one pass and is bound by compute.
- decode generates one token per step, yet each step reads all active weights plus the request's entire KV cache, so it is bound by memory bandwidth.
V4.1 Flash cuts both costs substantially, so both prefill and decode get much faster.
Here is decode memory traffic for three models in the same intelligence tier (per the AA Intelligence Index):
V4.1 reads the fewest bytes per step, and the gap widens as context grows. Prefill compute tells the same story: V4.1 needs several times to tens of times less compute than the other two models.
Note: In Figure 1, weights are counted at their actual safetensors dtypes; with a larger batch more experts get activated and weight reads go up. For KV, V4.1 uses the official FP4 format and includes each layer's KV for the latest 128 tokens; K3 is counted in FP8 with its linear-attention state in BF16; GLM-5.3 is estimated in FP8, with one copy for each of its 78 layers plus the scoring K for 21 layers, borrowing V3.2's byte layout. In Figure 2, one multiply-add counts as 2 FLOPs, covering matmuls, attention and scoring; the figure is the per-token average for a cold prefill of an input that long. V4.1 is counted as running each input token through only the first 20 layers (see 3.1 for why), ignoring the 128 tokens the last 20 layers have to compute.
2Attention architecture
Both savings from Section 1, the KV read during decode and the compute during prefill, come mainly from the attention architecture.
2.1Two kinds of KV, and DSA sparse attention
Each attention layer in V4.1 reads two kinds of KV (except the first two layers, which read only sliding-window KV):
- Sliding-window KV (sliding window attention, SWA): stores only the latest 128 KV entries, one copy per layer;
- Global KV: covers the entire history, but one copy is shared by several layers.
Traditional dense attention reads the KV of the entire history at every step, so the amount read grows linearly with context. V4.1 instead uses a scorer, the indexer, to score every past position first, and the actual attention reads only the 512 highest-scoring KV entries, i.e. the 512 positions the indexer picked (called the selected positions below).
The indexer K used for scoring is also stored in the global KV: global KV = main KV (the 512-dim vectors attention reads) + indexer K.
This is DSA (DeepSeek Sparse Attention). It was introduced back in DeepSeek V3.2, and GLM-5.3 uses the same design. With DSA, KV storage still grows linearly with context, but the compute and memory traffic of the main attention per step become essentially constant.
DSA is not free, though: the complexity moves into the indexer, which has to score the full KV. In V3.2 and GLM-5.3, at long context the indexer actually costs more than the main attention. As we will see, V4.1 Flash overhauls this part aggressively too.
2.2Encoder and decoder
V4.1 splits its 40 layers into a 20-layer encoder followed by a 20-layer decoder, which DeepSeek calls CED (Causal Encoder-Decoder). Note that encoder and decoder here are DeepSeek's own terms coined for this work and have nothing to do with the encoder-decoder of the original Transformer paper. Both the encoder and the decoder in this model use causal attention.
- encoder (L0–L19): every few layers (L2, L8, L14) the global KV is recomputed and scored by the indexer; the next few layers simply reuse the selected positions of that global KV, without producing or scoring KV of their own;
- decoder (L20–L39):
- L20 computes the global KV for the last time, and all later layers reuse L20's KV;
- L24, 28, 32 and 36 rescore the global KV and pick the top 512, which the next 3 layers after each of them reuse.
2.3Only 4 layers of global KV in the whole model
As 2.1 noted, global KV covers the whole history, so the longer the context, the more there is to store. GLM-5.3 has 78 layers and stores 78 copies, so its KV cache scales with depth and, at long context, outgrows the weights (see the figure in Section 1).
Of V4.1's 40 layers, 4 compute their own global KV, L0 and L1 are pure sliding window with no global KV, and the other 34 layers read the nearest copy before them. That is more than an order of magnitude fewer copies.
Sliding-window KV, however, is still one copy per layer. A request's sliding-window KV is about 3 MB regardless of context length; at short context it is on the same order as the global KV.
2.4Each copy of global KV is smaller too: shared K/V, compression, FP4
Beyond fewer copies, each copy also shrinks:
- K and V are the same vector (a single 512-dim head): one copy serves as both K and V, so only one is stored.
- In the encoder, layers that store global KV compress every 2 tokens into one entry; L20 is not compressed:
- The merge weights the 2 tokens with a learned gate (a per-channel softmax in the reference implementation) rather than a plain average;
- The number of tokens merged into one entry is the compression ratio: 2 in the encoder, 1 at L20.
- Both the main KV and the indexer K are stored in FP4.
All of this loses some information. The report says FP4 global KV is used during training as well, "with only marginal performance degradation".
L20's copy is shared by all 20 decoder layers.
V4.1 is about 1/4 of the previous V4 Flash. Fewer copies (2.3) plus smaller copies mean V4.1's global KV per token is nearly two orders of magnitude smaller than GLM-5.3's (about 60×).
Note: about 60× = GLM-5.3's 53,940 B ÷ 890 B; the GLM-5.3 figure is an FP8 estimate.
2.5The indexer reuses scores across layers, and decoder layers score only within a candidate pool
As mentioned earlier, the indexer scores every past position at every step, so its cost grows linearly with context and overtakes the main attention once context gets long. Each generation of models has cut the number of layers that run the indexer:
GLM-5.3 already has every 4 adjacent layers share one indexer result (21 of its 78 layers run the indexer). V4.1 goes further, combining cross-layer reuse of global KV and selected positions with a block-wise candidate pool. The report calls this CSA2 (Compressed Sparse Attention 2), and it splits layers into three kinds:
- Full: computes its own global KV and runs its own indexer.
- Reindex: reads global KV from an earlier layer and reselects positions with its own indexer Q.
- Reuse: reuses both the global KV and the selected positions of an earlier layer.
In the decoder, positions are reselected every 4 layers (L24, L28, L32, L36), and the following few layers reuse them directly. Moreover, the scoring range of the decoder's Reindex layers is set by L20: these layers score only within a few fixed regions:
- L20 scores the entire history, groups positions into blocks of 8, and gives each block the highest score within it.
- The 2048 highest-scoring blocks form the candidate pool.
- L24, L28, L32 and L36 score only the positions in the pool, each picking the positions it will read.
It is a bit like retrieval plus re-ranking in search: first pick a small batch coarsely by block, then choose finely within that batch.
As a result, the number of positions L24, L28, L32 and L36 score per step is fixed at 16,384 and no longer grows with context. However:
- Below 16K context, the candidate pool is the entire history, so nothing is saved.
- The few layers that score the entire history still grow linearly with context.
2.6Putting it together: four data paths in one attention layer
Using the figure below, let's place the two kinds of KV and the indexer from 2.1 inside a concrete layer. Click a layer number in the figure to see what each kind of layer looks like (interactive on unitstride.com only); the default is a Full layer (L2):
- Q branch: computes this layer's own query vectors; all 64 heads share the same KV.
- Sliding-window KV branch: computes this layer's KV for the latest 128 tokens.
- Main KV branch: compresses the main KV part of the global KV. The most recent positions have both a sliding-window KV and a main KV; in layers with compression ratio 2, the newest positions that don't yet fill a group are buffered and have no main KV for now.
- Indexer branch: scores with the indexer Q and indexer K to get the selected positions.
Finally, the sliding-window KV branch produces KV for the latest 128 tokens, while the main KV and indexer branches pick the 512 highest-scoring KV entries; the two sets are concatenated. Q and these 512 + 128 = 640 KV entries then go into sparse_attn to compute attention.
Note: when the context has fewer than 512 positions, min(512, T) positions are selected, where T is the number of visible main KV entries.
3SWA Bounded Replay: trading approximate sliding-window KV for compute and storage
Every layer of V4.1 has sliding window attention, so at the end of prefill each layer has to hand its sliding-window KV for the last 128 tokens over to decode.
3.1Why decoder layers don't need to compute every token
As 2.2 said, the global KV used by the decoder is just L20's copy, computed once and shared by all decoder layers; L21–L39 do not compute global KV of their own.
What prefill must hand over to decode is the logits of the last token, the global KV, and each layer's sliding-window KV for the latest 128 tokens. The global KV is complete once L20 is done; what the decoder layers still lack is the sliding-window KV of the last 128 tokens.
So during prefill, the decoder layers only need the queries of the last 128 tokens to read the global KV and sliding-window KV; none of the earlier tokens have to enter the decoder.
But you cannot compute only the last 128 tokens in every layer, or information is lost. Consider: the last layer's sliding-window KV for these 128 tokens is computed from that layer's inputs at those 128 positions, i.e. the previous layer's outputs at the same positions. To compute those 128 outputs, the previous layer needs, at each position, the sliding-window KV of the 128 positions before it, so it must prepare 128 + 128 positions, and the layer above that needs another 128 more.
With exact recomputation, the decoder computes an inverted trapezoid: 128 tokens in the last layer, 128 more for each layer going up, reaching about 2560 (128 × 20) at L20.
3.2Bounded Replay in the decoder: only the last 128 tokens go through the decoder during prefill
The report has two techniques both named Bounded Replay, used together in the same model: the decoder one (officially Decoder SWA Bounded Replay) saves prefill compute, and the encoder one (officially Encoder SWA Bounded Replay) saves KV cache storage. Both replace exact recomputation with an approximate replay.
DeepSeek's evaluation of V4.1 found that exact CED could go one step further: skip rebuilding the inverted trapezoid and compute only the last 128 tokens in every layer (this is Bounded Replay). The evaluations show a negligible effect on answer quality, and the same replay was simulated during post-training so the model adapts to it.
As you can see, compared with other models that fully compute every layer during prefill, CED + Bounded Replay makes the decoder's compute nearly negligible, leaving roughly half of the total. That assumes a long input, though; if the input is 128 tokens or fewer, all of those tokens have to pass through the decoder anyway, and the decoder layers save nothing.
Note: the report states, "CED successfully reduces nearly half of the prefill computation while maintaining performance comparable to the baseline." (§2.2)
3.3Bounded Replay in the encoder: sliding-window KV is kept for only a few minutes
Global KV and sliding-window KV live in two separate pools, so the global KV may hit while the sliding-window KV misses.
As 3.1 explained, on a sliding-window KV miss you would rebuild an inverted trapezoid upward from L19: 128 tokens in the last layer, 256 in the one before, and so on. Bounded Replay applies here too, and evaluations again show almost no quality impact: each encoder layer rebuilds sliding-window KV for only the last 128 tokens.
What this changes:
- Before: a miss meant rebuilding lots of KV at a high recompute cost, so you had to keep sliding-window KV around as long as possible to avoid misses, using more KV storage;
- Now: with Bounded Replay, each recompute is cheap, so sliding-window KV matters much less;
- Result: its retention time (TTL) can be cut drastically to minutes, freeing up more cache space.
4Engram: n-gram lookup tables kept in host memory
In earlier models, memorized knowledge and computational ability live in the same weights (the attention and MoE weights). All of these weights have to stay resident in GPU memory, and the selected experts take part in computation at every step.
The report describes Engram as "the conditional memory module introduced in our previous work to decouple memorization from computation". It moves some content that can be looked up directly from combinations of the last few tokens into large n-gram lookup tables: a lookup just fetches one row by address, with no matrix multiplication.
V4.1's Engram has 196B parameters split across two modules, which write their lookup results into the residual stream at L1 and L14. Each position takes the last 2, 3 and 4 tokens ending at it. Tokens are first normalized to compressed ids (e.g. " The", "the" and "THE" map to the same one). Each n-gram length is hashed with 8 hash functions into 8 addresses, 24 addresses in total, and each address fetches one row vector from the table.
The 24 rows are concatenated and projected once, yielding a value to add in and a key to compare against the current hidden state. The better the key matches the hidden state, the closer the gate gets to 1 and the more of the value is added; if they don't match, almost nothing is added.
Which rows to fetch depends only on the tokens, not on results computed by earlier layers, so the addresses can be computed before the forward pass even starts.
The official inference setup keeps the tables in host memory, saving a large amount of HBM. Needed rows are prefetched in the background over RDMA, and the prefetch for the L1 module overlaps with L0's computation.
This way, part of the knowledge that would otherwise have to be "simulated" through computation becomes an address-based table lookup in host memory: it saves HBM, and although the fetched vectors still go through projection and gating on the GPU, that is far cheaper than dense matrix computation.
The FP8 payload of the two tables is about 197 GB, or about 203 GB including scales.
Note: "simulating lookup through computation" comes from the abstract of the Engram paper (arXiv:2601.07372): "Transformers lack a native primitive for knowledge lookup, forcing them to inefficiently simulate retrieval through computation". In V4.1, Engram is a separate module added on top of MoE.
5mHC: the residual goes from one stream to four
In earlier models the residual is mostly a single stream: each sublayer (attention and MoE each count as one) takes its input from the stream and adds its result back.
mHC (Manifold-Constrained Hyper-Connections) turns the residual into 4 streams: after the embedding, it is copied into 4 identical streams. Each of V4.1's 80 sublayers does three things:
- Input mix A: multiply each of the 4 streams by a weight and sum them into one, which goes into the sublayer.
- Write-back C: multiply the sublayer output by a weight and add it to each stream.
- Stream mix B: each new stream takes a share from each of the 4 old streams (including itself).
Reading and writing all 4 streams increases activation memory traffic. Calling the 5120 values of one token in one stream 1d, the report gives the total read and written per token per sublayer (reads and writes both count):
- Original V4 implementation, 20d: residual update, coefficient prediction and input mix are three kernels run one after another, each reading everything once.
- Mega-mHC fusion, same architecture, 14d: the three steps are fused into one kernel, and residual update and coefficient prediction share a single read. This is a pure kernel optimization. The input mix still has to wait for this step's A, and A cannot be computed until the whole row has been read, so another read is needed.
- Mega-mHC plus Single-Pass, 10d: the input mix uses the A predicted in the previous step instead of waiting, so a single read suffices. This changes the model architecture, and V4.1's weights were trained this way.
mHC does not affect KV. Activation traffic is tiny compared with weights and KV, so what Single-Pass and Mega-mHC save is only a minor optimization, not the main reason V4.1 is fast. For batch = 1 decode, it is about 8 MB per token (80 sublayers × 10d × 2 bytes per value), while each step reads 13 GB of weights.
6DSpark
V4.1 also ships with its own speculative decoding module, DSpark. Speculative decoding has a small draft module guess the next few tokens, then lets the main model verify them in one forward pass and emit as many as were guessed correctly. DSpark's drafter proposes several candidate tokens at once, then decides per request how many to actually verify based on confidence.
DSpark has no notable new optimizations in this generation, so we won't cover it here; see other write-ups, or perhaps a dedicated post later.
7Summary
Global KV used to be long, taking lots of GPU memory and making attention slow. The industry sparsified attention with an indexer so that only the most relevant KV entries take part in attention at each step, but the indexer then became the new bottleneck.
V4.1 goes further: it shares and compresses global KV across layers, reuses indexer results, and adds a candidate pool for coarse-then-fine selection, sharply cutting both global KV storage and indexer cost. CED plus Bounded Replay cuts prefill compute by about half. Engram takes part of the knowledge out of computation and turns it into address-based table lookups in host memory, reducing HBM usage and costing far less than dense matrix computation.
While outperforming V4 Flash, these changes substantially lower the cost of inference.