CS336:从零开始的语言建模
摘要
斯坦福大学提供一门综合课程CS336,学生将从零开始构建语言模型,涵盖数据收集、Transformer构建、训练和评估。
暂无内容
查看缓存全文
缓存时间: 2026/06/01 16:43
# 斯坦福 CS336 | 从零构建语言模型
来源:https://cs336.stanford.edu/
## 内容
### 这门课讲什么?
语言模型是现代自然语言处理(NLP)应用的基石,并开创了用单一通用系统解决多种下游任务的新范式。随着人工智能(AI)、机器学习(ML)和NLP领域的持续发展,深入理解语言模型对科学家和工程师都至关重要。本课程旨在通过引导学生完整经历开发自己的语言模型的整个过程,为他们提供对语言模型的全面理解。受那些从零构建整个操作系统的操作系统课程启发,我们将带领学生走过语言模型创建的每个方面,包括预训练数据的收集与清洗、Transformer模型构建、模型训练以及部署前的评估。
### 先修要求
- **精通 Python**:大部分课程作业使用 Python。与大多数其他AI课程不同,这里给学生提供的脚手架极少。你需要编写的代码量至少比其他课程多一个数量级。因此,精通 Python 和软件工程至关重要。
- **深度学习和系统优化经验**:课程的一个重要部分涉及让神经语言模型在多个机器的 GPU 上快速高效运行。我们希望学生能非常熟悉 PyTorch,并了解基本系统概念(如内存层次结构)。
- **大学微积分、线性代数**(例如 MATH 51, CME 100):应能自如理解矩阵/向量符号及运算。
- **基础概率与统计**(例如 CS 109 或同等课程):应了解概率、高斯分布、均值、标准差等基础概念。
- **机器学习**(例如 CS221, CS229, CS230, CS124, CS224N):应熟悉机器学习和深度学习的基础知识。
注意:这是一门 5 学分的课程。本课程非常注重实现,请预留足够时间。
## 课程作业
### 作业
- **作业1:基础知识** (https://github.com/stanford-cs336/assignment1-basics/tree/main) - 实现训练标准 Transformer 语言模型所需的所有组件(分词器、模型架构、优化器)。 - 训练一个最小的语言模型。
- **作业2:系统** (https://github.com/stanford-cs336/assignment2-systems/tree/main) - 使用高级工具对作业1中的模型和层进行性能分析和基准测试,通过自己的 Triton FlashAttention2 实现优化注意力机制。 - 构建一个内存高效、分布式的作业1模型训练代码版本。
- **作业3:扩展** (https://github.com/stanford-cs336/assignment3-scaling/tree/main) - 理解 Transformer 各部分的功能。 - 查询训练 API 以拟合扩展法则,预测模型扩展。
- **作业4:数据** (https://github.com/stanford-cs336/assignment4-data/tree/main) - 将原始 Common Crawl 转储转换为可用的预训练数据。 - 执行过滤和去重以提升模型性能。
- **作业5:对齐与推理强化学习** (https://github.com/stanford-cs336/assignment5-alignment/tree/main) - 应用监督微调和强化学习,训练语言模型在解决数学问题时进行推理。 - **可选第二部分** (https://github.com/stanford-cs336/assignment5-alignment/blob/main/cs336_spring2025_assignment5_supplement_safety_rlhf.pdf):实现并应用安全对齐方法(如 DPO)。
所有(当前暂定的)截止日期列在日程表 (https://cs336.stanford.edu/#schedule) 中。
### 自学的 GPU 计算
如果你在家自学,可以通过云服务商获取 GPU 计算资源来完成作业。
以下是一些选项(截至2026年3月28日,单块 B200 GPU 的公开价格):
- Modal (https://modal.com/)(赞助商):**6.25美元/小时**。每月提供 30 美元免费计算额度。仅按实际计算量收费(无空闲资源),其用户体验使得在本地开发和大规模 GPU 实验间切换变得简单。(Modal 定价 (https://modal.com/pricing))
- Lambda Labs (https://lambda.ai/):**6.69美元/小时**(Lambda 定价 (https://lambda.ai/pricing))
- RunPod (https://www.runpod.io/):**4.99美元/小时**(RunPod 定价 (https://www.runpod.io/pricing))
- Nebius (https://nebius.com/):**5.50美元/小时**(可抢占实例 3.05美元/小时)(Nebius 定价 (https://nebius.com/prices))
- Together (https://www.together.ai/):**7.49美元/小时**,最少8块 GPU,长期承诺更便宜(Together 定价 (https://www.together.ai/pricing#gpu-clusters))
为方便省费用,我们建议先在 CPU 上调试实现的正确性,然后使用 GPU(数量按作业推荐)完成训练运行(A1, A4, A5)或 GPU 操作基准测试(A2)。
### 荣誉准则
与斯坦福所有其他课程一样,我们认真对待学生荣誉准则 (https://ed.stanford.edu/academics/masters-handbook/honor-code)。请遵守以下政策:
- **协作**:允许学习小组,但学生必须理解并独立完成自己的作业,每名学生提交一份作业。如果曾小组合作,请在作业开头写上小组成员的名字。如有疑问,请咨询协作政策。
- **AI 工具**:允许使用 ChatGPT 等 LLM 回答低级编程问题或关于语言模型的高级概念性问题,但禁止直接使用它们解决问题。强烈建议在完成作业时在 IDE 中禁用 AI 自动补全(例如 Cursor Tab, GitHub CoPilot)(非 AI 自动补全,如函数名自动补全完全可以)。我们发现 AI 自动补全会使深入理解内容变得更加困难。详见 AI 政策 (https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8)。
- **现有代码**:你将实现的许多内容在网上已有实现。我们提供的讲义将自包含,因此你无需查阅第三方代码即可自行实现。因此,除非讲义另有说明,否则你不应查看任何现有代码。
### 提交课程作业
- 所有课程作业均通过 Gradescope 在规定截止日期前提交。请勿通过邮件提交。
- 如有任何问题,请在 Slack 中提问或联系课程助教。
- 截止日期前可多次提交:我们只评阅最后一次提交。
- 提交部分完成的工作总比不提交好。
### 迟到天数
- 每位学生有 **6 个迟到日可用**。一个迟到日可将截止日期延长24小时。
- 每项作业最多使用 3 个迟到日。
### 重新评分请求
如果你认为课程工作人员在评分上犯了客观错误,可以在成绩公布后 3 天内通过 Gradescope 提交重新评分请求。
### 赞助商
我们感谢 Modal (https://modal.jobs/) 为本次课程赞助计算资源。
## 日程表 (YouTube 播放列表 (https://www.youtube.com/watch?v=JuoVZkPBiKk&list=PLoROMvodv4rMqXOcazWaTUHhq-yembLCV))
# | 日期 | 描述 | 课程材料 | 截止日期
--- | --- | --- | --- | ---
1 | 3月30日 周一 | 概述、分词 [Percy] | lecture_01.py (https://cs336.stanford.edu/lectures/?trace=lecture_01) | 作业1 **发布** [代码](https://github.com/stanford-cs336/assignment1-basics/tree/main) [预览](https://github.com/stanford-cs336/assignment1-basics/blob/main/cs336_assignment1_basics.pdf)
2 | 4月1日 周三 | PyTorch (einops)、资源核算 (FLOPs、内存、算术强度) [Percy] | lecture_02.py (https://cs336.stanford.edu/lectures/?trace=lecture_02) (录制版 (https://cs336.stanford.edu/lectures/?trace=lecture_02_recording)) |
3 | 4月6日 周一 | 架构、超参数 [Tatsu] | lecture 3.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_03.pdf) |
4 | 4月8日 周三 | 注意力机制替代方案和混合专家 [Tatsu] | lecture 4.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_04.pdf) |
5 | 4月13日 周一 | GPU、TPU [Tatsu] | lecture 5.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_05.pdf) |
6 | 4月15日 周三 | 内核、Triton [Percy] | lecture_06.py (https://cs336.stanford.edu/lectures/?trace=lecture_06) | 作业1 **截止** 作业2 **发布** [代码](https://github.com/stanford-cs336/assignment2-systems/tree/main) [预览](https://github.com/stanford-cs336/assignment2-systems/blob/main/cs336_assignment2_systems.pdf)
7 | 4月20日 周一 | 并行 [Percy] | lecture_07.py (https://cs336.stanford.edu/lectures/?trace=lecture_07) |
8 | 4月22日 周三 | 并行 [Tatsu] | lecture_08.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_08.pdf) |
9 | 4月27日 周一 | 扩展法则 [Tatsu] | lecture_09.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_09.pdf) |
10 | 4月29日 周三 | 推理 [Percy] | lecture_10.py (https://cs336.stanford.edu/lectures/?trace=lecture_10) | 作业2 **截止** 作业3 **发布** [代码](https://github.com/stanford-cs336/assignment3-scaling/tree/main) [预览](https://github.com/stanford-cs336/assignment3-scaling/blob/main/cs336_assignment3_scaling.pdf)
11 | 5月4日 周一 | 扩展法则 [Tatsu] | lecture_11.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_11.pdf) |
12 | 5月6日 周三 | 评估 [Percy] | lecture_12.py (https://cs336.stanford.edu/lectures/?trace=lecture_12) | 作业3 **截止** 作业4 **发布** [代码](https://github.com/stanford-cs336/assignment4-data/tree/main) [预览](https://github.com/stanford-cs336/assignment4-data/blob/main/cs336_assignment4_data.pdf)
13 | 5月11日 周一 | 数据(来源、数据集) [Percy] | lecture_13.py (https://cs336.stanford.edu/lectures/?trace=lecture_13) |
14 | 5月13日 周三 | 数据(过滤、去重、混合、合成数据) [Percy] | lecture_14.py (https://cs336.stanford.edu/lectures/?trace=lecture_14) |
15 | 5月18日 周一 | 中期/后期训练(SFT/RLHF) [Tatsu] | lecture_15.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_15.pdf) |
16 | 5月20日 周三 | 后期训练 - RLVR [Tatsu] | lecture_16.pdf (https://github.com/stanford-cs336/lectures/blob/main/lecture_16.pdf) | 作业4 **截止** 作业5 **发布** [代码](https://github.com/stanford-cs336/assignment5-alignment/tree/main) [预览](https://github.com/stanford-cs336/assignment5-alignment/blob/main/cs336_spring2026_assignment5_alignment.pdf) [可选第二部分](https://github.com/stanford-cs336/assignment5-alignment/blob/main/cs336_spring2025_assignment5_supplement_safety_rlhf.pdf)
- | 5月25日 周一 | *无课(阵亡将士纪念日)* | |
17 | 5月27日 周三 | 对齐 - 多模态 [Percy] | lecture_17.py (https://cs336.stanford.edu/lectures/?trace=lecture_17) |
18 | 6月1日 周一 | 特邀讲座:Daniel Selsam | |
19 | 6月3日 周三 | 特邀讲座:Dan Fu | | 作业5 **截止**
相似文章
@tan_maty: 吹爆这个课,计算机专业必看 CS336, 这是一门在 AI 圈子里最近封神的课程。 语言模型从零构建大语言 这门课由 Stanford 开设,授课老师是 NLP 领域的顶尖大佬 Percy Liang 和 Tatsunori Hashim…
A thread promoting Stanford's CS336 course on building language models from scratch, taught by NLP experts Percy Liang and Tatsunori Hashimoto, emphasizing hands-on understanding.
@stanfordnlp: CS336的“问题”不在于大约22小时的视频,而在于完成作业所需的大量时间。…
斯坦福大学的CS336课程宣布开课,从零开始教授语言建模,包含密集的实践作业,涵盖分词器、Transformer、数据和对齐。
@DanKornas: "斯坦福CS229 I 机器学习 I 构建大型语言模型(LLMs)"(斯坦福在线)... 你将学到:…
斯坦福CS229在线课程公告,涵盖构建大型语言模型、深度神经网络、TensorFlow、Keras、OpenCV以及使用spaCy的自然语言处理。
@Rassh_RAJ: 斯坦福CS336: Large Language Models from Scratch (2026) 现已完整上传至YouTube,相比2025版新增了一些内容…
斯坦福大学CS336课程《Large Language Models from Scratch》(2026)现已完整上线YouTube,在2025年播放列表基础上有所扩充,提供超越API调用的LLM深入理解。
@stanfordnlp:学习成功训练最先进语言模型的细节(即“技巧”或“秘诀”)有两条路径……
斯坦福NLP将CS336课程推广为学习成功训练最先进语言模型技巧的途径。