推理与解码系统
模型训练完之后,每一个 token 都要“花钱”生成。这一章讲两件事:怎么选下一个 token(解码策略),以及怎么又快又省地把 token 生出来(KV cache、roofline 性能模型、vLLM 式服务系统、speculative decoding、量化)。这是 Research Scientist 面试里“系统直觉”最集中的地方:面试官很喜欢让你当场算显存、算吞吐、解释为什么 decode 慢。
建议顺序:§1 解码(最基础,常考实现)→ §2 KV cache(必算)→ §3 性能模型(理解后面一切的钥匙)→ §4 服务系统 → §5 speculative decoding(常考证明)→ §6 量化 → §7 其他加速 → §8 场景题(模拟面试)→ §9 练习。
和其他模块的关系:attention / GQA / MLA 的结构细节在 模块 04;test-time compute(best-of-N、self-consistency)在 模块 07;训练侧的显存与并行(ZeRO、TP、PP)在 模块 09;RL rollout 的吞吐(为什么 RLHF 训练时间大部分花在生成上)和本章直接相关,见 模块 06。
§1 · DECODING STRATEGIES解码策略:怎么选下一个 token
语言模型每一步输出一个 logits 向量 \( z \in \mathbb{R}^{V} \)。“解码”就是把这个向量变成一个具体 token 的规则。规则分两类:确定性搜索(greedy、beam)和随机采样(temperature + 各种截断)。面试常问:各方法的数学定义、为什么 beam search 不适合开放式生成、top-p 和 min-p 的区别、多个处理器的顺序,以及“给你 logits 写一个 sampler”。
Greedy 与 Beam search:为什么“最可能的序列”不等于“最好的文本”
Greedy 每步取 argmax;beam search 保留 B 条累计 log-prob 最高的前缀,近似求最大概率序列。它们适合“答案唯一”的任务(翻译、ASR、短答案),但在开放式生成里会产生重复、乏味的文本,因为人类文本本来就不是最高概率的序列。
Greedy and beam search approximate the mode of the sequence distribution; that works for closed-ended tasks but yields repetitive, bland text for open-ended generation, because human text is not the highest-likelihood text.
Greedy:\( y_t = \arg\max_v p(v \mid y_{<t}) \)。最便宜,确定性,但它是“局部最优”:第一步选了概率 0.4 的词,可能导致后面整条序列概率很低,而第一步选 0.35 的词后面反而很顺。
Beam search(宽度 B):每步把 B 条前缀各扩展 V 个候选,得到 \( B\times V \) 条,按累计分数 \( \sum_t \log p(y_t\mid y_{<t}) \) 保留 top-B。遇到 EOS 的序列移到完成列表。由于每多一个 token 分数都会变小(log-prob 为负),beam search 偏爱短序列,所以通常加长度归一化:分数除以 \( |y|^{\alpha} \)(α 常取 0.6–1.0)。
为什么 beam search 在开放式生成里效果差?
- 重复退化(degeneration):一旦模型开始重复一句话,重复会自我强化(重复后的下一句重复概率更高),最大似然搜索正好掉进这个陷阱。Holtzman et al. 2019(The Curious Case of Neural Text Degeneration)展示了 beam search 生成文本的逐 token 概率远高于人类文本,而人类文本的概率起伏很大——人类会说“出人意料”的词。
- 众数不典型:在高维分布里,最高概率的单个序列往往不是“典型”的样本(类似高维高斯的众数在原点,但样本几乎都在一个球壳上)。最可能的回答常常是空洞、通用的。
- 多样性差:B 条 beam 往往只在最后几个词不同。
- 成本:B 倍的 KV cache 和计算。
什么时候还用确定性解码?机器翻译、语音识别、摘要等“输出空间窄”的任务;LLM 评测时为了可复现常用 greedy(temperature=0);推理模型做数学题时,有些模型官方建议用采样(例如 temperature≈0.6)而不是 greedy,因为 greedy 更容易陷入无限重复——具体以各模型的官方推荐为准。
代码:beam search(numpy,带长度归一化)
import numpy as np
def beam_search(step_logprobs, bos, eos, beam=3, max_len=10, alpha=0.7):
"""step_logprobs(prefix) -> (V,) log-probs of next token."""
beams = [([bos], 0.0)] # (tokens, cumulative logprob)
finished = []
for _ in range(max_len):
cand = []
for toks, score in beams:
lp = step_logprobs(toks)
for v in np.argsort(-lp)[:beam]: # only need top-B per beam
cand.append((toks + [int(v)], score + lp[v]))
cand.sort(key=lambda c: -c[1])
beams = []
for toks, score in cand:
(finished if toks[-1] == eos else beams).append((toks, score))
if len(beams) == beam:
break
if not beams:
break
finished += beams
# length normalization: score / len^alpha
return max(finished, key=lambda c: c[1] / (len(c[0]) - 1) ** alpha)[0]
- “beam 越大越好”:在 MT 里 beam 增大到一定程度后 BLEU 反而下降(beam search curse),因为找到了更多“概率高但短/空”的输出。
- “temperature=0 就是采样的一种”:数学上 T→0 的极限是 argmax,但实现上要特判(除以 0)。
- “greedy 一定可复现”:在 GPU 上,batch 组成不同会改变浮点归约顺序,logits 有微小差异,argmax 遇到近似并列时可能翻转——服务端即使 T=0 也不一定 bit 级可复现。
Greedy decoding takes the argmax at each step, and beam search keeps the top-B partial sequences by cumulative log-probability, so both approximate the most likely sequence. That is great for tasks with a narrow output space like translation or speech recognition. For open-ended generation it backfires: the highest-likelihood text tends to be generic and repetitive, and repetition reinforces itself. Holtzman et al. showed human text actually has much lower and more variable per-token probability than beam output. So for chat and creative generation we sample instead, usually with temperature plus a truncation rule like top-p or min-p.
- Why does beam search favor short outputs, and how do you fix it?
思路
每个 token 的 log-prob ≤ 0,累加只会变小,所以越短分数越高、越容易早早生成 EOS。修正:长度归一化(除以 \( |y|^\alpha \))、长度惩罚、或设置最小长度。 - What is the time and memory cost of beam search?
思路
每步做 B 次前向(可以 batch 起来),KV cache 是 B 份;选 top-B 需要在 B×V 个候选中做 top-k,可以先对每条 beam 取 top-B 再合并(B² 个候选),复杂度 \(O(BV + B^2\log B)\)。beam 之间共享前缀,PagedAttention 可以用 copy-on-write 共享 KV block(§4)。 - If greedy decoding loops forever on a math problem, what would you try?
思路
改用采样(适中 temperature,如 0.6–0.8,加 top-p 0.95);加轻微 repetition/presence penalty(但对代码/数学要小心,合法重复也会被惩罚);设置 max tokens;检查是否 chat template 错误导致模型“不知道该停”;从训练侧看可能是 SFT 数据里缺少 EOS 或 RL 奖励鼓励过长。
Temperature:数学定义与极限
\( p_i(T) = \mathrm{softmax}(z/T)_i \)。T<1 让分布更尖(更保守),T>1 让分布更平(更多样);T→0 退化为 argmax,T→∞ 退化为均匀分布。Temperature 不改变 token 的排名。
Temperature divides the logits before softmax: below 1 sharpens, above 1 flattens; it never changes the ranking, only how much mass the tail gets.
$$ p_i(T) = \frac{\exp(z_i/T)}{\sum_j \exp(z_j/T)} $$
两个概率的比值:\( \dfrac{p_i}{p_j} = \exp\!\big(\tfrac{z_i - z_j}{T}\big) \)。T 越小,logit 差距被放大越多,最大的那个占据几乎全部质量;T 越大,差距被压缩,所有 token 越来越接近。
等价视角:\( p(T) \propto p(1)^{1/T} \),即把原始概率取 \(1/T\) 次方再归一化。T=0.5 就是“把概率平方再归一化”。
熵随 T 单调递增(对非均匀的 logits 严格递增)。所以 temperature 是控制“探索/多样性”的一个旋钮:RL rollout 常用 T=1.0 保持与策略一致(采样分布就是要优化的 \(\pi_\theta\);用别的 T 采样会引入 off-policy 偏差),评测常用 T=0 或较低 T。
和训练的联系:知识蒸馏里也有 temperature(Hinton 2015):teacher 和 student 的 logits 都除以 T 后再算 KL,让 student 学到“暗知识”(非最大类之间的相对大小),损失乘 \(T^2\) 来保持梯度尺度。
- “提高 temperature 会让模型更有创造力而不出错”:T 过高会把质量分给成千上万个长尾垃圾 token(vocab 动辄 100k+),一旦采到一个就可能整句崩坏。这正是截断方法(top-k/top-p/min-p)存在的原因。
- “T 改变排名”:不会,只改变形状。截断方法则会直接删除 token。
- 在 RL 里用 T≠1 采样却按 T=1 的 log-prob 计算 ratio:训练和采样分布不一致,是常见 bug。
Temperature just divides the logits by T before the softmax. Equivalently, it raises each probability to the power one over T and renormalizes. As T goes to zero it becomes greedy argmax, and as T goes to infinity it becomes uniform; entropy increases monotonically with T. It never changes which token is ranked first, only how much probability mass leaks into the tail. Because a large vocabulary has a long tail of junk tokens, high temperature is usually paired with a truncation rule like top-p or min-p.
- Why should RL rollouts typically use temperature 1?
思路
Policy gradient 的期望是对 \(\pi_\theta\) 取的。如果用 \(\pi_\theta^{(T)}\) 采样,样本来自另一个分布,若计算 log-prob 时没有用同样的 T,梯度就有偏(off-policy)。有些系统会用 T≠1 然后在 logprob 计算中也除以 T,保持一致。另外截断采样(top-p)在 RL rollout 中也会造成采样分布和训练分布不一致。 - Show that entropy increases with temperature.
思路
令 \(\beta = 1/T\),\(p_i \propto e^{\beta z_i}\),\(\log Z(\beta) = \log\sum e^{\beta z_i}\)。熵 \(H = \log Z - \beta\, \mathbb{E}_p[z]\)。求导:\(dH/d\beta = \mathbb{E}[z] - \mathbb{E}[z] - \beta\,\mathrm{Var}_p(z) = -\beta\,\mathrm{Var}_p(z) \le 0\)(用到 \(d\log Z/d\beta = \mathbb{E}[z]\),\(d\mathbb{E}[z]/d\beta = \mathrm{Var}(z)\))。所以 H 随 β 递减,即随 T 递增。
截断采样:top-k、top-p (nucleus)、min-p、typical
截断采样把“长尾”token 的概率置零再重新归一化。top-k 保留固定 k 个;top-p 保留累计概率刚好达到 p 的最小集合(大小随分布自适应);min-p 保留概率 ≥ \(p_{\min}\cdot p_{\max}\) 的 token(阈值随模型置信度缩放)。
Truncation zeroes out the tail and renormalizes: top-k keeps a fixed count, top-p keeps the smallest set whose mass reaches p, and min-p keeps tokens whose probability is at least a fraction of the top token's probability.
| 方法 | 保留集合 | 自适应? | 弱点 |
|---|---|---|---|
| top-k | 概率最大的 k 个 | 否 | 分布尖时 k 太大(引入垃圾),分布平时 k 太小(砍掉合理候选) |
| top-p | 按概率降序,累计和首次 ≥ p 的最小前缀 | 是(集合大小随熵变化) | 高 temperature 下分布被拉平,达到 p 需要包含大量尾部 token |
| min-p | \( \{ i : p_i \ge p_{\min}\cdot \max_j p_j \} \) | 是(阈值随置信度变化) | 效果争议:有后续工作质疑其收益被夸大 |
| typical (η) | \( \lvert -\log p_i - H(p) \rvert \) 最小、累计达到 τ 的集合 | 是 | 实现稍复杂;会删掉最可能的 token |
| ε / η-sampling | 概率低于绝对阈值(或与熵相关的阈值)的删掉 | 部分 | 较少用 |
top-p 的例子:概率 [0.5, 0.2, 0.15, 0.1, 0.05],p=0.6。降序累计:0.5(<0.6,继续),0.7(≥0.6,停)。保留前两个,重新归一化为 [0.714, 0.286]。注意“越过 p 的那个 token 也要保留”,否则 p=0.6 时只剩一个 token。
min-p 的直觉(Nguyen et al., arXiv 2407.01082, ICLR 2025):模型很确定时(\(p_{\max}=0.9\)),阈值 \(0.1\times0.9=0.09\),几乎只留第一名;模型不确定时(\(p_{\max}=0.1\)),阈值 0.01,保留很多候选。作者声称它在高 temperature 下比 top-p 更能兼顾创造性与连贯性。但 后续工作(arXiv 2506.13681)对其实验和结论提出了质疑。面试中提到 min-p 时,给出定义和直觉,再说一句“its claimed gains have been debated”会显得很成熟。
typical sampling(Meister et al. 2022):信息论动机——人类倾向于让每个词携带“接近期望信息量”的信息,所以保留 surprisal \(-\log p_i\) 接近熵 \(H\) 的 token。
代码:PyTorch 批量 sampler(temperature → top-k → top-p → min-p)
import torch
def sample_logits(logits, temperature=1.0, top_k=0, top_p=1.0, min_p=0.0):
"""logits: (B, V). Returns next token ids (B,)."""
if temperature == 0:
return logits.argmax(dim=-1)
logits = logits / temperature
if top_k > 0:
kth = torch.topk(logits, top_k, dim=-1).values[..., -1, None] # (B,1)
logits = logits.masked_fill(logits < kth, float('-inf'))
if top_p < 1.0:
sorted_logits, idx = torch.sort(logits, descending=True, dim=-1)
probs = sorted_logits.softmax(dim=-1)
cum = probs.cumsum(dim=-1)
remove = (cum - probs) >= top_p # mass before this token already >= p
sorted_logits = sorted_logits.masked_fill(remove, float('-inf'))
logits = torch.full_like(logits, float('-inf')).scatter(-1, idx, sorted_logits)
if min_p > 0.0:
probs = logits.softmax(dim=-1)
thresh = min_p * probs.max(dim=-1, keepdim=True).values
logits = logits.masked_fill(probs < thresh, float('-inf'))
probs = logits.softmax(dim=-1)
return torch.multinomial(probs, num_samples=1).squeeze(-1)
要点:(1) 用 cum - probs(该 token 之前的累计质量)判断,保证越过 p 的 token 被保留,也保证第一名永远保留;(2) 排序后再 scatter 回原顺序;(3) 用 -inf 而不是 0 屏蔽,softmax 后自然为 0;(4) top-k 中并列时 < kth 可能保留多于 k 个,可以接受。等价 numpy 版本已用断言测试(包括 top-k=2 时经验频率 ≈ 0.714/0.286)。
- top-p 实现时用
cum > p删除——会把越过 p 的那个 token 删掉,p 很小时可能把所有 token 都删光(必须保证至少保留一个)。 - 忘记在截断后重新归一化(用 -inf + softmax 自动完成)。
- 认为 top-p 和 temperature 可交换:先 temperature 再 top-p,与先 top-p 再 temperature,保留集合不同(temperature 改变了形状,因此改变了累计和)。
Top-k keeps a fixed number of candidates, which is brittle because the right number depends on how peaked the distribution is. Top-p, or nucleus sampling, keeps the smallest set of tokens whose cumulative probability reaches p, so the candidate set grows when the model is uncertain and shrinks when it's confident. Min-p sets the threshold relative to the top token: keep anything with probability at least min-p times the max probability, which scales the cutoff with model confidence and is claimed to work better at high temperature, although those gains have been debated. In all cases you mask the rest to minus infinity and renormalize with a softmax.
- Is top-p sampling with p=1 and T=1 an unbiased sample from the model?
思路
是的,什么都没截断,就是原始分布。只要 p<1 或 k<V,采样分布就偏离模型分布,序列级别的分布也不再等于模型定义的分布(这对 RL、对“用采样估计期望”的场景很重要)。 - What is the complexity of top-p and how can it be made faster?
思路
全排序 \(O(V\log V)\),V~150k 时每步都排序在 GPU 上不便宜。优化:先 top-k(如 k=1000)预过滤再做 top-p;或者基于阈值的二分/拒绝采样式 kernel(例如 FlashInfer 提供不排序的 sampling kernel)。 - Construct a case where top-k is bad and top-p is good.
思路
k=50:在“The capital of France is”之后分布极尖(Paris 0.97),top-k 仍保留 50 个,可能采到垃圾;在“My favorite food is”之后分布平坦(上千个合理选项),top-k=50 又过度截断。top-p=0.9 在前者只保留 1 个,后者保留数百个。
重复惩罚:repetition / frequency / presence penalty
repetition penalty(CTRL 式)是乘法:对出现过的 token,正 logit 除以 θ、负 logit 乘以 θ;frequency penalty 按出现次数线性减 logit;presence penalty 只要出现过就减一个常数。
Repetition penalty rescales logits of seen tokens multiplicatively; frequency penalty subtracts proportional to the count; presence penalty subtracts a constant once a token has appeared.
设 token i 在已生成文本(有的实现包含 prompt)中出现了 \(c_i\) 次:
- Repetition penalty(CTRL, Keskar et al. 2019,θ≈1.2):\( z_i \leftarrow z_i/\theta \) 若 \(z_i>0\),否则 \( z_i \leftarrow z_i\cdot\theta \)(\(c_i>0\) 时)。为什么区分正负?如果对负 logit 也除以 θ,它会变大(更接近 0),反而提高了概率。
- Frequency penalty(OpenAI API 风格):\( z_i \leftarrow z_i - \alpha_{\text{freq}}\, c_i \)。出现越多惩罚越重。
- Presence penalty:\( z_i \leftarrow z_i - \alpha_{\text{pres}}\,\mathbb{1}[c_i>0] \)。鼓励引入新话题。
- no_repeat_ngram_size:硬性禁止重复出现某个 n-gram(常见于摘要任务)。
注意:代码、数学、JSON 天然需要重复(变量名、括号、数字),惩罚会伤害正确性。推理模型通常不建议使用重复惩罚。
代码(numpy,已测试)
import numpy as np
def apply_penalties(logits, prev_ids, rep=1.0, freq=0.0, pres=0.0):
logits = logits.copy()
ids, counts = np.unique(np.asarray(prev_ids, dtype=int), return_counts=True)
if len(ids):
l = logits[ids]
logits[ids] = np.where(l > 0, l / rep, l * rep) # CTRL-style
logits[ids] -= freq * counts + pres * (counts > 0) # OpenAI-style
return logits
lg = np.array([2.0, -1.0, 0.5])
assert np.allclose(apply_penalties(lg, [0, 1, 1], rep=2.0), [1.0, -2.0, 0.5])
assert np.allclose(apply_penalties(lg, [0, 1, 1], freq=0.5, pres=1.0), [0.5, -3.0, 0.5])
There are three common penalties. The CTRL-style repetition penalty divides positive logits and multiplies negative logits of previously seen tokens by a factor like 1.2, so it always lowers their probability. The frequency penalty subtracts a value proportional to how many times the token has appeared, and the presence penalty subtracts a constant once it has appeared at all. They help with loops in open-ended text, but they can hurt code and math, where legitimate repetition is everywhere.
约束解码(Constrained / grammar-guided decoding)
每一步根据语法(正则、JSON schema、CFG)的当前状态算出“合法 token 集合”,把非法 token 的 logit 设为 −∞,从而保证输出 100% 符合格式。
At each step, compute the set of tokens that keep the output a valid prefix under a grammar, and mask everything else to minus infinity.
原理:把正则/JSON schema 编译成有限状态机(FSM),CFG 则用下推自动机。难点在于 token ≠ 字符:一个 token 可能跨越多个语法状态。Willard & Louf 2023(Outlines)的做法是预先为每个 FSM 状态建立“合法 token 索引”,解码时 O(1) 查表得到 mask。后续系统(如 XGrammar、llguidance)进一步加速 CFG 的情形。
代价与陷阱:
- 分布扭曲:mask 后重新归一化,得到的是“局部条件化”的分布,并不等于“模型分布在满足约束的序列上的条件分布”(后者需要考虑未来能否完成)。模型可能被逼进一个它本来不会走的分支,导致内容质量下降。
- Tokenization 边界:强制的 token 切分可能和训练时的自然切分不一致(token healing 问题)。
- 格式过严时,可以让模型先自由“思考”,最后一段再加约束。
Constrained decoding compiles a regex, JSON schema or grammar into an automaton, and at each step masks out every token that would make the output an invalid prefix. The engineering trick is precomputing, for each automaton state, which vocabulary tokens are allowed, since tokens don't align with grammar symbols. It guarantees well-formed output, but it's a greedy, local projection, so it can distort the distribution and push the model into awkward continuations; that's why people often let the model reason freely first and constrain only the final answer.
Contrastive decoding 及其他变体(加分)
Contrastive decoding 用“大模型 log-prob − 小模型 log-prob”打分,并只在大模型认为足够可能的 token 中选,放大大模型相对小模型“额外懂的东西”。
Contrastive decoding scores tokens by expert minus amateur log-probability, restricted to tokens the expert finds plausible.
Li et al. 2022:\( \text{score}(i) = \log p_{\text{exp}}(i) - \log p_{\text{ama}}(i) \),候选限制在 \( \{i: p_{\text{exp}}(i) \ge \alpha \max_j p_{\text{exp}}(j)\} \)(这个 plausibility 约束和 min-p 的形式一样)。直觉:重复、通用的错误小模型也会犯,相减把它们抵消。相关思想:DoLa(对比同一模型的不同层)、classifier-free guidance 用于 LM、logit 算术(\(z_{\text{base}} + \lambda (z_{\text{tuned}} - z_{\text{ref}})\))。
Self-consistency / best-of-N 也算“解码策略”,但属于 test-time compute,见 模块 07。
Contrastive decoding runs a big expert model and a small amateur model, and picks tokens that maximize the difference in their log-probabilities, but only among tokens the expert already considers plausible. The idea is that generic failure modes like repetition are shared by both models and cancel out, while what's left is what the big model knows beyond the small one.
Logits 处理器的顺序与一个完整 sampler
常见顺序:原始 logits → 约束 mask / 重复惩罚 → temperature → 截断(top-k → top-p / min-p)→ softmax → 采样。顺序不同结果不同,不同框架的默认顺序也不完全一样,需要查文档。
A typical pipeline is: grammar masks and penalties, then temperature, then truncation, then softmax and sample; order matters and differs slightly across libraries.
为什么惩罚放在 temperature 之前?惩罚是在“logit 尺度”上定义的(比如减 0.5),如果先除以 T 再惩罚,惩罚的有效强度就被 T 改变了。为什么截断放在 temperature 之后?这样 temperature 能影响保留集合(高 T 让 top-p 保留更多 token),这是多数实现(如 Hugging Face 的 warper 列表、vLLM 的 sampler)采用的方式;min-p 论文的一个卖点正是“在高 T 下依然稳定”,这依赖于它在 temperature 之后计算阈值时仍是相对阈值。
一个可以在白板上写的完整 numpy sampler(已测试,含断言):
def sample_next(logits, prev_ids=(), temperature=1.0, top_k=0, top_p=1.0,
min_p=0.0, rep=1.0, freq=0.0, pres=0.0, rng=None):
rng = rng or np.random.default_rng()
logits = apply_penalties(np.asarray(logits, float), prev_ids, rep, freq, pres)
if temperature == 0: # greedy
return int(np.argmax(logits))
logits = logits / temperature
logits = top_k_filter(logits, top_k) # np.where(logits < kth, -inf, logits)
logits = top_p_filter(logits, top_p) # keep if (cum - p_i) < top_p
logits = min_p_filter(logits, min_p) # keep if p_i >= min_p * p_max
probs = softmax(logits)
return int(rng.choice(len(probs), p=probs))
I'd apply hard constraints and repetition penalties on the raw logits first, since those are defined on the logit scale. Then divide by temperature, then apply truncation like top-k and top-p, so temperature can influence which tokens survive. Finally softmax and sample with a multinomial. Libraries differ slightly in their default ordering, so when results look odd, the processor order is one of the first things I'd check.
§2 · KV CACHEKV cache:推理显存的头号消耗
KV cache 是 LLM 推理最核心的数据结构。面试几乎必考:为什么它有效、prefill 与 decode 的区别、显存公式与数值、怎么压缩(GQA/MQA/MLA/量化/驱逐)。请把公式和两个数字(LLaMA-2-7B 每 token 512 KiB、4k 上下文 2 GiB)背下来。
KV cache 为什么成立:因果注意力让过去的 K、V 不再改变
在 causal decoder 里,位置 t 的 K、V 只依赖于 token \(1..t\),后面生成新 token 不会改变它们。所以把每层的 K、V 存下来,每步只需为新 token 计算 q、k、v,把每步 attention 从“重算整个序列 \(O(t^2)\)”降到 \(O(t)\)。
With causal attention, keys and values of past positions never change, so we cache them per layer and only compute projections for the new token, turning each decoding step from quadratic recomputation into linear work.
推导:第 \(\ell\) 层位置 i 的隐藏状态 \(h_i^{(\ell)}\) 只依赖 \(x_{1..i}\)(因果 mask 保证)。于是 \(k_i^{(\ell)} = W_K h_i^{(\ell-1)}\)、\(v_i^{(\ell)} = W_V h_i^{(\ell-1)}\) 一旦算出就固定。生成第 t+1 个 token 时,新 query \(q_{t+1}\) 需要和所有 \(k_{1..t+1}\) 做点积,加权求和 \(v_{1..t+1}\):
$$ o_{t+1} = \sum_{i=1}^{t+1} \mathrm{softmax}_i\!\Big(\frac{q_{t+1}^\top k_i}{\sqrt{d_h}}\Big)\, v_i $$旧的 \(k_i, v_i\) 直接从 cache 读取。注意 Q 不需要缓存:过去位置的 query 只用于计算过去位置的输出,而过去位置的输出已经算完了。
计算量对比(生成 n 个 token):无 cache 时第 t 步要对长度 t 的序列做完整前向,总共 \(\sum_t O(t\cdot N_{\text{params}}) = O(n^2 N)\);有 cache 时每步只对 1 个 token 做线性层(\(O(N)\))加上 attention 读 t 个 KV(\(O(t\,d\,L)\)),总共 \(O(nN + n^2 dL)\)。
为什么双向模型(BERT)不能这样做?新 token 会改变所有旧位置的表示。为什么 RNN 不需要 KV cache?RNN 的“cache”就是固定大小的隐藏状态;transformer 的“状态”随长度线性增长——这正是 linear attention / SSM 想解决的(见模块 04)。
代码:带 KV cache 的注意力 + 测试 “有 cache 生成 == 无 cache 生成”
import torch, torch.nn as nn
class CachedSelfAttention(nn.Module):
def __init__(self, d, n_heads):
super().__init__()
self.h, self.dh = n_heads, d // n_heads
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.o = nn.Linear(d, d, bias=False)
def forward(self, x, cache=None):
# x: (B, T, d) new tokens; cache: dict with 'k','v' (B, H, S, dh) or None
B, T, _ = x.shape
q, k, v = self.qkv(x).split(self.h * self.dh, dim=-1)
q, k, v = (t.view(B, T, self.h, self.dh).transpose(1, 2) for t in (q, k, v))
if cache is not None and 'k' in cache:
k = torch.cat([cache['k'], k], dim=2)
v = torch.cat([cache['v'], v], dim=2)
if cache is not None:
cache['k'], cache['v'] = k, v
S = k.shape[2]
# query i (absolute pos S-T+i) may attend keys 0..S-T+i
mask = torch.ones(T, S, dtype=torch.bool, device=x.device).tril(diagonal=S - T)
att = (q @ k.transpose(-2, -1)) / self.dh ** 0.5
att = att.masked_fill(~mask, float('-inf')).softmax(dim=-1)
y = (att @ v).transpose(1, 2).reshape(B, T, -1)
return self.o(y)
@torch.no_grad()
def generate(model, prompt, n_new, use_cache=True):
ids = prompt
if not use_cache:
for _ in range(n_new):
nxt = model(ids)[:, -1].argmax(-1, keepdim=True)
ids = torch.cat([ids, nxt], dim=1)
return ids
caches = [dict() for _ in model.attn]
logits = model(ids, 0, caches) # prefill: whole prompt at once
for _ in range(n_new):
nxt = logits[:, -1].argmax(-1, keepdim=True)
ids = torch.cat([ids, nxt], dim=1)
logits = model(nxt, ids.shape[1] - 1, caches) # decode: one token, absolute position
return ids
# test (TinyLM = embeddings + positional embeddings + blocks using CachedSelfAttention):
# assert torch.equal(generate(m, p, 20, use_cache=False), generate(m, p, 20, use_cache=True))
三个最容易写错的点:(1) mask 的偏移:T 个新 query 对 S 个 key,用 tril(diagonal=S-T);decode 时 T=1,mask 全为 True;(2) 位置编码必须用绝对位置 start(RoPE 同理:新 token 的旋转角度是它的绝对位置);(3) prefill 一次处理整个 prompt。等价的 numpy 实现(2 层、4 头)已验证:生成序列逐 token 相同,且 logits 在 1e-8 内一致。
- “KV cache 也缓存 Q”:不需要。
- “有了 KV cache,decode 每步就是 O(1)”:线性层是 O(1)(相对序列长度),但 attention 仍要读全部 t 个 KV,长上下文下这部分会主导。
- “KV cache 只是加速,不影响结果”:数学上等价,但数值上(bf16 下 prefill 一次算 vs 逐个算的归约顺序不同)可能有微小差异。
In a causal decoder, the key and value at position i depend only on tokens up to i, so they never change once computed. The KV cache stores them per layer and head, and at each decoding step we only run the new token through the network, compute its query, key and value, append the new key and value, and attend over the cache. We don't need to cache queries because past queries are only used for past outputs. This turns generation from quadratic recomputation into linear work per step, but it trades compute for memory: the cache grows linearly with sequence length and batch size and becomes the main memory bottleneck in serving.
- How does RoPE interact with the KV cache?
思路
通常缓存的是已经旋转过的 K(按其绝对位置旋转),新 query 按它的绝对位置旋转,点积自然只依赖相对位置。如果要做 KV 驱逐/位置平移(如 StreamingLLM),需要注意缓存的 K 里已编码了原始位置;StreamingLLM 选择缓存未旋转的 K、按 cache 内位置重新施加 RoPE。 - Why can't you use a KV cache for a bidirectional encoder?
思路
新 token 会被旧 token attend 到,所有旧位置的表示(以及更高层的 K、V)都会改变,缓存失效。
Prefill vs Decode:两个性质完全不同的阶段
Prefill 并行处理整个 prompt(大矩阵乘,compute-bound),产出第一个 token 和 KV cache,决定 TTFT;decode 每步只处理 1 个新 token(矩阵-向量乘,memory-bandwidth-bound),决定 TPOT。
Prefill processes the whole prompt in parallel and is compute-bound, setting time-to-first-token; decode generates one token at a time, is memory-bandwidth-bound, and sets time per output token.
| Prefill | Decode | |
|---|---|---|
| 每个序列一次处理的 token | 整个 prompt(数百到数十万) | 1(speculative 时为 γ+1) |
| 主要运算 | GEMM:(T×d)·(d×d) | GEMV:(1×d)·(d×d)(batch 后为 (B×d)·(d×d)) |
| 瓶颈 | 算力(FLOPs) | 显存带宽(读权重 + 读 KV cache) |
| 对应延迟指标 | TTFT(time to first token) | TPOT / ITL(每个输出 token 的间隔) |
| attention | \(O(T^2)\),FlashAttention | 每步 \(O(t)\),读 cache;长上下文用 FlashDecoding(split-KV) |
| GPU 利用率 | 可以很高(MFU 40–60%) | batch 小时很低(大部分时间等数据) |
这种不对称是理解 continuous batching、chunked prefill、PD 分离、speculative decoding 的钥匙:decode 阶段 GPU 的算力大量闲置,所以要么把很多请求的 decode 合在一起(batching),要么一次验证多个 token(speculative),要么把 prefill 的“算力活”掺进来(chunked prefill),要么把两种活分到不同机器(disaggregation)。
Inference has two phases. Prefill runs the entire prompt through the model in one parallel pass; it's dominated by large matrix multiplies, so it's compute-bound and determines time to first token. Decode then generates one token per step per sequence; each step is basically matrix-vector work, so we spend most of the time streaming weights and KV cache from HBM, which makes it memory-bandwidth-bound and determines the inter-token latency. Most serving optimizations are about exploiting the idle compute during decode.
KV cache 显存公式与必背数字
\( \text{KV bytes} = 2 \times L \times n_{kv} \times d_{\text{head}} \times \text{bytes} \times \text{seq} \times \text{batch} \)。LLaMA-2-7B(MHA,bf16)每 token 512 KiB,4k 上下文 2 GiB/序列;同形状换成 8 个 KV 头(GQA)就是 128 KiB/token,0.5 GiB/4k。
KV bytes equal two (K and V) times layers times KV heads times head dim times bytes per element times sequence length times batch; LLaMA-2-7B in bf16 needs half a megabyte per token, two gigabytes for a 4k context.
每一层、每个 KV 头、每个 token 存一个长 \(d_{\text{head}}\) 的 K 向量和一个 V 向量:
$$ M_{KV} = \underbrace{2}_{K,V}\cdot L \cdot n_{kv} \cdot d_{\text{head}} \cdot b \cdot S \cdot B $$MHA 时 \(n_{kv}\, d_{\text{head}} = d_{\text{model}}\),公式简化为 \(2 L d\, b\, S B\)。
Worked example 1:LLaMA-2-7B(L=32,d=4096,32 头,\(d_h\)=128,MHA,bf16 → b=2):
- 每 token:\(2\times32\times32\times128\times2 = 524{,}288\) B = 512 KiB ≈ 0.5 MB
- 4096 token:\(512\text{ KiB}\times4096 = \) 2 GiB/序列
- 128k token(假设能支持):64 GiB/序列——比 13.5 GB 的权重大近 5 倍!
Worked example 2:同形状 + GQA 8 个 KV 头(即 LLaMA-3-8B 的 KV 配置:L=32,\(n_{kv}\)=8,\(d_h\)=128):
- 每 token:\(2\times32\times8\times128\times2\) = 128 KiB(缩小 4 倍)
- 4k:0.5 GiB;128k:16 GiB
- MQA(1 个 KV 头):16 KiB/token,128k 下 2 GiB
Worked example 3:LLaMA-2/3-70B(L=80,\(n_{kv}\)=8,\(d_h\)=128):每 token \(2\times80\times8\times128\times2\) = 320 KiB,128k 上下文 40 GiB/序列。
Worked example 4:DeepSeek-V2 的 MLA(arXiv 2405.04434):每层每 token 只缓存一个压缩 latent \(c^{KV}\)(\(d_c=512\))和一个共享的解耦 RoPE key(\(d_h^R=64\)),60 层,bf16:\((512+64)\times60\times2\) B ≈ 67.5 KiB/token。论文报告与其 MHA 基线相比 KV cache 降低约 93%。
| 配置(bf16) | 每 token | 4k / 序列 | 128k / 序列 |
|---|---|---|---|
| 7B MHA(32 KV 头) | 512 KiB | 2 GiB | 64 GiB |
| 7B 形状 GQA-8 | 128 KiB | 0.5 GiB | 16 GiB |
| 7B 形状 MQA | 16 KiB | 64 MiB | 2 GiB |
| 70B GQA-8(L=80) | 320 KiB | 1.25 GiB | 40 GiB |
| 13B MHA(L=40,d=5120) | 800 KiB | 3.125 GiB | 100 GiB |
KV 与权重的比较:权重是“所有请求共享的一份”,KV 是“每个请求一份且随长度增长”。batch × 长度一大,KV 就会超过权重,决定了能同时服务多少请求——也就决定了吞吐。
代码:KV 计算器(已运行,数字与上表一致)
def kv_bytes(L, n_kv, d_head, seq, batch, bytes_per=2):
return 2 * L * n_kv * d_head * bytes_per * seq * batch
GiB = 1024**3
print(kv_bytes(32, 32, 128, 1, 1) / 1024) # 512.0 KiB per token (LLaMA-2-7B)
print(kv_bytes(32, 32, 128, 4096, 1) / GiB) # 2.0 GiB
print(kv_bytes(32, 8, 128, 131072, 1) / GiB) # 16.0 GiB (GQA-8, 128k)
print(kv_bytes(80, 8, 128, 131072, 1) / GiB) # 40.0 GiB (70B, 128k)
- 忘了乘 2(K 和 V 各一份)——最常见的错误。
- 用 \(n_{\text{heads}}\) 而不是 \(n_{kv}\):GQA 模型的 query 头数和 KV 头数不同。
- 混用 GB(10⁹)和 GiB(2³⁰):面试时说清楚即可,数量级正确最重要。
- 忽略实际系统的额外开销:激活、CUDA context、碎片、PagedAttention 的 block 余量等,实际可用比理论少 10–20%。
The KV cache size is two, for keys and values, times the number of layers, times the number of KV heads, times the head dimension, times bytes per element, times sequence length, times batch size. For LLaMA-2-7B in bf16, that's 2 times 32 layers times 4096 times 2 bytes, which is 512 kilobytes per token, so a 4k context costs about 2 gigabytes per sequence and a 128k context would be 64 gigabytes, far more than the 13 gigabytes of weights. With grouped-query attention and 8 KV heads, it drops by 4x to 128 kilobytes per token. That's why KV cache, not weights, usually limits batch size and throughput.
- Does the KV cache size depend on the number of query heads or the FFN size?
思路
都不依赖。只依赖 L、\(n_{kv}\)、\(d_h\)、精度、长度、batch。这就是为什么 GQA/MQA/MLA 专门针对 KV 头动手:它们几乎不改变参数量和 FLOPs,却大幅降低 KV。 - For a sliding-window attention model, how does the formula change?
思路
seq 替换为 \(\min(S, W)\)。混合模型(例如部分层全局、部分层滑窗,Gemma 2/3、Mistral 7B 的 SWA)按层分别计算后求和。
压缩 KV:GQA / MQA / MLA、KV 量化、驱逐(H2O、StreamingLLM)
四条路线:减少 KV 头(MQA/GQA)、低秩压缩(MLA)、降低精度(FP8/INT4/2-bit KV 量化)、少存 token(滑窗、attention sink + 最近窗口、按重要性驱逐)。前三者在架构或数值上近似无损,最后一条会丢信息。
You can shrink the KV cache by sharing KV heads, compressing to a low-rank latent, quantizing it, or evicting tokens; eviction is the only one that throws information away outright.
1. 共享 KV 头(结构详见模块 04):MQA(Shazeer 2019)所有 query 头共享 1 组 K/V;GQA(Ainslie et al. 2023)分成 g 组。KV 缩小 \(n_h/n_{kv}\) 倍,decode 时读的字节同比例减少。GQA 论文还提出可以从 MHA checkpoint 把 KV 头 mean-pool 后少量继续训练(uptraining)。
2. MLA(DeepSeek-V2):把 K、V 投影成一个低维 latent \(c_t = W_{DKV} h_t\)(512 维)缓存;推理时上投影矩阵可以“吸收”进 \(W_Q\) 和 \(W_O\),所以不必显式重建完整 K、V。因为 RoPE 与这种矩阵吸收不兼容,额外缓存一个小的共享 RoPE key(64 维)。
3. KV 量化:FP8 KV cache 在 vLLM / TensorRT-LLM 等引擎中常见;更激进的如 KIVI(arXiv 2402.02750)做 2-bit:观察到 K 有明显的通道(channel)方向离群值,适合按通道量化;V 适合按 token 量化,并把最近一小段 token 保持全精度。
4. 驱逐(eviction):
- StreamingLLM(Xiao et al., arXiv 2309.17453):发现 attention sink——大量注意力权重落在最开头几个 token 上(softmax 必须把质量分到某处,初始 token 对所有位置可见,于是成为“垃圾桶”)。单纯滑窗丢掉开头几个 token 会让模型崩溃;保留前 4 个 sink token + 最近窗口即可稳定生成很长的流。注意:这让模型能持续生成,并不让它记住窗口外的内容。
- H2O(Zhang et al., arXiv 2306.14048):“Heavy Hitter”——少数 token 累计获得大部分注意力;保留 heavy hitters + 最近 token,驱逐其余。
- 其他:SnapKV、PyramidKV 等基于 prompt 末尾观察窗口挑选重要 KV。风险:后续问题可能恰好需要被驱逐的信息(needle-in-a-haystack 类任务掉点明显)。
| 方法 | 压缩比(典型) | 需要训练? | 信息损失 |
|---|---|---|---|
| GQA-8(从 32 头) | 4× | 预训练时决定(或 uptrain) | 架构上的容量折中 |
| MLA | 约 10× 以上(vs MHA) | 是 | 低秩假设 |
| FP8 KV | 2× | 否 | 很小 |
| 2-bit KV(KIVI) | ~8×(扣除 scale 开销后略低) | 否 | 小到中 |
| sink + 窗口 / H2O | 任意(固定预算) | 否 | 丢弃 token,长程检索变差 |
There are four levers. Architecturally, MQA and GQA share key-value heads across query heads, which cuts the cache by the ratio of query heads to KV heads; DeepSeek's MLA goes further by caching a low-rank latent per token and absorbing the up-projections into the query and output matrices. Numerically, we can quantize the cache to FP8 or even 2 bits, as in KIVI, which quantizes keys per channel and values per token. Finally, we can evict tokens: StreamingLLM keeps a few initial attention-sink tokens plus a recent window, and H2O keeps the heavy-hitter tokens that receive most attention. Eviction is the only lossy-by-design option and tends to hurt long-range retrieval.
- Why do attention sinks exist?
思路
Softmax 权重和为 1,当一个头“不想关注任何东西”时,必须把质量放到某个地方;开头的 token 对所有后续位置都可见,训练中成为固定的“垃圾桶”。缓解方法:训练时加入可学习的 sink token、或给 softmax 加一个“+1”(off-by-one softmax / gated attention 等)。 - Why are keys harder to quantize than values?
思路
K 的某些通道(维度)系统性地数值很大(与 RoPE 和离群特征有关),per-token 量化时这些通道会撑大 scale,压扁其他通道;所以 K 按通道量化。V 没有这种固定通道离群,且 attention 输出是 V 的加权平均,误差按 token 平均,per-token 量化即可。
§3 · PERFORMANCE MODEL性能模型:roofline、带宽瓶颈与延迟指标
这一节给你一个“信封背面”的计算框架:只用参数量、batch、上下文长度、GPU 的 FLOPs 和带宽,就能估出 TTFT、每 token 延迟和吞吐。面试官问“为什么 decode 慢”“batch 加大有什么用”“这个模型在 H100 上能跑多少 tokens/s”时,答案全部来自这里。
Arithmetic intensity 与 roofline
算术强度 = FLOPs / 从显存搬运的字节数。GPU 的“脊点”= 峰值 FLOPs / 峰值带宽(H100 SXM 约 295 FLOP/B,A100 约 153 FLOP/B)。强度低于脊点就是 memory-bound,高于就是 compute-bound。
Arithmetic intensity is FLOPs per byte moved; below the GPU's ridge point of peak FLOPs over bandwidth, a kernel is memory-bound, above it compute-bound.
Roofline 模型:可达性能 \( = \min(\text{peak FLOPs},\ \text{AI} \times \text{BW}) \)。画在 log-log 图上:左边是斜线(带宽限制),右边是水平线(算力限制),转折点就是脊点。
参考规格(dense,非稀疏;数字为厂商标称的近似值):
| GPU | BF16 dense | HBM 带宽 | 显存 | 脊点 FLOP/B |
|---|---|---|---|---|
| A100 80GB SXM | ≈312 TFLOPS | ≈2.0 TB/s | 80 GB | ≈153 |
| H100 SXM | ≈989 TFLOPS | ≈3.35 TB/s | 80 GB | ≈295 |
(H100 的 FP8 dense 约为 BF16 的 2 倍;厂商宣传页上常见的更大数字通常是 2:4 稀疏下的值。H200 主要提升了显存容量与带宽。具体以 NVIDIA 官方 datasheet 为准。)
线性层的算术强度:输入 \(X\in\mathbb{R}^{B\times d}\),权重 \(W\in\mathbb{R}^{d\times d}\),bf16。FLOPs \(=2Bd^2\);字节 \(\approx 2d^2\)(权重)\(+ 4Bd\)(输入输出)。当 \(B \ll d\) 时:
$$ \text{AI} \approx \frac{2Bd^2}{2d^2} = B \ \text{FLOP/byte} $$也就是说,batch 里有多少个 token,线性层的算术强度就是多少(bf16 权重下)。d=4096 时实测:B=1 → 1.0,B=64 → 62,B=256 → 228。要在 H100 上让线性层 compute-bound,需要约 300 个 token 同时参与这次矩阵乘。
- Prefill:一个 2k 的 prompt 就提供了 2048 个 token → AI 远高于脊点 → compute-bound。
- Decode:每个序列每步只有 1 个 token → AI = batch size → 除非 batch 几百,否则 memory-bound。
- Decode 的 attention 部分:每个序列读自己的 KV(不能跨序列共享),AI ≈ 1(MHA)到 \(n_h/n_{kv}\)(GQA,因为一个 KV 头被多个 query 头复用),无论 batch 多大都是 memory-bound。
Arithmetic intensity is FLOPs per byte moved from memory. The roofline says achievable throughput is the minimum of peak compute and intensity times bandwidth, and the ridge point for an H100 is roughly 300 FLOPs per byte in bf16. For a linear layer with bf16 weights, intensity is roughly the number of tokens in the batch, because every weight byte is used for two FLOPs per token. Prefill has thousands of tokens, so it's compute-bound; decode has one token per sequence, so unless the batch is in the hundreds, it's bandwidth-bound. And decode attention is bandwidth-bound regardless of batch, because each sequence reads its own KV cache.
推导:batch=1 时 tokens/s ≈ 带宽 / 权重字节
batch=1 decode 每步必须把全部权重从 HBM 读一遍(外加 KV),而算力几乎闲置,所以 \( \text{tokens/s} \le \text{BW} / (N \cdot b_w) \)。7B bf16 在 H100 上约 250 tok/s 上限,A100 约 150 tok/s。
At batch one, each decode step must stream all weights from HBM, so tokens per second is bounded by bandwidth over weight bytes: about 250 tokens per second for a 7B bf16 model on an H100.
每步需要的时间(下界):
$$ t_{\text{step}} \gtrsim \max\Big(\underbrace{\frac{N b_w + B\cdot S\cdot m_{kv}}{\text{BW}}}_{\text{memory}},\ \underbrace{\frac{2NB}{\text{FLOPs}}}_{\text{compute}}\Big) $$其中 N 为参数量,\(b_w\) 为每参数字节,\(m_{kv}\) 为每 token 的 KV 字节,S 为当前上下文长度。batch=1 且上下文不长时,KV 项可忽略,compute 项比 memory 项小两个数量级:
$$ \text{tokens/s} \approx \frac{\text{BW}}{N b_w} $$数值(LLaMA-2-7B,6.74B 参数,bf16 → 13.5 GB):A100:2.0 TB/s ÷ 13.5 GB ≈ 150 tok/s;H100:3.35 TB/s ÷ 13.5 GB ≈ 250 tok/s。实际实现能达到峰值带宽的 60–85%,所以实测更低。这个公式立刻告诉你两个优化方向:减小权重字节(INT4 权重 → 读的字节约为 1/4 → 上限约 4×;这正是 weight-only 量化对小 batch 推理有效的原因)和增加每次读取所产出的 token(batching、speculative decoding)。
Worked example:8B GQA 模型(KV 128 KiB/token),H100,上下文 2048,bf16(理想模型,近似)
| batch | 读取时间 | 计算时间 | 总吞吐 | 每序列速度 | KV 总量 |
|---|---|---|---|---|---|
| 1 | 4.9 ms | 0.02 ms | ≈206 tok/s | ≈206 tok/s | 0.27 GB |
| 16 | 6.1 ms | 0.26 ms | ≈2,600 tok/s | ≈165 tok/s | 4.3 GB |
| 64 | 9.9 ms | 1.0 ms | ≈6,500 tok/s | ≈100 tok/s | 17 GB |
| 128 | 15.0 ms | 2.1 ms | ≈8,500 tok/s | ≈67 tok/s | 34 GB |
读法:(1) batch 从 1 增大到 16,总吞吐涨约 13×,而单序列速度只降 20%——batching 几乎是“免费午餐”;(2) batch 继续增大,KV 读取开始主导(每个序列要读自己的 KV),吞吐增长放缓、单序列延迟变差——吞吐与延迟的权衡;(3) 最终受限于显存能容纳多少 KV。
TTFT 估算:prefill FLOPs ≈ \(2N\cdot T_{\text{prompt}}\)(忽略 attention 二次项)。8B 模型、2048 token、H100 50% MFU:\(2\times8\text{e}9\times2048 / (0.5\times989\text{e}12) \approx 66\) ms。长 prompt 时 attention 的 \(O(T^2)\) 项会变得显著。
代码:一个 15 行的性能估算器
H100 = dict(flops=989e12, bw=3.35e12) # bf16 dense, approximate
def step_time(gpu, n_params, B, ctx, kv_per_tok, wbytes=2):
mem = n_params * wbytes + B * ctx * kv_per_tok # bytes read per decode step
comp = 2 * n_params * B # FLOPs per decode step (no attention)
return mem / gpu['bw'], comp / gpu['flops']
KV8 = 2 * 32 * 8 * 128 * 2 # 8B with GQA-8: 128 KiB/token
for B in [1, 16, 64, 128]:
tm, tc = step_time(H100, 8e9, B, 2048, KV8)
t = max(tm, tc)
print(f'B={B}: {B/t:.0f} tok/s total, {1/t:.0f} tok/s per sequence')
- 用 FLOPs 估算 decode 速度:会得到离谱的 3 万 tok/s(\(989\text{e}12/(2\times7\text{e}9)/2\)…),完全错误。batch=1 decode 永远先看带宽。
- 认为 MoE 的 decode 速度只取决于激活参数:batch=1 时只读被激活的专家,确实接近“激活参数量”;但 batch 大时不同 token 激活不同专家,几乎要读所有专家权重,而且总参数必须放进显存。
- 忽略 KV 读取:长上下文、大 batch 时 KV 读取会超过权重读取。
At batch size one, every decode step has to read all the weights from HBM, but only does two FLOPs per parameter, so the GPU is starved for data. The step time is roughly weight bytes divided by memory bandwidth, so tokens per second is about bandwidth over weight bytes. For a 7B model in bf16 that's 13.5 gigabytes, so about 150 tokens per second on an A100 and 250 on an H100, as an upper bound. This tells you the two levers: shrink the bytes, with weight quantization, or get more tokens per weight read, with batching or speculative decoding.
- At what batch size does decoding become compute-bound on an H100?
思路
只看线性层:AI ≈ B,脊点 ≈ 295,所以 B 约 300 时达到。但 attention 部分的 KV 读取随 B 线性增长且 AI 很低,所以实际整体上很难真正 compute-bound;更常见的限制是 KV 显存先装不下。 - How does tensor parallelism change these numbers?
思路
TP=k 时每张卡只读 1/k 的权重,聚合带宽 k 倍,理想情况下 batch=1 的 tokens/s 提升近 k 倍(70B bf16 在 2×H100 上理想值约 48 tok/s);代价是每层两次 all-reduce 的通信延迟,在小 batch 时这部分是固定开销,所以扩展效率低于线性。
延迟与吞吐指标:TTFT、TPOT/ITL、E2E、throughput、goodput
TTFT = 从请求到第一个 token(排队 + prefill);TPOT / ITL = 后续 token 之间的平均/逐个间隔(decode 速度);E2E ≈ TTFT + TPOT × (输出长度 − 1);吞吐 = 系统每秒总 token 数;goodput = 满足延迟 SLO 的请求的吞吐。
TTFT covers queueing and prefill; TPOT or inter-token latency measures decode speed; throughput counts total tokens per second; goodput counts only requests that meet the latency SLOs.
- TTFT:交互体验最关键(用户等第一个字)。受 prompt 长度、排队、prefill 调度影响。
- TPOT(time per output token)/ ITL(inter-token latency):流式输出的“打字速度”。人类阅读约每秒几个词,所以 20–50 ms/token 通常够用;但推理模型生成上万 token 的思考,TPOT 直接决定总等待时间。
- Throughput(tokens/s 或 requests/s):决定成本($/M tokens)。
- Goodput(DistServe 提出的说法):在满足 TTFT 和 TPOT SLO 的前提下能达到的请求速率。单纯追求吞吐可能让 P99 延迟爆炸。
- 报告时看分位数(P50/P90/P99),而不是平均值。
核心张力:batch 大 → 吞吐高、TPOT 变差;新请求的 prefill 插进来 → 正在 decode 的请求出现卡顿(generation stall)。§4 的各种调度技术都在调和这一张力。
I'd look at four things. Time to first token, which is queueing plus prefill and matters most for interactivity. Time per output token, or inter-token latency, which is the streaming speed and is dominated by decode. Throughput in tokens per second, which drives cost. And goodput, the throughput you get while still meeting latency SLOs on both TTFT and TPOT, reported at P90 or P99, not just averages. Larger batches raise throughput but hurt per-token latency, and that tension drives most scheduling decisions.
§4 · SERVING SYSTEMS服务系统:continuous batching、PagedAttention、prefix caching、PD 分离
现代推理引擎(vLLM、SGLang、TensorRT-LLM 等)由几项关键技术构成。面试常问“vLLM 为什么快”——标准答案是 continuous batching + PagedAttention,再加 prefix caching、chunked prefill,以及在大规模部署中的 PD 分离和并行策略。
Static batching vs Continuous (iteration-level) batching
Static batching 要等 batch 里最长的请求结束才能接新请求;continuous batching(Orca 提出的 iteration-level scheduling)每个 decode step 都重新组 batch:完成的请求立即退出,新请求立即加入。
Static batching waits for the longest request in the batch; continuous batching reschedules at every iteration, evicting finished sequences and admitting new ones immediately.
Static batching 的问题:输出长度差异巨大(有的回答 10 token,有的 2000 token)。短请求完成后它的“槽位”空着(或被 padding 填充)直到最长请求结束,GPU 利用率低;新请求还要排队等整个 batch 结束,TTFT 很差。
Continuous batching(Orca, Yu et al., OSDI 2022):
- 调度粒度从“请求”变成“一次迭代(一个 token step)”。
- 不同序列长度不同,没法拼成一个规整的 attention 张量。Orca 的 selective batching:对线性层(与位置无关)把所有序列的 token 拼平成一个大矩阵一起算;对 attention 按序列分开算(现在用支持变长/分页的 attention kernel 处理)。
- 效果:同一延迟下吞吐提升显著;不需要 padding。
新问题:新请求加入时要做 prefill,一个长 prompt 的 prefill 会让这一步变得很长,所有正在 decode 的请求都被拖慢(TPOT 尖刺)——这引出了 chunked prefill。
With static batching, a batch runs until its longest sequence finishes, so short requests leave idle slots and new requests wait in the queue. Continuous batching, introduced by Orca, schedules at the granularity of a single decoding iteration: after every step, finished sequences leave and waiting requests join. Since sequences have different lengths, the linear layers run on the flattened tokens from all sequences, while attention is computed per sequence with a variable-length kernel. This dramatically improves utilization and throughput without hurting latency.
PagedAttention / vLLM:把操作系统的虚拟内存搬进 KV cache
把每个序列的 KV cache 切成固定大小的 block(如 16 token),通过 block table 把逻辑 block 映射到不连续的物理 block,按需分配。消除了预留最大长度造成的内部碎片和外部碎片,并支持 block 级共享与 copy-on-write。
PagedAttention stores KV cache in fixed-size blocks mapped through a per-sequence block table, like virtual memory pages, eliminating fragmentation and enabling sharing with copy-on-write.
问题:早期系统为每个请求按“最大可能长度”预分配一段连续的 KV 显存。输出长度事先未知 → 大量预留但未使用(内部碎片);不同大小的连续块分配释放后产生空洞(外部碎片)。vLLM 论文(Kwon et al., SOSP 2023, arXiv 2309.06180)测得已有系统的 KV 显存中只有一小部分真正存放了 token 状态,而 vLLM 的浪费接近于零(只有每个序列最后一个未满 block)。
| 操作系统概念 | vLLM 对应 |
|---|---|
| 进程 | 请求 / 序列 |
| 虚拟页 → 物理页(页表) | 逻辑 KV block → 物理 KV block(block table) |
| 按需分页 | 生成写满一个 block 才分配下一个 |
| fork + copy-on-write | parallel sampling / beam search 共享 prompt 的 block,写入时才复制(引用计数) |
| swap 到磁盘 | 显存不足时把 KV 换出到 CPU 内存,或直接丢弃之后重算(recompute) |
代价:attention kernel 必须通过 block table 间接寻址(gather 不连续的 block),kernel 更复杂;block 太小间接寻址开销大,太大内部碎片多(vLLM 默认 16 token)。
为什么这提高吞吐?§3 已说明 decode 吞吐近似随 batch 增长,而 batch 受 KV 显存限制。浪费从大部分降到接近 0 → 同样显存能放下多得多的并发序列 → 吞吐提升(论文报告相对当时的系统有数倍提升)。
示意:block table 的数据结构
BLOCK = 16
free_blocks = list(range(num_physical_blocks))
ref_count = [0] * num_physical_blocks
class Seq:
def __init__(self):
self.block_table = [] # logical block i -> physical block id
self.length = 0
def append_token(seq):
if seq.length % BLOCK == 0: # current block full (or first token)
b = free_blocks.pop(); ref_count[b] = 1
seq.block_table.append(b)
elif ref_count[seq.block_table[-1]] > 1: # shared last block -> copy-on-write
old = seq.block_table[-1]; b = free_blocks.pop(); ref_count[b] = 1
copy_block(old, b); ref_count[old] -= 1
seq.block_table[-1] = b
seq.length += 1 # then write K,V into slot (length-1) % BLOCK
def fork(seq): # parallel sampling: share all blocks
child = Seq(); child.block_table = list(seq.block_table); child.length = seq.length
for b in seq.block_table: ref_count[b] += 1
return child
这是讲解用的示意(伪代码,省略了 copy_block 等细节),不是 vLLM 的真实实现。
PagedAttention applies virtual memory ideas to the KV cache. Instead of reserving a contiguous chunk for the maximum length of each request, the cache is split into fixed-size blocks, say 16 tokens, and each sequence has a block table mapping logical blocks to physical blocks that can live anywhere in GPU memory. Blocks are allocated on demand, so the only waste is the partially filled last block. Because blocks are reference counted, parallel samples or beams can share the prompt's blocks and copy on write. Less waste means more concurrent sequences in the same memory, which directly translates into higher decode throughput.
- What happens when GPU memory runs out mid-generation?
思路
调度器抢占(preempt)部分序列:要么把它们的 KV block swap 到 CPU 内存,要么释放后之后重新 prefill(recompute)。重算对短序列更便宜,swap 对长序列更好。通常按 FCFS 抢占最晚到达的请求。 - What's the tradeoff in choosing block size?
思路
小 block:碎片少、共享粒度细,但 block table 大、kernel 间接寻址开销大、访存不连续;大 block:反之。
Prefix caching 与 RadixAttention(SGLang)
不同请求常共享相同前缀(系统提示、few-shot 示例、多轮对话历史、agent 的工具描述)。把已算过的前缀 KV 保留下来,新请求命中时跳过这部分 prefill。SGLang 用 radix tree(基数树)组织所有缓存的 KV,自动做最长前缀匹配 + LRU 驱逐。
Prefix caching reuses KV blocks for shared prompt prefixes; SGLang's RadixAttention keeps them in a radix tree with longest-prefix matching and LRU eviction.
- vLLM 的 automatic prefix caching:按 block 内容(连同其前缀)计算 hash,相同 hash 的 block 直接复用。
- RadixAttention(SGLang, Zheng et al., arXiv 2312.07104):树的边是 token 序列,节点挂着 KV;请求到来时沿树做最长前缀匹配;显存不够时按 LRU 驱逐叶子节点;调度器优先运行命中前缀更长的请求(cache-aware scheduling)。
- 收益最大的场景:多轮对话(每轮都重复整段历史)、agent(长工具描述 + 不断增长的轨迹)、self-consistency/best-of-N(同一个 prompt 采样 N 次)、RL rollout(GRPO 对一个 prompt 采 G 个回答)。
- 注意:只有完全相同的前缀才能复用(因果注意力下,KV 依赖整个前缀)。在 prompt 开头放时间戳或随机 ID 会让缓存完全失效——prompt 设计的实用经验:静态内容放前面,变化内容放后面。API 提供商的“prompt caching”折扣也基于这个原理。
Many requests share a prefix, like a long system prompt, few-shot examples, or the conversation history in a multi-turn chat. Since the KV cache of a prefix depends only on that prefix, we can keep it around and skip its prefill for later requests, which slashes time to first token and saves compute. vLLM does this by hashing blocks, and SGLang's RadixAttention keeps all cached sequences in a radix tree, does longest-prefix matching, evicts least recently used leaves, and schedules requests to maximize cache hits. It only works for exact prefix matches, so you want static content first in the prompt.
Chunked prefill:把长 prefill 切块,和 decode 混在同一批
把长 prompt 的 prefill 切成固定 token 预算的小块,每个迭代把一个 prefill 块和一批 decode token 拼在一起执行。decode 不再被长 prefill 卡住(TPOT 平稳),同时用 prefill 的算力“填满”decode 闲置的 GPU。
Chunked prefill splits long prompts into chunks and piggybacks decode tokens on each chunk, avoiding decode stalls while using the idle compute of decode steps.
Sarathi-Serve(Agrawal et al., OSDI 2024, arXiv 2403.02310)的两个想法:(1) chunked prefill;(2) stall-free scheduling:每次迭代设定 token 预算(如 512),先放入所有正在 decode 的请求(每个 1 token),剩余预算分给 prefill 块。
为什么有效?decode 步是 memory-bound,算力有余;prefill 块是 compute-bound。两者拼在一起,权重只读一次,同时服务两类工作——相当于 decode 的 token“搭了 prefill 的便车”。
代价:单个长 prompt 的 TTFT 略增(被切成多次迭代,每块都要重新读之前块的 KV);块太小会降低 prefill 效率。现在主流引擎默认或可选开启 chunked prefill。
With plain continuous batching, when a long prompt arrives, its prefill makes that iteration very slow and every ongoing decode stalls. Chunked prefill, from Sarathi-Serve, splits the prefill into fixed-size chunks and builds each iteration under a token budget: first all the decode tokens, then a prefill chunk to fill the rest. Since decode is bandwidth-bound and prefill is compute-bound, mixing them uses the GPU much better, and the inter-token latency stays smooth. The cost is a slightly higher time to first token for long prompts.
Prefill–Decode 分离(PD disaggregation)
把 prefill 和 decode 放在不同的 GPU 实例上,prefill 完成后把 KV cache 通过高速互联传给 decode 实例。两阶段不再互相干扰,可以分别选择并行策略和资源配比,优化 goodput。
Disaggregation runs prefill and decode on separate GPU pools and transfers the KV cache between them, removing interference and letting each phase be provisioned and parallelized independently.
DistServe(Zhong et al., OSDI 2024, arXiv 2401.09670)和 Splitwise(Patel et al., ISCA 2024, arXiv 2311.18677)几乎同时提出。动机:
- 干扰:同一 GPU 上混跑时,prefill 拖慢 decode(TPOT 尖刺),decode 也占用 prefill 的资源(TTFT 变差)。chunked prefill 缓解但不能消除。
- 资源需求不同:prefill 要算力,适合较少的 TP;decode 要带宽和显存容量(装 KV),适合大 batch。Splitwise 还提出 decode 阶段可以用算力较弱但带宽/显存性价比高的硬件。
- 独立扩缩:根据 TTFT 和 TPOT 的 SLO 分别调整 prefill 和 decode 实例的数量比例。
代价:KV 传输。例如 8B GQA 模型 4k prompt 的 KV 是 0.5 GiB,需要 NVLink 或 RDMA 级别带宽才能做到相对 prefill 时间可忽略;长 prompt 时 prefill 计算是 \(O(T)\)~\(O(T^2)\) 而 KV 传输是 \(O(T)\),所以 prompt 越长,传输占比通常越小。大规模部署(尤其是大 MoE 模型)中 PD 分离已经很常见。
| 方案 | 解决什么 | 代价 |
|---|---|---|
| Continuous batching | GPU 槽位空闲、排队 | prefill 干扰 decode |
| Chunked prefill | decode 卡顿 | 长 prompt TTFT 略增;干扰仍在 |
| PD 分离 | 两阶段干扰、资源配比 | KV 传输、更复杂的集群调度 |
Prefill and decode have opposite resource profiles and interfere when colocated: a big prefill stalls decodes, and decodes eat into prefill capacity. DistServe and Splitwise proposed disaggregating them: a prefill pool computes the prompt and ships the KV cache over a fast interconnect to a decode pool. Each pool can then use its own parallelism and batch size, and you can scale the ratio of prefill to decode instances to meet separate TTFT and TPOT SLOs, which they frame as maximizing goodput. The cost is KV transfer bandwidth and more complex orchestration.
推理中的并行:TP、PP、EP、数据并行副本
模型放不下一张卡或需要更低延迟时用 tensor parallel(层内切分,每层 2 次 all-reduce,需要 NVLink,降低单 token 延迟);跨节点时用 pipeline parallel(增加容量,不降低单 token 延迟);MoE 用 expert parallel;吞吐不够就加数据并行副本。
Tensor parallelism splits each layer across GPUs to cut latency, pipeline parallelism adds capacity across nodes, expert parallelism shards MoE experts, and replicas scale throughput.
TP(Megatron 风格):attention 按头切(KV cache 也按头分布在各卡上——GQA 的 KV 头数限制了 TP 度上限,超过时 KV 头要复制);MLP 第一层按列切,第二层按行切,之后 all-reduce。每张卡只读 1/k 的权重和 KV → decode 延迟近似降低 k 倍(减去通信)。
PP:不同层放在不同卡上;batch=1 时只有一张卡在工作,不降低延迟,但可以通过 micro-batch 流水提高吞吐,通信量小,适合跨节点。
EP(MoE):专家分布在不同卡上,token 通过 all-to-all 路由;大规模 MoE 部署(例如 DeepSeek-V3 的推理方案)会结合大 EP + PD 分离。训练侧并行细节见 模块 09。
For inference, tensor parallelism splits each layer across GPUs, so each GPU reads only a fraction of the weights and KV per step, which cuts per-token latency, at the cost of two all-reduces per layer, so it wants NVLink within a node. Pipeline parallelism splits layers across devices; it adds memory capacity with cheap communication but doesn't reduce single-request latency. MoE models add expert parallelism with all-to-all routing. And if you just need more throughput, you add replicas.
§5 · SPECULATIVE DECODINGSpeculative decoding:用便宜的猜测换取并行验证
decode 是 memory-bound 的:让目标模型一次前向处理 γ+1 个 token,几乎和处理 1 个 token 一样快(权重只读一次)。speculative decoding 正是利用这点:小模型(或额外的头)先猜 γ 个 token,大模型一次前向并行验证。关键在于用一个巧妙的拒绝采样规则,输出分布与只用大模型采样完全相同。这一节的证明是高频考点。
算法:draft → verify → accept / resample
draft 模型 q 依次采样 γ 个 token;目标模型 p 一次前向算出这 γ+1 个位置的分布;从左到右以概率 \(\min(1, p(x)/q(x))\) 接受每个 draft token,第一次拒绝时从残差分布 \( \mathrm{norm}(\max(0, p-q)) \) 重新采样并停止;全部接受则从 p 再额外采样 1 个 token。
Draft gamma tokens with the small model, score them all with one target forward pass, accept each with probability min of one and p over q, and on the first rejection resample from the normalized positive part of p minus q; if all are accepted, sample a bonus token from p.
由 Leviathan et al. 2022(Fast Inference from Transformers via Speculative Decoding)和 Chen et al. 2023(DeepMind, speculative sampling)独立提出。
- Draft:用小模型自回归采样 \(x_1,\dots,x_\gamma\),记录 \(q_i(\cdot)=q(\cdot\mid \text{prefix}, x_{<i})\)。
- Verify:把 prefix + \(x_{1..\gamma}\) 一次送入目标模型,得到 \(p_1,\dots,p_{\gamma+1}\)(因果 mask 让每个位置只看到它之前的 token)。
- Accept/reject:对 i=1..γ,抽 \(r\sim U(0,1)\),若 \(r < \min(1, p_i(x_i)/q_i(x_i))\) 接受;否则从 \(p'_i = \frac{\max(0, p_i - q_i)}{\sum \max(0, p_i - q_i)}\) 采样一个 token 替换 \(x_i\),丢弃后面所有 draft,结束本轮。
- Bonus:若 γ 个全接受,从 \(p_{\gamma+1}\) 再采样 1 个。所以每轮至少产出 1 个 token(不会比普通 decode 产出更少),至多 γ+1 个。
- 回滚:被拒 token 之后的 KV cache(目标和 draft 两边)要截断。
greedy 版本:如果用 greedy,就简单地接受直到第一个 \(x_i \ne \arg\max p_i\),然后用 \(\arg\max p_i\) 替换。
代码:toy 演示(numpy,经验分布与目标分布一致)
import numpy as np
rng = np.random.default_rng(0)
def speculative_step(prefix, gamma, p_model, q_model, V):
drafts, qs, ctx = [], [], list(prefix)
for _ in range(gamma): # 1) draft gamma tokens with q
q = q_model(ctx); x = rng.choice(V, p=q)
drafts.append(x); qs.append(q); ctx.append(x)
# 2) target scores all gamma+1 positions (one parallel forward pass in practice)
ps = [p_model(list(prefix) + drafts[:i]) for i in range(gamma + 1)]
out = []
for i, x in enumerate(drafts): # 3) accept / reject left to right
p, q = ps[i], qs[i]
if rng.random() < min(1.0, p[x] / q[x]):
out.append(x)
else:
resid = np.maximum(p - q, 0); resid /= resid.sum()
out.append(rng.choice(V, p=resid)) # resample from (p - q)+
return out
out.append(rng.choice(V, p=ps[gamma])) # 4) bonus token
return out
# With a 6-token toy target p and a *different* draft q, over 60,000 runs the
# empirical distribution of the first emitted token matches p within 0.01
# (target [0.132 0.269 0.129 0.011 0.305 0.153] vs empirical [0.133 0.268 0.129 0.011 0.306 0.152]),
# and the joint distribution of the first two tokens also matches p.
A small draft model proposes gamma tokens autoregressively. The target model then scores all of them in a single forward pass, which costs about the same as one decoding step because decode is memory-bound. We go left to right and accept each draft token with probability min of one and p over q. At the first rejection, we resample that position from the normalized positive part of p minus q and throw away the rest. If everything is accepted, we get a bonus token from the target's last distribution. So each round produces between one and gamma plus one tokens, and the output distribution is exactly the target's.
证明:输出分布恰好等于目标分布 p
\( P(\text{输出}=x) = q(x)\min(1,\tfrac{p(x)}{q(x)}) + (1-\beta)\,\tfrac{(p(x)-q(x))^+}{1-\beta} = \min(p,q)(x) + (p(x)-\min(p,q)(x)) = p(x) \),其中 \(\beta = \sum_x \min(p(x),q(x))\) 是接受概率。
The probability of emitting x is min of p and q from acceptance, plus the positive part of p minus q from the residual, which sums to exactly p of x.
考虑单个位置(多位置由归纳得到:每个被输出的 token 都服从给定前缀下的 p,后续位置以此为条件继续)。draft 采 \(x\sim q\)。
Step 1:接受概率。采到 x 且被接受的概率:
$$ q(x)\cdot\min\Big(1,\frac{p(x)}{q(x)}\Big) = \min\big(q(x), p(x)\big) $$总接受概率 \( \beta = \sum_x \min(p(x), q(x)) \),所以拒绝概率 \(1-\beta\)。
Step 2:残差分布的归一化常数。\( \sum_x (p(x)-q(x))^+ = \sum_x \big(p(x) - \min(p(x),q(x))\big) = 1 - \beta \)(因为 \(a - \min(a,b) = (a-b)^+\))。
Step 3:合并。
$$ P(\text{out}=x) = \underbrace{\min(p(x),q(x))}_{\text{accepted}} + \underbrace{(1-\beta)\cdot\frac{(p(x)-q(x))^+}{1-\beta}}_{\text{rejected, resampled}} = \min(p(x),q(x)) + (p(x)-q(x))^+ = p(x). \ \blacksquare $$(若 \(\beta=1\) 即 p=q,永远接受,结论显然。)
接受率的含义:\( \alpha = \beta = 1 - \mathrm{TV}(p,q) \)。draft 与目标越接近(总变差越小),接受率越高。
多位置的说明:第 i 个位置只有在前 i−1 个都被接受时才被考虑;被接受的前缀本身服从 p,所以以它为条件时第 i 个输出也服从 \(p(\cdot\mid\text{prefix})\)。bonus token 直接从 p 采样。因此整条输出序列的联合分布等于目标模型自回归采样的分布。我的 toy 实验同时检验了前两个 token 的联合分布。
- “拒绝后从 p 重新采样”:错!必须从残差 \((p-q)^+\) 采样,否则 \(\min(p,q)\) 区域会被重复计数,分布偏向 draft。
- “speculative decoding 是近似方法”:标准算法是无损的(输出分布不变,greedy 时输出完全相同,除浮点误差外)。有损变体(如放宽接受条件的 typical acceptance)需要单独说明。
- “draft 和目标必须同一 tokenizer”:标准算法要求同一词表;跨 tokenizer 需要额外方法。
Fix a position. The draft samples x from q and accepts with probability min of one and p over q, so the probability of emitting x via acceptance is min of p of x and q of x. The total acceptance probability beta is the sum of those mins. On rejection we sample from the positive part of p minus q, whose total mass is exactly one minus beta. So the rejection branch contributes one minus beta times the positive part over one minus beta, which is just the positive part of p minus q. Adding the two gives min of p and q plus p minus that min, which equals p. By induction over positions, the whole sequence is distributed as the target model's samples.
期望加速比:α、γ 与 draft 成本 c
假设每个 token 独立以概率 α 被接受,每轮期望产出 \( \frac{1-\alpha^{\gamma+1}}{1-\alpha} \) 个 token;一轮耗时约 \( \gamma c + 1 \)(以目标模型一次前向为单位,c 为 draft 相对成本),所以加速比 \( \frac{1-\alpha^{\gamma+1}}{(1-\alpha)(\gamma c+1)} \)。
With i.i.d. acceptance rate alpha, the expected tokens per round is one minus alpha to the gamma plus one over one minus alpha, and dividing by the round cost gamma c plus one gives the speedup.
推导:设接受的 draft 数为 K。第 i 个 token 被考虑且接受当且仅当前 i 个全接受:\(P(K\ge i) = \alpha^i\)。每轮产出 \(K+1\) 个 token(被接受的 K 个 + 一个重采样或 bonus)。
$$ \mathbb{E}[K+1] = 1 + \sum_{i=1}^{\gamma} P(K\ge i) = \sum_{i=0}^{\gamma}\alpha^i = \frac{1-\alpha^{\gamma+1}}{1-\alpha} $$每轮成本:γ 次 draft 前向(每次 c)+ 1 次目标前向(处理 γ+1 个 token,在 memory-bound 下 ≈ 1 次普通 decode)。
$$ \text{Speedup} = \frac{1-\alpha^{\gamma+1}}{(1-\alpha)(\gamma c + 1)} $$数值(c=0.05,已计算):
| α | E[tokens] γ=1/2/4/8 | speedup γ=1/2/4/8 |
|---|---|---|
| 0.6 | 1.60 / 1.96 / 2.31 / 2.47 | 1.52 / 1.78 / 1.92 / 1.77 |
| 0.8 | 1.80 / 2.44 / 3.36 / 4.33 | 1.71 / 2.22 / 2.80 / 3.09 |
| 0.9 | 1.90 / 2.71 / 4.10 / 6.13 | 1.81 / 2.46 / 3.41 / 4.38 |
观察:(1) α 是最重要的因素;(2) 存在最优 γ:α 低时 γ 太大反而变慢(后面的 draft 几乎都被拒,却要付出 draft 成本);(3) 期望产出上限 \(1/(1-\alpha)\)。
什么时候 speculative decoding 不划算?
- 大 batch / 高负载:batch 大时 decode 已经接近 compute-bound,验证 γ+1 个 token 不再“免费”,被拒 token 的计算纯属浪费。speculative 最适合低 batch、延迟敏感的场景。
- α 低:draft 与目标分布差距大(领域不匹配、高 temperature 采样——T 越高分布越平,TV 距离常常更大)。
- draft 太贵:c 太大。
- 额外显存(draft 模型的权重和 KV)与工程复杂度(KV 回滚、和 continuous batching/分页的结合)。
If each draft token is accepted independently with probability alpha, the probability that the first i drafts are all accepted is alpha to the i, and each round emits the accepted drafts plus one more token, so the expected output per round is the geometric sum, one minus alpha to the gamma plus one over one minus alpha. A round costs gamma draft steps plus one target step, so the speedup is that expectation divided by gamma c plus one. For alpha of 0.8, gamma of 4 and a cheap draft, that's around 2.8x. It works best at small batch sizes, where verification is nearly free; at high load the extra verification compute is no longer free and the gains shrink or vanish.
- How would you pick gamma adaptively?
思路
在线估计 α(最近接受率),按上式选使 speedup 最大的 γ;或者让 draft 在其自身置信度低时提前停止(EAGLE-2 用 draft 置信度动态构造树);根据当前 batch 大小/负载调整,高负载时关闭 speculation。 - Does speculative decoding help throughput or latency?
思路
主要降低单请求延迟。在低负载下也能提高吞吐(更多 token / 每次权重读取);在高负载下(已经接近 compute-bound)可能降低吞吐。
变体:Medusa、EAGLE、lookahead、self-speculative、MTP
核心问题是“draft 从哪来”:独立小模型;在目标模型上加多个预测头(Medusa);在目标模型特征层上做自回归的轻量 draft(EAGLE 系列);不用 draft 模型、靠 Jacobi 迭代和 n-gram 池(lookahead);跳过目标模型的部分层(self-speculative);或预训练时就带多 token 预测模块(DeepSeek-V3 的 MTP)。
Variants differ in where drafts come from: a separate small model, extra decoding heads, a lightweight feature-level draft head, Jacobi iteration with n-gram caches, layer skipping, or built-in multi-token prediction modules.
| 方法 | draft 来源 | 要点 |
|---|---|---|
| 独立 draft 模型 | 同族小模型(如 1B 给 70B) | 需同词表;要额外显存;分布匹配要好 |
| Medusa (2401.10774) | 在最后隐藏层上加 K 个头,第 k 个头预测第 t+k 个 token | 候选组合成树,用 tree attention mask 一次验证多条分支;头之间互相独立,越远越不准 |
| EAGLE (2401.15077) / EAGLE-2 / EAGLE-3 | 一个轻量(约一层 transformer)的自回归 draft head,在目标模型的特征上外推 | 特征层比 token 层更好预测;EAGLE-2 用置信度动态建树;EAGLE-3 改为直接预测 token 并融合多层特征,论文称可随训练数据扩展 |
| Lookahead decoding (2402.02057) | 无 draft 模型:Jacobi 迭代并行猜测多个位置,收集 n-gram 池,再验证 | 无需训练;对重复性强的文本(代码)效果好 |
| Self-speculative / Draft & Verify (2309.08168) | 跳过目标模型中的部分层作为 draft | 不需要额外模型;LayerSkip 等工作通过训练让早退层更可用 |
| Prompt lookup / n-gram | 从 prompt 中查找匹配 n-gram 作为 draft | RAG、代码编辑、摘要等“输出大量复制输入”的场景非常有效 |
| MTP(DeepSeek-V3) | 预训练时附带的多 token 预测模块 | 训练时提供更密的信号,推理时可直接作为 speculative 的 draft |
Tree attention:把多条候选路径组织成一棵 token 树,展平成一个序列,用自定义 attention mask 让每个节点只看到它的祖先;一次前向就验证整棵树,选最长被接受的路径。提高每轮期望接受长度。
The variants mostly differ in where the draft comes from. Medusa adds several heads on the last hidden state, each predicting a token further ahead, and verifies a tree of candidates with a tree attention mask. EAGLE trains a small autoregressive head that extrapolates the target's features, which is easier than predicting tokens, and later versions build dynamic trees and fuse multiple layers. Lookahead decoding needs no draft model, using Jacobi iterations to harvest n-grams. Self-speculative decoding skips some of the target's own layers to draft. And models like DeepSeek-V3 train multi-token prediction modules that can be reused as drafters.
§6 · QUANTIZATION量化:用更少的比特表示权重、激活和 KV
量化同时解决两个问题:显存容量(能不能放下)和带宽(decode 每步读多少字节)。面试要能讲清:各种数值格式、量化粒度、对称/非对称、PTQ vs QAT、为什么 LLM 激活难量化(离群值),以及 LLM.int8、SmoothQuant、GPTQ、AWQ 各自的核心想法。
数值格式:fp32 / bf16 / fp16 / fp8 (e4m3, e5m2) / int8 / int4
浮点 = 符号 + 指数(决定动态范围)+ 尾数(决定精度)。bf16 与 fp32 同范围(8 位指数)但精度低;fp16 精度高但范围小;fp8 有 e4m3(精度优先,常用于权重/激活前向)与 e5m2(范围优先,常用于梯度);int 格式是均匀网格,需要 scale。
Floating-point formats trade exponent bits for range against mantissa bits for precision; integer formats are uniform grids that need a scale factor.
| 格式 | 位(符号/指数/尾数) | 最大值(约) | 说明 |
|---|---|---|---|
| FP32 | 1/8/23 | 3.4e38 | 训练的 master weights、优化器状态 |
| BF16 | 1/8/7 | 3.4e38 | 与 FP32 同范围,训练和推理的默认格式,不需要 loss scaling |
| FP16 | 1/5/10 | 65504 | 精度更高但易溢出,训练需 loss scaling |
| FP8 E4M3 | 1/4/3 | 448 | 精度优先;前向权重/激活 |
| FP8 E5M2 | 1/5/2 | 57344 | 范围优先;常用于梯度 |
| INT8 | — | [−128, 127] | 均匀网格 + scale |
| INT4 | — | [−8, 7] | 常用于 weight-only;需 group-wise scale |
| NF4 / FP4 (MXFP4, NVFP4) | — | — | NF4(QLoRA)按正态分位数设计格点;MX 等格式用小 block 共享 scale |
Microscaling(MX)思路:每 32 个元素(或类似小 block)共享一个缩放因子,把“细粒度 scale”做进硬件格式。新一代 GPU 原生支持 FP4/FP6 类格式。
为什么 LLM 推理关心 bit 数:decode 带宽受限 → 权重字节减半,batch=1 速度上限大约翻倍;显存减半 → 能放更大的模型或更多 KV。
A float has a sign, exponent bits that set the dynamic range, and mantissa bits that set precision. BF16 keeps FP32's eight exponent bits, so it has the same range with less precision, which is why it doesn't need loss scaling, whereas FP16 has more precision but overflows above about 65 thousand. FP8 comes in E4M3, favoring precision, for forward weights and activations, and E5M2, favoring range, for gradients. Integer formats like INT8 and INT4 are uniform grids, so they need scale factors, and the granularity of those scales matters a lot.
量化基础:scale 与 zero-point、粒度、weight-only vs W+A、PTQ vs QAT
对称量化 \( q = \mathrm{clip}(\mathrm{round}(x/s)) \),\( s = \max|x| / q_{\max} \);非对称多一个 zero-point 用于偏斜分布。scale 的粒度从粗到细:per-tensor → per-channel → per-group(如 128 个元素)→ per-block;越细误差越小,scale 存储开销越大。
Symmetric quantization divides by an absmax scale and rounds; asymmetric adds a zero point; finer scale granularity, from per-tensor to per-channel to per-group, lowers error at the cost of storing more scales.
对称(absmax):\( s = \frac{\max|x|}{2^{b-1}-1},\ q = \mathrm{clip}(\mathrm{round}(x/s), -q_{\max}, q_{\max}),\ \hat{x} = s\cdot q \)。最大舍入误差 \(s/2\)。
非对称(min-max):\( s = \frac{x_{\max}-x_{\min}}{2^b - 1},\ z = \mathrm{round}(-x_{\min}/s),\ q = \mathrm{clip}(\mathrm{round}(x/s)+z, 0, 2^b-1),\ \hat{x} = s(q - z) \)。适合非零中心的分布(如 ReLU 后激活)。
粒度:一个离群值会撑大它所在分组的 scale,让同组其他值的有效精度下降。所以分组越小越抗离群。INT4 权重通常用 group size 64/128,每组存一个 fp16 scale(group 128 时约为每权重额外 0.125 bit)。
实验(numpy,已运行):对一个“不同行尺度差异很大”的 256×1024 权重矩阵:
| 方案 | 相对误差 ‖W−Ŵ‖/‖W‖ |
|---|---|
| INT8 per-tensor | 0.0329 |
| INT8 per-channel(对称) | 0.0078 |
| INT8 per-channel(非对称) | 0.0073 |
| INT4 per-channel | 0.1404 |
| INT4 group-128 | 0.1171 |
Weight-only vs Weight+Activation:
- Weight-only(W4A16、W8A16):权重以低比特存储,计算前在 kernel 里反量化回 bf16。省显存、省带宽 → 小 batch decode 加速;但计算仍是 bf16,大 batch(compute-bound)时没有加速甚至因反量化开销变慢。
- W8A8(INT8 或 FP8):权重和激活都量化,用低精度 tensor core 计算 → 加速 compute-bound 的 prefill 和大 batch。难点是激活的离群值。
PTQ vs QAT:PTQ(post-training quantization)用少量校准数据直接量化训练好的模型,便宜,LLM 最常用(GPTQ、AWQ、SmoothQuant 都是 PTQ)。QAT(quantization-aware training)训练时插入“伪量化”(前向量化-反量化,反向用 straight-through estimator 让梯度穿过 round),质量更好但需要训练成本;低于 4 bit 时通常需要 QAT 或类似方法。
代码:per-channel INT8 量化/反量化(PyTorch)
import torch
def quantize_per_channel_int8(w):
# w: (out, in). symmetric absmax per output channel
scale = w.abs().amax(dim=1, keepdim=True).clamp(min=1e-12) / 127
q = torch.clamp(torch.round(w / scale), -127, 127).to(torch.int8)
return q, scale
def dequantize(q, scale):
return q.float() * scale
w = torch.randn(256, 1024) * torch.randn(256, 1).exp()
q, s = quantize_per_channel_int8(w)
err = (w - dequantize(q, s)).norm() / w.norm() # ~0.008
assert ((w - dequantize(q, s)).abs() <= s / 2 + 1e-6).all() # error <= half a step
为什么用 ±127 而不是 −128..127?保持对称,0 精确表示,且避免 −128 带来的不对称。等价 numpy 版本已运行(误差 0.0078)。
- “INT4 权重量化一定让推理更快”:只在 memory-bound(小 batch decode)时成立;大 batch 下 W4A16 需要反量化,计算还是 bf16。
- “per-channel 指的是输入通道”:对权重 \(W\in\mathbb{R}^{\text{out}\times\text{in}}\),per-output-channel scale 可以在矩阵乘之后再乘回去(\(y_j = s_j \sum_i q_{ji}x_i\)),对计算友好;per-input-channel 则不能直接提出,这就是 SmoothQuant 要把激活的 per-channel 缩放“迁移”进权重的原因。
Symmetric quantization picks a scale as the absolute max divided by the largest integer, divides, rounds and clips; dequantization multiplies back. Asymmetric quantization adds a zero point for skewed ranges. The key design choice is granularity: a single scale per tensor is cheap but one outlier ruins the resolution for everything, so we use per-channel scales for INT8 and small groups of 64 or 128 for INT4. Weight-only quantization saves memory and bandwidth, so it speeds up small-batch decoding, while quantizing both weights and activations lets you use low-precision tensor cores for compute-bound work. Most LLM quantization is post-training with a small calibration set; quantization-aware training uses fake quantization with a straight-through estimator.
激活离群值:LLM.int8() 与 SmoothQuant
大模型(约 6.7B 以上)的激活中,少数特征维度会系统性地出现比其他维度大几十倍的值,per-tensor 激活量化因此崩溃。LLM.int8() 把离群维度拆出来用 fp16 算;SmoothQuant 用数学等价变换把激活的难度“迁移”到权重上。
Large models develop a few activation channels with huge magnitudes; LLM.int8 handles them in fp16 via mixed-precision decomposition, while SmoothQuant rescales channels to migrate the difficulty from activations into weights.
LLM.int8()(Dettmers et al. 2022, arXiv 2208.07339):发现大约在 6.7B 规模时出现“emergent outlier features”——少数隐藏维度在几乎所有层和 token 上数值巨大。方法:向量级(vector-wise)INT8 量化 + 混合精度分解:幅值超过阈值(如 6)的特征维度对应的列用 fp16 矩阵乘,其余用 INT8。质量几乎无损,但分解带来的开销使其在速度上不占优,主要价值是省显存。
SmoothQuant(Xiao et al. 2022, arXiv 2211.10438):激活难量化(按通道离群),权重容易量化。利用 \( Y = XW = (X\,\mathrm{diag}(s)^{-1})(\mathrm{diag}(s)\,W) \),对每个输入通道 j 选:
$$ s_j = \frac{\max|X_j|^{\alpha}}{\max|W_j|^{1-\alpha}},\quad \alpha \approx 0.5 $$把激活的离群通道缩小、权重对应行放大,两边都变得“好量化”。\(\mathrm{diag}(s)^{-1}\) 可以离线融合进前面的 LayerNorm 或上一个线性层,推理时无额外开销,从而实现 W8A8 per-tensor 激活量化。
实验(numpy,已运行):X 为 64×1024,其中一个维度放大 60 倍;W8A8 per-tensor 量化输出相对误差:朴素 0.141 → SmoothQuant 0.031(随机种子不同时数值略变,量级稳定)(并验证了变换前后精确相等)。
相关:旋转类方法(QuaRot、SpinQuant 等)用正交(如 Hadamard)旋转把离群值“摊平”到所有维度,再做低比特 W4A4 量化——\( XW = (XR)(R^\top W) \)。
代码:SmoothQuant 的核心变换(numpy,已测试)
import numpy as np
rng = np.random.default_rng(0)
def quant_sym(w, bits=8, axis=None):
qmax = 2**(bits - 1) - 1
amax = np.abs(w).max() if axis is None else np.abs(w).max(axis=axis, keepdims=True)
scale = np.maximum(amax, 1e-12) / qmax
return np.clip(np.round(w / scale), -qmax, qmax), scale
def qdq(a): # per-tensor quantize -> dequantize
q, s = quant_sym(a); return q * s
X = rng.normal(0, 1, (64, 1024)); X[:, 7] *= 60 # one outlier channel
W = rng.normal(0, 0.03, (1024, 512)); Y = X @ W
rel = lambda a, b: np.linalg.norm(a - b) / np.linalg.norm(a)
alpha = 0.5
s = np.abs(X).max(0)**alpha / np.abs(W).max(1)**(1 - alpha) # per input channel
Xs, Ws = X / s, W * s[:, None]
assert np.allclose(Xs @ Ws, Y) # exact equivalence
print(rel(Y, qdq(X) @ qdq(W)), rel(Y, qdq(Xs) @ qdq(Ws))) # 0.141 vs 0.031
Beyond a few billion parameters, transformers develop a handful of activation channels that are tens of times larger than the rest, consistently across tokens. With per-tensor activation quantization, those outliers set the scale and crush everything else to zero. LLM.int8 handles this by splitting out the outlier columns and computing them in fp16 while the rest runs in INT8. SmoothQuant instead uses the identity XW equals X times diag s inverse times diag s times W, choosing per-channel factors that shrink activation outliers and grow the corresponding weight rows, which balances difficulty so both can be INT8; the scaling folds into the preceding layer norm, so it's free at inference.
GPTQ 与 AWQ:4-bit weight-only 的两大主流方法
GPTQ:逐列量化权重,并用(基于校准激活的)Hessian 逆把每列的量化误差补偿到尚未量化的列上(源自 Optimal Brain Surgeon)。AWQ:少数“显著”权重通道(按激活幅值判断)最重要,量化前对它们按比例放大(等价地缩小对应激活),无需反向传播或重建。
GPTQ quantizes weights column by column and uses the inverse Hessian from calibration activations to push each column's error onto the remaining columns; AWQ protects salient channels, identified by activation magnitude, by scaling them up before quantization.
共同的目标:不是最小化 \(\|W-\hat W\|\),而是最小化输出误差 \( \|WX - \hat W X\|_F^2 \),X 是校准数据的输入激活。对每一行 w(输出通道)这是一个二次型:\( (w-\hat w)^\top H (w-\hat w) \),其中 \( H = 2XX^\top \)。
OBS 思想(Optimal Brain Surgeon):如果把第 q 个权重固定为量化值(改变 \(\delta_q = \hat w_q - w_q\)),其余权重的最优补偿是:
$$ \delta = -\frac{w_q - \hat w_q}{[H^{-1}]_{qq}}\, (H^{-1})_{:,q} $$(\(H^{-1}\) 取在尚未量化的权重子集上;这个 δ 的第 q 个分量恰好把 \(w_q\) 移到 \(\hat w_q\),其余分量是最优补偿。)引起的误差增加 \( \propto \frac{(w_q - \hat w_q)^2}{[H^{-1}]_{qq}} \)。我用 numpy 验证过:这个闭式补偿与“固定 \(w_q\) 后对其余权重做最小二乘”的解完全一致,且输出误差比只舍入不补偿小(0.44 vs 0.72)。
GPTQ(Frantar et al. 2022, arXiv 2210.17323)的工程化:(1) 所有行用相同的列顺序量化,所以 \(H^{-1}\) 的更新对所有行共享,可以批处理;(2) 按 block(如 128 列)懒更新,提高 GPU 利用率;(3) 用 Cholesky 分解稳定地得到需要的 \(H^{-1}\) 行。结果:单卡几小时量化 175B 模型到 3–4 bit,困惑度损失小。
AWQ(Lin et al. 2023, arXiv 2306.00978):
- 观察:保护约 1% 的显著权重通道(保持 fp16)就能大幅降低误差;显著性应按激活幅值而非权重幅值判断。
- 但混合精度对硬件不友好,于是改为按通道缩放:权重通道乘 s>1、激活除以 s。放大后该通道的相对舍入误差变小(scale 由组内最大值决定,基本不变)。
- s 用激活幅值的幂 \(s = \bar{|x|}^{\alpha}\) 参数化,对 α 做小网格搜索最小化输出误差。不需要反向传播,对校准集过拟合风险小。
| GPTQ | AWQ | SmoothQuant | LLM.int8() | |
|---|---|---|---|---|
| 量化对象 | 权重(W4/W3) | 权重(W4) | W8A8 | W8A8(混合精度) |
| 核心手段 | 二阶误差补偿 | 按激活显著性缩放通道 | 激活→权重难度迁移 | 离群维度 fp16 |
| 主要收益 | 显存 + 小 batch 速度 | 同左 | INT8 计算加速 | 显存 |
70B 模型权重显存:bf16 140 GB(2 张 80GB 卡都不太够,还需 KV);INT8 70 GB;INT4 35 GB(单张 80GB 卡可放下并留出 KV 空间;group scale 再多约 3%);3-bit 约 26 GB。
量化对什么伤害最大?经验上,低比特量化对多步推理(数学、代码)、长上下文、多语言和长尾知识的伤害往往比对常见基准的平均分更大——单一 perplexity 可能掩盖这些退化,评测时应包含推理类任务。
Both methods minimize the layer's output error on calibration data, not the raw weight error. GPTQ comes from Optimal Brain Surgeon: it quantizes one column at a time and uses the inverse Hessian, which is just X X transpose of the calibration inputs, to update the not-yet-quantized columns so they compensate for the rounding error; using the same column order for all rows makes it fast enough for hundred-billion-parameter models. AWQ observes that a small fraction of weight channels matter most, identified by large input activations, and protects them by scaling those channels up before quantization and scaling the activations down, searching the scaling exponent on a small grid. Both give near-lossless 4-bit weight-only models, which mainly speed up memory-bound decoding.
- Why does scaling a channel up reduce its relative quantization error in AWQ?
思路
组 scale 由组内 absmax 决定,放大单个通道(如果它不是组内最大值)几乎不改变 scale \(\Delta\),舍入误差上界仍是 \(\Delta/2\);但该权重的值变大了 s 倍,再乘以缩小 s 倍的激活,输出误差中这一项被缩小约 s 倍。s 过大会让它成为组内最大值、增大 \(\Delta\),所以需要搜索。 - How would you evaluate whether a quantized model is good enough?
思路
不仅看 perplexity:跑下游任务(尤其是数学/代码/长上下文/多语言)、与原模型的逐 token KL 或 top-1 一致率、生成长度分布变化、在真实流量样本上做 A/B 或 LLM-judge 比较;同时测真实延迟/吞吐,确认 kernel 真的带来了加速。
§7 · OTHER ACCELERATIONS其他加速手段
这些话题通常不会深挖,但需要能各说两三句:蒸馏、剪枝(含 2:4 稀疏)、FlashDecoding、CUDA graphs 与 kernel 融合,以及长上下文服务的成本。
蒸馏与剪枝(unstructured / structured / 2:4)
蒸馏训练一个小模型模仿大模型(soft label 的 KL 或在 teacher 生成的数据上 SFT),从根本上减少每 token 的计算与字节。剪枝删除权重:非结构化剪枝压缩率高但难以加速;结构化剪枝(删头、删层、删通道)能直接加速但损失大;2:4 半结构化稀疏有硬件支持。
Distillation trains a smaller student to imitate the teacher; pruning removes weights, where unstructured sparsity is hard to accelerate, structured pruning speeds up directly, and 2:4 sparsity has hardware support.
- 蒸馏:logit 蒸馏 \( \mathrm{KL}(p_T^{(\tau)} \| p_S^{(\tau)}) \)(需要同词表、能访问 teacher logits);序列级蒸馏(在 teacher 采样的输出上 SFT,例如把推理模型的长 CoT 蒸馏到小模型);on-policy 蒸馏(学生自己采样、teacher 打分,缓解 exposure bias)。
- 非结构化剪枝:按幅值或二阶信息(SparseGPT、Wanda 等)删除单个权重,50% 稀疏度下质量可保持,但不规则稀疏在 GPU 上很难加速(需要非常高的稀疏度才划算)。
- 2:4 稀疏:每连续 4 个权重中恰好 2 个为零,NVIDIA Ampere 及之后的 sparse tensor core 原生支持,理论上矩阵乘吞吐 2×(实际加速通常更小);质量损失比非结构化剪枝大,往往需要再训练。
- 结构化剪枝:删除注意力头、FFN 通道、整层(深层的很多层相似度高,删掉一部分损失小)+ 蒸馏恢复。NVIDIA 的 Minitron 系列就是“剪枝 + 蒸馏”路线。
Distillation is the most reliable way to cut inference cost: train a smaller student on the teacher's soft labels or on teacher-generated sequences, which is how many small reasoning models are produced. Pruning removes weights; unstructured magnitude or second-order pruning keeps quality at moderate sparsity but is hard to turn into real speedups on GPUs, while 2:4 semi-structured sparsity is supported by sparse tensor cores, and structured pruning of heads, channels or whole layers gives direct speedups but usually needs distillation to recover quality.
FlashDecoding、CUDA graphs、kernel 融合
FlashDecoding 在 decode 时沿 KV 长度方向切分(split-KV),多个线程块并行处理不同 KV 段,再用 log-sum-exp 合并,解决“batch 小、query 只有 1 个时 GPU 并行度不足”的问题。CUDA graphs 把一整步的数百个小 kernel 录制成一张图一次性提交,消除 CPU launch 开销;kernel 融合减少中间结果读写。
FlashDecoding splits the KV sequence across thread blocks and merges partial softmaxes, restoring parallelism for single-query long-context decode; CUDA graphs remove kernel launch overhead and fusion removes intermediate memory traffic.
- FlashDecoding(Dao et al. 2023 博客):FlashAttention 在 batch×heads 和 query 块上并行;decode 时 query 长度为 1,若 batch 小,块数不足以填满上百个 SM。split-KV:每块算出局部的 \((m_j, \ell_j, o_j)\)(最大值、归一化和、未归一化输出),最后按 online softmax 规则合并:\( m=\max_j m_j,\ \ell=\sum_j e^{m_j-m}\ell_j,\ o=\sum_j e^{m_j-m}o_j/\ell \)。
- CUDA graphs:小 batch decode 每步的 GPU 计算可能只有几毫秒,而 Python + 数百次 kernel launch 的 CPU 开销同一量级。录制成 graph 后重放,CPU 开销几乎消失(需要静态形状,所以引擎会为若干 batch size 预录制)。
torch.compile的 “reduce-overhead” 模式就使用 CUDA graphs。 - 融合:把 RMSNorm + 量化、RoPE + attention、SwiGLU 的逐元素部分、采样等融合进一个 kernel,减少 HBM 往返。
During decode there's only one query per sequence, so with a small batch FlashAttention can't launch enough blocks to fill the GPU, especially at long context. FlashDecoding splits the KV cache along the sequence dimension, computes partial attention with local max and sum statistics in parallel, and merges them with the same log-sum-exp trick as online softmax. Separately, CUDA graphs record the hundreds of small kernels in a decode step and replay them with one launch, which matters because at small batch the CPU launch overhead can rival the GPU time; and kernel fusion cuts memory round trips.
长上下文服务的成本
长上下文的代价有三处:prefill 的注意力 \(O(T^2)\) 计算(TTFT 变长),KV cache 显存 \(O(T)\)(并发数下降,吞吐下降),以及 decode 每步读取 KV 的带宽 \(O(T)\)(TPOT 变长)。
Long context costs quadratic prefill attention, linear KV memory that limits concurrency, and linear KV reads per decode step that slow every token.
- Prefill:每层注意力 FLOPs ≈ \(4T^2 d\)(QKᵀ 和 AV 各 \(2T^2d\);因果 mask 理论上可省一半)。对 7B(d=4096,L=32)而言,大约在 T 达到数万时注意力项就与线性层 \(2NT\) 相当。超长 prompt 需要 context parallel / ring attention 把序列切到多卡(见模块 09)。
- KV 显存:例如 70B GQA-8 在 128k 下每序列 40 GiB——单卡(甚至单机)只能服务极少量并发。这是 API 对长上下文收更高价格的原因之一。
- Decode 带宽:每步读的 KV 字节随 T 线性增长,长上下文下会超过权重本身(7B MHA 在 32k 时 KV 16 GiB,已超过 13.5 GB 权重)。
- 缓解:GQA/MLA、KV 量化、滑窗/混合注意力(部分层全局、部分层局部)、prefix caching(长文档复用)、KV offload 到 CPU、稀疏注意力/检索式 KV 选择。
Long context hurts in three places. Prefill attention grows quadratically, so time to first token climbs. The KV cache grows linearly per sequence, so fewer sequences fit and throughput drops. And every decode step must read the whole cache, so inter-token latency also grows linearly, eventually exceeding the cost of reading the weights. The mitigations are GQA or MLA, KV quantization, hybrid local-global attention, prefix caching, offloading, and sparse or retrieval-based attention over the cache.
§8 · INTERVIEW SCENARIOS面试场景题(模拟面试)
下面的题目模拟面试官的开放式提问。先自己用英文口头回答(限时 2–3 分钟),再展开参考思路。好的回答结构:先给数量级 / 诊断框架 → 再列假设 → 最后给可执行的实验或改动。
12 道场景题
回答场景题的通用套路:先确定瓶颈属于 compute、memory bandwidth、memory capacity、通信还是调度/CPU 开销,再对症下药。
For any inference scenario, first identify the bottleneck: compute, bandwidth, capacity, communication, or scheduling overhead, then pick the matching fix.
- S1. What's the maximum batch size for LLaMA-2-13B at 4k context on one 80GB GPU in bf16?
思路
权重:13B × 2 B ≈ 26 GB。KV/token:\(2\times40\times5120\times2\) = 800 KiB(13B 是 MHA:L=40,d=5120)。每个 4k 序列:3.125 GiB ≈ 3.36 GB。剩余 ≈ 80 − 26 = 54 GB → 理论上限约 16 个序列。扣除激活、CUDA context、碎片等(预留约 10–15%),实际约 13–14 个。
能提高的方法:KV 用 FP8(翻倍到约 30);权重 INT8/INT4(多出 13–19 GB);PagedAttention(多数请求实际用不到 4k,按需分配可以支持更多并发);TP=2。
"Weights take about 26 GB, KV is 800 KB per token, so 3.3 GB per 4k sequence; the remaining 54 GB fits roughly 16 sequences in theory, more like 13 or 14 after overhead."
- S2. Our decode throughput is low. How do you debug it?
思路
(1) 先算理论值:batch × BW / (权重字节 + KV 字节),和实测比较。(2) batch 是否太小?看并发请求数、调度器是否在攒 batch、KV 显存是否限制了 batch(显存利用率、抢占次数)。(3) GPU 是否在等 CPU?profile 看 kernel 之间的空隙(Python 开销、未启用 CUDA graphs、采样/去 token 化在 CPU 上、tokenizer 瓶颈)。(4) kernel 是否高效:是否用了 paged/flash attention、量化 kernel 是否真的被调用、是否有不必要的 dtype 转换。(5) 多卡:TP 的 all-reduce 是否走了 PCIe 而不是 NVLink。(6) 长上下文下 KV 读取占主导 → GQA/KV 量化。
- S3. TTFT is high but TPOT is fine. What could be wrong?
思路
TTFT = 排队 + prefill。(1) 排队:请求到达率过高、调度策略让新请求等待(比如优先 decode)、没有足够实例。(2) prefill 慢:prompt 很长(attention 二次项)、没有 prefix caching(多轮对话每轮重算历史;prompt 开头有变化内容导致缓存不命中)、prefill 被切成太小的 chunk。(3) 解决:prefix caching、更大的 chunk / 优先 prefill、PD 分离并增加 prefill 实例、对长 prompt 用 TP 或 context parallel、缩短 system prompt。
- S4. When does speculative decoding fail to help or even hurt?
思路
(1) 大 batch / 高负载:系统已接近 compute-bound,验证额外 token 不再免费。(2) 接受率低:draft 与 target 不匹配(领域不同、target 经过大量 RL 后分布偏移、高 temperature、不同 chat template)。(3) draft 成本高或 γ 设得过大。(4) 工程开销:draft 的 KV、回滚、和 paged KV/continuous batching 的协同。(5) 对超长输出且内容不可预测的创造性任务收益较小;对代码编辑、RAG(大量复制输入)收益大。
- S5. After INT4 quantization, the model's math accuracy dropped a lot but chat quality looks fine. Why, and what do you do?
思路
原因:多步推理中每一步的小误差会累积,一个错误 token 就可能毁掉整条推理链;长 CoT 意味着更多出错机会;聊天质量的评估更宽松。另外,校准数据如果全是普通网页文本,对数学/代码相关的激活分布覆盖不足。
做法:用包含数学/代码/长 CoT 的校准集;用更小的 group size(64);对敏感层(首尾层、attention 输出投影、LM head)保持高精度;改用 W8A8 / FP8;尝试 AWQ vs GPTQ;QAT 或量化后少量蒸馏微调;评测时测 pass@1 而非 perplexity。
- S6. How would you serve a 70B model at low latency for interactive chat?
思路
bf16 权重 140 GB → 至少 2 张 80GB,一般 TP=4 或 8 以降低每 token 延迟(每卡读 1/k 权重),走 NVLink。FP8 权重(70 GB)可以减少卡数或提升速度。加上:continuous batching + paged KV、prefix caching(system prompt)、speculative decoding(低负载时,用同族 8B 或 EAGLE head)、CUDA graphs。估算:bf16 TP=4 的 batch=1 上限 ≈ 4×3.35 TB/s ÷ 140 GB ≈ 96 tok/s(理想值,实际受通信影响更低)。
- S7. Your RL training spends 80% of its time on rollout generation. How do you speed it up?
思路
Rollout 就是推理问题:用高效推理引擎(vLLM/SGLang)而不是训练框架的 generate;大 batch + continuous batching;GRPO 对同一 prompt 采 G 个回答 → prefix caching / 共享 prompt KV;长尾问题:少数超长回答拖住整个 batch → 设置最大长度、异步/部分 rollout(未完成的回答带到下一轮)、过度采样后提前截止;FP8 rollout(注意 rollout 与训练的数值不一致导致的 off-policy 偏差,可用重要性采样修正);训练与推理分离部署并异步执行(权重同步的代价)。
- S8. The model generates repetitive loops in production. What do you check?
思路
解码参数(是否被设成 greedy 或 T 很低)、是否需要轻微 repetition/presence penalty 或 min-p/top-p;chat template / stop token 是否正确(模型不知道何时停止);量化后的数值问题;长上下文超出训练长度(RoPE 外推失败);KV cache 相关 bug(位置偏移错误、cache 复用错误)。区分“模型行为问题”和“系统 bug”:用参考实现(不带 cache、bf16)复现同一个 prompt。
- S9. Outputs differ between batch size 1 and batch size 32 with temperature 0. Is this a bug?
思路
不一定。不同 batch size 会选择不同的 kernel/切分方式,浮点加法的归约顺序不同 → logits 有微小差异 → 当 top-2 非常接近时 argmax 翻转,之后的序列完全分岔。这是“batch 不变性”问题;要做到确定性需要 batch-invariant kernel(以性能为代价)。若差异很大(不是偶发),检查 padding/mask 是否正确、位置 id 在左 padding 时是否处理正确。
- S10. You have 1M prompts to label offline as cheaply as possible. How do you configure inference?
思路
离线 = 只关心吞吐,不关心延迟:尽可能大的 batch(直到 KV 显存满)、按长度排序/分桶减少浪费、共享前缀的 prompt 放在一起(prefix caching)、FP8/INT8 W8A8 加速 compute-bound 部分、关闭 speculative decoding、限制输出长度(例如结构化输出 + 约束解码)、多副本数据并行而不是大 TP。
- S11. Estimate the cost per million output tokens for an 8B model on an H100.
思路
用 §3 的估算:batch 64、2k 上下文时理想吞吐约 6,500 tok/s,按 50% 效率算约 3,000 tok/s ≈ 1,080 万 token/小时。若 H100 每小时约 2–3 美元,则每百万输出 token 约 0.2–0.3 美元(只算 GPU,粗略量级;且没有计入 prefill)。关键是展示推理链:吞吐由 batch 和带宽决定 → 换算成每小时 token → 除以 GPU 价格。
- S12. Should we use a MoE model or a dense model for serving at scale?
思路
MoE:每 token 计算量低(激活参数少),在 compute-bound 的大 batch 场景很划算;但总参数必须放进显存(需要更多卡 / EP),小 batch 时每步要读的专家权重接近激活参数,而大 batch 下几乎所有专家都被激活,读取量接近总参数;all-to-all 通信、负载不均衡也增加复杂度。高吞吐大规模部署(大 EP + PD 分离)时 MoE 性价比很高;小规模、低并发、显存受限时 dense 更简单。
My general approach to any serving question is to first do a back-of-the-envelope estimate from parameter count, KV bytes per token, memory bandwidth and FLOPs, then compare that against what we observe. If we're far below the bandwidth bound at small batch, it's usually overhead: kernel launches, CPU-side work, or poor batching. If memory is full, it's KV capacity, and the fixes are GQA, KV quantization, paging and prefix sharing. If it's prefill-heavy, I look at caching, chunking and disaggregation.
§9 · EXERCISES练习(数值题 + 编码题)
所有数值答案都已用脚本计算核对。建议先在纸上算,限时 3–5 分钟一题——面试中这类题就是要你“边说边算”。
LLaMA-3-8B(L=32,32 个 query 头,8 个 KV 头,\(d_h\)=128),bf16 KV。batch=8、每个序列 32k token 时 KV cache 有多大?
提示
参考答案
一个 70B 模型用 INT4 weight-only 量化(忽略 scale 开销),在单张 H100(3.35 TB/s)上 batch=1 decode 的理论速度上限是多少?和 bf16 相比呢?
参考答案
证明:在 bf16 权重下,(B×d)·(d×d) 矩阵乘的算术强度约为 B(当 B≪d)。在 H100 上,batch 需要多大才能让线性层变为 compute-bound?
参考答案
speculative decoding 中 α=0.7,draft 相对成本 c=0.1。(a) γ=3 时期望每轮产出多少 token?加速比多少?(b) 最优 γ 是多少?
参考答案
(b) 枚举 γ=1..7 的加速比:1.55, 1.83, 1.95, 1.98, 1.96, 1.91, 1.85 → 最优 γ=4(约 1.98×),之后下降。
目标分布 p=[0.5, 0.3, 0.2],draft 分布 q=[0.2, 0.5, 0.3]。求接受率 α 和拒绝后的残差分布。
参考答案
编码:实现 top-p 过滤 top_p_filter(logits, p)(numpy,1D),要求越过 p 的那个 token 保留、至少保留 1 个 token。用 logits=log([0.5,0.2,0.15,0.1,0.05]) 测试 p=0.6 应保留 2 个,p=0.9 应保留 4 个。
提示
参考答案
import numpy as np
def softmax(z):
z = z - z.max(); e = np.exp(z); return e / e.sum()
def top_p_filter(logits, p):
if p >= 1.0:
return logits
order = np.argsort(-logits)
probs = softmax(logits[order])
cum = np.cumsum(probs)
keep_sorted = (cum - probs) < p # first token always kept (0 < p)
keep = np.zeros_like(keep_sorted)
keep[order] = keep_sorted # scatter back to original order
return np.where(keep, logits, -np.inf)
lg = np.log(np.array([0.5, 0.2, 0.15, 0.1, 0.05]))
assert np.isfinite(top_p_filter(lg, 0.6)).sum() == 2
assert np.isfinite(top_p_filter(lg, 0.9)).sum() == 4
assert np.isfinite(top_p_filter(lg, 0.45)).sum() == 1
复杂度 \(O(V\log V)\)。对向量 x=[0.1, −0.5, 1.27, 0.02] 做对称 absmax 量化:(a) INT8;(b) INT4。写出 scale、整数值和反量化结果,INT4 的最大误差是多少?
参考答案
(b) INT4:s = 1.27/7 ≈ 0.1814;q = round([0.55, −2.76, 7, 0.11]) = [1, −3, 7, 0];反量化 ≈ [0.181, −0.544, 1.27, 0];最大误差 ≈ 0.081(第一个元素),而 0.02 被量化为 0——小值在粗网格下被“吞掉”,这就是为什么离群值(这里的 1.27)会伤害同组其他值的精度。
编码:实现带 KV cache 的增量 decode,并写一个测试证明“有 cache 的 greedy 生成”与“每步重算整个序列”的结果完全相同。(提示:注意 mask 偏移和位置编码的绝对位置。)
参考答案
assert torch.equal(generate(m, p, 20, use_cache=False), generate(m, p, 20, use_cache=True));更强的测试是比较 logits:对完整序列做一次前向,与“prefill 前 10 个 + decode 第 11 个”的 logits 比较 allclose。常见 bug:decode 时位置从 0 开始而不是从当前长度开始;mask 用 tril() 而不是 tril(diagonal=S-T)。8B 模型(GQA,KV 128 KiB/token)在单张 H100 上服务一个 4000 token 的 prompt,生成 500 token(batch=1)。估算 TTFT 和总生成时间(prefill 按 50% MFU,decode 按理论带宽)。
参考答案
编码:FlashDecoding 的 split-KV 合并。给定一个 query 和长度 1000 的 K、V,分成 4 段分别计算 \((m_j, \ell_j, o_j)\)(局部最大值、局部指数和、未归一化输出),再合并成与完整 softmax attention 完全相同的结果。
参考答案
import numpy as np
rng = np.random.default_rng(0); d, S = 16, 1000
q = rng.normal(size=d); K = rng.normal(size=(S, d)); V = rng.normal(size=(S, d))
sc = K @ q / np.sqrt(d)
w = np.exp(sc - sc.max()); ref = (w / w.sum()) @ V
def partial(sl):
s = sc[sl]; m = s.max(); e = np.exp(s - m)
return m, e.sum(), e @ V[sl] # (local max, local sum, unnormalized out)
parts = [partial(slice(i, i + 256)) for i in range(0, S, 256)]
m = max(p[0] for p in parts)
l = sum(np.exp(mj - m) * lj for mj, lj, _ in parts)
o = sum(np.exp(mj - m) * oj for mj, _, oj in parts) / l
assert np.allclose(o, ref)
每段可以在不同的线程块上并行计算;合并只需 O(段数)。这与 FlashAttention 的 online softmax 是同一个恒等式。70B 模型(L=80,GQA-8,\(d_h\)=128)以 FP8 权重部署在单张 80GB GPU 上。预留 15% 开销,最多能同时服务几个 8k 上下文的请求?如果 KV 也用 FP8 呢?
参考答案
logits = [2, 1, 0]。分别计算 T=1、0.5、2 时的 softmax 分布。T=0.5 和“把 T=1 的概率平方再归一化”是否相同?
参考答案
推导:在 OBS/GPTQ 框架下,把权重 \(w_q\) 固定为 \(\hat w_q\) 后,其余权重的最优补偿为 \(\delta = -\frac{w_q-\hat w_q}{[H^{-1}]_{qq}}H^{-1}_{:,q}\)。
提示
参考答案
§ · CHEAT SHEET 一页速查一页速查
面试前一晚过一遍。
| 主题 | 必背 |
|---|---|
| Temperature | \(\mathrm{softmax}(z/T)\);\(p(T)\propto p^{1/T}\);T→0 argmax,T→∞ uniform;不改变排名 |
| top-k / top-p / min-p | 固定 k 个 / 累计 ≥ p 的最小集合(越过 p 的保留)/ \(p_i \ge p_{\min}\cdot p_{\max}\) |
| 处理器顺序 | mask/惩罚 → temperature → top-k → top-p/min-p → softmax → sample |
| Repetition penalty | 正 logit ÷θ,负 logit ×θ;frequency −α·count;presence −α·1[count>0] |
| KV 公式 | \(2\cdot L\cdot n_{kv}\cdot d_h\cdot b\cdot S\cdot B\) |
| KV 数字 | 7B MHA:512 KiB/token,4k=2 GiB,128k=64 GiB;GQA-8:128 KiB/token;70B GQA-8:320 KiB/token;13B MHA:800 KiB/token |
| Roofline | AI = FLOPs/bytes;线性层 AI ≈ batch token 数;脊点 H100 ≈ 295,A100 ≈ 153 |
| Decode 上限 | batch=1:tok/s ≈ BW / 权重字节;7B bf16:A100 ≈ 150,H100 ≈ 250 |
| Prefill | FLOPs ≈ \(2N\cdot T\)(+ 注意力 \(\approx 4LT^2d\));TTFT = 排队 + prefill |
| GPU 规格(约) | A100:312 TF bf16,2.0 TB/s;H100 SXM:989 TF bf16 dense,3.35 TB/s;均 80 GB |
| 服务系统 | Orca = iteration-level batching;vLLM = PagedAttention(block table、CoW);SGLang = RadixAttention;Sarathi = chunked prefill;DistServe/Splitwise = PD 分离 |
| Spec decoding | 接受 \(\min(1,p/q)\);拒绝从 \(\mathrm{norm}((p-q)^+)\) 采样;\(\alpha = \sum\min(p,q) = 1-\mathrm{TV}\);\(E[\text{tokens}] = \frac{1-\alpha^{\gamma+1}}{1-\alpha}\);speedup ÷ \((\gamma c+1)\) |
| 量化 | \(s = \max|x|/(2^{b-1}-1)\);per-tensor < per-channel < per-group 精度;weight-only 加速 memory-bound;W8A8 加速 compute-bound |
| 方法一句话 | LLM.int8 = 离群维度 fp16;SmoothQuant = \(s_j = \max|X_j|^\alpha/\max|W_j|^{1-\alpha}\);GPTQ = OBS 误差补偿;AWQ = 激活显著通道放大 |
| 70B 权重 | bf16 140 GB;INT8/FP8 70 GB;INT4 35 GB |
| 格式 | bf16 1/8/7;fp16 1/5/10(max 65504);e4m3 max 448;e5m2 max 57344 |
高频面试题清单
| # | 问题 | 对应概念 |
|---|---|---|
| 1 | Why is beam search bad for open-ended generation? | Greedy & Beam |
| 2 | What does temperature do mathematically? | Temperature |
| 3 | Top-k vs top-p vs min-p? | 截断采样 |
| 4 | Implement a sampler with temperature/top-k/top-p. | 截断采样, 处理器顺序 |
| 5 | How do repetition penalties work? | 重复惩罚 |
| 6 | How does JSON-constrained decoding work? | 约束解码 |
| 7 | Why does KV caching work? Why not cache Q? | KV cache 原理 |
| 8 | Compute the KV cache size for model X. | KV 公式 |
| 9 | How do GQA/MQA/MLA reduce memory? | 压缩 KV |
| 10 | What is an attention sink? How does StreamingLLM work? | 压缩 KV |
| 11 | Difference between prefill and decode? | Prefill vs Decode |
| 12 | Why is decoding memory-bound? Explain roofline. | Roofline |
| 13 | How many tokens/s can a 7B model do on an H100? | 带宽上限 |
| 14 | TTFT vs TPOT vs throughput vs goodput? | 延迟指标 |
| 15 | What is continuous batching? | Continuous batching |
| 16 | Why is vLLM fast? Explain PagedAttention. | PagedAttention |
| 17 | What is prefix caching / RadixAttention? | Prefix caching |
| 18 | What problem does chunked prefill solve? | Chunked prefill |
| 19 | Why disaggregate prefill and decode? | PD 分离 |
| 20 | Explain speculative decoding. | Spec 算法 |
| 21 | Prove speculative sampling is lossless. | 证明 |
| 22 | Expected speedup of speculative decoding? When does it fail? | 加速比, S4 |
| 23 | Medusa vs EAGLE? | 变体 |
| 24 | bf16 vs fp16 vs fp8? | 数值格式 |
| 25 | Per-tensor vs per-channel vs group quantization? | 量化基础 |
| 26 | Why are activations hard to quantize? SmoothQuant? | 离群值 |
| 27 | Explain GPTQ and AWQ. | GPTQ/AWQ |
| 28 | Max batch size for 13B at 4k on 80GB? | S1 |
| 29 | How to speed up RL rollouts? | S7 |
| 30 | Why do outputs differ at temperature 0 across batch sizes? | S9 |
§ · RECALL自测卡片
先自己说出答案(最好用英文出声),再点开对照并打分。