面向大模型开发者的 SGLang 系统化开源教程:从推理基础与环境搭建开始,逐步学习模型部署、结构化生成、服务开发和性能优化, 结合实战案例带你从 0 到 1 掌握 SGLang,构建高性能 LLM 推理应用
zero-to-sglang
📚 《从零手搓SGLang》
A hands-on tutorial on LLM inference: build a mini-sglang from scratch, then read the real SGLang source.
从推理的本质出发,手搓一个 mini-sglang,再吃透真实 SGLang 的源码
Datawhale × RadixArk
## 🎯 项目介绍
  大模型已经从「训练」卷到「推理」了。应用爆发之后,推理成本和延迟成了真正的瓶颈:同样一块卡,好的推理引擎能多扛好几倍的请求。但市面上讲推理引擎的教程不多,要么只讲概念不动手,要么直接让你啃 SGLang 源码,从哪儿下手都不知道。
  这门课想补上这个缺口。前半部分,我们从推理最核心的问题讲起:KV Cache 为什么要有、prefill 和 decode 差在哪、compute-bound 和 memory-bound 到底是什么意思。中间部分,带着你从零手搓一个 mini-sglang:前向、生成、KV Cache、HTTP 服务、Continuous Batching、Paged KV Cache、RadixAttention,一样一样加上去。后半部分,回到真实的 SGLang,讲清楚它那些前沿优化具体是怎么做的,最后教你怎么给 SGLang 提第一个像样的 PR。
  本项目由 Datawhale 和 RadixArk(SGLang 团队创立的公司)共同发起,值得信赖!SGLang 在推理框架领域技术扎实、迭代活跃,认可这个项目的话,欢迎去 [sglang官方仓库](https://github.com/sgl-project/sglang) 点个 ⭐。
## ✨ 你能学到什么
## 📋 前置要求
## 🔗 相关链接
## 📖 课程目录
状态图例:✅ 已完成 🔄 更新中 📝 待完善 🚧 筹备中 ⏸️ 暂缓
>
标注 粗体 的章节由 SGLang 官方成员编写。
| 章节 | 关键内容 | 状态 |
|---|---|---|
| Part 0 — 开课之前 | ||
| [0.1 Coding Ethics and Open-Source Spirit(编码伦理与开源精神)](course-material/ch/part0/Part0-编码伦理与开源精神.md) | 对自己代码负责、沟通时说人话、Profile 永远是第一步、开源精神 | ✅ |
| [0.2 Deploy Your First SGLang Server(部署你的第一个 SGLang 服务)](course-material/ch/part0/Part0-部署你的第一个SGLang服务.md) | 环境设置,在你自己的 GPU 上用 SGLang 跑起 Qwen3-0.6B | ✅ |
| Part I — 基础概念(concepts only,无代码、无 GPU) | ||
| [1. Introduction to LLM](course-material/ch/part1/第1章_LLM入门.md) | LLM 的定义与发展脉络、Transformer 架构、自回归生成、关键基础概念 | ✅ |
| [2. Introduction to inference](course-material/ch/part1/第2章_推理入门.md) | 训练 vs 推理、prefill/decode、compute-bound vs memory-bound、Roofline model | ✅ |
| 3. Introduction to GPU | GPU 架构基础、LLM 推理在 GPU 上的执行流程、从硬件理解推理瓶颈 | 🔄 |
| [4. KV Cache: The Core Data Structure of Inference](course-material/ch/part1/第4章_推理的核心数据结构入门.md) | 从 Attention 推导 KV Cache、cache 生命周期、显存占用定量分析 | ✅ |
| [5. Introduction to Benchmark](course-material/ch/part1/第5章_Benchmark入门.md) | TTFT / TPOT / ITL / Goodput 等核心指标、百分位与尾延迟、怎么设计/跑/读 benchmark | 🔄 |
| Part II — 从零手搓 Mini SGL | ||
| 1. mini-sglang:推理引擎长什么样 | 推理引擎的总体架构、模块划分、本部分的 roadmap | 📝 |
| 2. Inside SGLang: The Path of a Request | 一个请求从进入到返回的完整生命周期 | 🚧 |
| 3. Your First 200 Lines: Forward Pass and Generation | 手写前向传播与自回归生成循环 | 🚧 |
| 4. KV Cache: From O(n²) to O(n) | 缓存实现与注意力计算优化 | 🚧 |
| 5. Serving It: HTTP and Concurrent Requests | HTTP 服务化、并发请求处理 | 🚧 |
| 6. Continuous Batching and the Scheduler | 连续批处理与调度器设计 | 🚧 |
| 7. Paged KV Cache and Memory Management | 分页 KV Cache 与显存管理 | 🚧 |
| 8. RadixAttention and Prefix Caching | RadixAttention 与前缀缓存 | 🚧 |
| 9. Multi-process & Tensor Parallelism | 多进程与张量并行 | 🚧 |
| 10. Speculative Decoding | 投机解码 | 🚧 |
| Part III — 高级推理技术(深入真实 SGLang) | ||
| 1. Attention Backends(FlashInfer / Triton / FA3 / FlashMLA)and CUDA Graph | 主流 Attention 后端对比与 CUDA Graph | 🚧 |
| 2. Quantization and Low-Precision Inference | 量化与低精度推理 | 🚧 |
| 3. Hierarchical Caching | 分层缓存 | 🚧 |
| 4. Scaling Out: DP Attention, EP, PP | 横向扩展:DP Attention / EP / PP | 🚧 |
| 5. Prefill-Decode Disaggregation | Prefill-Decode 分离 | 🚧 |
| Part IV — 如何为 SGLang 做贡献(可选) | ||
| 1. Deploying SGLang with the Cookbook | 使用 Cookbook 部署 SGLang | 🚧 |
| 2. Measuring It All: Profiling and Trace Analysis | Profiling 与 Trace 分析 | 🚧 |
| 3. SGLang PR Workflow | SGLang PR 全流程 | 🚧 |
## 🗓️ 编写进度
起始日期:2026 年 8 月 24 日(周一)。每章约一周,加粗章节由 SGLang 官方成员编写。
| 部分 | 时间安排 | 状态 |
|---|---|---|
| Part 0 — 开课之前 | 已完成 | ✅ |
| Part I — 基础概念 | 8.24 ~ 9.06 编写,9.07 ~ 9.13 review | 🔄 |
| Part II — 从零手搓 Mini SGL | 9.14 ~ 10.04 编写,10.05 ~ 10.10 review | 🚧 |
| Part III — 高级推理技术 | 10.10 ~ 11.15 | 🚧 |
| Part IV — 如何为 SGLang 做贡献 | 11.16 ~ 12.6 | 🚧 |
## 🚀 快速开始
# 克隆仓库 git clone https://github.com/datawhalechina/zero-to-sglang.git cd zero-to-sglang # 安装基础依赖(根据具体章节需求安装)
### 学习路径
1️⃣ 读 Part I,先把推理的整体认知建立起来(不需要 GPU)
2️⃣ 跟着 Part II,从 0 到 1 手搓一个 mini-sglang
3️⃣ 进 Part III,对照真实 SGLang 源码理解前沿优化
4️⃣ 完成 Part IV,走通 profiling、trace 分析和 PR 全流程
### 项目结构
zero-to-sglang/ ├── course-material/ # 课程正文 │ ├── ch/ # 中文(在线阅读 /ch/) │ │ ├── part0/ # Part 0:开课之前 │ │ ├── part1/ # Part I:基础概念(更新中) │ │ ├── part2/ # Part II:从零手搓 Mini SGL(规划中)