unitstride
DeepSeek V4.1 Flash through an inference-acceleration lens · unitstride

DeepSeek V4.1 Flash, Illustrated: An Inference Perspective

Published Nov 4, 2026

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:

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):

8K contextK3Weights137.0 + 0.4 GBGLM-5.3Weights41.4 + 0.4 GBV4.113.0 + <0.1 GB1M contextK3Weights137.0 + 14.7 GBGLM-5.3WeightsKV41.4 + 56.6 GBV4.113.0 + 0.9 GB
At 1M context, GLM-5.3's KV is already larger than its active weights, while V4.1's KV is under 1 GB. (Bytes read per decode step, batch = 1; see the note at the end of this section for methodology.)

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.

8K contextK3215GLM-5.3101V4.1181M contextK3982GLM-5.3193V4.124
The longer the context, the more K3's dense attention costs it. (Theoretical prefill compute per input token, in GFLOPs; see the note at the end of this section for methodology.)

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):

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.

Dense: read every positionScorePast KVRead per step:All history
Each step reads only the main KV at the 512 selected positions plus the latest 128 sliding-window KV entries, instead of the whole history. (Schematic: 40 cells stand for the whole history, 8 cells for the 512, and the rightmost 4 cells for the latest 128.)

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.

KV sharedL2’s global KVL8’s global KVL14’s global KVL20’s global KVSelection sharedL2L8L14L20L24L28L32L36V4.10123456789101112131415161718192021222324252627282930313233343536373839encoder L0–19decoder L20–39SWA onlyFull: own global KV, own selectionReindex: reused KV, own selectionReuse: reuses both
Of the 40 layers, 4 compute their own global KV (Full), 4 reselect positions on L20's global KV (Reindex), and the rest reuse both (Reuse); L0 and L1 are pure sliding window. (One cell per layer, numbered from 0; based on config.json.)

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.

GLM-5.378 copiesV4.1L2L8L14L204 copiesL0, L1: SWA onlyuses L2uses L8uses L14uses L20
GLM-5.3 stores a separate KV copy for every layer, 78 in total; V4.1 stores only 4 copies of global KV. (One cell per layer, numbered from 0; each row represents the whole model.)

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:

All of this loses some information. The report says FP4 global KV is used during training as well, "with only marginal performance degradation".

L2 178L8 178L14 178L20 356encoder: 3 copies, 2 tokens → 1 entry: 534decoder: 1 copy, uncompressed: 356 (40%)
Of the 890 B per token, the uncompressed L20 accounts for 40%. (Global KV bytes per cached token, excluding sliding-window KV; each entry is 356 B = 288 B main KV + 68 B indexer K.)

L20's copy is shared by all 20 decoder layers.

V1389,120V3.248,068V4 Flash3,514V4.1890
From the first DeepSeek (V1) to V4.1, global KV per token has dropped by more than two orders of magnitude. (Bytes per token, excluding sliding-window KV; from Figure 1b of the technical report.)

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:

V3.261 layersGLM-5.321 layersV4.18 layers28142024283236L2, L8, L14, L20: score the entire historyL24, L28, L32, L36: score only within the candidate pool
V4.1 runs the indexer in the fewest layers, and 4 of them score only within the candidate pool. (One cell per layer; blank cells do not run the indexer and reuse earlier selected positions; based on the indexer settings in each model's config.json.)

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:

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:

  1. L20 scores the entire history, groups positions into blocks of 8, and gives each block the highest score within it.
  2. The 2048 highest-scoring blocks form the candidate pool.
  3. L24, L28, L32 and L36 score only the positions in the pool, each picking the positions it will read.
Step 1: L20 scoresL20L24L24 scores:—
L24 scores only within the candidate pool picked by L20. (Schematic: each box is a block, drawn here with 4 cells; the L24 row applies equally to L28, L32 and L36.)

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:

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):

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.

x · attention input[s, 5120]Q branchSWA KV branch · every layerMain KV branchIndexer branchwq_a · q_norm5120→1280[s, 1280]Q latentqrwq_b · RoPE1280→64×512[s, 64, 512]Query Qqwkv · kv_normattn.wkv 5120→512[s, 512]RoPEtoken positionSWA KVkvwrite [s, 512]SWA KV cache[W, 512]read [W, 512]prefill: this chunk’s kv goesstraight to concat, no cache readchunk kv [s, 512]x [s, 5120]wkv5120→512wgate5120→512[s, 512][s, 512]Merge by ratioratio 2Bufferwait for 2[ΔT, 512]Main KVno RoPE yetmain KV [ΔT, 512]Indexer K is projected from it,then RoPE and FP4 quantizationAdd RoPE · FP4write [ΔT, 512]Main KV cache[T, 512]FP4 288 B→ read by L3–7read [T, 512]Head weights wweights_proj 5120→32[s, 32]qr [s, 1280]Indexer Qwq_b·RoPE·FP4Indexer Kwk·norm·RoPE·FP4write [ΔT, 128]Indexer K cache[T, 128]FP4 68 B[s, 32, 128]read [T, 128]ScoreΣ_heads w·ReLU(q·k), no softmax[s, T]Candidate pool2048 blocks of 8none in encoder−∞ outside poolSelected positionsk=min(512,T) · sorted by position→ reused by L3–7[s, k]Window indicesfrom positions[s, W]Concat kvcat[SWA KV, selected main KV]Concat indicesselected positions + offset[W+T, 512][s, W+k]Sparse attention sparse_attnone KV serves as both K and Vo [s, 64, 512]Inverse RoPElast 64 dims[s, 8, 4096]wo_a in 8 groups4096→1024 per group[s, 8192]wo_b8192→5120[s, 5120]Attention outputoptensorcachenot in this layerborrowed from another layer
Each layer's attention has four paths; at the end the 128 sliding-window KV entries and the main KV at the 512 selected positions are concatenated for attention. Click a layer number in the top row to switch layers. (Shown: L2, decode. Shapes omit the batch dimension: s is the number of tokens in this forward pass (1 for decode), T is the number of visible main KV entries, ΔT is the number of entries newly written in this pass, W = 128, k = min(512, T). Shapes come from the official reference implementation model.py.)

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.

tokenencoderL0–19decoderL20–39Other modelsAll tokens computedEvery layer computes all input tokensV4.1All tokens computedL39L20Step 1: L39’s last 128
Other models compute every input token in every layer; for V4.1, exactly recomputing the decoder's sliding-window KV covers an inverted trapezoid. (Schematic: the top row is the input tokens, each row below is a layer, L0 at the top; in V4.1's decoder one cell = 128 tokens, drawn with 20 layers; the left panel uses V4.1's 40 layers to illustrate computing every token in every layer; magenta = what prefill computes.)

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.

tokenencoderL0–19decoderL20–39Long inputOther modelsAll tokens computedEvery layer computed in full16B active params per tokenCED, exactAll tokens computedskippeddecoder: last ~2560 tokens~8B active params per tokenCED + Bounded ReplayAll tokens computedskippeddecoder: last 128 tokens only8B active params per tokenShort input (≤ 128 tokens)All computed, no savingsAll computed, no savingsAll computed, no savings
With long inputs, CED plus Bounded Replay leaves the decoder with almost nothing to compute; with inputs of 128 tokens or fewer, all three approaches compute the same amount. (Schematic, the top row is the input tokens, not to scale; each row below is a layer, L0 at the top; magenta = what prefill computes. Active parameters are the parameters each token passes through, and the active weights in Section 1 are their bytes; the first panel uses V4.1's 40 layers to illustrate other models computing every layer in full; 16B is V4.1's active parameters per token during decode, from the technical report.)

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.

tokenL0L19encoderSWA KV hitCached prefixNewGlobal KV hitnot recomputedCompute the new input onlySWA KV miss, exact recomputeCached prefixNewGlobal KV hitnot recomputed+ last ~2560 prefix tokens (trapezoid)Encoder Bounded ReplayCached prefixNewGlobal KV hitnot recomputed+ last 128 prefix tokens (approximate)Too expensiveWhat V4.1 does
On a sliding-window KV miss, exact recomputation must go back about 2560 tokens, while replay goes back only 128. (Schematic, showing only the 20 encoder layers; the top row is tokens, not to scale; L0 at the top; magenta = what gets computed; the prefix's global KV is hit in all three cases and never recomputed.)

What this changes:

V4 FlashGlobal KVSWA KV1drop SWAGlobal KV× 1/2, Encoder Bounded ReplayV4.1 Flashthen × 1/4, global KV compression
Once sliding-window KV is no longer kept long-term, the KV cache used for prefix reuse drops to 1/8 of V4 Flash's. (Per-token footprint, V4 Flash = 1, under the same workload; technical report §3.2.1: dropping stored sliding-window KV nearly halves it, and global KV is 1/4 of V4's.)

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.

Last few tokens…weather in Beijingn-gram hash2/3/4 tokens × 8Lookupfetch 24 rowsProjectionkey and valueGatekey vs. hidden stateAdd to hidden+ gate × valueToken-only: addresses computed early, rows prefetchedNeeds the current hidden state
The lookup half depends only on tokens and can be done ahead of time; the gate needs the current hidden state and is computed only at its layer. (Based on technical report §2.4.2 and the Engram module in the official reference implementation model.py; each row is 256-dim, and both the tables and the projection are FP8.)

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.

GPURDMAL0L1 lookupL2…L14 lookup…Fetch L1 rowsFetch L14 rows
Prefetching table rows from host memory overlaps with the computation of earlier layers. (Schematic.)

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:

  1. Input mix A: multiply each of the 4 streams by a weight and sum them into one, which goes into the sublayer.
  2. Write-back C: multiply the sublayer output by a weight and add it to each stream.
  3. Stream mix B: each new stream takes a share from each of the 4 old streams (including itself).
Plain residual1 streamattentionMoEmHC4 streams, 5120 dims eachattentionMoES1S2S3S40.50.30.30.20.80.50.30.20.70.60.650.650.10.150.050.20.10.150.150.20.050.10.050.10.70.20.40.10.30.90.40.60.60.650.60.650.20.10.150.050.150.150.10.10.20.10.10.1Predict coeffsread → 24 coeffsPredict coeffsread → 24 coeffsInput mixResidual updateInput mixResidual updateStream mix B×164 per streamWrite-back C×41 per streamInput mix A×41 per streamInput mix A×41 per streamprev. predictionStream mix B×164 per streamWrite-back C×41 per streamA kept for next
mHC adds three steps around each sublayer, input mix, write-back and stream mix, and goes from 1 stream to 4. (Numbers and band widths are illustrative; the real ratios differ for every token and every sublayer. Write-back and input-mix bands are scaled to their total width where they merge.)

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):

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

HBM KVPersistent KVDecode trafficPrefill computeHBM weightsCross-layer global KV sharing★★★★★★★K = V, compression, FP4★★★★★★Attend to only 512 + 128★★★★Indexer sharing, candidate pool★★★CED + decoder Replay★★Encoder Replay★★Engram★★Single-Pass + Mega-mHC★★
Most changes target KV and decode memory traffic; prefill compute savings come mainly from CED. (Each column is a cost the technique reduces; ★★ = major effect, ★ = some effect, blank = no effect; judged from the sections above, not a breakdown given in the report.)

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.