zero-to-sglang README

项目的 README,来自 github.com/datawhalechina/zero-to-sglang。合并为一个 markdown 文件:README.md。

zero-to-sglang banner

zero-to-sglang

English 简体中文

📚 Build SGLang from Scratch

A hands-on tutorial on LLM inference: build a mini-sglang from scratch, then read the real SGLang source.

GitHub stars GitHub forks Language GitHub Project Online Reading Download PDF

Start from what inference really is, build a mini-sglang by hand, then read the real SGLang source

Datawhale × RadixArk


Course status: All chapters, from Part 0 through Part IV, are complete in both English and Chinese. The full companion video series is expected to be completed by the end of November 2026, with videos published on YouTube and Bilibili. Links will be added to the Syllabus below as videos are published. You can switch to 简体中文 at any time. Want to help? See Contributing.

🎯 About

  Large models have moved on from the training race to the inference race. Once applications took off, inference cost and latency became the real bottleneck: on the same GPU, a good inference engine can serve several times more requests. Yet there are few tutorials on inference engines. They either stay at the concept level without touching code, or drop you straight into the SGLang source with no idea where to start.

  This course fills that gap. The first half starts from the core questions of inference: why KV Cache exists, how prefill differs from decode, what compute-bound and memory-bound actually mean. The middle part walks you through building a mini-sglang from scratch: forward pass, generation, KV Cache, HTTP serving, Continuous Batching, Paged KV Cache, RadixAttention, added one at a time. The last part returns to the real SGLang, explains how its cutting-edge optimizations are actually implemented, and finally shows you how to land your first solid PR in SGLang.

  The project is jointly launched by Datawhale and RadixArk (the company founded by the SGLang team). SGLang is technically solid and actively iterated in the inference framework space. If you like this project, please give the official SGLang repository a ⭐.

✨ What you will learn

📋 Prerequisites

📖 Syllabus

Status legend: ✅ Done 🔄 In progress 📝 Needs polish 🚧 Planned ⏸️ Paused

Chapters in bold are written by SGLang core members.

Chapter Key content Status
Part 0 — Before you learn
0.1 Coding Ethics and Open-Source Spirit Own your code, communicate like a human, profile first, the open-source spirit ✅
0.2 Deploy Your First SGLang Server Environment setup; run Qwen3-0.6B with SGLang on your own GPU ✅
Part I — Foundations (concepts only, no code, no GPU)
1. Introduction to LLM What an LLM is and how it evolved, the Transformer architecture, autoregressive generation, key concepts ✅
2. Introduction to Inference Training vs. inference, prefill/decode, compute-bound vs. memory-bound, the Roofline model ✅
3. Introduction to GPU GPU architecture basics, how LLM inference executes on a GPU, understanding inference bottlenecks from the hardware ✅
4. KV Cache: The Core Data Structure of Inference Deriving KV Cache from attention, cache lifecycle, quantitative memory analysis ✅
5. Introduction to Benchmark Core metrics such as TTFT / TPOT / ITL / Goodput, percentiles and tail latency, how to design, run and read a benchmark ✅
Part II — Build Your Own Mini SGL
1. mini-sglang: What an Inference Engine Looks Like Overall architecture of an inference engine, module breakdown, roadmap for this part ✅
2. Inside SGLang: The Path of a Request (Concepts) · Code Walkthrough The full lifecycle of a request from arrival to response ✅
3. Your First 200 Lines: Forward Pass and Generation (Concepts) · Code Walkthrough A source walkthrough of embeddings, attention, the LM head, and generation ✅
4. KV Cache: From O(n²) to O(n) (Concepts) · Code Walkthrough Implementing the cache and optimizing attention computation ✅
5. HTTP and Concurrent Requests (Concepts) · Code Walkthrough HTTP basics and request flow; implementing isolation, streaming, and cancellation ✅
6. Continuous Batching and the Scheduler (Concepts) · Code Walkthrough Continuous batching, scheduler state, resource limits, and latency/throughput tradeoffs ✅
7. Paged KV Cache and Memory Management (Concepts) Paged KV Cache and GPU memory management ✅
8. RadixAttention and Prefix Caching (Concepts) · Code Walkthrough RadixAttention and prefix caching ✅
9. Multi-process & Tensor Parallelism(Concepts) · Code Walkthrough Multi-process execution and tensor parallelism ✅
10. Speculative Decoding (Concepts) · Code Walkthrough Drafting and verification, greedy and sampling-based acceptance, rejection correction, and performance tradeoffs ✅
Part III — Advanced Inference Technique (deep into the real SGLang)
1. Attention Backends (FlashInfer / Triton / FA3 / FlashMLA) Backend responsibilities, tiled Attention, implementation tradeoffs, and selection ✅
2. Quantization and Low-Precision Inference Numeric formats and scales, Decode speedup and KV Cache capacity, accuracy tradeoffs, quantization labels and loading ✅
3. Hierarchical Caching L1/L2/L3 cache tiers, prefix matching, backup and restoration, prefetching and performance tradeoffs ✅
4. Scaling Out: DP Attention, EP, PP TP scaling limits, DP Attention, EP, PP, and combined parallelism ✅
5. Prefill-Decode Disaggregation Prefill interference, request handoff, KV transfer costs, resource allocation, and workload evaluation ✅
Part IV — How to Make Contribution to SGLang (optional)
1. Deploying SGLang with the Cookbook Recipe selection, launch configuration, service validation, and a small benchmark ✅
2. Measuring It All: Profiling and Trace Analysis Profiling fundamentals and a merged PR case study: traces, code changes, and validation ✅
3. SGLang PR Workflow Open-source collaboration, PR lifecycle, CI and review, and first contributions ✅

🗓️ Writing schedule

Writing began on August 24, 2026. All chapters are complete in both English and Chinese as of November 10, 2026.

Part Schedule Status
Part 0 — Before you learn Done ✅
Part I — Foundations Done ✅
Part II — Build Your Own Mini SGL Done ✅
Part III — Advanced Inference Technique Done ✅
Part IV — How to Make Contribution to SGLang Done ✅
Full zero-to-sglang companion video series Expected completion: end of November 2026. YouTube / Bilibili links will be added to the Syllabus as videos are published. 🔄

🚀 Quick start

# Clone the repository
git clone https://github.com/datawhalechina/zero-to-sglang.git
cd zero-to-sglang
# Install dependencies as each chapter requires

Learning path

1️⃣ Read Part I to build an overall picture of inference (no GPU needed)
2️⃣ Follow Part II to build a mini-sglang from 0 to 1
3️⃣ Move to Part III and understand the frontier optimizations against the real SGLang source
4️⃣ Finish Part IV: profiling, trace analysis, and the full PR workflow

Project structure

zero-to-sglang/
├── course-material/         # Course text
│   ├── ch/                  # Chinese edition (read online at /ch/)
│   │   ├── part0/           # Part 0: Before you learn
│   │   ├── part1/           # Part I: Foundations (complete)
│   │   ├── part2/           # Part II: Build Your Own Mini SGL (complete)
│   │   ├── part3/           # Part III: Advanced Inference Technique (complete)
│   │   ├── part4/           # Part IV: How to Make Contribution to SGLang (complete)
│   │   └── WRITING_TEMPLATE.md
│   └── eng/                 # English edition (read online at /eng/, complete)
│       ├── part0/
│       └── WRITING_TEMPLATE.md
├── community/               # Community corner: build logs, pitfalls, study notes
│   ├── ch/
│   └── eng/
├── docs/.vitepress/         # VitePress site config
├── README.md                # Project overview (English, default)
├── README_zh.md             # Project overview (Chinese)
├── README_en.md             # Legacy English entry (links to README.md)
└── .gitignore               # Git ignore rules

🤝 Contributing

We are an open community and welcome contributions of every kind. Before you start, please read Part 0 to understand the coding ethics and open-source spirit we expect.

1. Improve the course text

2. Share your practice

Community content goes under the matching directory in community/eng/ (English) or community/ch/ (Chinese).

How to open a PR

  1. Fork the repository and create a branch from main
  2. PR requirements are in community/eng/PR_requirement.md; check every item before submitting

❓ FAQ

Q: Can I follow the course without a GPU?

Yes. Part I involves no GPU at all. Most of the code in Part II can be debugged on CPU, but full performance verification and the deeper content of Part III are best done on a GPU (cloud is fine).

Q: Do I need to master CUDA first?

No. The course starts from inference concepts. Low-level topics such as attention backends only come in Part III, and you can catch up on CUDA as needed at that point.

💬 Reader Community

Join the zero-to-sglang reader community to learn together, ask questions, and share ideas: Join our Discord community

👥 Contributors

We thank every developer who has contributed to this project!

Special thanks

🎓 Citation

If zero-to-sglang helps your research or work, please cite it:

@misc{zero_to_sglang2026,
  title  = {zero-to-sglang: Building an LLM Inference Engine from Scratch},
  author = {TODO},
  year   = {2026},
  url    = {https://github.com/datawhalechina/zero-to-sglang},
  note   = {GitHub repository}
}

⭐ Star History

If this project helps you, please give it a Star ⭐️!

📄 License

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


Helping more people learn to build LLM inference engines, systematically

Made with ❤️ by Datawhale & RadixArk

zero-to-sglang (datawhalechina/zero-to-sglang)

正在搜索数据库…

这是做什么的?这是 SGLang 与 Datawhale 官方合作的大语言模型推理课程:讲解推理引擎的工作原理,带你从零实现一个 mini-sglang,然后深入剖析真正的 SGLang 源代码。
这是一门阅读型的课程,还是需要克隆下来运行的代码库?主要是课程:以文档网站的形式提供文字章节(用 VitePress 搭建),另有一个参考实现 mini-sglang,供你在第二部分(Part II)中跟着一起写代码。
如果我现在开始,学习路径是什么样的?分四步:先读第一部分(Part I)掌握推理基础知识(不需要 GPU),再在第二部分(Part II)从零构建一个 mini-sglang,接着在第三部分(Part III)学习真实 SGLang 的优化技术,最后完成第四部分(Part IV)的性能分析、trace 分析和 PR 工作流程。
开始之前我需要会些什么?熟练的 Python 以及基本的软件工程习惯,PyTorch 和神经网络基础,还有线性代数和概率论。CUDA 知识不是必需的——课程会随进度补上你需要的内容。
课程写完了吗,现在能全部读完吗?还在持续更新中。项目于 2026 年 8 月 24 日启动,大约每周更新一章:第 0 部分(Part 0)已完成,第一部分(Part I)接近完工,但第二部分(Part II)的大多数章节还在准备中,第三、四部分(Parts III-IV)预计 2026 年 12 月前完成。
我能把这些材料用于自己的工作坊或公司培训吗?要小心:它采用 CC BY-NC-SA 4.0 许可证,你必须注明出处、以相同许可证共享改编内容,而且只能用于非商业用途——公司培训需要另行获得授权。
学完第二部分(Part II)后,我实际能做出什么?一个能跑起来的 mini-sglang 引擎:前向传播和自回归生成、KV Cache、处理并发请求的 HTTP 服务器、带调度器的 continuous batching、Paged KV Cache、支持前缀缓存的 RadixAttention、多进程张量并行以及投机解码——这些是一步一步逐步加上的。
这门课有英文版吗?有——项目同时提供英文和中文版本。翻译好的章节放在 course-material/eng/ 目录下,参与翻译也是项目列出的贡献方式之一。
正课开始前的第 0 部分(Part 0)讲什么?包含两个入门章节:编程道德与开源精神,以及部署你的第一个 SGLang 服务器——在接触任何理论之前,先搭好环境,在自己的 GPU 上用真实的 SGLang 跑起 Qwen3-0.6B。
大小
132.6 MB
星标
2.3K
时长
3个月
提交 · 90 天