LogoCookLLM Docs
LogoCookLLM Docs
HomeCookLLM

Principles

Tokenization
Tokenization BasicsBPE AlgorithmGPT TokenizersBPE Training Engineering
Model Architecture
Transformer LM
From token ids to logitsEmbedding and LM Head
Attention Mechanisms
From Self-Attention to GQAAttention Sink
Position Encoding
Position Encoding BasicsRoPE Math DerivationRoPE ImplementationLength Extrapolation
GPU Programming Basics
GPU Architecture BasicsTensor LayoutTriton Basics: Vector Add
FlashAttention
Flash Attention PrinciplesFrom Naive Implementation to Auto-TuningBlock Pointers and Multi-Dim SupportCausal Masking OptimizationGrouped Query AttentionBackward Pass Implementation
Distributed Training
Data ParallelismZeRO OptimizerFully Sharded Data ParallelTensor ParallelismPipeline ParallelismMulti-Dimensional Hybrid Parallelism

Hands-on Training

Overview
Pretraining
Pretraining DataTokenizer TrainingModel ArchitectureData PipelineTraining LoopMonitoring and Validation
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 行为。

Table of Contents

概述
章节内容
参考资料