Inference Engineering 学习路线总结
来源:Twitter / X 博文,作者
@itsmenikhitha(AI Engineer,专注 Agentic AI / LLM / GenAI)。 原文性质:一份 9 个学习节点的 Inference Engineering 路线图,每个节点配一个高质量外部资源链接,目标是“理解事情到底是怎么工作的“(how things actually work under the hood),而不是只调 API。 本文性质:翻译 + 导读原文 + 中文工程语境补充 + 结构化对照表 + 工程师笔记,便于后端 / SRE / 平台工程师按图索骥。
一、原文(导读)
“If you want to learn Inference Engineering, here’s the roadmap I’m following to build the foundations, with resource links to understand how things actually work under the hood. 👇”
整条推文结构非常“工程师导向“:它不是教你怎么调用 OpenAI API,而是教你怎么把 LLM 推理这件事从硅片层面理解清楚。这恰好是国内大部分 LLM 教程(LangChain 调包、Prompt Engineering 套路)的盲区——
做 AI 应用的人很多,做“让 AI 跑得起来 / 跑得便宜 / 跑得稳“的人很少。
这也是为什么 2025 年开始,Inference Engineer 在海外被单独拎出来作为一个工种(Anyscale、Modal、Together、Fireworks 这些公司的核心招聘方向)。
二、九大节点全解读
顺序就是推文顺序。每一节都包含:它在解决什么问题 → 推荐资源 → 中文工程视角补注 → 工程师笔记。
节点 1 · Inference Compute & Memory(推理算力与显存)
- 它解决什么:模型到底吃多少 FLOPs?显存峰值怎么算?为什么 7B 模型单卡 A100 跑不起来,70B 模型必须量化?这一节是所有推理优化的地基。
- 资源:Transformer Inference Arithmetic(Kipply 的经典长文)
- 核心公式族:
- Prefill 阶段:
FLOPs ≈ 2 × N_params × tokens(密集矩阵乘) - Decode 阶段:
FLOPs/token ≈ 2 × N_params(受 memory-bound 限制) - 显存:
显存 ≈ weights + KV cache + activations + overhead
- Prefill 阶段:
- 工程师笔记:
- 后端视角:把这两套算清楚就能预测任何 GPU 配置下的吞吐上限和 OOM 风险,不用每次都靠试错。
- Ops 视角:把
tokens/s/$(每美元吞吐量)作为采购 GPU 的核心指标,而不是裸硬件价格。 - 实操:在 Llama-3-70B 上,FP16 weights 占 ~140GB,KV cache 在
seq=4k, batch=8时还能再加 ~30GB——这就能解释为什么必须做张量并行 / 量化 / PagedAttention。
节点 2 · Quantization Basics(量化基础)
- 它解决什么:FP16/FP32 太重怎么办?INT8 / INT4 / FP8 / AWQ / GPTQ / GGUF 这些后缀到底什么意思?什么时候会掉精度?
- 资源:A Visual Guide to Quantization(Maarten Grootendorst 的可视化图解)
- 三种主流路径:
- PTQ(Post-Training Quantization):训完再压,GPTQ / AWQ / SmoothQuant。
- QAT(Quantization-Aware Training):训练时模拟量化噪声,掉点最少但贵。
- Weight-only vs Weight-and-Activation:前者只压权重,后者连中间激活一起压。
- 工程师笔记:
- 后端视角:线上服务默认建议 W4A16(权重 INT4,激活 FP16) 起步——Llama-3-70B 可压到 ~35GB,单卡 H100 就能跑。
- Ops 视角:量化要进 CI,必须有
ppl(困惑度)+eval任务回归集,量化不能只看显存,要看业务指标。 - 避坑:AWQ 对 MoE 不友好;GGUF + llama.cpp 是边缘部署的事实标准。
节点 3 · KV Cache(KV 缓存)
- 它解决什么:为什么生成式推理慢?为什么不能像训练那样 batch?为什么
max_seq_len设大了就 OOM?这一节讲的是推理特有的“自回归 IO 放大效应“。 - 资源:KV Cache in LLM Inference
- 关键洞察:
- 每个 token 的生成,需要把之前所有 token 的 K/V 矩阵重新“过一遍“注意力。
- 所以显存随序列长度线性增长,且每次只新增一行的 K/V。
- PagedAttention(vLLM 的核心发明)= 把 KV cache 切成不连续物理页,避免传统连续分配导致的内存碎片和浪费。
- 工程师笔记:
- 后端视角:长上下文(>32k)的成本曲线不是线性的——KV cache 可能比 weights 还大,必须算清楚。
- Ops 视角:
prompt_tokens和completion_tokens在云厂商计费里通常分开计费,要分别监控。 - 进阶:Multi-Query Attention (MQA) / Grouped-Query Attention (GQA) 把 KV 头数砍掉,显存直接减 4~8 倍,Llama-2/3、Mixtral 都用了 GQA。
节点 4 · Inference Optimization(推理优化)
- 它解决什么:除了量化,还有什么招能让推理更快?这一节是工具箱——kernel 优化、算子融合、投机解码、连续批处理。
- 资源:Top 5 AI Model Optimization Techniques for Faster, Smarter Inference(NVIDIA 官方博客)
- 五大武器(截至 2026 仍然成立):
- Continuous Batching(vLLM 首创):去掉 static padding,新请求随时插队。
- Speculative Decoding:用小模型草稿 + 大模型验证,吞吐提升 2~3x 不掉质量。
- Kernel Fusion(FlashAttention / FlashInfer):把 attention 算子在 GPU SRAM 内融合,避免 HBM 往返。
- TensorRT-LLM / TensorRT:NVIDIA 的图优化编译器。
- Prefix Caching / Prompt Cache:相同前缀的请求共享 KV,多轮对话 / RAG 场景直接起飞。
- 工程师笔记:
- 后端视角:上线新模型前先评估“它的 speculative draft 模型有没有现成的“——这通常是最便宜的 2x 提速。
- Ops 视角:continuous batching 是 GPU 利用率从 30% → 70%+ 的关键开关,任何不支持的引擎都不应该选。
节点 5 · LLM Serving Engines(推理引擎)
- 它解决什么:跑起来有了,那用什么“引擎“?vLLM / TGI / Ollama / SGLang / LMDeploy / TensorRT-LLM 怎么选?
- 资源:11 Production LLM Serving Engines: vLLM vs TGI vs Ollama
- 核心选型矩阵:
| 引擎 | 强项 | 弱项 | 典型场景 |
|---|---|---|---|
| vLLM | PagedAttention,continuous batching,生态最广 | 单卡极致优化不如 TensorRT | 通用在线服务 / RAG |
| TGI (HuggingFace) | Rust 实现,HF 集成最丝滑 | 性能略输 vLLM | HF 全家桶用户 |
| SGLang | RadixAttention,prefix cache 极致 | 生态较新 | 长 prompt + 多轮对话 |
| TensorRT-LLM | NVIDIA 官方图优化,绝对性能之王 | 编译链复杂,灵活性差 | 大厂高 QPS 生产环境 |
| LMDeploy | 国产(商汤),TurboMind 对中文友好 | 海外文档少 | 国内中文场景 |
| Ollama | 本地一键跑,单机体验最好 | 不是为生产设计 | 开发机 / 边缘 |
| llama.cpp | CPU / Apple Silicon / 嵌入式 | 性能天花板低 | 笔记本 / 树莓派 |
- 工程师笔记:
- 后端视角:选型优先级 vLLM > SGLang > TensorRT-LLM,TGI 在 2026 已经被边缘化。
- Ops 视角:引擎都内置 OpenAI 兼容 API,接入现有网关(LiteLLM / Higress / APISIX)即可,不要造轮子。
节点 6 · Forward Pass & Inference Speed(前向传播与推理速度)
- 它解决什么:为什么 batch=1 时推理慢?为什么预填(prefill)和解码(decode)要用不同策略?这一节拆开看单次前向到底卡在哪。
- 资源:How Fast Can We Perform a Forward Pass?(Bounded Regret 博客,HORACE HE / Tri Dao 等人的思路)
- 两阶段拆分:
- Prefill(处理 prompt):compute-bound,吃 FLOPS。
- Decode(逐 token 生成):memory-bound,吃带宽。
- 优化方向完全不同:Prefill 靠 tensor parallelism;Decode 靠 KV cache 压缩 + speculative decoding。
- TTFT vs TPOT 两个核心延迟指标:
- TTFT(Time To First Token)= 用户看到第一个字的等待时间,Prefill 决定。
- TPOT(Time Per Output Token)= 每个字之间的间隔,Decode 决定。
- 工程师笔记:
- 后端视角:SLA 写“响应 < 2s“是错的——要拆 TTFT < 500ms + TPOT < 50ms,这才是可度量的。
- Ops 视角:监控 dashboard 必须分两条曲线,分别告警——否则一个慢查询就把整个指标糊掉。
节点 7 · MoE vs Dense Models(混合专家 vs 密集模型)
- 它解决什么:为什么 Mixtral / Qwen3-MoE / GPT-4 传闻是 MoE?为什么 MoE 推理更难?何时选 Dense,何时选 MoE?
- 资源:MoE vs Dense Models: Inference(Epoch AI Gradient Updates)
- 核心权衡:
- Dense:每个 token 都激活所有参数,推理显存 = 全参数,计算量固定。
- MoE:每个 token 只激活
top-k(通常 2/4/8)个专家,计算量大幅下降,但显存必须加载所有专家。 - MoE 的推理陷阱:专家并行(Expert Parallelism, EP)通信开销巨大,小 batch 时反而比 Dense 慢。
- 工程师笔记:
- 后端视角:高并发 + 长 prompt → Dense 占优;低并发 + 短 prompt → MoE 占优。
- Ops 视角:MoE 模型必须用专门的 serving 框架(DeepSeek-MoE / vLLM-EP),传统 vLLM 跑 MoE 效率减半。
- 业务视角:私有化部署显存吃紧时,宁愿选 Dense-13B 而不是 MoE-8x7B,因为后者需要 96GB+ 显存才能加载。
节点 8 · LLM Routing(大模型路由)
- 它解决什么:GPT-4o-mini 也能干的活,没必要发给 GPT-4o。怎么自动把请求分发给“刚刚好够用“的模型?
- 资源:LLM Routing(刘逊拙的博客)
- 三种主流路由策略:
- 规则路由:按 token 数、用户等级分流——简单但脆弱。
- 成本优先路由:用便宜模型打头阵,质量不达标时升级——动态 fallback。
- 学习式路由:训练一个“路由器模型“,根据 query 预测最优目标——
RouteLLM/OpenRouter/NotDiamond都是这类。
- 工程师笔记:
- 后端视角:80% 的生产请求其实不需要最强模型,路由可以省 50~70% 的成本。
- Ops 视角:路由层要“可观测“——记录“为什么这条请求被路由到了 X 模型“,否则出问题时无法归因。
- 架构视角:LLM 路由 + 缓存 + fallback = AI Gateway 的三件套,跟传统微服务网关同构。
节点 9 · End-to-End LLM Inference(端到端推理全景)
- 它解决什么:把所有碎片拼起来——从 HTTP 请求到 GPU 指令,一条请求到底经过哪些层?
- 资源:How LLM Inference Works(Arpit Bhayani 的端到端拆解)
- 完整链路(按时间序):
- API 网关:鉴权、限流、计费、prompt 缓存。
- 调度器(engine scheduler):continuous batching 决策,连续请求排队。
- Tokenizer:BPE/SentencePiece,文本 → token IDs。
- Prefill:整段 prompt 一次性并行计算,建立 KV cache。
- Decode loop:每步 sample 一个 token → 写入 KV cache → 终止判断。
- Detokenizer:token IDs → 文本 → 流式返回。
- 后处理:tool call 解析、logprobs、usage 计费回写。
- 工程师笔记:
- 后端视角:任何一环都可能成为瓶颈,tracing 必须打通端到端——OpenTelemetry 配 GenAI semantic conventions。
- Ops 视角:故障定位时按链路分层:网关 → 引擎 → kernel → GPU。不要一上来就跑
nvidia-smi。
三、九大节点的依赖图(学习顺序建议)
[1] Compute & Memory ─┐
[2] Quantization ─────┼─→ [3] KV Cache ─→ [4] Inference Optimization ─→ [5] Serving Engines
│ │
│ ↓
│ [6] Forward Pass 拆分
│ │
│ ↓
└─→ [7] MoE vs Dense ─→ [8] LLM Routing ─→ [9] End-to-End 全景建议:按 1→2→3→4→5→6 的顺序打底(这是“Inference 基本功“),然后 7→8 是“前沿选型“,最后 9 是“系统整合“——也是面试 / 架构设计的最高频考点。
四、核心对照清单(给工程师 5 分钟速览)
| 维度 | 你必须能答出来的问题 | 实战对应 |
|---|---|---|
| 显存 | 70B 模型 FP16 占多少?INT4 量化后多少?KV cache 在 seq=8k, batch=32 时多少? | 选卡、容量规划 |
| 吞吐 | tokens/s/$ 怎么算?continuous batching 为什么是 GPU 利用率开关? | GPU 采购、SLA |
| 延迟 | TTFT 和 TPOT 是什么?分别由 Prefill / Decode 哪个决定? | 线上 SLA、用户体验 |
| 优化 | Speculative decoding 提速多少?什么场景掉质量?FlashAttention 省什么? | 性能调优 |
| 引擎 | vLLM / SGLang / TensorRT-LLM 怎么选?什么时候必须上 EP? | 架构选型 |
| 架构 | LLM Router 的三种策略?AI Gateway 三件套是什么? | 平台设计、成本优化 |
五、工程师视角的总评
- 国内最缺的不是 LLM 应用工程师,是 Inference Engineer——能调 vLLM 参数、理解 TTFT/TPOT、算得清显存峰值的人,2026 年在大厂能开百万年薪。
- 这条路线图的妙处在于它自下而上:先讲硅片(FLOPs / 显存),再讲算法(量化、KV cache),再讲系统(引擎、路由),最后讲端到端。这是工程师的思维,不是调包侠的思维。
- 建议的实操路径:
- 第一周:跑通
vllm serve meta-llama/Llama-3-70B-Instruct --quantization awq,观察gpu_memory_utilization、max_num_seqs参数影响。 - 第二周:用
genai-bench/vllm bench测 TTFT / TPOT / tokens/s,画曲线。 - 第三周:上 Speculative Decoding(用 EAGLE-2 或 Medusa),对比基准。
- 第四周:搭一个简易 AI Gateway(LiteLLM + Redis 缓存),跑通路由 + 缓存。
- 第一周:跑通
- 避坑:先别追 MoE / 投机解码 / prefix cache 这些花活,先把 1~5 打扎实——80% 的性能问题出在引擎选错 / batch 配错 / 量化不到位。
六、姊妹篇:@akshay_pachaar《Why your first token is slow》(KV Caching 第一性原理)
来源:Twitter / X 长文推文(图文+动画),作者
@akshay_pachaar(Lightning AI 前员工,Daily Dose of Data Science 联合创始人)。 原文性质:用 6 段 GIF 动画,从第一性原理讲清楚 KV Caching 这件事——为什么 ChatGPT/Claude 第一个字出来得慢,后面却能“刷“出来;以及为什么这个工程决策让 LLM 推理快 5 倍。 本文性质:完整翻译 + 动画文字描述 + 中文工程视角(“显存账 / 延迟账 / 并发账”)+ 与第一条推文节点 3(KV Cache)的互补深化。
6.1 原文逐节导读
开场:每个人用 ChatGPT 都见过的现象
“You’ve seen it every time you use ChatGPT or Claude. The first token takes noticeably longer to appear. Then the rest stream out almost instantly.”
“That’s not a UI quirk. It’s a deliberate engineering decision called KV caching. It makes LLM inference roughly 5x faster.”
“Let’s understand how it works, from first principles.”
导读:这是非常经典的“产品体验→底层原理“切入法——用一个用户每天都看到、但 99% 的人解释不了的现象反推到 KV Cache 这个机制。
工程师笔记:
- 后端视角:所谓“TTFT(Time-To-First-Token)“和“TPOT(Time-Per-Output-Token)“两个指标的体感差异,就是这条推文讲的事。
- Ops 视角:监控 dashboard 上 TTFT 飙升而 TPOT 正常——典型就是 KV cache miss(命中了不同 prefix,或新 prompt 没走 prefill 优化)。
Part 1 · How LLMs Generate Tokens(LLM 是怎么生成 token 的)
“The transformer processes all input tokens and produces a hidden state for each one. Those hidden states get projected into vocabulary space, producing logits (one score per word in the vocabulary).”
“But only the logits from the last token matter. You sample from them, get the next token, append it to the input, and repeat.”
“This is the key insight: to generate the next token, you only need the hidden state of the most recent token. Every other hidden state is an intermediate byproduct.”
导读:先把“自回归生成“这件事的物理过程摆出来——整段输入走一遍前向,每个位置都得到一个 hidden state,但只有最后一个位置的 logits 决定下一个 token。
工程师笔记:
- 这是为什么 LLM 推理不能像训练一样大批量并行——生成是 sequential 的。
- 也是为什么“中间位置的 hidden state“看起来“算完就丢“——算上但扔掉 vs 不算 vs 缓存起来,是三种截然不同的取舍,**KV Cache 是第三种“。
Part 2 · What Attention Actually Computes(注意力到底在算什么)
“Inside each transformer layer, every token gets three vectors: a query (Q), a key (K), and a value (V). Attention multiplies queries against keys for scores, then uses those scores to weight the values.”
“Now focus on just the last token.”
“The last row of QK^T uses:
- The query vector of the last token
- All key vectors in the sequence“
“The final attention output for that row uses:
- The same query vector
- All key and value vectors“
“So to compute the only hidden state we need, every attention layer requires Q from the latest token, and K and V from everything.”
导读:这是全文最关键的一步——把注意力计算锁定到最后一个 token 的视角上:Q 只需要最新的,K 和 V 需要历史所有的。
工程师笔记(极其重要):
- 这就是 KV Cache 存在的全部理由——K 和 V 是会被反复复用的,而 Q 每次都是新的。
- Q 不能缓存(每个新 token 都有自己的 Q),K 和 V 可以且应该缓存。
- 从公式视角:
Attention(Q_last, K_1..t, V_1..t) → hidden_state_for_next_token
Part 3 · The Redundancy(冗余:O(n²) 的浪费)
“Generating token 50 requires K and V vectors for tokens 1 through 50. Generating token 51 requires K and V vectors for tokens 1 through 51.”
“The K and V vectors for tokens 1 through 49 were already computed. They haven’t changed. Same inputs, same outputs. Yet the model recomputes them from scratch every step.”
“That’s O(n) redundant work per step. Over an entire generation, O(n²) wasted compute.”
导读:用一个具体的数字(token 50 vs token 51)让“冗余“这件事可见——前 49 个 token 的 K/V 已经算过、且不会变,但你每生成一个新 token 都要重新算一遍。
工程师笔记:
- 算法复杂度视角:纯生成 n 个 token,naive 实现是 O(n²) 的 K/V 投影。Llama-3-70B 上,n=2048 就要算 4M 次 Q/K/V 矩阵乘——这是纯粹的浪费。
- 工程视角:在 H100 上一次 4096x4096x8192 的矩阵乘约 ~2ms,O(n²) 的累计开销对 1000 token 的回复就是 1+ 秒——这就是 ChatGPT 不用 KV Cache 会“卡顿“的原因。
Part 4 · The Fix(修复方案:缓存 K/V)
“Instead of recomputing all K and V vectors at every step, store them. For each new token:”
- Compute Q, K, and V for only the newest token.
- Append the new K and V to the cache.
- Retrieve all previous K and V vectors from the cache.
- Run attention using the new Q against the full cached K and V.
“That’s KV caching. One new K and one new V per layer per step. Everything else comes from memory.”
“The attention computation still scales with sequence length (you’re attending over all keys and values). But the expensive projections to produce K and V happen only once per token, not once per step.”
导读:四步流程图,每步一行字。这是工程化最干净的表达:把“算 + 存 + 读 + 用“四件事分开。
工程师笔记:
- 算法视角:K/V 投影次数从 O(n²) 降到 O(n)——线性化。
- 工程视角:注意第 4 步——注意力本身还是要扫所有 K/V(O(n)),KV Cache 优化的是投影那部分,不是注意力本身。这是一个高频误解点。
- 后端视角:很多新手以为“KV Cache 让推理变成 O(1)“——错的。它只是把投影从 O(n²) 变成 O(n),注意力还是 O(n)。
Part 5 · Time-to-First-Token(第一个字为什么慢)
“When you send a prompt, the model processes the entire input in one forward pass, computing and caching K and V vectors for every token. This is the prefill phase, and it’s the most compute-intensive part of the request.”
“Once the cache is warm, each subsequent token needs only a single forward pass with one token. Fast.”
“That initial delay is called time-to-first-token (TTFT). Longer prompts mean longer prefills, which mean longer waits. Optimizing TTFT (chunked prefill, speculative decoding, prompt caching) is its own deep topic, but the dynamic is always the same: building the cache is expensive, reading from it is cheap.”
导读:把“第一次慢、后面快“这个产品现象精确归因到 prefill vs decode 的差异上。
工程师笔记:
- TTFT 由 prefill 决定,TPOT 由 decode 决定——这是 LLM 服务端最核心的两个延迟指标。
- prefill 是 compute-bound:处理整段 prompt,能用 tensor parallelism 摊到多卡。
- decode 是 memory-bound:每次只读一个 token,瓶颈在带宽(HBM)。
- 优化方向完全不同:TTFT 靠 chunked prefill(分段预填)+ prompt caching(共享前缀)+ speculative decoding(投机解码);TPOT 靠 KV cache 压缩(GQA/MQA)+ 量化(INT8 KV)+ speculative decoding。
Part 6 · The Tradeoff(代价:显存)
“KV caching trades compute for memory. Every layer stores K and V vectors for every token. For Qwen 2.5 72B (80 layers, 32K context, hidden dim 8192), the KV cache for a single request can consume several gigabytes of GPU memory. At hundreds of concurrent requests, it often exceeds the model weights themselves.”
“This is why grouped-query attention (GQA) and multi-query attention (MQA) exist: share key/value heads across query heads, cut memory, minimal quality loss. It’s also why doubling context length is hard. Double the window, double the KV cache per request, fewer concurrent users.”
导读:这是最容易被忽视的一段——KV Cache 不是免费的,它的成本是显存,而且这个成本是线性的:
KV cache 显存 ≈ 2 × n_layers × n_heads × head_dim × seq_len × bytes_per_element × n_concurrent_requests
工程师笔记(以 Qwen 2.5 72B 32K 为例精确算账):
| 参数 | 值 |
|---|---|
| 模型层数 L | 80 |
| 隐藏维 H | 8192 |
| 注意力头数(原始 MHA) | 64 |
| head_dim | 128 |
| 单请求 32K context 的 KV cache(FP16,无 GQA) | 2 × 80 × 64 × 128 × 32768 × 2B ≈ 80 GB |
| 模型权重本身(FP16) | ~140 GB |
| 结论 | 单条请求 KV cache = 模型权重的 57%,100 条并发 → 显存被 KV 塞爆 |
上面就是为什么 Qwen2.5 / Llama-3 / Mixtral 都用了 GQA(Grouped-Query Attention)——把 K/V 头数从 64 砍到 8(Llama-3 70B)或 4(Mixtral),显存直接减 8~16 倍,质量损失 < 1%。
tl;dr(原推文总结)
“KV caching eliminates redundant computation during autoregressive generation. Previous tokens always produce the same K and V vectors, so you compute them once and store them. Each new token only needs its own Q, K, and V. Then attention runs against the full cache.”
“5x speedup in practice. The cost is GPU memory, which becomes the binding constraint at scale. Every LLM serving stack (vLLM, TGI, TensorRT-LLM) builds on this idea.”
6.2 中文工程视角的“三本账“
推文讲的是机制,工程师真正关心的是账。下面把 KV Cache 这件事翻译成 3 本账:
账本 1 · 延迟账(Latency Budget)
| 阶段 | 算力性质 | 优化方向 | 监控指标 |
|---|---|---|---|
| Prefill | compute-bound(吃 FLOPS) | tensor parallelism、chunked prefill | TTFT(Time-To-First-Token) |
| Decode | memory-bound(吃带宽) | KV 压缩(GQA/MQA)、INT8 KV、spec decoding | TPOT(Time-Per-Output-Token) |
核心结论:KV Cache 让整段对话的延迟预算从 O(n²) 降到 O(n)——n=1000 token 时是 1000 倍的差距。
账本 2 · 显存账(VRAM Budget)
KV cache 总显存公式:
KV_cache_memory = 2 × L × H_kv × d × S × B × bytes_per_elem其中:
L= 层数(如 80)H_kv= K/V 头数(MHA=64,GQA=8,MQA=1)d= head_dim(如 128)S= 序列长度(如 32768)B= 并发请求数bytes_per_elem= 2(FP16)/ 1(INT8 KV)
Llama-3-70B(8 H_kv,GQA)+ 8K context + 32 并发 + FP16 KV:
= 2 × 80 × 8 × 128 × 8192 × 32 × 2B
≈ 4 GB / 请求 × 32 = 128 GB→ 这就是为什么 70B 模型必须上 H100 80GB × 2 张卡,或者用更狠的 KV 量化(INT8 KV 直接砍一半)。
账本 3 · 并发账(Concurrency Budget)
单卡能支撑的并发 ≈ (总显存 - weights) / 单请求 KV cache:
concurrent = (80GB - 14GB) / 4GB ≈ 16 requests/card→ 这就是 SLO 的天花板。要让 100 QPS,必须上多卡 + PagedAttention(vLLM)——把 KV 切成不连续物理页,避免碎片浪费。
七、两条推文对照与综合学习路径
第一条(@itsmenikhitha)是路线图(9 个节点的“广度“地图); 第二条(@akshay_pachaar)是单点深挖(KV Cache 这件事的“深度“解剖)。 它们的最佳学习姿势是:先用 @itsmenikhitha 的地图知道自己站在哪,再用 @akshay_pachaar 的解剖补一个具体章节的血肉。
7.1 推文风格与定位对照
| 维度 | @itsmenikhitha(路线图) | @akshay_pachaar(KV Cache 解剖) |
|---|---|---|
| 核心载体 | 9 个外部资源链接(博客 + 论文 + 文档) | 6 段自创 GIF 动画 + 文字解说 |
| 学习姿势 | 自上而下(地图 → 选 → 跳) | 自下而上(问题 → 第一性原理 → 公式) |
| 适合谁 | 想建立完整 Inference 知识体系的人 | 想真懂 KV Cache 是什么、为什么的人 |
| 学习时长 | 1~3 个月(每个节点一篇 30 分钟) | 30 分钟(一个完整长推) |
| 可执行产出 | 一份自己的 Inference 学习笔记 / Roadmap | 能向同事讲清楚“为什么 ChatGPT 第一字慢“ |
| 重叠章节 | 节点 3(KV Cache)↔ 本篇核心内容 | 节点 3 的全景上下文 |
7.2 内容深度对照(围绕 KV Cache)
| 维度 | @itsmenikhitha(节点 3) | @akshay_pachaar(推文主体) |
|---|---|---|
| KV Cache 是什么 | 一句话介绍 | 4 步流程图(compute → append → retrieve → run attention) |
| 为什么需要 KV Cache | 没讲 | 显式说明 O(n²) 冗余的“肉眼可见“例子(token 50 vs 51) |
| 代价 | “KV cache 随序列长度线性增长” | 显式给出 Qwen-2.5-72B 在 32K 下的具体账(数 GB / request) |
| 显存公式 | “KV cache 可能比 weights 还大”(定性) | 通过 GQA/MQA 的存在反推“这是个真问题“ |
| TTFT 关联 | 单独作为节点 6(Forward Pass & Inference Speed) | 在 Part 5 显式串联 prefill + TTFT |
| 工程化方案 | 节点 4(Speculative Decoding / Kernel Fusion / Prefix Cache) | 提到 vLLM / TGI / TensorRT-LLM 都“build on this idea“ |
结论:@akshay_pachaar 在 KV Cache 这件事上做到了 3 倍深度,@itsmenikhitha 给出了 9 个相邻技术的导航。两者完全互补。
7.3 综合学习路径(结合两份资源)
[第一周] 速览 @itsmenikhitha 的 9 个节点,标记自己已经会的、感兴趣的、没听过的
↓
[第二周] 按 1→2→3→4→5 顺序读完推荐资源
↓
[第三周] 对节点 3(KV Cache),用 @akshay_pachaar 推文补血肉
↓
- 看完 6 段 GIF,能复述 4 步流程
- 用 Llama-3-8B 在 vLLM 里跑 benchmark,对比开/关 KV Cache 的 TTFT
- 计算自己的模型在目标并发下的 KV cache 显存
↓
[第四周] 回到节点 6 / 7 / 8 / 9,把 KV Cache 知识串到 TTFT、MoE 显存、LLM Routing 里
↓
[持续] 每周一篇博客 / 团队分享,把"理解"固化成"能讲出来"7.4 5 分钟对照清单(给老板的 TL;DR)
如果你只能记住 5 件事,把这两条推文压缩成下面这张卡:
| # | 关键事实 | 出处 |
|---|---|---|
| 1 | LLM 推理自回归生成,所以慢——不做优化是 O(n²) | @akshay_pachaar Part 3 |
| 2 | KV Cache 把 K/V 投影从 O(n²) 降到 O(n),代价是显存 | @akshay_pachaar Part 4 / @itsmenikhitha 节点 3 |
| 3 | TTFT(prefill,决定)≠ TPOT(decode,决定),要分开监控 | @akshay_pachaar Part 5 / @itsmenikhitha 节点 6 |
| 4 | KV cache 在 70B+32K 下单请求就能吃掉几 GB,并发 100 时反超 weights | @akshay_pachaar Part 6 |
| 5 | 救兵:GQA/MQA(砍头)、PagedAttention(碎片整理)、Speculative Decoding(投机) | @akshay_pachaar Part 6 / @itsmenikhitha 节点 4+5 |
7.5 一句话总结
@itsmenikhitha 告诉你“Inference Engineering 这片大陆长什么样“,@akshay_pachaar 告诉你“这片大陆上最有价值的一座山是怎么从地壳里冒出来的“——把两份资源拼起来,你就拥有从工程师到架构师的完整视角。
附录 · 元信息
| 推文 | 作者 | 链接 | 性质 |
|---|---|---|---|
| 第一条 | @itsmenikhitha | https://x.com/itsmenikhitha/status/2097218509629751778 | Inference Engineering 学习路线图(9 节点 + 资源链接) |
| 第二条 | @akshay_pachaar | https://x.com/akshay_pachaar/status/2020840784782913605 | KV Caching 第一性原理图解长文 |
笔记位置:Inference Engineering 学习路线总结 生成时间:2026-09-09 风格:长文 + 翻译/导读原文 + 中文工程视角(后端 / Ops / 平台)+ 结构化对照表 + 工程师笔记