LogoCookLLM文档
LogoCookLLM文档
首页CookLLM

原理精讲

词元化
Tokenization 基础BPE 算法详解GPT 系列 TokenizerBPE 训练工程化
模型架构
Transformer LM
从 token ids 到 logitsEmbedding 与 LM Head
Attention 机制
Self-Attention 到 GQAAttention Sink
位置编码
位置编码基础RoPE 数学推导RoPE 代码实现长度外推
GPU 编程基础
GPU 架构基础张量布局Triton 入门:向量加法
FlashAttention
Flash Attention 原理详解从朴素实现到 Auto-TuningBlock Pointer 与多维支持Causal Masking 优化Grouped Query Attention反向传播实现
分布式训练
数据并行ZeRO 优化器全分片数据并行张量并行流水线并行多维混合并行
推理优化
KV CacheContinuous BatchingPagedAttention

动手训练

概述
预训练
预训练数据Tokenizer 训练模型架构数据流水线训练循环监控与验证
X (Twitter)
系统工程

推理优化

从 KV cache 到批处理调度,理解 LLM 推理为什么慢、怎么加速

概述

训练关心的是"一次把整段序列并行算完",推理却是"一个 token 接一个 token 地挤出来"。这个差别决定了推理有一套自己的瓶颈和优化手段。本系列从 KV cache 讲起,逐步深入批处理与显存管理,回答一个核心问题:为什么推理慢,以及怎么让它更快、更省显存。

建议先掌握 Transformer 前向传播 与 注意力机制。本系列默认你熟悉 Q/K/V 和自回归生成。

章节内容

KV Cache

缓存历史 token 的 K/V,让 decode 每步只算新 token,以及它的显存代价

Continuous Batching

静态批处理让 GPU 大量空转,迭代级调度与 chunked prefill 怎么把它填满

PagedAttention

按最大长度预留连续显存只用上两三成,用操作系统的分页思路把利用率做到 96%

参考资料

  • Orca: A Distributed Serving System for Transformer-Based Generative Models (OSDI '22):迭代级调度与 selective batching 的原始论文。
  • Efficient Memory Management for LLM Serving with PagedAttention (SOSP '23):vLLM 的显存分页管理,块表、写时复制与共享。
  • Sarathi-Serve: Taming Throughput-Latency Tradeoff in LLM Inference (OSDI '24):chunked prefill 与 stall-free 调度。
  • DistServe: Disaggregating Prefill and Decoding (OSDI '24):goodput 指标定义与 prefill/decode 分离。
  • vAttention: Serving LLMs without PagedAttention (ASPLOS '25):对非连续布局代价的批评,以及基于 CUDA 虚拟内存 API 的替代方案。
  • Fast Transformer Decoding: One Write-Head is All You Need:MQA 的原始论文,KV cache 压缩的起点。
  • How continuous batching enables 23x throughput in LLM inference:静态与连续批处理的对比实验。
  • vLLM V1: A Major Upgrade to vLLM's Core Architecture:prefix caching 从默认关闭到默认开启的演进背景。
  • vLLM 调度器与块分配器源码:等待队列、token 预算、抢占、引用计数的真实实现。
  • Hugging Face: Text generation strategies:generate 与缓存相关的 API 行为。

多维混合并行

ParallelContext 坐标系统与 TP+DP+PP 的工业级组合

KV Cache

decode 每步重算历史 token 的 K/V 是纯浪费,KV cache 把它省掉,代价是显存

目录

概述
章节内容
参考资料