从 MLA 到
CSA / HCA
DeepSeek-V4 如何同时改变 attention 的存、读、算,以及它给训练与推理系统留下了什么新问题。
基于本 reading kit 的 00–04;05/06 GPU profiling 尚未执行。2026-07-20。
MHA:每个 query head 拥有自己的 K/V memory
Q / K / V heads
outputs
elements / source token / layer
每生成一个 token,每个 query head 都读取自己对应的整段 K/V history。
GQA / MQA:不减少 query heads,只共享 KV heads
MLA:缓存 shared latent,再恢复或吸收 head-specific view
+ shared RoPE key
K / V views
与 query-head 数量解耦;V3-shaped example 为 512 + 64 = 576 elements。
projection 可移到 query / output sides,因此无需逐 token materialize 所有 heads 的 K/V。
NSA:同一层里并行看 coarse、selected fine 和 recent local
branch
KV blocks
window
aggregation
便宜地获得 coarse global view,也提供 selection signal。
回到被选中的 original-token fine KV blocks。
固定覆盖最近局部细节。
DSA:用一个窄 indexer,选择 original-token MLA entries
index keys
original IDs
entries
St = TopKs(It,s), ot = Attn(qt, KVSₜMLA)
scan: O(L) narrow index
main: O(K) wide MLA
Store remains O(L) main + O(L) index.
V4 的关键跃迁:被 attention 读取的单位不再是原始 token
CSA · ratio 4
HCA · ratio 128
Conceptual compressor equation; exact overlapping streams and positional handling: 01 §2–5.
V4 的变化不只是“更稀疏”
长期记忆的基本单元仍是一条 original-token latent;DSA 只稀疏读取。
先形成 learned compressed entry,再对 compressed history 做 top-k 检索。
把序列压得足够短后,直接 dense read,作为稳定的粗粒度全局通路。
把 product attention 拆成三个几乎正交的设计轴
一条 memory 多宽?
MHA · GQA · MQA · MLA
存多少、读哪些?
Full · SWA · DSA · NSA · CSA · HCA · KDA
一次验证几个位置?
autoregressive · MTP / speculative
MLA:用更多矩阵计算,换更窄的 KV cache
cKV
Q/K/V views
相对 materialized 128-head K/V,元素数约少 71×。
但 dense MLA 仍为每个 token 保留一条 latent,并扫描全部历史。
00 · MLA Primer · cache comparison is logical elements, not allocator bytes.
MLA 是更好的 Pareto 点,不是免费的速度
它直接改善什么
- cache capacity 与 HBM history traffic
- shared latent 在 query heads 之间的复用
- 更大的 resident batch / prefix 空间
它付出什么
- absorbed latent-space QK / AV 算术
- 更复杂的 projection 与 kernel dataflow
- MTP 加宽 query 后更容易跨到 compute-bound
必须分别问:存多少、每步读多少、算多少
| Mechanism | Persistent store | Decode read | Main compute | 本质 |
|---|---|---|---|---|
| Dense MLA | small bytes × L | O(L) | O(L) | feature compression |
| DSA over MLA | main + index = O(L) | index O(L) + main O(K) | 同左 | sparse access |
| NSA | fine O(L) + coarse/local | compressed + selected + local | 同左 | three branches |
| CSA | main/index O(L/4) + O(W) | index O(L/4) + main O(K+W) | 同左 | compress then retrieve |
| HCA | O(L/128) + O(W) | O(L/128 + W) | 同左 | compress then dense-read |
00c · O(·) hides dimensions, quantization, top-k, gather and kernel efficiency.
同样 128K context:cache 小,不代表每步读得少
| Model | Attention stack | Resident state | History read / step | Core attn FLOPs |
|---|---|---|---|---|
| DeepSeek-V3 | 61 dense MLA | 8.58 GiB | 8.58 GiB | 2.23 TF |
| DeepSeek-V3.2 | 61 DSA-MLA · top-2048 | 10.48 GiB | 2.04 GiB | 166.8 GF |
| Kimi K2 | 61 dense MLA · 64 Q heads | 8.58 GiB | 8.58 GiB | 1.11 TF |
| Kimi K3 | KDA + Gated MLA · config pending | TBD | TBD | TBD |
| GLM-5.2 | 78 DSA-MLA · 21 route scans + 57 ID reuses | 11.63 GiB | 0.83 GiB | ≈ 44.8 GF |
| MiMo-V2-Flash | 9 global + 39 SWA-128 GQA | 2.84 GiB | 2.84 GiB | 48.5 GF |
| gpt-oss-120b | 18 global + 18 SWA-128 GQA | 4.50 GiB | 4.50 GiB | 38.7 GF |
| DeepSeek-V4-Flash | 2 SWA + 21 CSA + 20 HCA | 0.85 GiB | 0.20 GiB | ≈ 16.1 GF |
| DeepSeek-V4-Pro | 30 CSA + 31 HCA | 1.21 GiB | 0.30 GiB | ≈ 34.5 GF |
统一口径:L=131,072 · batch=1 · one target query · BF16 logical attention state · ideal one-pass history read · QK/AV + sparse-index score only。
Analytical architecture comparison, not latency. Excludes projection, compression, top-k/softmax, MoE, communication, MTP and allocator effects. V4 published mixed formats reduce the 128K payloads further to ≈0.43 GiB (Flash) / 0.62 GiB (Pro). K3 exact configuration is not public as of 2026-07-20; weights/report are promised by 2026-07-27. Derivations and primary sources: 00b · Attention Serving Comparison.
V4 同时在层内、层间做 hybrid
V4-Flash · 43 main layers
2 pure SWA 21 CSA 20 HCA
层内
compressed-global candidates 与 local 128 tokens 共用一次 softmax。
层间
CSA / HCA 交替,分别承担检索型与 dense-global 信息通路。
Pro: 61 main layers = 30 CSA + 31 HCA. MTP block is separate pure SWA. 01 §9
CSA:先压缩 4×,再检索 compressed entries
states
two-stream C4
L / 4
over L / 4
512 / 1024
local 128
main entry + separate index key
narrow scan + bounded main fetch
不会回到旧 original-token KV
HCA:压到 128× 后,dense 反而更简单
states
one-stream C128
history L / 128
128
为什么保留 HCA?
- 无需 indexer / top-k / irregular gather
- 每层都有稳定的粗粒度全局覆盖
- 补偿 CSA 离散检索可能漏掉的信息
一条 request 同时维护四种 cadence
| State | Append / update cadence | Representative format | Lifetime |
|---|---|---|---|
| raw local shared-KV | 每 token | 448 FP8 + 64 BF16 RoPE | ring, W=128 |
| C4 main | 每 4 tokens | same 512-d shared K=V | persistent |
| C4 index key | 每 4 tokens | 128-d MXFP4 + scales | persistent |
| C128 main | 每 128 tokens | same 512-d shared K=V | persistent |
| incomplete compressor tail | 每 token | bounded residual state | until boundary |
长上下文下,CSA 的增长项是 index scan
Modeled FLOPs per generated token, one CSA layer. Fixed projection/compressor/output ≈ 252M FLOPs.
约 93% modeled FLOPs 来自 index QK;main selected attention 仍约 84M FLOPs。
16 MiB FP4 index-key read / layer / query。
Analytical model, not GPU measurement. 01 §8.1
Shared K=V 让 RoPE 多出一个 value-side 修正
512-d shared entry 中,448 维 NoPE;最后 64 维随 entry position 旋转。
= Σj atjRpⱼ−tCjR
把 value 输出从 absolute-position frame 转成 query-relative frame。
NSA 是结构启发;DSA 是 CSA 的直接操作血缘
NSA · three parallel paths
仍需保留 full fine KV,供 selected branch 将来读取。
DSA → CSA · index then top-k
CSA 继承 lightning indexer / top-k,但改变了被检索的 memory unit。
DSA 省 read / compute,但不省 persistent-history 阶
main entries
index keys
main read
read = O(L) narrow scan + O(K) main
为什么不能删旧 main KV?
未来任何 query 都可能选择任意 old token。没有额外 eviction semantics,就必须保留完整 main history。
训练不是一个固定 operator graph
CSA main 看所有 completed C4 entries;还没有 indexer。
sparse main + dense teacher distribution,训练 lightning indexer。
top-k main + selected-set teacher;仍有 narrow full index scan。
V4 CP 是专用算法,不只是套 ring attention
raw-token shard
left boundary
compression
all-gather
global compressed K
CP 真正省下
raw activations、local Q rows、每 rank 的 sparse main work。
当前仍复制
all-gather 后的 ratio-4 main/index compressed history;1M 下可能成为通信与容量瓶颈。
03 §4–6, §11 · code state is pinned in the note.
Flash attention 之外,仍有很大的研究面
Dense plumbing
Q/KV projection · mixed precision · compression · RMSNorm / RoPE · grouped output
Sparse core
index score · radix top-k · FlashMLA sparse forward · cuDNN DSA backward · LSE
Distributed runtime
THD segment maps · boundary P2P · compressed all-gather · logical/physical IDs · DDP readiness
03 §8–12 · Megatron functional, fused, packed and CP PR map is recorded there.
Prefill、decode、verify 是三张不同的图
| Stage | Projection | Compression / index | Main attention | Runtime priority |
|---|---|---|---|---|
| Cold / chunked prefill | large-token GEMM | many boundaries; causal top-k rows | sparse prefill, often workspace gather | TTFT, workspace, chunk scheduling |
| Decode | small-M GEMM | one/few query scan; incremental tail | paged sparse decode | launch, HBM, persistent cache |
| MTP verify | wider small-M | reuse history; transactional updates | multiple query rows | acceptance × step time |
V4-Flash 的 attention cache 大头仍是 CSA
V4-Flash @ 1,048,576 tokens
V4-Pro ≈ 4.92 GiB. Excludes allocator, workspaces, fragmentation and model state.
“Inference CP”至少有两种完全不同的含义
分 prompt query rows,降低 TTFT / activation;若随后 all-gather 完整 compressed KV,persistent cache 仍可能复制。
真正把 persistent cache 与 history scan 分到 P ranks;但 CSA 需要 exact global top-k + owner routing。
ideal DCP, P=8: ~0.43 GiB / rank + replicated tail
共享 KV 让 DP Attention + EP 变得自然
TP-only · low batch / latency
DPA + EP · sustained concurrency
同一个模型,也要按负载选择并行与 MTP
| Goal | V4-Flash / B200 | V4-Pro / B200 | MTP policy |
|---|---|---|---|
| Low latency | TP4 | TP8 | EAGLE 3 steps, 4 draft tokens |
| Balanced | TP4 · DPA4 · EP4 | TP8 · DPA8 · EP8 | 1 step, 2 draft tokens |
| High throughput | TP4 · DPA4 · EP4 | TP8 · DPA8 · EP8 | disabled |
V4-Flash starting point
V4-Pro starting point
Public recipes, not measurements reproduced here. 04a §6–7
Agent 不是“一篇很长的文档”
burst + pause
decode burst、tool call、等待、resume;cache 温度反复变化。
prefix tree
同一 repo / system prompt 上反复 append、fork、rollback。
long + short
成熟 1M session 与短请求并存,P95/P99 和 cache affinity 更重要。
HiCache 与 HiSparse 解决的不是同一个问题
移动 coherent prefix pages + sidecars,服务 pause/resume、branch 和 eviction。
full index
selected fetch
索引常驻 device;C4 main 在 host;每步只拉 selected entries。
DP Attention 的负载均衡必须理解 cache ownership
useful prefix
holder
queue cost wins
仅按 request count 不够
- uncached suffix
- current context
- expected output
- MTP width
- shared MoE synchronization
04a §5.4 · policy above is a synthesis / future scheduler target.
V4 把机会从“少存一点”扩展到整个 runtime
Capacity
- larger batch
- more resident prefixes
- wider speculative verify
- cheaper tiering
Kernels
- FP4 scan + top-k
- stateful C4/C128 fusion
- paged sparse prefill
- inverse-RoPE fusion
Distributed runtime
- distributed exact top-k
- owner-local attention
- prefix-aware DPA
- transactional MTP state
现在知道什么;还缺什么
Source-backed
- CSA / HCA operator、layer schedule、cache formats
- NSA / DSA 的结构与训练 lineage
- Megatron CP / varlen contract 与 kernel map
- vLLM / SGLang public inference paths and limits
- analytical cache / FLOP / traffic models
Needs GPU validation
- H100 vs B200 crossover points
- per-stage kernel timing and achieved bandwidth
- CP all-gather / global-top-k scaling
- batch × MTP acceptance × throughput surface
- real 4×B200 resident-token envelope
- coding-agent trace P95/P99 and tiering behavior
三个可以带走的判断
MLA 压缩每个 token 的 feature state;CSA/HCA 进一步压缩 sequence state。两者不是同一层级的优化。
CSA = compression + learned retrieval;HCA = heavier compression + dense global。交替结构是在准确率与系统规则性间折中。
真正的新瓶颈是 index scan、stateful compression、distributed top-k、cache ownership;系统价值常通过更大 batch / MTP / prefix reuse 实现。
一句话:V4 把 attention 从“逐 token 的 dense memory”推进到“多尺度、可检索、可分层管理的 learned memory”。
从 slides 回到详细笔记
00 · Foundation
01–02 · Algorithm
Primary sources and pinned implementation revisions are maintained in the linked notes and reading-plan README.