diff --git a/README.md b/README.md
index ec85abc..362864d 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,37 @@
This repository contains the lecture materials for Stanford's Language Modeling from Scratch (CS336).
+## Bilingual Learning Version / 中英对照学习版
+
+This branch adds a bilingual learning layer on top of the original Stanford CS336 lecture materials.
+
+本分支在 Stanford CS336 原始课程资料之上增加中英对照学习层。
+
+- The original English text is preserved.
+- 英文原文完整保留。
+- Chinese translations are provided for study and comprehension support.
+- 中文译文用于学习和辅助理解。
+- This is not an official Stanford translation.
+- 本项目不是 Stanford 官方翻译。
+- PDF lectures and text embedded inside images are not covered in the first executable-lecture translation pass.
+- 第一轮可执行讲义翻译暂不覆盖 PDF 讲义和图片内部文字。
+- Current executable lecture coverage: `lecture_01.py`, `lecture_02.py`, `lecture_06.py`, `lecture_07.py`, `lecture_10.py`, `lecture_12.py`, `lecture_13.py`, `lecture_14.py`, and `lecture_17.py`.
+- 当前可执行讲义覆盖范围:`lecture_01.py`、`lecture_02.py`、`lecture_06.py`、`lecture_07.py`、`lecture_10.py`、`lecture_12.py`、`lecture_13.py`、`lecture_14.py` 和 `lecture_17.py`。
+- Reference notes in `references.py` include Chinese study notes while preserving formal titles, authors, dates, and URLs.
+- `references.py` 中的引用说明已增加中文学习说明;正式标题、作者、日期和 URL 保持原样。
+
+Translation tracking files:
+
+- `docs/translation-inventory.md`: AST-based inventory of executable lecture text.
+- `docs/bilingual-glossary.md`: shared terminology.
+- `docs/translation-review.md`: items needing human review.
+
+翻译跟踪文件:
+
+- `docs/translation-inventory.md`:基于 AST 的可执行讲义文本盘点。
+- `docs/bilingual-glossary.md`:统一术语表。
+- `docs/translation-review.md`:需要人工复核的内容。
+
## Executable lectures
These are named `lecture_XX.py`.
diff --git a/docs/bilingual-glossary.md b/docs/bilingual-glossary.md
new file mode 100644
index 0000000..4a3b646
--- /dev/null
+++ b/docs/bilingual-glossary.md
@@ -0,0 +1,38 @@
+# Bilingual Glossary
+
+This glossary keeps terminology consistent across the bilingual learning version.
+
+本术语表用于保持中英对照学习版中的术语一致。
+
+| English | 中文译法 | Acceptable short form | First lecture | Notes |
+|---|---|---|---|---|
+| language model | 语言模型 | LM | lecture_01 | 首次出现保留 English term。 |
+| tokenization | 分词 | tokenizer / token | lecture_01 | 具体 tokenizer 名称不翻译。 |
+| Transformer | Transformer 架构 | Transformer | lecture_01 | 正式模型/架构名保留英文。 |
+| attention | 注意力机制 | attention | lecture_01 | 数学公式中的符号不翻译。 |
+| mixture of experts | 专家混合 | MoE | lecture_01 | 首次出现写作“专家混合(mixture of experts, MoE)”。 |
+| scaling laws | 缩放定律 | scaling laws | lecture_01 | 课程核心术语。 |
+| compute | 计算量 | compute | lecture_02 | 视上下文也可译为“算力预算”。 |
+| memory | 内存 | memory | lecture_02 | GPU 场景可译为“显存”。 |
+| FLOPs | 浮点运算次数 | FLOPs | lecture_02 | 缩写保留。 |
+| arithmetic intensity | 算术强度 | AI | lecture_02 | 避免与 artificial intelligence 混淆。 |
+| tensor | 张量 | tensor | lecture_02 | 代码变量和 shape 符号不翻译。 |
+| gradient | 梯度 | gradient | lecture_02 | 与 optimizer 相关。 |
+| optimizer | 优化器 | optimizer | lecture_02 | Adam/AdamW 等正式名称不翻译。 |
+| benchmark | 基准测试 | benchmark | lecture_06 | 正式 benchmark 名称保留英文。 |
+| profiling | 性能剖析 | profiler | lecture_06 | PyTorch profiler / Nsight 名称保留。 |
+| kernel | 核函数 | kernel | lecture_06 | GPU/Triton 语境。 |
+| inference | 推理 | inference | lecture_10 | 与 training 区分。 |
+| evaluation | 评测 | eval | lecture_12 | benchmark 名称保留英文。 |
+| data filtering | 数据过滤 | filtering | lecture_13 | 数据处理语境。 |
+| deduplication | 去重 | dedup | lecture_14 | 数据清洗语境。 |
+| multimodality | 多模态 | multimodal | lecture_17 | 模型正式名称不翻译。 |
+| KV cache | KV cache | KV cache | lecture_10 | 正式系统术语保留英文,解释为键值缓存。 |
+| latency | 延迟 | latency | lecture_10 | 与 throughput 成对出现。 |
+| throughput | 吞吐量 | throughput | lecture_10 | tokens/second 语境。 |
+| collective operation | 集合通信操作 | collective | lecture_07 | Broadcast/all-reduce 等操作名保留英文。 |
+| data curation | 数据策划 | curation | lecture_13 | 数据选择、清洗和组织的总称。 |
+| Jaccard similarity | Jaccard 相似度 | Jaccard | lecture_14 | 公式和集合符号保持原样。 |
+| MinHash | 最小哈希 | MinHash | lecture_14 | 算法名保留英文。 |
+| vision encoder | 视觉编码器 | encoder | lecture_17 | CLIP/SigLIP 等正式名称保留英文。 |
+| diffusion model | 扩散模型 | diffusion | lecture_17 | 图像生成语境。 |
diff --git a/docs/translation-inventory.md b/docs/translation-inventory.md
new file mode 100644
index 0000000..b297d9e
--- /dev/null
+++ b/docs/translation-inventory.md
@@ -0,0 +1,28 @@
+# Translation Inventory
+
+Generated with Python AST scanning. Counts are a starting point for translation review, not a substitute for human judgment.
+
+| File | text(...) | image(...) | link(...) | verbatim | docstrings | comments | ordinary strings | course text strings | runtime/display strings | path/url/id strings | review blocks |
+|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|
+| lecture_01.py | 368 | 14 | 121 | 0 | 9 | 7 | 469 | 368 | 387 | 61 | 40 |
+| lecture_02.py | 210 | 11 | 14 | 0 | 5 | 54 | 275 | 210 | 226 | 44 | 21 |
+| lecture_06.py | 195 | 7 | 2 | 34 | 2 | 74 | 214 | 189 | 171 | 26 | 0 |
+| lecture_07.py | 104 | 7 | 6 | 0 | 7 | 74 | 187 | 104 | 139 | 36 | 47 |
+| lecture_10.py | 279 | 28 | 33 | 0 | 3 | 21 | 348 | 279 | 277 | 61 | 8 |
+| lecture_12.py | 220 | 43 | 46 | 0 | 0 | 0 | 329 | 220 | 210 | 101 | 8 |
+| lecture_13.py | 396 | 18 | 28 | 0 | 0 | 3 | 463 | 396 | 373 | 80 | 0 |
+| lecture_14.py | 249 | 18 | 27 | 7 | 0 | 6 | 341 | 249 | 235 | 81 | 11 |
+| lecture_17.py | 178 | 32 | 19 | 0 | 0 | 3 | 233 | 178 | 151 | 56 | 0 |
+
+## Classification Rules
+
+- Course text strings: first positional string passed to `text(...)`.
+- Runtime/display strings: non-path strings that may be visible in examples, prints, assertions, or generated output.
+- Path/url/id strings: URL, image path, file path, identifier-like, or command-like values that should usually remain unchanged.
+- Review blocks: strings that need human review before translation because their role is not obvious from AST context.
+
+## Translation Notes
+
+- `verbatim=True` blocks should keep the English ASCII table/output unchanged and receive a Chinese explanation underneath.
+- Strings that participate in assertions, parsing, control flow, or tokenizer examples should remain unchanged unless a separate explanatory line is added.
+- The generated trace JSON files are outputs and should be regenerated after source translation rather than edited directly.
diff --git a/docs/translation-review.md b/docs/translation-review.md
new file mode 100644
index 0000000..c5bb784
--- /dev/null
+++ b/docs/translation-review.md
@@ -0,0 +1,28 @@
+# Translation Review
+
+This file tracks uncertain terminology, strings, or implementation choices that need human review.
+
+本文件记录需要人工复核的术语、句子和实现选择。
+
+## Open Items
+
+| Location | Item | Reason | Proposed handling |
+|---|---|---|---|
+| All lectures | Official model, dataset, benchmark, and paper names | 翻译后可能破坏检索和引用准确性 | 保留英文正式名称,必要时添加中文解释。 |
+| `verbatim=True` blocks | ASCII tables and example outputs | 直接插入中文会破坏对齐或改变示例输出 | 原块保持英文,在下方添加中文说明。 |
+| Tokenization examples | Example strings and token outputs | 字符串本身用于演示 tokenizer 行为 | 示例保持原样,只翻译周围解释。 |
+| Generated traces | `var/traces/*.json` | 生成产物,不应作为源头翻译 | 修改 Python 源文件后重新生成。 |
+| All translated lectures | Batch-generated Chinese text | 本轮优先完成全部可执行讲义覆盖,部分句子仍需人工润色以提高自然度 | 保留英文为准,逐讲人工校对中文表达。 |
+| `lecture_12.py` | `"\%"` SyntaxWarning | 原文件已有转义警告;本轮按要求未大范围修改,避免改变显示结果 | 如需修复,单独确认后改为 `"\\%"` 或 raw string。 |
+| Images | Embedded English labels | 本轮不修改图片文件 | 在后续 pass 为关键图片补充更细图下注释或标签对照。 |
+| `references.py` | `notes` Chinese render behavior | `Reference` 正式字段结构未知,不新增非原生字段 | 在 `notes` 中保留英文并紧跟中文说明。 |
+
+## Review Checklist
+
+- English source text is preserved.
+- Chinese text immediately follows the corresponding English.
+- Formulas, code, commands, URLs, and formal names remain intact.
+- Chinese explanations do not change assertions, control flow, or parser-sensitive strings.
+- Tables and code/output blocks keep their original alignment.
+- Static executable lecture `text(...)` calls have been converted to bilingual helpers.
+- `references.py` formal citation metadata remains unchanged.
diff --git a/lecture_01.py b/lecture_01.py
index b244cf0..25d9cac 100644
--- a/lecture_01.py
+++ b/lecture_01.py
@@ -5,7 +5,7 @@
from dataclasses import dataclass
from collections import defaultdict
from edtrace import link, text, image
-from lecture_util import article_link, post_link, video_link, get_local_url
+from lecture_util import article_link, post_link, video_link, get_local_url, bilingual_text
from references import shannon_1950, lstm_1997, brants_2007, bengio_2003, glorot_2010, seq2seq_2014
from references import bahdanau_2015_attention, transformer_2017, gpt2_2019, t5_2019, kaplan_scaling_laws_2020, mup_2022
from references import dpo_2023, adamw_2017, adam_2014, grpo, ppo_2017, muon_2024
@@ -46,190 +46,190 @@ def main():
tokenization() # First unit
- text("Next time: resource accounting")
+ bilingual_text("Next time: resource accounting", '下次:资源核算。')
def welcome():
- text("## CS336: Language Models From Scratch (Spring 2026)"),
+ bilingual_text("## CS336: Language Models From Scratch (Spring 2026)", '## CS336:从零开始构建语言模型(2026 春季)'),
image("images/course-staff.png", width=600)
- text("...bringing you the 3rd offering of CS336.")
+ bilingual_text("...bringing you the 3rd offering of CS336.", '这是 CS336 第三次开课。')
- text("Lectures from 2nd offering (Spring 2025) are on [YouTube](https://www.youtube.com/playlist?list=PLoROMvodv4rOY23Y0BoGoBGgQ1zmU_MT_).")
- text("What's new?")
- text("- Same 'from scratch' philosophy")
- text("- Prioritize high value-per-time concepts, don't lose the forest for the trees")
- text("- More coverage of modern LM ingredients (mixture of experts, long-context, agents)")
+ bilingual_text("Lectures from 2nd offering (Spring 2025) are on [YouTube](https://www.youtube.com/playlist?list=PLoROMvodv4rOY23Y0BoGoBGgQ1zmU_MT_).", '第二次开课(2025 春季)的课程录像在 [YouTube](https://www.youtube.com/playlist?list=PLoROMvodv4rOY23Y0BoGoBGgQ1zmU_MT_) 上。')
+ bilingual_text("What's new?", '有哪些新内容?')
+ bilingual_text("- Same 'from scratch' philosophy", '- 仍然坚持“从零开始”的理念。')
+ bilingual_text("- Prioritize high value-per-time concepts, don't lose the forest for the trees", '- 优先讲单位时间价值最高的概念,不因细节而看不见整体。')
+ bilingual_text("- More coverage of modern LM ingredients (mixture of experts, long-context, agents)", '- 更多覆盖现代语言模型要素(专家混合、长上下文、智能体)。')
def why_this_course_exists():
- text("## Why did we make this course?")
+ bilingual_text("## Why did we make this course?", '## 我们为什么开设这门课?')
- text("Problem: researchers are becoming **disconnected** from the underlying technology.")
- text("- 2016: researchers implemented and trained their own models.")
- text("- 2018: researchers downloaded models (e.g., BERT) and fine-tuned them.")
- text("- Today: researchers prompt API models (e.g., GPT/Claude/Gemini).")
+ bilingual_text("Problem: researchers are becoming **disconnected** from the underlying technology.", '问题:研究者正在与底层技术逐渐**脱节**。')
+ bilingual_text("- 2016: researchers implemented and trained their own models.", '- 2016 年:研究者自己实现并训练模型。')
+ bilingual_text("- 2018: researchers downloaded models (e.g., BERT) and fine-tuned them.", '- 2018 年:研究者下载模型(例如 BERT)并进行微调。')
+ bilingual_text("- Today: researchers prompt API models (e.g., GPT/Claude/Gemini).", '- 今天:研究者通过提示词调用 API 模型(例如 GPT/Claude/Gemini)。')
- text("Moving up levels of abstraction boosts productivity, but")
- text("- These abstractions are leaky (in contrast to programming languages or operating systems).")
- text("- There is still fundamental research to be done that requires tearing up the stack.")
+ bilingual_text("Moving up levels of abstraction boosts productivity, but", '抽象层级上移会提升生产力,但是:')
+ bilingual_text("- These abstractions are leaky (in contrast to programming languages or operating systems).", '- 这些抽象并不严密,会泄漏底层细节(这不同于编程语言或操作系统)。')
+ bilingual_text("- There is still fundamental research to be done that requires tearing up the stack.", '- 仍有一些基础研究需要拆开整个技术栈才能完成。')
- text("**Full understanding** of this technology is necessary for **fundamental research**.")
+ bilingual_text("**Full understanding** of this technology is necessary for **fundamental research**.", '要做**基础研究**,必须对这项技术有**完整理解**。')
- text("Philosophy of this course: **understanding via building**.")
- text("But there's one small problem...")
+ bilingual_text("Philosophy of this course: **understanding via building**.", '这门课的理念是:通过构建来理解。')
+ bilingual_text("But there's one small problem...", '但这里有一个小问题……')
- text("## The industrialization of language models")
+ bilingual_text("## The industrialization of language models", '## 语言模型的工业化')
image("https://upload.wikimedia.org/wikipedia/commons/c/cc/Industrialisation.jpg", width=400)
- text("Frontier models are really expensive:")
- text("- 2023: GPT-4 supposedly cost $100M to train. "), article_link("https://www.wired.com/story/openai-ceo-sam-altman-the-age-of-giant-ai-models-is-already-over/")
- text("- 2025: xAI builds cluster with 230K GPUs for training Grok. "), article_link("https://x.com/elonmusk/status/1947701807389515912")
+ bilingual_text("Frontier models are really expensive:", '前沿模型非常昂贵:')
+ bilingual_text("- 2023: GPT-4 supposedly cost $100M to train. ", '- 2023 年:据称 GPT-4 的训练成本为 1 亿美元。'), article_link("https://www.wired.com/story/openai-ceo-sam-altman-the-age-of-giant-ai-models-is-already-over/")
+ bilingual_text("- 2025: xAI builds cluster with 230K GPUs for training Grok. ", '- 2025 年:xAI 建设了包含 23 万块 GPU 的集群来训练 Grok。'), article_link("https://x.com/elonmusk/status/1947701807389515912")
- text("There are no public details on how frontier models are built.")
- text("From the GPT-4 technical report "), link(gpt_4_2023), text(":")
+ bilingual_text("There are no public details on how frontier models are built.", '关于前沿模型如何构建,目前没有公开的完整细节。')
+ bilingual_text("From the GPT-4 technical report ", '来自 GPT-4 技术报告'), link(gpt_4_2023), bilingual_text(":", '中文标点:')
image("images/gpt4-no-details.png", width=600)
- text("Frontier models are out of reach for us.")
- text("We could build small language models (<1B parameters), but this might not be representative of large language models.")
+ bilingual_text("Frontier models are out of reach for us.", '前沿模型超出了我们这门课可以直接触及的范围。')
+ bilingual_text("We could build small language models (<1B parameters), but this might not be representative of large language models.", '我们可以构建小型语言模型(少于 10 亿参数),但它们未必能代表大语言模型的行为。')
- text("Example 1: fraction of FLOPs spent in attention versus MLP changes with scale. "), post_link("https://x.com/stephenroller/status/1579993017234382849")
+ bilingual_text("Example 1: fraction of FLOPs spent in attention versus MLP changes with scale. ", '示例 1:花在注意力与 MLP 上的 FLOPs 占比会随规模改变。'), post_link("https://x.com/stephenroller/status/1579993017234382849")
image("images/roller-flops.png", width=400)
- text("Example 2: emergence of behavior with scale "), link("https://arxiv.org/pdf/2206.07682")
+ bilingual_text("Example 2: emergence of behavior with scale ", '示例 2:行为会随规模出现涌现。'), link("https://arxiv.org/pdf/2206.07682")
image("images/wei-emergence-plot.png", width=600)
- text("## What can we learn in this class that transfers to frontier models?")
- text("There are three types of knowledge:")
- text("- **Mechanics**: how things work (what a Transformer is, how model parallelism works)")
- text("- **Mindset**: squeezing the most out of the hardware, taking scaling seriously")
- text("- **Intuitions**: which data and modeling decisions yield good accuracy")
+ bilingual_text("## What can we learn in this class that transfers to frontier models?", '## 这门课中哪些知识可以迁移到前沿模型?')
+ bilingual_text("There are three types of knowledge:", '这里有三类知识:')
+ bilingual_text("- **Mechanics**: how things work (what a Transformer is, how model parallelism works)", '- **机制**:事物如何工作(例如 Transformer 是什么、模型并行如何工作)。')
+ bilingual_text("- **Mindset**: squeezing the most out of the hardware, taking scaling seriously", '- **思维方式**:尽可能榨干硬件性能,并严肃对待规模化。')
+ bilingual_text("- **Intuitions**: which data and modeling decisions yield good accuracy", '- **直觉**:哪些数据和建模决策会带来更好的准确率。')
- text("We can teach mechanics and mindset (these do transfer).")
- text("We can only partially teach intuitions (do not necessarily transfer across scales).")
+ bilingual_text("We can teach mechanics and mindset (these do transfer).", '我们可以教授机制和思维方式(它们确实可以迁移)。')
+ bilingual_text("We can only partially teach intuitions (do not necessarily transfer across scales).", '我们只能部分教授直觉(它们不一定能跨规模迁移)。')
- text("## Intuitions? 🤷")
- text("Some design decisions are simply not (yet) justifiable and just come from experimentation.")
- text("Example: Noam Shazeer paper that introduced SwiGLU "), link(shazeer_2020)
+ bilingual_text("## Intuitions? 🤷", '## 直觉?🤷')
+ bilingual_text("Some design decisions are simply not (yet) justifiable and just come from experimentation.", '有些设计决策目前还无法充分解释,只能来自实验经验。')
+ bilingual_text("Example: Noam Shazeer paper that introduced SwiGLU ", '示例:Noam Shazeer 提出 SwiGLU 的论文。'), link(shazeer_2020)
image("images/divine-benevolence.png", width=600)
- text("## The bitter lesson")
- text("Wrong interpretation: scale is all that matters, algorithms don't matter.")
- text("Right interpretation: algorithms that scale are what matter.")
- text("### accuracy = efficiency x resources")
- text("In fact, efficiency is way more important at larger scales (can't afford to be wasteful).")
- link("https://arxiv.org/abs/2005.04305"), text(" showed 44x algorithmic efficiency on ImageNet between 2012 and 2019.")
+ bilingual_text("## The bitter lesson", '## 苦涩的教训')
+ bilingual_text("Wrong interpretation: scale is all that matters, algorithms don't matter.", '错误解读:规模就是一切,算法并不重要。')
+ bilingual_text("Right interpretation: algorithms that scale are what matter.", '正确解读:真正重要的是能够随规模扩展的算法。')
+ bilingual_text("### accuracy = efficiency x resources", '### 准确率 = 效率 x 资源')
+ bilingual_text("In fact, efficiency is way more important at larger scales (can't afford to be wasteful).", '事实上,在更大规模下效率更加重要,因为浪费不起。')
+ link("https://arxiv.org/abs/2005.04305"), bilingual_text(" showed 44x algorithmic efficiency on ImageNet between 2012 and 2019.", '展示了 2012 到 2019 年间 ImageNet 上算法效率提升了 44 倍。')
- text("Framing: what is the best model one can build given a certain compute and data budget?")
- text("In other words, **maximize efficiency**!")
+ bilingual_text("Framing: what is the best model one can build given a certain compute and data budget?", '问题框架:在给定计算量和数据预算时,能构建出的最佳模型是什么?')
+ bilingual_text("In other words, **maximize efficiency**!", '换句话说,就是要**最大化效率**!')
def current_lm_landscape():
- text("## Pre-neural (before 2010s)")
- text("- Language model to measure the entropy of English "), link(shannon_1950)
- text("- N-gram language models (used in machine translation and speech recognition systems) "), link(brants_2007)
-
- text("## Neural ingredients (2010s)")
- text("- Long-Short Term Memory (LSTM) "), link(lstm_1997)
- text("- First neural language model "), link(bengio_2003)
- text("- Sequence-to-sequence modeling (for machine translation) "), link(seq2seq_2014)
- text("- Adam optimizer "), link(adam_2014)
- text("- Attention mechanism (for machine translation) "), link(bahdanau_2015_attention)
- text("- Transformer architecture (for machine translation) "), link(transformer_2017)
- text("- Mixture of experts "), link(moe_2017)
- text("- Model parallelism "), link(gpipe_2018), link(zero_2019), link(megatron_lm_2019)
-
- text("## Early foundation models (late 2010s)")
- text("- ELMo: pretraining with LSTMs, fine-tuning improves downstream tasks "), link(elmo_2018)
- text("- BERT: pretraining with Transformer, fine-tuning improves downstream tasks "), link(bert_2018)
- text("- Google's T5 (11B): cast everything as text-to-text "), link(t5_2019)
-
- text("## Embracing scaling")
- text("- OpenAI's GPT-2 (1.5B): fluent text, first signs of zero-shot "), link(gpt2_2019)
- text("- Scaling laws: provide hope / predictability for scaling "), link(kaplan_scaling_laws_2020)
- text("- OpenAI's GPT-3 (175B): in-context learning "), link(gpt_3_2020)
- text("- Google's PaLM (540B): massive scale, undertrained "), link(palm_2022)
- text("- DeepMind's Chinchilla (70B): compute-optimal scaling laws "), link(chinchilla_2022)
-
- text("## Open models")
- text("Early attempts (attempts to replicate GPT-3):")
- text("- EleutherAI's open datasets (The Pile) and models (GPT-J) "), link(the_pile_2020), link(gpt_j_2021)
- text("- Meta's OPT (175B): GPT-3 replication, lots of hardware issues "), link(opt_175b_2022)
- text("- Hugging Face / BigScience's BLOOM (176B): focused on data sourcing "), link(bloom_2022)
-
- text("Credible open-weight models (weights + paper):")
- text("- Meta's Llama models "), link(llama_2023), link(llama_2_2023), link(llama_3_2024)
- text('- Mistral\'s models '), link(mistral_7b_2023), link(mixtral_2024)
- text("- DeepSeek\'s models "), link(deepseek_67b_2024), link(deepseek_v2_2024), link(deepseek_v3_2024), link(deepseek_r1_2025), link(deepseek_v3_2_2025)
- text("- Alibaba\'s Qwen models "), link(qwen_2_5_2024), link(qwen_3_2025)
- text("- Moonshot's Kimi models "), link(kimi_1_5_2025), link(kimi_k2_5_2026)
- text("- Z.ai's GLM models "), link(glm_4_5_2025), link(glm_5_2026)
- text("- Minimax\'s models "), link(minimax_m2_5_2026)
- text("- Xiaomi's MIMO models "), link(xiaomi_mimo_v2_2026)
- text("These models are approaching closed models (GPT, Claude, Gemini, etc.).")
-
- text("Open-source models (weights + paper + code + data):")
- text("- AI2's Olmo models "), link(olmo_7b_2024), link(olmo_2_2025), link(olmo_3_2025)
- text("- NVIDIA's Nemotron models "), link(nemotron_15b_2024), link(nemotron_3_2025)
- text("- Marin's models (open development) "), link(marin_8b_2025), link(marin_32b_2025)
-
- text("Openness is important for trust and innovation "), link("https://arxiv.org/abs/2403.07918")
- text("Ideas from open models enable us to teach CS336.")
-
- text("What is a language model?")
- text("- 2018 (BERT): something you fine-tune")
- text("- 2020 (GPT-3): something you prompt")
- text("- 2022 (ChatGPT): something you talk to "), link(title="example conversation", url="https://huggingface.co/datasets/HuggingFaceTB/smoltalk/viewer/all/train?row=72&conversation-viewer=72")
- text("- 2026 (agents): something that acts autonomously "), link(title="example trace", url="https://huggingface.co/datasets/nebius/SWE-rebench-openhands-trajectories/viewer/default/train?conversation-viewer=1")
-
- text("The fundamentals are the same (attention, kernels, optimization).")
- text("The specs are different (longer context, inference efficiency matters even more).")
+ bilingual_text("## Pre-neural (before 2010s)", '## 神经网络之前(2010 年代以前)')
+ bilingual_text("- Language model to measure the entropy of English ", '- 用语言模型度量英语的熵。'), link(shannon_1950)
+ bilingual_text("- N-gram language models (used in machine translation and speech recognition systems) ", '- N-gram 语言模型(用于机器翻译和语音识别系统)。'), link(brants_2007)
+
+ bilingual_text("## Neural ingredients (2010s)", '## 神经网络要素(2010 年代)')
+ bilingual_text("- Long-Short Term Memory (LSTM) ", '- 长短期记忆网络(LSTM)。'), link(lstm_1997)
+ bilingual_text("- First neural language model ", '- 第一个神经语言模型。'), link(bengio_2003)
+ bilingual_text("- Sequence-to-sequence modeling (for machine translation) ", '- 序列到序列建模(用于机器翻译)。'), link(seq2seq_2014)
+ bilingual_text("- Adam optimizer ", '- Adam 优化器。'), link(adam_2014)
+ bilingual_text("- Attention mechanism (for machine translation) ", '- 注意力机制(用于机器翻译)。'), link(bahdanau_2015_attention)
+ bilingual_text("- Transformer architecture (for machine translation) ", '- Transformer 架构(用于机器翻译)。'), link(transformer_2017)
+ bilingual_text("- Mixture of experts ", '- 专家混合(Mixture of experts)。'), link(moe_2017)
+ bilingual_text("- Model parallelism ", '- 模型并行。'), link(gpipe_2018), link(zero_2019), link(megatron_lm_2019)
+
+ bilingual_text("## Early foundation models (late 2010s)", '## 早期基础模型(2010 年代后期)')
+ bilingual_text("- ELMo: pretraining with LSTMs, fine-tuning improves downstream tasks ", '- ELMo:使用 LSTM 预训练,微调可提升下游任务表现。'), link(elmo_2018)
+ bilingual_text("- BERT: pretraining with Transformer, fine-tuning improves downstream tasks ", '- BERT:使用 Transformer 预训练,微调可提升下游任务表现。'), link(bert_2018)
+ bilingual_text("- Google's T5 (11B): cast everything as text-to-text ", '- Google 的 T5(11B):把所有任务都表示为文本到文本。'), link(t5_2019)
+
+ bilingual_text("## Embracing scaling", '## 拥抱规模化')
+ bilingual_text("- OpenAI's GPT-2 (1.5B): fluent text, first signs of zero-shot ", '- OpenAI 的 GPT-2(1.5B):文本流畅,出现零样本能力的早期迹象。'), link(gpt2_2019)
+ bilingual_text("- Scaling laws: provide hope / predictability for scaling ", '- 规模定律:为规模化提供希望和可预测性。'), link(kaplan_scaling_laws_2020)
+ bilingual_text("- OpenAI's GPT-3 (175B): in-context learning ", '- OpenAI 的 GPT-3(175B):上下文学习。'), link(gpt_3_2020)
+ bilingual_text("- Google's PaLM (540B): massive scale, undertrained ", '- Google 的 PaLM(540B):规模巨大,但训练不足。'), link(palm_2022)
+ bilingual_text("- DeepMind's Chinchilla (70B): compute-optimal scaling laws ", '- DeepMind 的 Chinchilla(70B):计算最优的规模定律。'), link(chinchilla_2022)
+
+ bilingual_text("## Open models", '## 开放模型')
+ bilingual_text("Early attempts (attempts to replicate GPT-3):", '早期尝试(试图复现 GPT-3):')
+ bilingual_text("- EleutherAI's open datasets (The Pile) and models (GPT-J) ", '- EleutherAI 的开放数据集(The Pile)和模型(GPT-J)。'), link(the_pile_2020), link(gpt_j_2021)
+ bilingual_text("- Meta's OPT (175B): GPT-3 replication, lots of hardware issues ", '- Meta 的 OPT(175B):复现 GPT-3,遇到大量硬件问题。'), link(opt_175b_2022)
+ bilingual_text("- Hugging Face / BigScience's BLOOM (176B): focused on data sourcing ", '- Hugging Face / BigScience 的 BLOOM(176B):重点关注数据来源。'), link(bloom_2022)
+
+ bilingual_text("Credible open-weight models (weights + paper):", '可信的开放权重模型(权重 + 论文):')
+ bilingual_text("- Meta's Llama models ", '- Meta 的 Llama 模型。'), link(llama_2023), link(llama_2_2023), link(llama_3_2024)
+ bilingual_text('- Mistral\'s models ', '- Mistral 的模型。'), link(mistral_7b_2023), link(mixtral_2024)
+ bilingual_text("- DeepSeek\'s models ", '- DeepSeek 的模型。'), link(deepseek_67b_2024), link(deepseek_v2_2024), link(deepseek_v3_2024), link(deepseek_r1_2025), link(deepseek_v3_2_2025)
+ bilingual_text("- Alibaba\'s Qwen models ", '- 阿里巴巴的 Qwen 模型。'), link(qwen_2_5_2024), link(qwen_3_2025)
+ bilingual_text("- Moonshot's Kimi models ", '- Moonshot 的 Kimi 模型。'), link(kimi_1_5_2025), link(kimi_k2_5_2026)
+ bilingual_text("- Z.ai's GLM models ", '- Z.ai 的 GLM 模型。'), link(glm_4_5_2025), link(glm_5_2026)
+ bilingual_text("- Minimax\'s models ", '- Minimax 的模型。'), link(minimax_m2_5_2026)
+ bilingual_text("- Xiaomi's MIMO models ", '- 小米的 MIMO 模型。'), link(xiaomi_mimo_v2_2026)
+ bilingual_text("These models are approaching closed models (GPT, Claude, Gemini, etc.).", '这些模型正在接近闭源模型(GPT、Claude、Gemini 等)。')
+
+ bilingual_text("Open-source models (weights + paper + code + data):", '开源模型(权重 + 论文 + 代码 + 数据):')
+ bilingual_text("- AI2's Olmo models ", '- AI2 的 Olmo 模型。'), link(olmo_7b_2024), link(olmo_2_2025), link(olmo_3_2025)
+ bilingual_text("- NVIDIA's Nemotron models ", '- NVIDIA 的 Nemotron 模型。'), link(nemotron_15b_2024), link(nemotron_3_2025)
+ bilingual_text("- Marin's models (open development) ", '- Marin 的模型(开放开发)。'), link(marin_8b_2025), link(marin_32b_2025)
+
+ bilingual_text("Openness is important for trust and innovation ", '开放性对信任和创新非常重要。'), link("https://arxiv.org/abs/2403.07918")
+ bilingual_text("Ideas from open models enable us to teach CS336.", '开放模型中的思想让我们能够讲授 CS336。')
+
+ bilingual_text("What is a language model?", '语言模型是什么?')
+ bilingual_text("- 2018 (BERT): something you fine-tune", '- 2018(BERT):一种用来微调的东西。')
+ bilingual_text("- 2020 (GPT-3): something you prompt", '- 2020(GPT-3):一种用提示词使用的东西。')
+ bilingual_text("- 2022 (ChatGPT): something you talk to ", '- 2022(ChatGPT):一种可以对话的东西。'), link(title="example conversation", url="https://huggingface.co/datasets/HuggingFaceTB/smoltalk/viewer/all/train?row=72&conversation-viewer=72")
+ bilingual_text("- 2026 (agents): something that acts autonomously ", '- 2026(智能体):一种能自主行动的东西。'), link(title="example trace", url="https://huggingface.co/datasets/nebius/SWE-rebench-openhands-trajectories/viewer/default/train?conversation-viewer=1")
+
+ bilingual_text("The fundamentals are the same (attention, kernels, optimization).", '基本原理仍然相同(注意力、内核、优化)。')
+ bilingual_text("The specs are different (longer context, inference efficiency matters even more).", '规格已经不同(上下文更长,推理效率更加重要)。')
def what_is_this_program():
- text("This is an *executable lecture*, a program whose execution delivers the content of a lecture.")
- text("Executable lectures make it possible to:")
- text("- view and run code (since everything is code!),")
+ bilingual_text("This is an *executable lecture*, a program whose execution delivers the content of a lecture.", '这是一份*可执行讲义*:运行这个程序就会呈现讲义内容。')
+ bilingual_text("Executable lectures make it possible to:", '可执行讲义让以下事情成为可能:')
+ bilingual_text("- view and run code (since everything is code!),", '- 查看并运行代码(因为一切都是代码!)')
total = 0 # @inspect total
for x in [1, 2, 3]: # @inspect x
total += x # @inspect total
- text("- see the hierarchical structure of the lecture")
+ bilingual_text("- see the hierarchical structure of the lecture", '- 看到讲义的层级结构。')
def course_logistics():
- text("All information online: "), link(title="course website", url="https://stanford-cs336.github.io/spring2026/")
+ bilingual_text("All information online: ", '所有信息都在线上:'), link(title="course website", url="https://stanford-cs336.github.io/spring2026/")
- text("This is a 5-unit class.")
- text("Comment from Spring 2024 course evaluation:")
- text("> *The entire assignment was approximately the same amount of work as all 5 assignments from CS 224n plus the final project. And that's just the first homework assignment.*")
+ bilingual_text("This is a 5-unit class.", '这是一门 5 学分课程。')
+ bilingual_text("Comment from Spring 2024 course evaluation:", '来自 2024 春季课程评价的一条评论:')
+ bilingual_text("> *The entire assignment was approximately the same amount of work as all 5 assignments from CS 224n plus the final project. And that's just the first homework assignment.*", '> *整个作业量大约等于 CS 224n 五个作业加期末项目的总和,而且这还只是第一份作业。*')
- text("## Why you should take this course")
- text("- You have an obsessive need to understand how things work.")
- text("- You want to build up your research engineering muscles.")
+ bilingual_text("## Why you should take this course", '## 为什么你应该选这门课')
+ bilingual_text("- You have an obsessive need to understand how things work.", '- 你强烈想理解事物到底如何工作。')
+ bilingual_text("- You want to build up your research engineering muscles.", '- 你想训练自己的研究工程能力。')
- text("## Why you should not take this course")
- text("- You actually want to get research done this quarter. (Talk to your advisor.)")
- text("- You are interested in learning about the hottest new techniques in AI (e.g., multimodality, RAG, etc.). (You should take a seminar class for that.)")
- text("- You want to get good results on your own application domain. (You should just prompt or fine-tune an existing model.)")
+ bilingual_text("## Why you should not take this course", '## 为什么你不应该选这门课')
+ bilingual_text("- You actually want to get research done this quarter. (Talk to your advisor.)", '- 你这个学期真的想推进研究成果。(请和导师聊聊。)')
+ bilingual_text("- You are interested in learning about the hottest new techniques in AI (e.g., multimodality, RAG, etc.). (You should take a seminar class for that.)", '- 你主要想学习 AI 中最热门的新技术(例如多模态、RAG 等)。(这类内容更适合专题研讨课。)')
+ bilingual_text("- You want to get good results on your own application domain. (You should just prompt or fine-tune an existing model.)", '- 你想在自己的应用领域取得好结果。(你应该直接提示或微调现有模型。)')
- text("## How you can follow along at home")
- text("- All lecture materials and assignments will be posted online, so feel free to follow on your own.")
- text("- Lectures are recorded via [CGOE](https://cgoe.stanford.edu/).")
+ bilingual_text("## How you can follow along at home", '## 如何在校外/家中跟学')
+ bilingual_text("- All lecture materials and assignments will be posted online, so feel free to follow on your own.", '- 所有讲义材料和作业都会发布到网上,所以你可以自由自学。')
+ bilingual_text("- Lectures are recorded via [CGOE](https://cgoe.stanford.edu/).", '- 课程会通过 [CGOE](https://cgoe.stanford.edu/) 录制。')
- text("## Assignments")
- text("- 5 assignments (basics, systems, scaling laws, data, alignment).")
- text("- No scaffolding code, but we provide unit tests and adapter interfaces to help you check correctness.")
- text("- Implement locally to test for correctness, then run on cluster for benchmarking (accuracy and speed).")
- text("- Leaderboard for some assignments (minimize perplexity given training budget).")
+ bilingual_text("## Assignments", '## 作业')
+ bilingual_text("- 5 assignments (basics, systems, scaling laws, data, alignment).", '- 5 次作业(基础、系统、规模定律、数据、对齐)。')
+ bilingual_text("- No scaffolding code, but we provide unit tests and adapter interfaces to help you check correctness.", '- 没有脚手架代码,但我们提供单元测试和适配器接口,帮助你检查正确性。')
+ bilingual_text("- Implement locally to test for correctness, then run on cluster for benchmarking (accuracy and speed).", '- 先在本地实现并测试正确性,再在集群上运行基准测试(准确率和速度)。')
+ bilingual_text("- Leaderboard for some assignments (minimize perplexity given training budget).", '- 部分作业有排行榜(在给定训练预算下最小化困惑度)。')
- text("## AI policy")
- text("- Coding agents can solve all the assignments, but you won't learn anything.")
- text("- AI can be tremendously useful for answering questions and tutoring.")
- text("- You must use our provided AGENTS.md file, which asks the AI to be pedagogically-minded.")
- text("- Please read our [AI policy guide](https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8/edit?tab=t.0).")
+ bilingual_text("## AI policy", '## AI 使用政策')
+ bilingual_text("- Coding agents can solve all the assignments, but you won't learn anything.", '- 编码智能体可以完成所有作业,但那样你学不到东西。')
+ bilingual_text("- AI can be tremendously useful for answering questions and tutoring.", '- AI 在答疑和辅导方面可以非常有用。')
+ bilingual_text("- You must use our provided AGENTS.md file, which asks the AI to be pedagogically-minded.", '- 你必须使用我们提供的 AGENTS.md 文件,其中要求 AI 以教学为导向。')
+ bilingual_text("- Please read our [AI policy guide](https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8/edit?tab=t.0).", '- 请阅读我们的 [AI 使用政策指南](https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8/edit?tab=t.0)。')
- text("## Compute")
- text("- Thanks to [Modal](https://modal.com/) for providing compute. 🙏")
- text("- Please read the [guide](https://docs.google.com/document/d/1cHE0iKVyXLJ3XpIs2XuXTmZ-HMmPk2hIPeCvy-AydMg/edit?tab=t.otis27tacaef) on how to access and use the compute.")
+ bilingual_text("## Compute", '## 计算资源')
+ bilingual_text("- Thanks to [Modal](https://modal.com/) for providing compute. 🙏", '- 感谢 [Modal](https://modal.com/) 提供计算资源。🙏')
+ bilingual_text("- Please read the [guide](https://docs.google.com/document/d/1cHE0iKVyXLJ3XpIs2XuXTmZ-HMmPk2hIPeCvy-AydMg/edit?tab=t.otis27tacaef) on how to access and use the compute.", '- 请阅读这份[指南](https://docs.google.com/document/d/1cHE0iKVyXLJ3XpIs2XuXTmZ-HMmPk2hIPeCvy-AydMg/edit?tab=t.otis27tacaef),了解如何访问和使用计算资源。')
def course_syllabus():
@@ -239,18 +239,18 @@ def course_syllabus():
data() # Assignment 4: evaluation, curation, transformation, filtering, deduplication, mixing
alignment() # Assignment 5: RLHF, RL algorithms, RL systems
- text("Remember it's all about **efficiency**:")
- text("- Resources: data + hardware (compute, memory, communication bandwidth)")
- text("- How do you train the best model given a fixed set of resources?")
+ bilingual_text("Remember it's all about **efficiency**:", '请记住,核心始终是**效率**:')
+ bilingual_text("- Resources: data + hardware (compute, memory, communication bandwidth)", '- 资源:数据 + 硬件(计算量、内存、通信带宽)。')
+ bilingual_text("- How do you train the best model given a fixed set of resources?", '- 在固定资源下,怎样训练出最好的模型?')
- text("Today, we are compute-constrained, so design decisions will reflect squeezing the most out of given hardware.")
- text("- Systems: clearly about efficiency")
- text("- Tokenization: working with raw bytes is elegant, but compute-inefficient with today's model architectures")
- text("- Model architecture: many changes motivated by reducing memory or FLOPs (e.g., sharing KV caches, sliding window attention)")
- text("- Data filtering: avoid wasting precious compute updating on bad / irrelevant data")
- text("- Scaling laws: use less compute on smaller models to do hyperparameter tuning")
+ bilingual_text("Today, we are compute-constrained, so design decisions will reflect squeezing the most out of given hardware.", '今天,我们受计算量约束,因此设计决策会体现如何尽可能榨干给定硬件。')
+ bilingual_text("- Systems: clearly about efficiency", '- 系统:显然关乎效率。')
+ bilingual_text("- Tokenization: working with raw bytes is elegant, but compute-inefficient with today's model architectures", '- 分词:直接处理原始字节很优雅,但在今天的模型架构下计算效率低。')
+ bilingual_text("- Model architecture: many changes motivated by reducing memory or FLOPs (e.g., sharing KV caches, sliding window attention)", '- 模型架构:许多改动都来自减少内存或 FLOPs 的动机(例如共享 KV cache、滑动窗口注意力)。')
+ bilingual_text("- Data filtering: avoid wasting precious compute updating on bad / irrelevant data", '- 数据过滤:避免把宝贵计算量浪费在坏数据或无关数据上。')
+ bilingual_text("- Scaling laws: use less compute on smaller models to do hyperparameter tuning", '- 规模定律:用较小模型和较少计算量做超参数调优。')
- text("Tomorrow, we will become data-constrained...")
+ bilingual_text("Tomorrow, we will become data-constrained...", '之后,我们还会面临数据约束……')
class Tokenizer(ABC):
@@ -263,226 +263,226 @@ def decode(self, indices: list[int]) -> str:
def basics():
- text("Goal: be able to train a basic language model")
- text("Components: tokenization, model architecture, training")
+ bilingual_text("Goal: be able to train a basic language model", '目标:能够训练一个基础语言模型。')
+ bilingual_text("Components: tokenization, model architecture, training", '组成部分:分词、模型架构、训练。')
- text("## Tokenization")
- text("What are the atoms that the model operates on?")
- text("Formally: a tokenizer converts between raw inputs (bytes) and sequences of integers (tokens)")
+ bilingual_text("## Tokenization", '## 分词')
+ bilingual_text("What are the atoms that the model operates on?", '模型操作的“原子单位”是什么?')
+ bilingual_text("Formally: a tokenizer converts between raw inputs (bytes) and sequences of integers (tokens)", '形式化地说:分词器在原始输入(字节)和整数序列(token)之间转换。')
image("images/tokenized-example.png", width=600)
- text("Popular tokenizer: **Byte-Pair Encoding** (BPE) "), link(sennrich_2016)
- text("Intuition: break input into frequently-occuring chunks")
- text("Efficiency lens")
- text("- Reduce context length (1000 bytes → ~250 tokens)")
- text("- Adaptive computation (more modeling capacity on interesting parts of input)")
-
- text("The dream: tokenizer-free model architectures, which operate directly on bytes "), link(byt5_2021), link(megabyte_2023), link(blt_2024), link(tfree_2024), link(hnet_2025)
- text("These are promising, but have not yet been scaled up to the frontier.")
+ bilingual_text("Popular tokenizer: **Byte-Pair Encoding** (BPE) ", '常用分词器:**Byte-Pair Encoding**(BPE,字节对编码)。'), link(sennrich_2016)
+ bilingual_text("Intuition: break input into frequently-occuring chunks", '直觉:把输入切成经常出现的片段。')
+ bilingual_text("Efficiency lens", '从效率角度看:')
+ bilingual_text("- Reduce context length (1000 bytes → ~250 tokens)", '- 缩短上下文长度(1000 字节 → 约 250 个 token)。')
+ bilingual_text("- Adaptive computation (more modeling capacity on interesting parts of input)", '- 自适应计算(把更多建模能力用于输入中更重要的部分)。')
+
+ bilingual_text("The dream: tokenizer-free model architectures, which operate directly on bytes ", '理想方向:无分词器的模型架构,直接在字节上操作。'), link(byt5_2021), link(megabyte_2023), link(blt_2024), link(tfree_2024), link(hnet_2025)
+ bilingual_text("These are promising, but have not yet been scaled up to the frontier.", '这些方向很有前景,但还没有扩展到前沿模型规模。')
- text("## Model architecture")
- text("Starting point: original Transformer "), link(transformer_2017)
+ bilingual_text("## Model architecture", '## 模型架构')
+ bilingual_text("Starting point: original Transformer ", '起点:原始 Transformer。'), link(transformer_2017)
image("images/transformer-architecture.png", width=500)
- text("Refinements:")
- text("- Activation functions: ReLU, SwiGLU "), link(shazeer_2020)
- text("- Positional encodings: sinusoidal, RoPE "), link(rope_2021)
- text("- Normalization: LayerNorm, RMSNorm, QK norm, pre-norm versus post-norm "), link(layernorm_2016), link(rms_norm_2019), link(qk_norm_2023), link(pre_post_norm_2020)
- text("- Attention: full, sparse/local attention, group-query attention (GQA), multi-head latent attention (MLA) "), link(sparse_transformer_2019), link(gqa_2023), link(mla_2024)
- text("- Recurrence/state-space models/linear attention: Mamba, Gated DeltaNet "), link(linear_attention_2020), link(mamba_2_2024), link(gdn_2024), link(mamba_3_2026)
- text("- MLP: dense, mixture of experts "), link(moe_2017), link(switch_transformers_2021)
- text("- Shape (hidden dimension, depth, number of heads, number of experts)")
-
- text("## Training")
- text("How do you set the parameters of the model?")
- text("- Loss function (e.g., multi-token prediction) "), link(mtp_2024), link(deepseek_v3_2024)
- text("- Optimizer (e.g., AdamW, SOAP, Muon) "), link(adam_2014), link(adamw_2017), link(soap_2024), link(muon_2024)
- text("- Initialization scale (e.g., Xavier init, muP) "), link(glorot_2010), link(mup_2022)
- text("- Learning rate schedule (e.g., cosine, WSD) "), link(cosine_learning_rate_2017), link(wsd_2024)
- text("- Regularization (e.g., dropout, weight decay)")
- text("- Batch size (e.g., critical batch size) "), link(large_batch_training_2018)
- text("- MoE specific: load balancing (e.g., aux-free) "), link(auxfree_2024), link(deepseek_v3_2024)
-
- text("## Assignment 1 (basics)")
+ bilingual_text("Refinements:", '改进方向:')
+ bilingual_text("- Activation functions: ReLU, SwiGLU ", '- 激活值 functions: ReLU, SwiGLU'), link(shazeer_2020)
+ bilingual_text("- Positional encodings: sinusoidal, RoPE ", '- 说明:Positional encodings: sinusoidal, RoPE'), link(rope_2021)
+ bilingual_text("- Normalization: LayerNorm, RMSNorm, QK norm, pre-norm versus post-norm ", '- 说明:Normalization: LayerNorm, RMSNorm, QK norm, pre-norm versus post-norm'), link(layernorm_2016), link(rms_norm_2019), link(qk_norm_2023), link(pre_post_norm_2020)
+ bilingual_text("- Attention: full, sparse/local attention, group-query attention (GQA), multi-head latent attention (MLA) ", '- 注意力: full, sparse/local 注意力, group-query 注意力 (GQA), multi-head latent 注意力 (MLA)'), link(sparse_transformer_2019), link(gqa_2023), link(mla_2024)
+ bilingual_text("- Recurrence/state-space models/linear attention: Mamba, Gated DeltaNet ", '- Recurrence/state-space models/linear 注意力: Mamba, Gated DeltaNet'), link(linear_attention_2020), link(mamba_2_2024), link(gdn_2024), link(mamba_3_2026)
+ bilingual_text("- MLP: dense, mixture of experts ", '- 说明:MLP: dense, mixture of experts'), link(moe_2017), link(switch_transformers_2021)
+ bilingual_text("- Shape (hidden dimension, depth, number of heads, number of experts)", '- 形状(隐藏维度、深度、头数、专家数)。')
+
+ bilingual_text("## Training", '## 训练')
+ bilingual_text("How do you set the parameters of the model?", '如何设置模型参数?')
+ bilingual_text("- Loss function (e.g., multi-token prediction) ", '- 损失 function (e.g., multi-token prediction)'), link(mtp_2024), link(deepseek_v3_2024)
+ bilingual_text("- Optimizer (e.g., AdamW, SOAP, Muon) ", '- 优化器 (e.g., AdamW, SOAP, Muon)'), link(adam_2014), link(adamw_2017), link(soap_2024), link(muon_2024)
+ bilingual_text("- Initialization scale (e.g., Xavier init, muP) ", '- 说明:Initialization scale (e.g., Xavier init, muP)'), link(glorot_2010), link(mup_2022)
+ bilingual_text("- Learning rate schedule (e.g., cosine, WSD) ", '- 说明:Learning rate schedule (e.g., cosine, WSD)'), link(cosine_learning_rate_2017), link(wsd_2024)
+ bilingual_text("- Regularization (e.g., dropout, weight decay)", '- 说明:Regularization (e.g., dropout, weight decay)')
+ bilingual_text("- Batch size (e.g., critical batch size) ", '- 批大小 (e.g., critical 批大小)'), link(large_batch_training_2018)
+ bilingual_text("- MoE specific: load balancing (e.g., aux-free) ", '- 说明:MoE specific: load balancing (e.g., aux-free)'), link(auxfree_2024), link(deepseek_v3_2024)
+
+ bilingual_text("## Assignment 1 (basics)", '## 作业 1(基础)')
link(title="GitHub", url="https://github.com/stanford-cs336/assignment1-basics"), link(title="PDF", url="https://github.com/stanford-cs336/assignment1-basics/blob/main/cs336_spring2026_assignment1_basics.pdf")
- text("- Implement BPE tokenizer")
- text("- Implement Transformer, cross-entropy loss, AdamW optimizer, training loop")
- text("- Do resource accounting")
- text("- Train on TinyStories and OpenWebText")
- text("- Leaderboard: minimize OpenWebText perplexity given 45 minutes on a B200 "), link(title="last year's leaderboard", url="https://github.com/stanford-cs336/spring2025-assignment1-basics-leaderboard")
+ bilingual_text("- Implement BPE tokenizer", '- 实现 BPE 分词器。')
+ bilingual_text("- Implement Transformer, cross-entropy loss, AdamW optimizer, training loop", '- 实现 Transformer、交叉熵损失、AdamW 优化器和训练循环。')
+ bilingual_text("- Do resource accounting", '- 做资源核算。')
+ bilingual_text("- Train on TinyStories and OpenWebText", '- 在 TinyStories 和 OpenWebText 上训练。')
+ bilingual_text("- Leaderboard: minimize OpenWebText perplexity given 45 minutes on a B200 ", '- Leaderboard: minimize OpenWebText 困惑度 given 45 minutes on a B200'), link(title="last year's leaderboard", url="https://github.com/stanford-cs336/spring2025-assignment1-basics-leaderboard")
- text("High-level principle: everything is about balancing the following:")
- text("- Expressivity (can represent complex dependencies in the data)")
- text("- Stability (keep parameter and gradient norms in goldilocks zone)")
- text("- Efficiency (runs fast on hardware, both training and inference)")
+ bilingual_text("High-level principle: everything is about balancing the following:", '高层原则:一切都在平衡以下因素:')
+ bilingual_text("- Expressivity (can represent complex dependencies in the data)", '- 表达能力(能表示数据中的复杂依赖关系)。')
+ bilingual_text("- Stability (keep parameter and gradient norms in goldilocks zone)", '- 稳定性(让参数和梯度范数保持在合适区间)。')
+ bilingual_text("- Efficiency (runs fast on hardware, both training and inference)", '- 效率(训练和推理时都能在硬件上快速运行)。')
def systems():
- text("Goal: squeeze the most out of the hardware (GPU or TPU)")
- text("Components: kernels, parallelism, inference")
+ bilingual_text("Goal: squeeze the most out of the hardware (GPU or TPU)", '目标:尽可能榨干硬件(GPU 或 TPU)。')
+ bilingual_text("Components: kernels, parallelism, inference", '组成部分:内核、并行、推理。')
- text("## Basics")
- text("- Resource accounting: memory and compute characteristics of a model")
+ bilingual_text("## Basics", '## 基础')
+ bilingual_text("- Resource accounting: memory and compute characteristics of a model", '- 资源核算: 内存 and 计算量 characteristics of a model')
total_flops = 6 * 70e9 * 1e12 # Training 70B parameters on 1T tokens = 4.2e23 FLOPs @inspect total_flops
image("images/compute-memory.png", width=300)
- text("- Model parameters must be moved from memory (HBM) to the compute (SMs)")
- text("- Example: B200 can perform 2.25 PFLOP/sec (bf16) with 8TB/sec memory bandwidth")
- text("- Roofline analysis: understand whether we're compute-bound or memory-bound")
- text("- Benchmarking and profiling (nsight): see what happens in practice")
+ bilingual_text("- Model parameters must be moved from memory (HBM) to the compute (SMs)", '- Model 参数 must be moved from 内存 (HBM) to the 计算量 (SMs)')
+ bilingual_text("- Example: B200 can perform 2.25 PFLOP/sec (bf16) with 8TB/sec memory bandwidth", '- 示例:B200 can perform 2.25 PFLOP/sec (bf16) with 8TB/sec memory bandwidth')
+ bilingual_text("- Roofline analysis: understand whether we're compute-bound or memory-bound", "- 屋顶线分析: understand whether we're 计算量-bound or 内存-bound")
+ bilingual_text("- Benchmarking and profiling (nsight): see what happens in practice", '- 基准测试 and 性能分析 (nsight): see what happens in practice')
- text("[DGX B200](https://docs.nvidia.com/dgx/dgxb200-user-guide/introduction-to-dgxb200.html):")
+ bilingual_text("[DGX B200](https://docs.nvidia.com/dgx/dgxb200-user-guide/introduction-to-dgxb200.html):", '说明:[DGX B200](https://docs.nvidia.com/dgx/dgxb200-user-guide/introduction-to-dgxb200.html):')
image("https://docs.nvidia.com/dgx/dgxb200-user-guide/_images/dgx-b200-system-topology.png", width=500)
- text("## Kernels")
- text("- Kernel is a function that runs on GPU")
- text("- When using PyTorch, each primitive operation launches a standard kernel")
- text("- Can write custom kernels to make GPUs go brrr")
- text("- Principle: organize computation to minimize data movement")
- text("- Naive: read HBM; compute A; write HBM; read HBM; compute B; write HBM")
- text("- Fused: read HBM; compute A and B; write HBM")
- text("- Strategies: operator fusion (matmul + activation), tiling (FlashAttention)")
- text("- Warp divergence, memory coalescing, bank conflicts, occupancy, bulk-async memory transfers")
- text("- Write kernels in CUDA/**Triton**/CUTLASS/ThunderKittens")
-
- text("## Parallelism")
- text("- What if we have 1024 GPUs?")
- text("- Data movement between GPUs is even slower, but same 'minimize data movement' principle holds")
- text("- Use classic collective operations (e.g., gather, reduce, all-reduce)")
- text("- Shard memory (parameters, activations, gradients, optimizer states) across GPUs")
- text("- How to split computation: {data,tensor,pipeline,sequence,expert} parallelism")
+ bilingual_text("## Kernels", '## 内核')
+ bilingual_text("- Kernel is a function that runs on GPU", '- 说明:Kernel is a function that runs on GPU')
+ bilingual_text("- When using PyTorch, each primitive operation launches a standard kernel", '- 说明:When using PyTorch, each primitive operation launches a standard kernel')
+ bilingual_text("- Can write custom kernels to make GPUs go brrr", '- 说明:Can write custom kernels to make GPUs go brrr')
+ bilingual_text("- Principle: organize computation to minimize data movement", '- Principle: organize computation to minimize 数据 movement')
+ bilingual_text("- Naive: read HBM; compute A; write HBM; read HBM; compute B; write HBM", '- Naive: read HBM; 计算量 A; write HBM; read HBM; 计算量 B; write HBM')
+ bilingual_text("- Fused: read HBM; compute A and B; write HBM", '- Fused: read HBM; 计算量 A and B; write HBM')
+ bilingual_text("- Strategies: operator fusion (matmul + activation), tiling (FlashAttention)", '- Strategies: operator fusion (matmul + 激活值), tiling (Flash注意力)')
+ bilingual_text("- Warp divergence, memory coalescing, bank conflicts, occupancy, bulk-async memory transfers", '- Warp divergence, 内存 coalescing, bank conflicts, occupancy, bulk-async 内存 transfers')
+ bilingual_text("- Write kernels in CUDA/**Triton**/CUTLASS/ThunderKittens", '- 说明:Write kernels in CUDA/Triton/CUTLASS/ThunderKittens')
+
+ bilingual_text("## Parallelism", '## 并行')
+ bilingual_text("- What if we have 1024 GPUs?", '- 说明:What if we have 1024 GPUs?')
+ bilingual_text("- Data movement between GPUs is even slower, but same 'minimize data movement' principle holds", "- 数据 movement between GPUs is even slower, but same 'minimize 数据 movement' principle holds")
+ bilingual_text("- Use classic collective operations (e.g., gather, reduce, all-reduce)", '- 说明:Use classic collective operations (e.g., gather, reduce, all-reduce)')
+ bilingual_text("- Shard memory (parameters, activations, gradients, optimizer states) across GPUs", '- Shard 内存 (参数, 激活值, 梯度, 优化器状态) across GPUs')
+ bilingual_text("- How to split computation: {data,tensor,pipeline,sequence,expert} parallelism", '- How to split computation: {数据,张量,pipeline,sequence,expert} parallelism')
- text("## Inference")
- text("Goal: generate tokens given a prompt (needed to actually use models!)")
- text("Inference is also needed for reinforcement learning, test-time compute, evaluation")
- text("Two phases: prefill and decode")
+ bilingual_text("## Inference", '## 推理')
+ bilingual_text("Goal: generate tokens given a prompt (needed to actually use models!)", '目标:给定提示词生成 token(这是实际使用模型所必需的!)')
+ bilingual_text("Inference is also needed for reinforcement learning, test-time compute, evaluation", '强化学习、测试时计算和评测也需要推理。')
+ bilingual_text("Two phases: prefill and decode", '两个阶段:预填充(prefill)和解码(decode)。')
image("images/prefill-decode.png", width=500)
- text("- Prefill (similar to training): tokens are given, can process all at once (compute-bound)")
- text("- Decode: need to generate one token at a time (memory-bound)")
- text("Methods to speed up decoding:")
- text("- Use cheaper model (via model pruning, quantization, distillation)")
- text("- Speculative decoding: use a cheaper \"draft\" model to generate multiple tokens, then use the full model to score in parallel (exact decoding!)")
- text("- Systems optimizations: fused kernels, continuous batching")
-
- text("## Assignment 2 (systems)")
+ bilingual_text("- Prefill (similar to training): tokens are given, can process all at once (compute-bound)", '- Prefill (similar to 训练): token are given, can process all at once (计算量-bound)')
+ bilingual_text("- Decode: need to generate one token at a time (memory-bound)", '- Decode: need to generate one token at a time (内存-bound)')
+ bilingual_text("Methods to speed up decoding:", '加速解码的方法:')
+ bilingual_text("- Use cheaper model (via model pruning, quantization, distillation)", '- 说明:Use cheaper model (via model pruning, quantization, distillation)')
+ bilingual_text("- Speculative decoding: use a cheaper \"draft\" model to generate multiple tokens, then use the full model to score in parallel (exact decoding!)", '- 说明:Speculative decoding: use a cheaper "draft" model to generate multiple tokens, then use the full model to score in parallel (exact decoding!)')
+ bilingual_text("- Systems optimizations: fused kernels, continuous batching", '- 说明:Systems optimizations: fused kernels, continuous batching')
+
+ bilingual_text("## Assignment 2 (systems)", '## 作业 2(系统)')
link(title="GitHub", url="https://github.com/stanford-cs336/assignment2-systems"), link(title="PDF from Spring 2025", url="https://github.com/stanford-cs336/assignment2-systems/blob/spring2025/cs336_spring2025_assignment2_systems.pdf")
- text("- Implement a fused RMSNorm kernel in Triton")
- text("- Implement distributed data parallel training")
- text("- Implement optimizer state sharding")
- text("- Benchmark and profile the implementations")
+ bilingual_text("- Implement a fused RMSNorm kernel in Triton", '- 说明:Implement a fused RMSNorm kernel in Triton')
+ bilingual_text("- Implement distributed data parallel training", '- Implement distributed 数据 parallel 训练')
+ bilingual_text("- Implement optimizer state sharding", '- Implement 优化器状态 sharding')
+ bilingual_text("- Benchmark and profile the implementations", '- 基准 and profile the implementations')
- text("Recommended book: [How to Scale Your Model](https://jax-ml.github.io/scaling-book/)")
- text("- Nicely lays out how to approach systems for LLMs conceptually")
- text("- From Google, so it foregrounds TPUs, but high-level concepts are similar")
+ bilingual_text("Recommended book: [How to Scale Your Model](https://jax-ml.github.io/scaling-book/)", '推荐书:[How to Scale Your Model](https://jax-ml.github.io/scaling-book/)。')
+ bilingual_text("- Nicely lays out how to approach systems for LLMs conceptually", '- 很好地从概念层面说明如何理解 LLM 系统。')
+ bilingual_text("- From Google, so it foregrounds TPUs, but high-level concepts are similar", '- 作者来自 Google,所以更强调 TPU,但高层概念是相似的。')
def scaling_laws():
- text("Setting: if you had 1e25 FLOPs of compute, what hyperparameters would you use to train a good model?")
- text("Too expensive to do hyperparameter tuning at full scale!")
-
- text("Key conceptual shift: instead of a single scale, think of a **scaling recipe** (FLOPs → hyperparameters)")
- text("For a scaling recipe:")
- text("- Run experiments to compute the loss at various smaller scales (e.g., up to 1e24 FLOPs)")
- text("- Fit a scaling law to predict the loss of the scaling recipe at the target scale (e.g., 1e25 FLOPs)")
-
- text("Now you can:")
- text("1. Optimize the scaling recipe targeting a larger scale using smaller scale experiments")
- text("2. Predict the loss at the target scale before actually running the experiment!")
- text("Scaling laws don't happen automatically, they require careful construction of a scaling recipe.")
- text("Parameterize the model in a way to get **hyperparameter transfer** "), link(mup_2022)
- text("Predictability is at least as important as optimality!")
-
- text("Question: given a FLOPs budget (C = 6 N D), use a bigger model (N) or train on more tokens (D)?")
- text("Classic compute-optimal scaling laws: "), link(kaplan_scaling_laws_2020), link(chinchilla_2022)
- text("- ISOFLOP curves: for multiple small FLOPs budgets, find optimal N")
- text("- Then fit a scaling law to extrapolate to large FLOPs budgets")
+ bilingual_text("Setting: if you had 1e25 FLOPs of compute, what hyperparameters would you use to train a good model?", 'Setting: if you had 1e25 FLOPs of 计算量, what 超参数 would you use to train a good model?')
+ bilingual_text("Too expensive to do hyperparameter tuning at full scale!", 'Too expensive to do 超参数 tuning at full scale!')
+
+ bilingual_text("Key conceptual shift: instead of a single scale, think of a **scaling recipe** (FLOPs → hyperparameters)", '关键概念转变:不要只考虑单一规模,而要考虑一套**规模化配方**(FLOPs → 超参数)。')
+ bilingual_text("For a scaling recipe:", '对于一套规模化配方:')
+ bilingual_text("- Run experiments to compute the loss at various smaller scales (e.g., up to 1e24 FLOPs)", '- Run experiments to 计算量 the 损失 at various smaller scales (e.g., up to 1e24 FLOPs)')
+ bilingual_text("- Fit a scaling law to predict the loss of the scaling recipe at the target scale (e.g., 1e25 FLOPs)", '- Fit a 规模定律 to predict the 损失 of the 规模化 recipe at the target scale (e.g., 1e25 FLOPs)')
+
+ bilingual_text("Now you can:", '说明:Now you can:')
+ bilingual_text("1. Optimize the scaling recipe targeting a larger scale using smaller scale experiments", '1. Optimize the 规模化 recipe targeting a larger scale using smaller scale experiments')
+ bilingual_text("2. Predict the loss at the target scale before actually running the experiment!", '2. Predict the 损失 at the target scale before actually running the experiment!')
+ bilingual_text("Scaling laws don't happen automatically, they require careful construction of a scaling recipe.", "规模定律 don't happen automatically, they require careful construction of a 规模化 recipe.")
+ bilingual_text("Parameterize the model in a way to get **hyperparameter transfer** ", '参数ize the model in a way to get 超参数 transfer'), link(mup_2022)
+ bilingual_text("Predictability is at least as important as optimality!", '说明:Predictability is at least as important as optimality!')
+
+ bilingual_text("Question: given a FLOPs budget (C = 6 N D), use a bigger model (N) or train on more tokens (D)?", '问题:given a FLOPs budget (C = 6 N D), use a bigger model (N) or train on more tokens (D)?')
+ bilingual_text("Classic compute-optimal scaling laws: ", 'Classic 计算量-optimal 规模定律:'), link(kaplan_scaling_laws_2020), link(chinchilla_2022)
+ bilingual_text("- ISOFLOP curves: for multiple small FLOPs budgets, find optimal N", '- 说明:ISOFLOP curves: for multiple small FLOPs budgets, find optimal N')
+ bilingual_text("- Then fit a scaling law to extrapolate to large FLOPs budgets", '- Then fit a 规模定律 to extrapolate to large FLOPs budgets')
image("images/chinchilla-isoflop.png", width=800)
- text("TL;DR: D = 20 N is roughly optimal (e.g., 70B parameter model should be trained on ~1.4T tokens)")
- text("Caveat: this doesn't take into account inference costs (want a smaller model)")
+ bilingual_text("TL;DR: D = 20 N is roughly optimal (e.g., 70B parameter model should be trained on ~1.4T tokens)", 'TL;DR: D = 20 N is roughly optimal (e.g., 70B 参数 model should be trained on ~1.4T token)')
+ bilingual_text("Caveat: this doesn't take into account inference costs (want a smaller model)", "注意:this doesn't take into account inference costs (want a smaller model)")
- text("Live example from Marin "), post_link("https://x.com/percyliang/status/2034367256277533100")
+ bilingual_text("Live example from Marin ", '说明:Live example from Marin'), post_link("https://x.com/percyliang/status/2034367256277533100")
image("https://pbs.twimg.com/media/HDuErvvbsAAQ5Yt?format=jpg&name=4096x4096", width=600)
- text("Should be done training this week, should see how well we match the preregistered loss!")
+ bilingual_text("Should be done training this week, should see how well we match the preregistered loss!", 'Should be done 训练 this week, should see how well we match the preregistered 损失!')
- text("## Assignment 3 (scaling laws)")
+ bilingual_text("## Assignment 3 (scaling laws)", '## 作业 3(规模定律)')
link(title="GitHub", url="https://github.com/stanford-cs336/assignment3-scaling"), link(title="PDF from Spring 2025", url="https://github.com/stanford-cs336/assignment3-scaling/blob/master/cs336_spring2025_assignment3_scaling.pdf")
- text("- We define a training API (hyperparameters → loss) based on previous runs")
- text("- Submit \"training jobs\" (under a FLOPs budget) and gather data points")
- text("- Fit scaling laws to the data points")
- text("- Submit extrapolated hyperparameters and loss predictions")
- text("- Leaderboard: minimize loss given FLOPs budget")
+ bilingual_text("- We define a training API (hyperparameters → loss) based on previous runs", '- 我们基于之前的运行定义一个训练 API(超参数 → 损失)。')
+ bilingual_text("- Submit \"training jobs\" (under a FLOPs budget) and gather data points", '- 提交“训练作业”(受 FLOPs 预算约束)并收集数据点。')
+ bilingual_text("- Fit scaling laws to the data points", '- Fit 规模定律 to the 数据 points')
+ bilingual_text("- Submit extrapolated hyperparameters and loss predictions", '- Submit extrapolated 超参数 and 损失 predictions')
+ bilingual_text("- Leaderboard: minimize loss given FLOPs budget", '- Leaderboard: minimize 损失 given FLOPs budget')
def data():
- text("Question: What capabilities do we want the model to have?")
- text("Multilingual? Good at conversation? Agentic coding capabilities?")
-
- text("## Evaluation")
- text("What is the purpose of evaluation?")
- text("1. Internal: guide model development (smoothness across scales, relative performance matters)")
- text("2. External: measure absolute quality of a real use case (ecological validity matters)")
- text("Examples of evaluations:")
- text("1. Perplexity: ideally run on private documents not on Internet (avoid contamination)")
- text("2. Advanced use cases: GPQA, HLE, SWE-Bench, Terminal-Bench")
- text("LMs are general purpose, require a diverse set of evaluations!")
-
- text("## Data curation")
- text("- Data does not just fall from the sky.")
- text("- Sources: webpages crawled from the Internet, books, arXiv papers, GitHub code, etc.")
+ bilingual_text("Question: What capabilities do we want the model to have?", '问题:我们希望模型具备哪些能力?')
+ bilingual_text("Multilingual? Good at conversation? Agentic coding capabilities?", '多语言?擅长对话?具备智能体式编码能力?')
+
+ bilingual_text("## Evaluation", '## 评测')
+ bilingual_text("What is the purpose of evaluation?", '评测的目的是什么?')
+ bilingual_text("1. Internal: guide model development (smoothness across scales, relative performance matters)", '1. 说明:Internal: guide model development (smoothness across scales, relative performance matters)')
+ bilingual_text("2. External: measure absolute quality of a real use case (ecological validity matters)", '2. 说明:External: measure absolute quality of a real use case (ecological validity matters)')
+ bilingual_text("Examples of evaluations:", '评测示例:')
+ bilingual_text("1. Perplexity: ideally run on private documents not on Internet (avoid contamination)", '1. 困惑度: ideally run on private documents not on Internet (avoid contamination)')
+ bilingual_text("2. Advanced use cases: GPQA, HLE, SWE-Bench, Terminal-Bench", '2. 说明:Advanced use cases: GPQA, HLE, SWE-Bench, Terminal-Bench')
+ bilingual_text("LMs are general purpose, require a diverse set of evaluations!", '语言模型是通用系统,因此需要多样化的评测集合!')
+
+ bilingual_text("## Data curation", '## 数据策划')
+ bilingual_text("- Data does not just fall from the sky.", '- 数据不会从天上掉下来。')
+ bilingual_text("- Sources: webpages crawled from the Internet, books, arXiv papers, GitHub code, etc.", '- 说明:Sources: webpages crawled from the Internet, books, arXiv papers, GitHub code, etc.')
image("https://ar5iv.labs.arxiv.org/html/2101.00027/assets/pile_chart2.png", width=600)
- text("- Appeal to fair use to train on copyright data? "), link("https://arxiv.org/pdf/2303.15715.pdf")
- text("- Might have to license data (e.g., Google with Reddit data) "), article_link("https://www.reuters.com/technology/reddit-ai-content-licensing-deal-with-google-sources-say-2024-02-22/")
- text("- Raw data is HTML, PDF, directories (not text), requires processing")
-
- text("## Data processing")
- text("- Transformation: convert HTML/PDF to text (extract main content)")
- text("- Filtering: keep high quality data, remove harmful content (via classifiers)")
- text("- Deduplication: save compute, avoid memorization; use Bloom filters or MinHash")
- text("- Data mixing: how much to upweight/downweight each source? "), link(regmix_2025), link(olmix_2026)
- text("- Rewriting / synthetic data: use LM to augment real data, more similar to downstream tasks "), link(wrap_2024)
-
- text("Types of data:")
- text("- Pretraining data: large and diverse")
- text("- Mid-training data: high quality, including long-context")
- text("- Post-training data: supervised fine-tuning (conversations, agentic traces with tool calling)")
-
- text("## Assignment 4 (data)")
+ bilingual_text("- Appeal to fair use to train on copyright data? ", '- Appeal to fair use to train on copyright 数据?'), link("https://arxiv.org/pdf/2303.15715.pdf")
+ bilingual_text("- Might have to license data (e.g., Google with Reddit data) ", '- Might have to license 数据 (e.g., Google with Reddit 数据)'), article_link("https://www.reuters.com/technology/reddit-ai-content-licensing-deal-with-google-sources-say-2024-02-22/")
+ bilingual_text("- Raw data is HTML, PDF, directories (not text), requires processing", '- Raw 数据 is HTML, PDF, directories (not text), requires processing')
+
+ bilingual_text("## Data processing", '## 数据处理')
+ bilingual_text("- Transformation: convert HTML/PDF to text (extract main content)", '- 说明:Transformation: convert HTML/PDF to text (extract main content)')
+ bilingual_text("- Filtering: keep high quality data, remove harmful content (via classifiers)", '- 过滤: keep high quality 数据, remove harmful content (via classifiers)')
+ bilingual_text("- Deduplication: save compute, avoid memorization; use Bloom filters or MinHash", '- 去重: save 计算量, avoid memorization; use Bloom filters or MinHash')
+ bilingual_text("- Data mixing: how much to upweight/downweight each source? ", '- 数据 mixing: how much to upweight/downweight each source?'), link(regmix_2025), link(olmix_2026)
+ bilingual_text("- Rewriting / synthetic data: use LM to augment real data, more similar to downstream tasks ", '- Rewriting / synthetic 数据: use LM to augment real 数据, more similar to downstream tasks'), link(wrap_2024)
+
+ bilingual_text("Types of data:", '数据类型:')
+ bilingual_text("- Pretraining data: large and diverse", '- Pre训练 数据: large and diverse')
+ bilingual_text("- Mid-training data: high quality, including long-context", '- Mid-训练 数据: high quality, including long-context')
+ bilingual_text("- Post-training data: supervised fine-tuning (conversations, agentic traces with tool calling)", '- Post-训练 数据: supervised 微调 (conversations, agentic traces with tool calling)')
+
+ bilingual_text("## Assignment 4 (data)", '## 作业 4(数据)')
link(title="GitHub", url="https://github.com/stanford-cs336/assignment4-data"), link(title="PDF from Spring 2025", url="https://github.com/stanford-cs336/assignment4-data/blob/spring2025/cs336_spring2025_assignment4_data.pdf")
- text("- Convert Common Crawl HTML to text")
- text("- Train classifiers to filter for quality and harmful content")
- text("- Deduplication using MinHash")
- text("- Leaderboard: minimize perplexity given token budget")
+ bilingual_text("- Convert Common Crawl HTML to text", '- 说明:Convert Common Crawl HTML to text')
+ bilingual_text("- Train classifiers to filter for quality and harmful content", '- 说明:Train classifiers to filter for quality and harmful content')
+ bilingual_text("- Deduplication using MinHash", '- 去重 using MinHash')
+ bilingual_text("- Leaderboard: minimize perplexity given token budget", '- Leaderboard: minimize 困惑度 given token budget')
def alignment():
- text("So far, we have trained a model on full supervision (predict the next token).")
- text("Now that the model should be reasonable, we can improve it further from **weak supervision**.")
- text("Why weak supervision? When it is easier to critique than to generate.")
-
- text("Basic template:")
- text("1. Generate responses from the model.")
- text("2. Score responses with a {human, verifier, LM judge}.")
- text("3. Update the model to prefer better responses.")
-
- text("Algorithms:")
- text("- Proximal Policy Optimization (PPO) from reinforcement learning "), link(ppo_2017), link(instruct_gpt_2022)
- text("- Direct Policy Optimization (DPO): for preference data, simpler "), link(dpo_2023)
- text("- Group Relative Preference Optimization (GRPO): remove value function "), link(grpo)
-
- text("Challenges:")
- text("- RL algorithms are unstable and hard to tune")
- text("- At scale, this requires a lot of new infrastructure (inference with async rollouts)")
- text("- Constantly trading off systems efficiency and on-policyness")
-
- text("## Assignment 5 (alignment)")
+ bilingual_text("So far, we have trained a model on full supervision (predict the next token).", '到目前为止,我们用完全监督训练模型(预测下一个 token)。')
+ bilingual_text("Now that the model should be reasonable, we can improve it further from **weak supervision**.", '现在模型应该已经比较合理,我们可以进一步用**弱监督**改进它。')
+ bilingual_text("Why weak supervision? When it is easier to critique than to generate.", '为什么使用弱监督?因为有时评价比生成更容易。')
+
+ bilingual_text("Basic template:", '基本模板:')
+ bilingual_text("1. Generate responses from the model.", '1. 说明:Generate responses from the model.')
+ bilingual_text("2. Score responses with a {human, verifier, LM judge}.", '2. 说明:Score responses with a {human, verifier, LM judge}.')
+ bilingual_text("3. Update the model to prefer better responses.", '3. 说明:Update the model to prefer better responses.')
+
+ bilingual_text("Algorithms:", '算法:')
+ bilingual_text("- Proximal Policy Optimization (PPO) from reinforcement learning ", '- Proximal Policy Optimization (PPO) from 强化学习'), link(ppo_2017), link(instruct_gpt_2022)
+ bilingual_text("- Direct Policy Optimization (DPO): for preference data, simpler ", '- Direct Policy Optimization (DPO): for preference 数据, simpler'), link(dpo_2023)
+ bilingual_text("- Group Relative Preference Optimization (GRPO): remove value function ", '- 说明:Group Relative Preference Optimization (GRPO): remove value function'), link(grpo)
+
+ bilingual_text("Challenges:", '挑战:')
+ bilingual_text("- RL algorithms are unstable and hard to tune", '- 说明:RL algorithms are unstable and hard to tune')
+ bilingual_text("- At scale, this requires a lot of new infrastructure (inference with async rollouts)", '- At scale, this requires a lot of new infrastructure (推理 with async rollouts)')
+ bilingual_text("- Constantly trading off systems efficiency and on-policyness", '- Constantly trading off systems 效率 and on-policyness')
+
+ bilingual_text("## Assignment 5 (alignment)", '## 作业 5(对齐)')
link(title="GitHub", url="https://github.com/stanford-cs336/assignment5-alignment"), link(title="PDF from Spring 2025", url="https://github.com/stanford-cs336/assignment5-alignment/blob/spring2025/cs336_spring2025_assignment5_alignment.pdf")
- text("- Implement Direct Preference Optimization (DPO)")
- text("- Implement Group Relative Preference Optimization (GRPO)")
+ bilingual_text("- Implement Direct Preference Optimization (DPO)", '- 说明:Implement Direct Preference Optimization (DPO)')
+ bilingual_text("- Implement Group Relative Preference Optimization (GRPO)", '- 说明:Implement Group Relative Preference Optimization (GRPO)')
############################################################
# Tokenization
def tokenization():
- text("This unit was inspired by Andrej Karpathy's video on tokenization; check it out! "), video_link("https://www.youtube.com/watch?v=zduSFxRajkE")
+ bilingual_text("This unit was inspired by Andrej Karpathy's video on tokenization; check it out! ", "This unit was inspired by Andrej Karpathy's video on 分词; check it out!"), video_link("https://www.youtube.com/watch?v=zduSFxRajkE")
intro_to_tokenization()
tokenization_examples()
@@ -491,15 +491,15 @@ def tokenization():
word_tokenizer()
bpe_tokenizer()
- text("Summary:")
- text("- Tokenizer: strings ↔ tokens (indices)")
- text("- Character-based, byte-based, word-based tokenization are highly suboptimal")
- text("- BPE is an effective heuristic that is data-driven")
- text("- Tokenization is a separate step, maybe one day do it end-to-end from bytes...")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Tokenizer: strings ↔ tokens (indices)", '- 分词器:字符串 ↔ token(索引)。')
+ bilingual_text("- Character-based, byte-based, word-based tokenization are highly suboptimal", '- 基于字符、字节或单词的分词都很不理想。')
+ bilingual_text("- BPE is an effective heuristic that is data-driven", '- BPE 是一种由数据驱动的有效启发式方法。')
+ bilingual_text("- Tokenization is a separate step, maybe one day do it end-to-end from bytes...", '- 分词是一个单独步骤,也许未来可以直接从字节端到端建模……')
- text("But whatever solution needs to satisfy:")
- text("1. Model (e.g., Transformer) should operate on chunks (abstractions) of the sequence (text, video, DNA, etc.)")
- text("2. Chunks should be variable (allocate more model capacity to interesting chunks)")
+ bilingual_text("But whatever solution needs to satisfy:", '但无论采用什么方案,都需要满足:')
+ bilingual_text("1. Model (e.g., Transformer) should operate on chunks (abstractions) of the sequence (text, video, DNA, etc.)", '1. 模型(例如 Transformer)应该在序列(文本、视频、DNA 等)的片段(抽象单位)上操作。')
+ bilingual_text("2. Chunks should be variable (allocate more model capacity to interesting chunks)", '2. 片段应该是可变的(给重要片段分配更多模型容量)。')
class CharacterTokenizer(Tokenizer):
@@ -577,41 +577,41 @@ def get_gpt5_tokenizer():
def intro_to_tokenization():
- text("Raw text is generally represented as Unicode strings.")
+ bilingual_text("Raw text is generally represented as Unicode strings.", '原始文本通常表示为 Unicode 字符串。')
string = "Hello, 🌍! 你好!"
- text("A language model places a probability distribution over sequences of tokens (usually represented by integer indices).")
+ bilingual_text("A language model places a probability distribution over sequences of tokens (usually represented by integer indices).", '语言模型为 token 序列(通常用整数索引表示)分配概率分布。')
indices = [15496, 11, 995, 0]
- text("So we need a procedure that *encodes* strings into tokens.")
- text("We also need a procedure that *decodes* tokens back into strings.")
- text("A "), link(Tokenizer), text(" is a class that implements the encode and decode methods.")
+ bilingual_text("So we need a procedure that *encodes* strings into tokens.", '因此我们需要一个过程,把字符串*编码*成 token。')
+ bilingual_text("We also need a procedure that *decodes* tokens back into strings.", '我们也需要一个过程,把 token *解码*回字符串。')
+ bilingual_text("A ", '说明:A'), link(Tokenizer), bilingual_text(" is a class that implements the encode and decode methods.", '说明:is a class that implements the encode and decode methods.')
def tokenization_examples():
- text("To get a feel for how tokenizers work, play with this "), link(title="interactive site", url="https://tiktokenizer.vercel.app/?encoder=gpt2")
+ bilingual_text("To get a feel for how tokenizers work, play with this ", 'To get a feel for how 分词器s work, play with this'), link(title="interactive site", url="https://tiktokenizer.vercel.app/?encoder=gpt2")
- text("## Observations")
- text("- A word and its preceding space are part of the same token (e.g., \" world\").")
- text("- A word at the beginning and in the middle are represented differently (e.g., \"hello hello\").")
- text("- Numbers are tokenized into every few digits.")
+ bilingual_text("## Observations", '## 观察')
+ bilingual_text("- A word and its preceding space are part of the same token (e.g., \" world\").", '- 说明:A word and its preceding space are part of the same token (e.g., " world").')
+ bilingual_text("- A word at the beginning and in the middle are represented differently (e.g., \"hello hello\").", '- 说明:A word at the beginning and in the middle are represented differently (e.g., "hello hello").')
+ bilingual_text("- Numbers are tokenized into every few digits.", '- 说明:Numbers are tokenized into every few digits.')
- text("Here's the GPT-5 tokenizer from OpenAI (tiktoken) in action.")
+ bilingual_text("Here's the GPT-5 tokenizer from OpenAI (tiktoken) in action.", "Here's the GPT-5 分词器 from OpenAI (tiktoken) in action.")
tokenizer = get_gpt5_tokenizer() # @stepover
string = "Hello, 🌍! 你好!" # @inspect string
- text("Check that encode() and decode() roundtrip:")
+ bilingual_text("Check that encode() and decode() roundtrip:", 'Check that encode() and decode() 往返转换:')
indices = tokenizer.encode(string) # @inspect indices
reconstructed_string = tokenizer.decode(indices) # @inspect reconstructed_string
assert string == reconstructed_string
- text("Compression ratio: number of bytes per token")
+ bilingual_text("Compression ratio: number of bytes per token", '压缩率:每个 token 对应的字节数。')
compression_ratio = get_compression_ratio(string, indices) # @inspect compression_ratio
- text("The larger the compression ratio, the shorter the sequence (good since attention is quadratic in sequence length).")
- text("One could increase compression ratio by increasing **vocabulary size** (number of possible token values increases), leading to sparsity.")
+ bilingual_text("The larger the compression ratio, the shorter the sequence (good since attention is quadratic in sequence length).", 'The larger the 压缩率, the shorter the sequence (good since 注意力 is quadratic in 序列长度).')
+ bilingual_text("One could increase compression ratio by increasing **vocabulary size** (number of possible token values increases), leading to sparsity.", 'One could increase 压缩率 by increasing 词表 size (number of possible token values increases), leading to sparsity.')
vocabulary_size = tokenizer.n_vocab # @inspect vocabulary_size
- text("Let's take a look at the actual vocabulary: "), link(title="vocab", url=get_local_url("var/gpt5_tokenizer_vocab.txt"))
+ bilingual_text("Let's take a look at the actual vocabulary: ", "Let's take a look at the actual 词表:"), link(title="vocab", url=get_local_url("var/gpt5_tokenizer_vocab.txt"))
output_tokenizer(tokenizer, "var/gpt5_tokenizer_vocab.txt") # @stepover
@@ -625,109 +625,109 @@ def output_tokenizer(tokenizer, path: str):
def character_tokenizer():
- text("A Unicode string is a sequence of Unicode characters.")
- text("Each character can be converted into a code point (integer) via `ord`.")
+ bilingual_text("A Unicode string is a sequence of Unicode characters.", '说明:A Unicode string is a sequence of Unicode characters.')
+ bilingual_text("Each character can be converted into a code point (integer) via `ord`.", '说明:Each character can be converted into a code point (integer) via ord.')
assert ord("a") == 97
assert ord("🌍") == 127757
- text("It can be converted back via `chr`.")
+ bilingual_text("It can be converted back via `chr`.", '说明:It can be converted back via chr.')
assert chr(97) == "a"
assert chr(127757) == "🌍"
- text("Now let's build a `Tokenizer` and make sure it round-trips:")
+ bilingual_text("Now let's build a `Tokenizer` and make sure it round-trips:", "Now let's build a 分词器 and make sure it round-trips:")
tokenizer = CharacterTokenizer()
string = "Hello, 🌍! 你好!" # @inspect string
indices = tokenizer.encode(string) # call ord @inspect indices @stepover
reconstructed_string = tokenizer.decode(indices) # call chr @inspect reconstructed_string @stepover
assert string == reconstructed_string
- text("There are approximately 150K Unicode characters. "), link(title="Wikipedia", url="https://en.wikipedia.org/wiki/List_of_Unicode_characters")
+ bilingual_text("There are approximately 150K Unicode characters. ", '说明:There are approximately 150K Unicode characters.'), link(title="Wikipedia", url="https://en.wikipedia.org/wiki/List_of_Unicode_characters")
vocabulary_size = max(indices) + 1 # This is a lower bound @inspect vocabulary_size
- text("Problem 1: this is a very large vocabulary.")
- text("Problem 2: many characters are quite rare (e.g., 🌍), which is inefficient use of the vocabulary.")
+ bilingual_text("Problem 1: this is a very large vocabulary.", 'Problem 1: this is a very large 词表.')
+ bilingual_text("Problem 2: many characters are quite rare (e.g., 🌍), which is inefficient use of the vocabulary.", '问题 2:许多字符非常少见(例如 🌍),这会低效地占用词表。')
compression_ratio = get_compression_ratio(string, indices) # @inspect compression_ratio @stepover
- text("This tokenizer is the worst of both worlds (large vocabulary, low compression ratio).")
+ bilingual_text("This tokenizer is the worst of both worlds (large vocabulary, low compression ratio).", 'This 分词器 is the worst of both worlds (large 词表, low 压缩率).')
def byte_tokenizer():
- text("Unicode strings can be represented as a sequence of bytes, which can be represented by integers between 0 and 255.")
- text("The most common Unicode encoding is "), link(title="UTF-8", url="https://en.wikipedia.org/wiki/UTF-8")
+ bilingual_text("Unicode strings can be represented as a sequence of bytes, which can be represented by integers between 0 and 255.", 'Unicode strings can be represented as a sequence of 字节, which can be represented by integers between 0 and 255.')
+ bilingual_text("The most common Unicode encoding is ", '说明:The most common Unicode encoding is'), link(title="UTF-8", url="https://en.wikipedia.org/wiki/UTF-8")
- text("Some Unicode characters are represented by one byte:")
+ bilingual_text("Some Unicode characters are represented by one byte:", 'Some Unicode characters are represented by one 字节:')
assert bytes("a", encoding="utf-8") == b"a"
- text("Others take multiple bytes:")
+ bilingual_text("Others take multiple bytes:", 'Others take multiple 字节:')
assert bytes("🌍", encoding="utf-8") == b"\xf0\x9f\x8c\x8d"
- text("Now let's build a `Tokenizer` and make sure it round-trips:")
+ bilingual_text("Now let's build a `Tokenizer` and make sure it round-trips:", "Now let's build a 分词器 and make sure it round-trips:")
tokenizer = ByteTokenizer()
string = "Hello, 🌍! 你好!" # @inspect string
indices = tokenizer.encode(string) # @inspect indices @stepover
reconstructed_string = tokenizer.decode(indices) # @inspect reconstructed_string @stepover
assert string == reconstructed_string
- text("The vocabulary is nice and small: a byte can represent 256 values.")
+ bilingual_text("The vocabulary is nice and small: a byte can represent 256 values.", 'The 词表 is nice and small: a 字节 can represent 256 values.')
vocabulary_size = 256 # @inspect vocabulary_size
- text("What about the compression rate?")
+ bilingual_text("What about the compression rate?", '说明:What about the compression rate?')
compression_ratio = get_compression_ratio(string, indices) # @inspect compression_ratio @stepover
assert compression_ratio == 1
- text("The compression ratio is terrible, which means the sequences will be too long.")
- text("Given that the context length of a Transformer is limited (since attention is quadratic), this is not looking great...")
+ bilingual_text("The compression ratio is terrible, which means the sequences will be too long.", 'The 压缩率 is terrible, which means the sequences will be too long.')
+ bilingual_text("Given that the context length of a Transformer is limited (since attention is quadratic), this is not looking great...", 'Given that the 上下文长度 of a Transformer is limited (since 注意力 is quadratic), this is not looking great...')
def word_tokenizer():
- text("Another approach (closer to what was done classically in NLP) is to split strings into words.")
+ bilingual_text("Another approach (closer to what was done classically in NLP) is to split strings into words.", '说明:Another approach (closer to what was done classically in NLP) is to split strings into words.')
string = "I'll say supercalifragilisticexpialidocious!"
chunks = regex.findall(r"\w+|.", string) # @inspect chunks
- text("This regular expression keeps all alphanumeric characters together (words).")
+ bilingual_text("This regular expression keeps all alphanumeric characters together (words).", 'This 正则表达式 keeps all alphanumeric characters together (words).')
- text("To turn this into a `Tokenizer`, we need to map these chunks into integers.")
- text("Then, we can build a mapping from each chunk into an integer.")
+ bilingual_text("To turn this into a `Tokenizer`, we need to map these chunks into integers.", 'To turn this into a 分词器, we need to map these chunks into integers.')
+ bilingual_text("Then, we can build a mapping from each chunk into an integer.", '说明:Then, we can build a mapping from each chunk into an integer.')
- text("What's good: each token is meaningful (since humans invented words).")
+ bilingual_text("What's good: each token is meaningful (since humans invented words).", "说明:What's good: each token is meaningful (since humans invented words).")
vocabulary_size = "Number of distinct chunks in the training data"
compression_ratio = get_compression_ratio(string, chunks) # @inspect compression_ratio @stepover
- text("Compression ratio is good, but vocabulary size can be huge.")
+ bilingual_text("Compression ratio is good, but vocabulary size can be huge.", '压缩率 is good, but 词表 size can be huge.')
- text("Moreover:")
- text("- Many words are rare and the model won't learn much about them.")
- text("- This doesn't obviously provide a fixed vocabulary size.")
- text("- New words we haven't seen during training get a special UNK token, which is ugly and can mess up perplexity calculations.")
+ bilingual_text("Moreover:", '说明:Moreover:')
+ bilingual_text("- Many words are rare and the model won't learn much about them.", "- 说明:Many words are rare and the model won't learn much about them.")
+ bilingual_text("- This doesn't obviously provide a fixed vocabulary size.", "- This doesn't obviously provide a fixed 词表 size.")
+ bilingual_text("- New words we haven't seen during training get a special UNK token, which is ugly and can mess up perplexity calculations.", "- New words we haven't seen during 训练 get a special UNK token, which is ugly and can mess up 困惑度 calculations.")
def bpe_tokenizer():
- text("## Byte Pair Encoding (BPE)")
- text("The BPE algorithm was introduced by Philip Gage in 1994 for data compression. "), article_link("http://www.pennelynn.com/Documents/CUJ/HTML/94HTML/19940045.HTM")
- text("It was adapted to NLP for neural machine translation. "), link(sennrich_2016)
- text("(Previously, papers had been using word-based tokenization.)")
- text("BPE was then used by GPT-2. "), link(gpt2_2019)
+ bilingual_text("## Byte Pair Encoding (BPE)", '## 字节对编码(Byte Pair Encoding, BPE)')
+ bilingual_text("The BPE algorithm was introduced by Philip Gage in 1994 for data compression. ", 'The BPE algorithm was introduced by Philip Gage in 1994 for 数据 compression.'), article_link("http://www.pennelynn.com/Documents/CUJ/HTML/94HTML/19940045.HTM")
+ bilingual_text("It was adapted to NLP for neural machine translation. ", '说明:It was adapted to NLP for neural machine translation.'), link(sennrich_2016)
+ bilingual_text("(Previously, papers had been using word-based tokenization.)", '(Previously, papers had been using word-based 分词.)')
+ bilingual_text("BPE was then used by GPT-2. ", '说明:BPE was then used by GPT-2.'), link(gpt2_2019)
- text("Basic idea: *train* the tokenizer on raw text to construct a vocabulary tailored to the data.")
- text("Intuition: common sequences of bytes are represented by a single token, rare sequences are represented by many tokens.")
+ bilingual_text("Basic idea: *train* the tokenizer on raw text to construct a vocabulary tailored to the data.", 'Basic idea: train the 分词器 on raw text to construct a 词表 tailored to the 数据.')
+ bilingual_text("Intuition: common sequences of bytes are represented by a single token, rare sequences are represented by many tokens.", 'Intuition: common sequences of 字节 are represented by a single token, rare sequences are represented by many token.')
- text("Sketch: start with each byte as a token, and successively merge the most common pair of adjacent tokens.")
+ bilingual_text("Sketch: start with each byte as a token, and successively merge the most common pair of adjacent tokens.", 'Sketch: start with each 字节 as a token, and successively merge the most common pair of adjacent token.')
- text("## Training the tokenizer")
+ bilingual_text("## Training the tokenizer", '## 训练分词器')
string = "the cat in the hat" # @inspect string
params = train_bpe(string, num_merges=3)
- text("## Using the tokenizer")
- text("Now, given a new text, we can encode it.")
+ bilingual_text("## Using the tokenizer", '## 使用分词器')
+ bilingual_text("Now, given a new text, we can encode it.", '现在,给定一段新文本,我们可以对它编码。')
tokenizer = BPETokenizer(params) # @stepover
string = "the quick brown fox" # @inspect string
indices = tokenizer.encode(string) # @inspect indices
reconstructed_string = tokenizer.decode(indices) # @inspect reconstructed_string @stepover
assert string == reconstructed_string
- text("In Assignment 1, you will go beyond this in the following ways:")
- text("- encode() currently loops over all merges. Only loop over merges that matter.")
- text("- Detect and preserve special tokens (e.g., <|endoftext|>).")
- text("- Use pre-tokenization (e.g., the GPT-2 tokenizer regex).")
- text("- Try to make the implementation as fast as possible.")
+ bilingual_text("In Assignment 1, you will go beyond this in the following ways:", '说明:In Assignment 1, you will go beyond this in the following ways:')
+ bilingual_text("- encode() currently loops over all merges. Only loop over merges that matter.", '- 说明:encode() currently loops over all merges. Only loop over merges that matter.')
+ bilingual_text("- Detect and preserve special tokens (e.g., <|endoftext|>).", '- 说明:Detect and preserve special tokens (e.g., <|endoftext|>).')
+ bilingual_text("- Use pre-tokenization (e.g., the GPT-2 tokenizer regex).", '- Use pre-分词 (e.g., the GPT-2 分词器 regex).')
+ bilingual_text("- Try to make the implementation as fast as possible.", '- 说明:Try to make the implementation as fast as possible.')
def train_bpe(string: str, num_merges: int) -> BPETokenizerParams: # @inspect string, @inspect num_merges
- text("Start with the list of bytes of `string`.")
+ bilingual_text("Start with the list of bytes of `string`.", 'Start with the list of 字节 of string.')
indices = list(map(int, string.encode("utf-8"))) # @inspect indices
merges: dict[tuple[int, int], int] = {} # index1, index2 => merged index
vocab: dict[int, bytes] = {x: bytes([x]) for x in range(256)} # index -> bytes
diff --git a/lecture_02.py b/lecture_02.py
index 38171e8..37a090e 100644
--- a/lecture_02.py
+++ b/lecture_02.py
@@ -7,35 +7,35 @@
from einops import rearrange, einsum, reduce
from edtrace import text, image, link
-from lecture_util import article_link
+from lecture_util import article_link, bilingual_text
from gpu_util import cuda_if_available, get_max_memory_usage
from facts import h100_flop_per_sec, h100_bytes_per_sec
from references import deepseek_v3_2_2025, adagrad_2011, nemotron_3_super_2026
def main():
- text("Announcements:")
- text("- Join the CS336 slack")
- text("- Sign up on Modal with your **Stanford** email")
- text("- Read the [AI policy guide](https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8/edit?tab=t.0)")
- text("- Read the [cluster guide](https://docs.google.com/document/d/1cHE0iKVyXLJ3XpIs2XuXTmZ-HMmPk2hIPeCvy-AydMg/edit?tab=t.otis27tacaef)")
+ bilingual_text("Announcements:", '通知:')
+ bilingual_text("- Join the CS336 slack", '- 加入 CS336 Slack。')
+ bilingual_text("- Sign up on Modal with your **Stanford** email", '- 使用你的 **Stanford** 邮箱注册 Modal。')
+ bilingual_text("- Read the [AI policy guide](https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8/edit?tab=t.0)", '- 阅读 [AI 使用政策指南](https://docs.google.com/document/d/1SZAlExB1qAc9izHt54gwunNpjKE6wXb8Y7yA_e-baK8/edit?tab=t.0)。')
+ bilingual_text("- Read the [cluster guide](https://docs.google.com/document/d/1cHE0iKVyXLJ3XpIs2XuXTmZ-HMmPk2hIPeCvy-AydMg/edit?tab=t.otis27tacaef)", '- 阅读 [集群使用指南](https://docs.google.com/document/d/1cHE0iKVyXLJ3XpIs2XuXTmZ-HMmPk2hIPeCvy-AydMg/edit?tab=t.otis27tacaef)。')
- text("Marin 1e23 FLOPs run finished and [matched forecasts](https://x.com/WilliamBarrHeld/status/2039373983632814318)!")
+ bilingual_text("Marin 1e23 FLOPs run finished and [matched forecasts](https://x.com/WilliamBarrHeld/status/2039373983632814318)!", 'Marin 的 1e23 FLOPs 训练运行已经完成,并且[符合预测](https://x.com/WilliamBarrHeld/status/2039373983632814318)!')
image("https://pbs.twimg.com/media/HE1P1HmaUAAjLXF?format=jpg&name=medium", width=800)
- text("Last lecture: overview, tokenization")
- text("Today: resource accounting (systems)")
+ bilingual_text("Last lecture: overview, tokenization", '上节课:概览、分词。')
+ bilingual_text("Today: resource accounting (systems)", '今天:资源核算(系统)。')
- text("Recall: what's the best model one can train given fixed resources (compute, memory)?")
- text("In other words: maximize (computational) **efficiency**.")
- text("Prerequisite: understand the resources (compute, memory) for a given computation.")
+ bilingual_text("Recall: what's the best model one can train given fixed resources (compute, memory)?", '回顾:在固定资源(计算量、内存)下,可以训练出的最佳模型是什么?')
+ bilingual_text("In other words: maximize (computational) **efficiency**.", '换句话说:最大化(计算)**效率**。')
+ bilingual_text("Prerequisite: understand the resources (compute, memory) for a given computation.", '前提:理解一次给定计算所需的资源(计算量、内存)。')
motivating_questions()
- text("What knowledge to take away from this lecture:")
- text("- Mechanics: straightforward (PyTorch semantics)")
- text("- Mindset: resource accounting (remember to do it)")
- text("- Intuitions: get a sense of how resources are spent, no ML magic today")
+ bilingual_text("What knowledge to take away from this lecture:", '这节课希望你带走的知识:')
+ bilingual_text("- Mechanics: straightforward (PyTorch semantics)", '- 机制:相对直接(PyTorch 语义)。')
+ bilingual_text("- Mindset: resource accounting (remember to do it)", '- 思维方式:资源核算(记得要做)。')
+ bilingual_text("- Intuitions: get a sense of how resources are spent, no ML magic today", '- 直觉:理解资源花在了哪里,今天没有机器学习魔法。')
# Memory accounting
tensors_basics()
@@ -59,50 +59,50 @@ def main():
gradient_accumulation()
activation_checkpointing()
- text("Summary:")
- text("- Everything is operations on tensors (parameters, gradients, activations, optimizer states, data)")
- text("- einops: better way to think about tensor operations")
- text("- 6 (# data points) (# parameters) FLOPs per training step")
- text("- Arithmetic intensity / roofline analysis: compute-bound or memory-bound?")
- text("- Matrix multiplications are compute-bound, elementwise operations are memory-bound")
- text("- Gradient accumulation, activation checkpointing: reduce memory to use bigger batch sizes")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Everything is operations on tensors (parameters, gradients, activations, optimizer states, data)", '- 一切都是对张量的操作(参数、梯度、激活值、优化器状态、数据)。')
+ bilingual_text("- einops: better way to think about tensor operations", '- einops:理解张量操作的更好方式。')
+ bilingual_text("- 6 (# data points) (# parameters) FLOPs per training step", '- 每个训练步骤约为 6 ×(数据点数)×(参数数)FLOPs。')
+ bilingual_text("- Arithmetic intensity / roofline analysis: compute-bound or memory-bound?", '- 算术强度 / 屋顶线分析:受计算限制还是受内存带宽限制?')
+ bilingual_text("- Matrix multiplications are compute-bound, elementwise operations are memory-bound", '- 矩阵乘法通常受计算限制,逐元素操作通常受内存带宽限制。')
+ bilingual_text("- Gradient accumulation, activation checkpointing: reduce memory to use bigger batch sizes", '- 梯度累积、激活检查点:减少内存占用,以使用更大的批大小。')
def motivating_questions():
- text("**Question**: How long would it take to train a 70B parameter model on 15T tokens on 1024 H100s?")
+ bilingual_text("**Question**: How long would it take to train a 70B parameter model on 15T tokens on 1024 H100s?", '**问题**:在 1024 块 H100 上,用 15T token 训练一个 70B 参数模型需要多久?')
total_flops = 6 * 70e9 * 15e12 # @inspect total_flops
h100_flop_per_sec = 1979e12 / 2
mfu = 0.5
flops_per_day = h100_flop_per_sec * mfu * 1024 * 60 * 60 * 24 # @inspect flops_per_day
days = total_flops / flops_per_day # @inspect days
- text("**Question**: What's the largest model that can you can train on 8 H100s using AdamW?")
+ bilingual_text("**Question**: What's the largest model that can you can train on 8 H100s using AdamW?", '**问题**:使用 AdamW,在 8 块 H100 上最多能训练多大的模型?')
h100_bytes = 80e9 # @inspect h100_bytes
bytes_per_parameter = 2 + 2 + (4 + 4) # parameters (2), gradients (2), optimizer state (4 + 4) @inspect bytes_per_parameter
num_parameters = (h100_bytes * 8) / bytes_per_parameter # @inspect num_parameters
- text("Caveat: activations are not accounted for (depends on batch size and sequence length), so this is an upper bound.")
+ bilingual_text("Caveat: activations are not accounted for (depends on batch size and sequence length), so this is an upper bound.", '注意:这里没有计入激活值(它取决于批大小和序列长度),因此这是一个上界。')
- text("This is a rough back-of-the-envelope calculation.")
- text("But it gives you the flavor of napkin math one can quickly do to get a sense of resources.")
+ bilingual_text("This is a rough back-of-the-envelope calculation.", '这是一个粗略的信封背面估算。')
+ bilingual_text("But it gives you the flavor of napkin math one can quickly do to get a sense of resources.", '但它展示了如何快速做纸巾估算,来获得对资源需求的直觉。')
def tensors_basics():
- text("Tensors are the basic building block for storing everything:")
- text("- data")
- text("- parameters")
- text("- gradients")
- text("- optimizer state")
- text("- activations")
-
- text("Example: parameters of the DeepSeek v3.2 model "), link(deepseek_v3_2_2025)
+ bilingual_text("Tensors are the basic building block for storing everything:", '张量是存储一切内容的基本构件:')
+ bilingual_text("- data", '- 数据。')
+ bilingual_text("- parameters", '- 参数。')
+ bilingual_text("- gradients", '- 梯度。')
+ bilingual_text("- optimizer state", '- 优化器状态。')
+ bilingual_text("- activations", '- 激活值。')
+
+ bilingual_text("Example: parameters of the DeepSeek v3.2 model ", '示例:DeepSeek v3.2 模型的参数。'), link(deepseek_v3_2_2025)
link(title="DeepSeek v3.2 model on Hugging Face", url="https://huggingface.co/deepseek-ai/DeepSeek-V3.2?show_file_info=model.safetensors.index.json")
- text("Each tensor has a rank, which is the number of dimensions.")
+ bilingual_text("Each tensor has a rank, which is the number of dimensions.", '每个张量都有一个秩,也就是维度数量。')
x = torch.zeros(4) # rank 1 tensor (vector) @inspect x
x = torch.zeros(4, 8) # rank 2 tensor (matrix) @inspect x
x = torch.zeros(4, 8, 2) # rank 3 tensor @inspect x
- text("In Transformers, will see tensors of rank 4:")
+ bilingual_text("In Transformers, will see tensors of rank 4:", '在 Transformer 中,我们会看到 4 阶张量:')
B = 32 # Batch size
S = 16 # Sequence length
H = 16 # Number of heads
@@ -111,89 +111,89 @@ def tensors_basics():
def tensors_memory():
- text("Elements of tensors are generally floating point numbers.")
+ bilingual_text("Elements of tensors are generally floating point numbers.", '张量中的元素通常是浮点数。')
- text("## fp32")
+ bilingual_text("## fp32", '## fp32(单精度浮点)')
link(title="Wikipedia", url="https://en.wikipedia.org/wiki/Single-precision_floating-point_format")
image("images/fp32.png", width=700)
- text("The fp32 data type (also known as float32 or single precision) is the default.")
- text("Traditionally, in scientific computing, fp32 is the baseline; you could use double precision (fp64) in some cases.")
- text("In deep learning, you can be a lot sloppier.")
+ bilingual_text("The fp32 data type (also known as float32 or single precision) is the default.", 'fp32 数据类型(也称为 float32 或单精度)是默认类型。')
+ bilingual_text("Traditionally, in scientific computing, fp32 is the baseline; you could use double precision (fp64) in some cases.", '传统上,在科学计算中 fp32 是基线;某些情况下也可以使用双精度(fp64)。')
+ bilingual_text("In deep learning, you can be a lot sloppier.", '在深度学习中,数值精度通常可以更“粗糙”一些。')
- text("Let's examine memory usage of these tensors.")
- text("Memory is determined by the (i) number of values and (ii) data type of each value.")
+ bilingual_text("Let's examine memory usage of these tensors.", '让我们检查这些张量的内存使用。')
+ bilingual_text("Memory is determined by the (i) number of values and (ii) data type of each value.", '内存由两件事决定:(i)数值个数;(ii)每个数值的数据类型。')
x = torch.zeros(4, 8) # @inspect x
assert x.dtype == torch.float32 # Default type
assert x.numel() == 4 * 8
assert x.element_size() == 4 # Float is 4 bytes
assert get_memory_usage(x) == 4 * 8 * 4 # 128 bytes
- text("One matrix in the feedforward layer of GPT-3:")
+ bilingual_text("One matrix in the feedforward layer of GPT-3:", 'GPT-3 前馈层中的一个矩阵:')
assert get_memory_usage(torch.empty(12288 * 4, 12288)) == 2304 * 1024 * 1024 # 2.3 GB @stepover
- text("## fp16")
+ bilingual_text("## fp16", '## fp16(半精度浮点)')
link(title="Wikipedia", url="https://en.wikipedia.org/wiki/Half-precision_floating-point_format")
image("images/fp16.png", width=400)
- text("The fp16 data type (also known as float16 or half precision) cuts down the memory.")
+ bilingual_text("The fp16 data type (also known as float16 or half precision) cuts down the memory.", 'fp16 数据类型(也称为 float16 或半精度)可以减少内存。')
x = torch.zeros(4, 8, dtype=torch.float16) # @inspect x
assert x.element_size() == 2
- text("However, the dynamic range (especially for small numbers) isn't great.")
+ bilingual_text("However, the dynamic range (especially for small numbers) isn't great.", '不过,它的动态范围(尤其对小数)并不好。')
x = torch.tensor([1e-8], dtype=torch.float16) # @inspect x
assert x == 0 # Underflow!
- text("If this happens when you train, you can get instability.")
+ bilingual_text("If this happens when you train, you can get instability.", '如果训练时发生这种情况,可能会导致不稳定。')
- text("## bf16")
+ bilingual_text("## bf16", '## bf16(脑浮点,brain floating point)')
link(title="Wikipedia", url="https://en.wikipedia.org/wiki/Bfloat16_floating-point_format")
image("images/bf16.png", width=400)
- text("Google Brain developed brain floating point (bf16) in 2018 to address this issue.")
- text("bf16 uses the same memory as fp16 but has the same dynamic range as fp32!")
- text("The only catch is that the resolution is worse, but this matters less for deep learning.")
+ bilingual_text("Google Brain developed brain floating point (bf16) in 2018 to address this issue.", 'Google Brain 在 2018 年开发了 brain floating point(bf16)来解决这个问题。')
+ bilingual_text("bf16 uses the same memory as fp16 but has the same dynamic range as fp32!", 'bf16 的内存占用与 fp16 相同,但动态范围与 fp32 相同!')
+ bilingual_text("The only catch is that the resolution is worse, but this matters less for deep learning.", '唯一的代价是精度分辨率更低,但这对深度学习影响较小。')
x = torch.tensor([1e-8], dtype=torch.bfloat16) # @inspect x
assert x != 0 # No underflow!
- text("## Mixed precision")
- text("Implications on training:")
- text("- Training with fp32 works, but requires lots of memory.")
- text("- Training with fp16 and even bf16 is risky, and you can get instability.")
+ bilingual_text("## Mixed precision", '## 混合精度')
+ bilingual_text("Implications on training:", '对训练的影响:')
+ bilingual_text("- Training with fp32 works, but requires lots of memory.", '- 使用 fp32 训练可行,但需要大量内存。')
+ bilingual_text("- Training with fp16 and even bf16 is risky, and you can get instability.", '- 使用 fp16 甚至 bf16 训练有风险,可能出现不稳定。')
- text("Solution: mixed precision training "), link("https://arxiv.org/pdf/1710.03740.pdf")
- text("- Use bf16 for parameters, activations, and gradients")
- text("- Use fp32 for optimizer states")
+ bilingual_text("Solution: mixed precision training ", '解决方案:混合精度训练。'), link("https://arxiv.org/pdf/1710.03740.pdf")
+ bilingual_text("- Use bf16 for parameters, activations, and gradients", '- 对参数、激活值和梯度使用 bf16。')
+ bilingual_text("- Use fp32 for optimizer states", '- 对优化器状态使用 fp32。')
- text("Pytorch has an automatic mixed precision (AMP) library. "), link(title="docs", url="https://pytorch.org/docs/stable/amp.html")
- text("Tries to cast things into bf16 when safe (matmuls, not exp).")
+ bilingual_text("Pytorch has an automatic mixed precision (AMP) library. ", 'PyTorch 提供了自动混合精度(AMP)库。'), link(title="docs", url="https://pytorch.org/docs/stable/amp.html")
+ bilingual_text("Tries to cast things into bf16 when safe (matmuls, not exp).", '它会在安全时把计算转换为 bf16(例如矩阵乘法,但不包括 exp)。')
with torch.amp.autocast("cuda", dtype=torch.bfloat16):
x = torch.zeros(4, 8) # @inspect x
- text("## fp8")
- text("In 2022, fp8 was standardized, motivated by machine learning workloads [primer](https://docs.nvidia.com/deeplearning/transformer-engine/user-guide/examples/fp8_primer.html).")
+ bilingual_text("## fp8", '## fp8(8 位浮点)')
+ bilingual_text("In 2022, fp8 was standardized, motivated by machine learning workloads [primer](https://docs.nvidia.com/deeplearning/transformer-engine/user-guide/examples/fp8_primer.html).", '2022 年,受机器学习工作负载推动,fp8 被标准化;可参考这篇[入门资料](https://docs.nvidia.com/deeplearning/transformer-engine/user-guide/examples/fp8_primer.html)。')
image("https://docs.nvidia.com/deeplearning/transformer-engine/user-guide/_images/fp8_formats.png", width=600)
- text("H100s support two variants of FP8: E4M3 (range [-448, 448]) and E5M2 ([-57344, 57344]).")
- text("Reference: "), link("https://arxiv.org/pdf/2209.05433.pdf")
+ bilingual_text("H100s support two variants of FP8: E4M3 (range [-448, 448]) and E5M2 ([-57344, 57344]).", 'H100 支持两种 FP8 变体:E4M3(范围 [-448, 448])和 E5M2(范围 [-57344, 57344])。')
+ bilingual_text("Reference: ", '参考资料:'), link("https://arxiv.org/pdf/2209.05433.pdf")
- text("## fp4")
- text("In 2025, NVIDIA developed [nvfp4](https://developer.nvidia.com/blog/introducing-nvfp4-for-efficient-and-accurate-low-precision-inference/)")
- text("Only 4 bits per value!")
- text("Values: -6, -4, -3, -2, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2, 3, 4, 6")
- text("Use a separate scale factor per block, so actually get more dynamic range (but just can't vary freely from neighbors).")
- text("Nemotron 3 Super was trained in NVFP4 "), link(nemotron_3_super_2026)
+ bilingual_text("## fp4", '## fp4(4 位浮点)')
+ bilingual_text("In 2025, NVIDIA developed [nvfp4](https://developer.nvidia.com/blog/introducing-nvfp4-for-efficient-and-accurate-low-precision-inference/)", '2025 年,NVIDIA 开发了 [nvfp4](https://developer.nvidia.com/blog/introducing-nvfp4-for-efficient-and-accurate-low-precision-inference/)。')
+ bilingual_text("Only 4 bits per value!", '每个数值只用 4 bit!')
+ bilingual_text("Values: -6, -4, -3, -2, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2, 3, 4, 6", '可表示的值:-6, -4, -3, -2, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2, 3, 4, 6。')
+ bilingual_text("Use a separate scale factor per block, so actually get more dynamic range (but just can't vary freely from neighbors).", '每个块使用单独的缩放因子,因此实际获得更大的动态范围(但相邻值之间不能完全自由变化)。')
+ bilingual_text("Nemotron 3 Super was trained in NVFP4 ", 'Nemotron 3 Super 使用 NVFP4 训练。'), link(nemotron_3_super_2026)
- text("Some of this is done in NVIDIA libraries outside of user control.")
+ bilingual_text("Some of this is done in NVIDIA libraries outside of user control.", '其中一些由 NVIDIA 库在用户控制之外完成。')
def tensors_on_gpus():
- text("By default, tensors are stored in CPU memory.")
+ bilingual_text("By default, tensors are stored in CPU memory.", '默认情况下,张量存储在 CPU 内存中。')
x = torch.zeros(32, 32)
assert x.device == torch.device("cpu")
- text("However, what about GPUs?")
+ bilingual_text("However, what about GPUs?", '那么 GPU 呢?')
image("images/cpu-gpu.png", width=600)
device = cuda_if_available() # @inspect device
- text("In order to take advantage of the massive parallelism of GPUs, we need to move them to GPU memory.")
+ bilingual_text("In order to take advantage of the massive parallelism of GPUs, we need to move them to GPU memory.", '为了利用 GPU 的大规模并行能力,我们需要把张量移到 GPU 内存中。')
x = x.to(device)
- text("Or create the tensor directly on the GPU:")
+ bilingual_text("Or create the tensor directly on the GPU:", '也可以直接在 GPU 上创建张量:')
with torch.device(device):
x = torch.zeros(32, 32)
assert x.device == device
@@ -202,8 +202,8 @@ def tensors_on_gpus():
def tensor_einops():
einops_motivation()
- text("Einops is a library for manipulating tensors where dimensions are named.")
- text("It is inspired by Einstein summation notation (Einstein, 1916).")
+ bilingual_text("Einops is a library for manipulating tensors where dimensions are named.", 'Einops 是一个操作张量的库,它给维度命名。')
+ bilingual_text("It is inspired by Einstein summation notation (Einstein, 1916).", '它受到爱因斯坦求和记号(Einstein, 1916)的启发。')
link(title="Einops tutorial", url="https://einops.rocks/1-einops-basics/")
einops_einsum()
@@ -212,15 +212,15 @@ def tensor_einops():
def einops_motivation():
- text("Traditional PyTorch code:")
+ bilingual_text("Traditional PyTorch code:", '传统 PyTorch 代码:')
x = torch.ones(2, 2, 3) # batch seq hidden @inspect x
y = torch.ones(2, 2, 3) # batch seq hidden @inspect y
z = x @ y.transpose(-2, -1) # batch seq seq @inspect z
- text("Easy to mess up the dimensions (what is -2, -1?)...")
+ bilingual_text("Easy to mess up the dimensions (what is -2, -1?)...", '维度很容易写错(-2、-1 到底是什么?)……')
def einops_einsum():
- text("Einsum is generalized matrix multiplication with good bookkeeping.")
+ bilingual_text("Einsum is generalized matrix multiplication with good bookkeeping.", 'Einsum 是带有良好记账方式的广义矩阵乘法。')
x = torch.ones(3, 4) # seq1 hidden @inspect x
y = torch.ones(4, 3) # hidden seq2 @inspect y
@@ -231,7 +231,7 @@ def einops_einsum():
# New (einops) way
z = einsum(x, y, "seq1 hidden, hidden seq2 -> seq1 seq2") # @inspect z
- text("Let's try a more complex example...") # @clear x y z
+ bilingual_text("Let's try a more complex example...", '让我们试一个更复杂的例子……') # @clear x y z
x = torch.ones(2, 3, 4) # batch seq1 hidden @inspect x
y = torch.ones(2, 3, 4) # batch seq2 hidden @inspect y
@@ -241,14 +241,14 @@ def einops_einsum():
# New (einops) way
z = einsum(x, y, "batch seq1 hidden, batch seq2 hidden -> batch seq1 seq2") # @inspect z
- text("Dimensions that are not named in the output are summed over.")
+ bilingual_text("Dimensions that are not named in the output are summed over.", '没有出现在输出中的维度会被求和消去。')
# Or can use `...` to represent broadcasting over any number of dimensions
z = einsum(x, y, "... seq1 hidden, ... seq2 hidden -> ... seq1 seq2") # @inspect z
def einops_reduce():
- text("You can reduce a single tensor via some operation (e.g., sum, mean, max, min).")
+ bilingual_text("You can reduce a single tensor via some operation (e.g., sum, mean, max, min).", '你可以用某种操作规约单个张量(例如 sum、mean、max、min)。')
x = torch.ones(2, 3, 4) # batch seq hidden @inspect x
# Old way
@@ -259,11 +259,11 @@ def einops_reduce():
def einops_rearrange():
- text("Sometimes, a dimension represents two dimensions")
- text("...and you want to operate on one of them.")
+ bilingual_text("Sometimes, a dimension represents two dimensions", '有时,一个维度其实代表两个维度。')
+ bilingual_text("...and you want to operate on one of them.", '……而你想只对其中一个维度进行操作。')
x = torch.ones(3, 8) # seq total_hidden @inspect x
- text("...where `total_hidden` is a flattened representation of `heads * hidden1`")
+ bilingual_text("...where `total_hidden` is a flattened representation of `heads * hidden1`", '……其中 `total_hidden` 是 `heads * hidden1` 展平后的表示。')
w = torch.ones(4, 4) # hidden1 hidden2 @inspect w
# Break up `total_hidden` into two dimensions (`heads` and `hidden1`
@@ -277,25 +277,25 @@ def einops_rearrange():
def tensor_operations_flops():
- text("Having gone through all the operations, let us examine their computational cost.")
+ bilingual_text("Having gone through all the operations, let us examine their computational cost.", '看过这些操作之后,我们来检查它们的计算成本。')
- text("A floating-point operation (FLOP) is a basic operation like addition (x + y) or multiplication (x y).")
+ bilingual_text("A floating-point operation (FLOP) is a basic operation like addition (x + y) or multiplication (x y).", '浮点运算(FLOP)是加法(x + y)或乘法(x y)这样的基本操作。')
- text("Two terribly confusing acronyms (pronounced the same!):")
- text("- FLOPs: floating-point operations (measure of computation done)")
- text("- FLOP/s: floating-point operations per second (also written as FLOPS), which is used to measure the speed of hardware.")
+ bilingual_text("Two terribly confusing acronyms (pronounced the same!):", '两个非常容易混淆、读音相同的缩写:')
+ bilingual_text("- FLOPs: floating-point operations (measure of computation done)", '- FLOPs:浮点运算次数(衡量完成了多少计算)。')
+ bilingual_text("- FLOP/s: floating-point operations per second (also written as FLOPS), which is used to measure the speed of hardware.", '- FLOP/s:每秒浮点运算次数(也写作 FLOPS),用于衡量硬件速度。')
- text("## Intuitions")
- text("Training GPT-3 (2020) took 3.14e23 FLOPs. "), article_link("https://lambdalabs.com/blog/demystifying-gpt-3")
- text("Training GPT-4 (2023) is speculated to take 2e25 FLOPs. "), article_link("https://patmcguinness.substack.com/p/gpt-4-details-revealed")
+ bilingual_text("## Intuitions", '## 直觉')
+ bilingual_text("Training GPT-3 (2020) took 3.14e23 FLOPs. ", '训练 GPT-3(2020)用了 3.14e23 FLOPs。'), article_link("https://lambdalabs.com/blog/demystifying-gpt-3")
+ bilingual_text("Training GPT-4 (2023) is speculated to take 2e25 FLOPs. ", '据推测训练 GPT-4(2023)用了 2e25 FLOPs。'), article_link("https://patmcguinness.substack.com/p/gpt-4-details-revealed")
- text("H100 has a peak performance of 1979 teraFLOP/s with sparsity, 50% without "), link(title="spec", url="https://resources.nvidia.com/en-us-tensor-core/nvidia-tensor-core-gpu-datasheet")
+ bilingual_text("H100 has a peak performance of 1979 teraFLOP/s with sparsity, 50% without ", 'H100 在利用稀疏性时峰值性能为 1979 teraFLOP/s,不利用稀疏性时约为其 50%。'), link(title="spec", url="https://resources.nvidia.com/en-us-tensor-core/nvidia-tensor-core-gpu-datasheet")
h100_flop_per_sec = 1979e12 / 2
- text("8 H100s for 2 weeks:")
+ bilingual_text("8 H100s for 2 weeks:", '8 块 H100 运行 2 周:')
total_flops = 8 * 2 * (60 * 60 * 24 * 7) * h100_flop_per_sec # @inspect total_flops
- text("## Linear model")
+ bilingual_text("## Linear model", '## 线性模型')
if torch.cuda.is_available():
B = 16384 # Number of points
D = 32768 # Dimension of each point
@@ -309,44 +309,43 @@ def tensor_operations_flops():
w = torch.randn(D, K, device=cuda_if_available())
y = x @ w
- text("How many FLOPs is this matmul?")
- text("We have one multiplication (x[i][j] * w[j][k]) and one addition per (i, j, k) triple.")
+ bilingual_text("How many FLOPs is this matmul?", '这次矩阵乘法需要多少 FLOPs?')
+ bilingual_text("We have one multiplication (x[i][j] * w[j][k]) and one addition per (i, j, k) triple.", '对每个 (i, j, k) 三元组,都有一次乘法(x[i][j] * w[j][k])和一次加法。')
actual_num_flops = 2 * B * D * K # @inspect actual_num_flops
- text("We can also time this operation to see how long it takes.")
+ bilingual_text("We can also time this operation to see how long it takes.", '我们也可以给这个操作计时,看看它需要多久。')
actual_time = benchmark(lambda: x @ w) # @inspect actual_time
- text("The actual FLOP/s of this operation:")
+ bilingual_text("The actual FLOP/s of this operation:", '这次操作实际达到的 FLOP/s:')
actual_flop_per_sec = actual_num_flops / actual_time # @inspect actual_flop_per_sec
- text("Each GPU has a specification sheet that provides the peak performance.")
- text("- Example: "), link(title="H100 spec", url="https://resources.nvidia.com/en-us-gpu-resources/h100-datasheet-24306")
- text("Note that the FLOP/s depends heavily on the data type!")
+ bilingual_text("Each GPU has a specification sheet that provides the peak performance.", '每个 GPU 都有规格表,给出其峰值性能。')
+ bilingual_text("- Example: ", '- 示例:'), link(title="H100 spec", url="https://resources.nvidia.com/en-us-gpu-resources/h100-datasheet-24306")
+ bilingual_text("Note that the FLOP/s depends heavily on the data type!", '注意,FLOP/s 很大程度取决于数据类型!')
promised_flop_per_sec = get_promised_flop_per_sec(x.dtype) # @inspect promised_flop_per_sec
- text("## Model FLOPs utilization (MFU)")
+ bilingual_text("## Model FLOPs utilization (MFU)", '## 模型 FLOPs 利用率(MFU)')
- text("Definition: MFU = (actual FLOP/s) / (promised FLOP/s) [ignore communication/overhead]")
+ bilingual_text("Definition: MFU = (actual FLOP/s) / (promised FLOP/s) [ignore communication/overhead]", '定义:MFU =(实际 FLOP/s)/(标称 FLOP/s)[忽略通信和其他开销]。')
mfu = actual_flop_per_sec / promised_flop_per_sec if promised_flop_per_sec else None # @inspect mfu
- text("Usually, MFU of ≥ 0.5 is quite good!")
-
- text("But why is MFU not closer to 1?")
- text("To answer this question, we need to look more closely at how computations are done on GPUs...")
+ bilingual_text("Usually, MFU of ≥ 0.5 is quite good!", '通常,MFU ≥ 0.5 已经相当不错!')
+ bilingual_text("But why is MFU not closer to 1?", '但为什么 MFU 不更接近 1 呢?')
+ bilingual_text("To answer this question, we need to look more closely at how computations are done on GPUs...", '要回答这个问题,我们需要更仔细地看 GPU 上的计算是如何完成的……')
def arithmetic_intensity():
image("images/compute-memory.png", width=300)
- text("How to compute a thing:")
- text("1. Send inputs from memory to accelerator")
- text("2. Perform computation")
- text("3. Send outputs from accelerator to memory")
+ bilingual_text("How to compute a thing:", '如何完成一次计算:')
+ bilingual_text("1. Send inputs from memory to accelerator", '1. 把输入从内存发送到加速器。')
+ bilingual_text("2. Perform computation", '2. 执行计算。')
+ bilingual_text("3. Send outputs from accelerator to memory", '3. 把输出从加速器发送回内存。')
- text("How long does this take?")
+ bilingual_text("How long does this take?", '这需要多长时间?')
- text("Depends on two things:")
- text("1. Accelerator speed (FLOP/s)")
- text("2. Memory bandwidth (bytes/s)")
+ bilingual_text("Depends on two things:", '取决于两件事:')
+ bilingual_text("1. Accelerator speed (FLOP/s)", '1. 加速器速度(FLOP/s)。')
+ bilingual_text("2. Memory bandwidth (bytes/s)", '2. 内存带宽(bytes/s)。')
assert h100_flop_per_sec == 1979e12 / 2 # Half without sparsity
assert h100_bytes_per_sec == 3.35e12
@@ -371,30 +370,30 @@ def arithmetic_intensity_relu():
communication_time = bytes / h100_bytes_per_sec # @inspect communication_time
computation_time = flops / h100_flop_per_sec # @inspect computation_time
- text("Assume we can overlap communication and computation perfectly.")
+ bilingual_text("Assume we can overlap communication and computation perfectly.", '假设通信和计算可以完全重叠。')
total_time = max(communication_time, computation_time) # @inspect total_time
- text("What is the bottleneck?")
- text("- Memory-bound: communication time > computation time")
- text("- Compute-bound: computation time > communication time")
+ bilingual_text("What is the bottleneck?", '瓶颈是什么?')
+ bilingual_text("- Memory-bound: communication time > computation time", '- 受内存带宽限制:通信时间 > 计算时间。')
+ bilingual_text("- Compute-bound: computation time > communication time", '- 受计算限制:计算时间 > 通信时间。')
- text("In this case, ReLU is memory-bound.")
+ bilingual_text("In this case, ReLU is memory-bound.", '在这个例子中,ReLU 受内存带宽限制。')
- text("Alternative way to see this:")
- text("Accelerator intensity: how much work can the accelerator do per byte transferred?")
+ bilingual_text("Alternative way to see this:", '另一种看法:')
+ bilingual_text("Accelerator intensity: how much work can the accelerator do per byte transferred?", '加速器强度:每传输 1 字节,加速器能完成多少工作?')
h100_accelerator_intensity = h100_flop_per_sec / h100_bytes_per_sec # @inspect h100_accelerator_intensity
- text("Arithmetic intensity: how much actual work per byte for this workload?")
+ bilingual_text("Arithmetic intensity: how much actual work per byte for this workload?", '算术强度:这个工作负载每字节实际完成多少工作?')
arithmetic_intensity = flops / bytes # ~1/4 @inspect arithmetic_intensity
- text("What is the bottleneck?")
- text("- Memory-bound: arithmetic intensity < accelerator intensity")
- text("- Compute-bound: arithmetic intensity > accelerator intensity")
+ bilingual_text("What is the bottleneck?", '瓶颈是什么?')
+ bilingual_text("- Memory-bound: arithmetic intensity < accelerator intensity", '- 受内存带宽限制:算术强度 < 加速器强度。')
+ bilingual_text("- Compute-bound: arithmetic intensity > accelerator intensity", '- 受计算限制:算术强度 > 加速器强度。')
assert arithmetic_intensity < h100_accelerator_intensity
- text("In general, we'll find ourselves memory bound.")
- text("Can we increase arithmetic intensity?")
+ bilingual_text("In general, we'll find ourselves memory bound.", '一般来说,我们会发现自己受内存带宽限制。')
+ bilingual_text("Can we increase arithmetic intensity?", '我们能提高算术强度吗?')
def arithmetic_intensity_gelu():
@@ -410,9 +409,9 @@ def arithmetic_intensity_gelu():
h100_accelerator_intensity = h100_flop_per_sec / h100_bytes_per_sec # @inspect h100_accelerator_intensity
assert arithmetic_intensity < h100_accelerator_intensity
- text("Note that GeLU does more work than ReLU per byte moved, so it has higher arithmetic intensity.")
- text("But still memory-bound!")
- text("In other words, ReLU is not faster than GeLU (when doing things in an isolated way).")
+ bilingual_text("Note that GeLU does more work than ReLU per byte moved, so it has higher arithmetic intensity.", '注意,每移动 1 字节,GeLU 比 ReLU 做更多计算,因此算术强度更高。')
+ bilingual_text("But still memory-bound!", '但它仍然受内存带宽限制!')
+ bilingual_text("In other words, ReLU is not faster than GeLU (when doing things in an isolated way).", '换句话说,在孤立执行这些操作时,ReLU 并不比 GeLU 更快。')
def arithmetic_intensity_dot_product():
@@ -428,7 +427,7 @@ def arithmetic_intensity_dot_product():
h100_accelerator_intensity = h100_flop_per_sec / h100_bytes_per_sec # @inspect h100_accelerator_intensity
assert arithmetic_intensity < h100_accelerator_intensity
- text("Memory-bound!")
+ bilingual_text("Memory-bound!", '受内存带宽限制!')
def arithmetic_intensity_matrix_vector_product():
@@ -444,7 +443,7 @@ def arithmetic_intensity_matrix_vector_product():
h100_accelerator_intensity = h100_flop_per_sec / h100_bytes_per_sec # @inspect h100_accelerator_intensity
assert arithmetic_intensity < h100_accelerator_intensity
- text("Memory-bound!")
+ bilingual_text("Memory-bound!", '受内存带宽限制!')
def arithmetic_intensity_matmul():
n = 1024
@@ -459,55 +458,55 @@ def arithmetic_intensity_matmul():
h100_accelerator_intensity = h100_flop_per_sec / h100_bytes_per_sec # @inspect h100_accelerator_intensity
assert arithmetic_intensity > h100_accelerator_intensity
- text("Finally, compute-bound!")
+ bilingual_text("Finally, compute-bound!", '终于受计算限制了!')
- text("As long as we have large matrices, we're compute-bound (saturating the accelerator).")
- text("Training Transformers involves big matrix multiplications.")
- text("Matrix-vector product is what happens during inference, which is why inference is memory-bound.")
+ bilingual_text("As long as we have large matrices, we're compute-bound (saturating the accelerator).", '只要矩阵足够大,就会受计算限制(能够打满加速器)。')
+ bilingual_text("Training Transformers involves big matrix multiplications.", '训练 Transformer 涉及大型矩阵乘法。')
+ bilingual_text("Matrix-vector product is what happens during inference, which is why inference is memory-bound.", '推理时发生的是矩阵-向量乘法,这就是推理受内存带宽限制的原因。')
- text("Note: arithmetic/accelerator intensity also depends on the precision (bf16 versus fp32).")
+ bilingual_text("Note: arithmetic/accelerator intensity also depends on the precision (bf16 versus fp32).", '注意:算术强度/加速器强度也取决于精度(bf16 与 fp32)。')
def roofline_plots():
- text("We can visualize the relationship between arithmetic intensity and performance using roofline plots.")
+ bilingual_text("We can visualize the relationship between arithmetic intensity and performance using roofline plots.", '我们可以用屋顶线图可视化算术强度和性能之间的关系。')
image("https://jax-ml.github.io/scaling-book/assets/img/roofline-improved-1400.webp", width=600)
- text("- Each slice on the x-axis is a particular computation (with some arithmetic intensity)")
- text("- Each piecewise linear function corresponds to a particular hardware")
- text("- Kink is the accelerator intensity (transition from memory-bound to compute-bound)")
+ bilingual_text("- Each slice on the x-axis is a particular computation (with some arithmetic intensity)", '- x 轴上的每一段代表某个具体计算(具有一定算术强度)。')
+ bilingual_text("- Each piecewise linear function corresponds to a particular hardware", '- 每条分段线性函数对应一种特定硬件。')
+ bilingual_text("- Kink is the accelerator intensity (transition from memory-bound to compute-bound)", '- 拐点是加速器强度(从受内存带宽限制转为受计算限制)。')
- text("We can now relate this back to MFU:")
- text("MFU = min(1, arithmetic-intensity / accelerator-intensity)")
+ bilingual_text("We can now relate this back to MFU:", '现在可以把它和 MFU 联系起来:')
+ bilingual_text("MFU = min(1, arithmetic-intensity / accelerator-intensity)", 'MFU = min(1, 算术强度 / 加速器强度)。')
link(title="reference", url="https://jax-ml.github.io/scaling-book/roofline/")
def gradients_basics():
- text("So far, we've constructed tensors and passed them through operations (forward).")
- text("Now, we're going to compute the gradient (backward).")
+ bilingual_text("So far, we've constructed tensors and passed them through operations (forward).", '到目前为止,我们构造了张量,并让它们通过操作向前传播。')
+ bilingual_text("Now, we're going to compute the gradient (backward).", '现在,我们要计算梯度(反向传播)。')
- text("As a simple example, let's consider the simple linear model:")
- text("y = 0.5 (x * w - 5)^2")
+ bilingual_text("As a simple example, let's consider the simple linear model:", '作为一个简单例子,考虑这个简单线性模型:')
+ bilingual_text("y = 0.5 (x * w - 5)^2", '公式保持不变:y = 0.5 (x * w - 5)^2。')
- text("Forward pass: compute loss")
+ bilingual_text("Forward pass: compute loss", '前向传播:计算损失。')
x = torch.tensor([1., 2, 3])
w = torch.tensor([1., 1, 1], requires_grad=True) # Want gradient
pred_y = x @ w
loss = 0.5 * (pred_y - 5).pow(2)
- text("Backward pass: compute gradients")
+ bilingual_text("Backward pass: compute gradients", '反向传播:计算梯度。')
loss.backward()
assert torch.equal(w.grad, torch.tensor([1, 2, 3])) # @inspect w.grad
def gradients_flops():
- text("Let us count the FLOPs for computing gradients.")
+ bilingual_text("Let us count the FLOPs for computing gradients.", '让我们统计计算梯度需要的 FLOPs。')
image("images/deep-network.png", width=800)
B = 1024 # Number of points
D = 256 # Dimension
- text("Define a simplified model (2-layer linear network):")
+ bilingual_text("Define a simplified model (2-layer linear network):", '定义一个简化模型(两层线性网络):')
x = torch.ones(B, D, device=cuda_if_available())
w1 = torch.randn(D, D, device=cuda_if_available(), requires_grad=True)
w2 = torch.randn(D, D, device=cuda_if_available(), requires_grad=True)
@@ -522,17 +521,17 @@ def gradients_flops():
h2.retain_grad() # For debugging
loss.backward()
- text("## Zoom in on one layer")
- text("Let's focus on the second layer (h2 = h1 @ w2)")
+ bilingual_text("## Zoom in on one layer", '## 放大观察其中一层')
+ bilingual_text("Let's focus on the second layer (h2 = h1 @ w2)", '我们关注第二层(h2 = h1 @ w2)。')
- text("**Forward pass**: Recall the number of forward FLOPs: ")
+ bilingual_text("**Forward pass**: Recall the number of forward FLOPs: ", '**前向传播**:回忆前向 FLOPs 数量:')
num_forward_flops = 2 * B * D * D # @inspect num_forward_flops
- text("**Backward pass**: How many FLOPs is running the backward pass?")
+ bilingual_text("**Backward pass**: How many FLOPs is running the backward pass?", '**反向传播**:运行反向传播需要多少 FLOPs?')
- text("We need to compute:")
- text("- h1.grad = d loss / d h1")
- text("- w2.grad = d loss / d w2")
+ bilingual_text("We need to compute:", '我们需要计算:')
+ bilingual_text("- h1.grad = d loss / d h1", '- h1.grad 表示损失对 h1 的梯度:d loss / d h1。')
+ bilingual_text("- w2.grad = d loss / d w2", '- w2.grad 表示损失对 w2 的梯度:d loss / d w2。')
h1_grad = einsum(h2.grad, w2, "batch out, in out -> batch in")
assert torch.allclose(h1.grad, h1_grad)
@@ -542,23 +541,23 @@ def gradients_flops():
num_backward_flops = (2 * B * D * D) + (2 * B * D * D) # @inspect num_backward_flops
- text("Note that the backward pass is 2x more expensive than the forward pass.")
+ bilingual_text("Note that the backward pass is 2x more expensive than the forward pass.", '注意,反向传播的计算量是前向传播的 2 倍。')
- text("## Consider all layers")
- text("This was just for w2, need to apply it to all parameters in the network.")
+ bilingual_text("## Consider all layers", '## 考虑所有层')
+ bilingual_text("This was just for w2, need to apply it to all parameters in the network.", '刚才只分析了 w2,需要把它应用到网络中的所有参数。')
- text("Putting it together:")
- text("- Forward pass: 2 (# data points) (# parameters) FLOPs")
- text("- Backward pass: 4 (# data points) (# parameters) FLOPs")
- text("- Total: 6 (# data points) (# parameters) FLOPs")
+ bilingual_text("Putting it together:", '合在一起:')
+ bilingual_text("- Forward pass: 2 (# data points) (# parameters) FLOPs", '- 前向传播:2 ×(数据点数)×(参数数)FLOPs。')
+ bilingual_text("- Backward pass: 4 (# data points) (# parameters) FLOPs", '- 反向传播:4 ×(数据点数)×(参数数)FLOPs。')
+ bilingual_text("- Total: 6 (# data points) (# parameters) FLOPs", '- 总计:6 ×(数据点数)×(参数数)FLOPs。')
- text("This is for multilayer perceptrons (MLPs)")
- text("...but it turns out to be a good approximation for Transformers for short context lengths as well.")
+ bilingual_text("This is for multilayer perceptrons (MLPs)", '这是针对多层感知机(MLP)的结论。')
+ bilingual_text("...but it turns out to be a good approximation for Transformers for short context lengths as well.", '……但事实证明,对短上下文长度的 Transformer 来说,这也是很好的近似。')
def deep_network():
image("images/deep-network.png", width=800)
- text("Consider a deep network with L layers and D-dimensional inputs, activations, and outputs.")
+ bilingual_text("Consider a deep network with L layers and D-dimensional inputs, activations, and outputs.", '考虑一个深度网络,它有 L 层,输入、激活值和输出都是 D 维。')
# Define the network
D = 8 # Dimensionality of input, activations, and output
@@ -600,19 +599,19 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
def optimizer():
- text("Recall our deep network.")
+ bilingual_text("Recall our deep network.", '回忆我们的深度网络。')
B = 2 # Batch size
D = 4 # Dimensionality of input, activations, and output
L = 3 # Number of layers
model = DeepNetwork(dim=D, num_layers=L).to(cuda_if_available()) # @stepover
- text("Let's define the AdaGrad optimizer")
- text("- momentum = SGD + exponential averaging of grad")
- text("- AdaGrad = SGD + averaging by grad^2")
- text("- RMSProp = AdaGrad but with exponential averaging of grad^2")
- text("- Adam = RMSProp + momentum")
+ bilingual_text("Let's define the AdaGrad optimizer", '让我们定义 AdaGrad 优化器。')
+ bilingual_text("- momentum = SGD + exponential averaging of grad", '- 动量 = SGD + 梯度的指数平均。')
+ bilingual_text("- AdaGrad = SGD + averaging by grad^2", '- AdaGrad = SGD + 按 grad^2 做平均。')
+ bilingual_text("- RMSProp = AdaGrad but with exponential averaging of grad^2", '- RMSProp = AdaGrad,但对 grad^2 使用指数平均。')
+ bilingual_text("- Adam = RMSProp + momentum", '- Adam = RMSProp + 动量。')
- text("AdaGrad "), link(adagrad_2011)
+ bilingual_text("AdaGrad ", '说明:AdaGrad'), link(adagrad_2011)
optimizer = AdaGrad(model.parameters(), lr=0.01) # @stepover
state = model.state_dict() # @inspect state
@@ -630,31 +629,31 @@ def optimizer():
# Free up the memory
optimizer.zero_grad(set_to_none=True)
- text("## Memory")
+ bilingual_text("## Memory", '## 内存')
num_parameters = D * D * L
parameter_memory = 2 * num_parameters # (2 bytes for bf16) @inspect parameter_memory
gradient_memory = 2 * num_parameters # (2 bytes for bf16) @inspect gradient_memory
optimizer_state_memory = 4 * num_parameters # (4 bytes for fp32) @inspect optimizer_state_memory
activation_memory = 2 * (B * D * L) # (2 bytes for bf16) @inspect activation_memory
- text("It is customary to use fp32 for stability (accumulating averages over powers over many steps).")
- text("Optimizer state memory:")
- text("- AdaGrad: 4 bytes/parameter for storing second moments")
- text("- Adam: 8 bytes/parameter for storing first and second moments")
+ bilingual_text("It is customary to use fp32 for stability (accumulating averages over powers over many steps).", '为了稳定性,通常使用 fp32(在许多步骤上累积幂的平均值)。')
+ bilingual_text("Optimizer state memory:", '优化器状态内存:')
+ bilingual_text("- AdaGrad: 4 bytes/parameter for storing second moments", '- AdaGrad:每个参数 4 字节,用于存储二阶矩。')
+ bilingual_text("- Adam: 8 bytes/parameter for storing first and second moments", '- Adam:每个参数 8 字节,用于存储一阶矩和二阶矩。')
# Putting it all together
total_memory = parameter_memory + activation_memory + gradient_memory + optimizer_state_memory # @inspect total_memory
- text("## Compute (for one training step)")
+ bilingual_text("## Compute (for one training step)", '## 计算量(一次训练步骤)')
num_parameters = D * D * L
flops = 6 * B * num_parameters # @inspect flops
- text("## Transformers")
- text("The accounting for a Transformer is more complicated, but the same idea.")
- text("Assignment 1 will ask you to do that.")
+ bilingual_text("## Transformers", '## Transformer(变换器)')
+ bilingual_text("The accounting for a Transformer is more complicated, but the same idea.", 'Transformer 的核算更复杂,但思路相同。')
+ bilingual_text("Assignment 1 will ask you to do that.", '作业 1 会要求你完成这件事。')
- text("Blog post describing memory usage for Transformer training "), article_link("https://erees.dev/transformer-memory/")
- text("Blog post describing FLOPs for a Transformer: "), article_link("https://www.adamcasson.com/posts/transformer-flops")
+ bilingual_text("Blog post describing memory usage for Transformer training ", 'Blog post describing 内存 usage for Transformer 训练'), article_link("https://erees.dev/transformer-memory/")
+ bilingual_text("Blog post describing FLOPs for a Transformer: ", '说明:Blog post describing FLOPs for a Transformer:'), article_link("https://www.adamcasson.com/posts/transformer-flops")
class AdaGrad(torch.optim.Optimizer):
@@ -716,26 +715,26 @@ def get_batch() -> tuple[torch.Tensor, torch.Tensor]:
def gradient_accumulation():
- text("Large batch sizes: improve training stability")
- text("However, activation memory scales with batch size, so might run out.")
+ bilingual_text("Large batch sizes: improve training stability", '大批大小:提升训练稳定性。')
+ bilingual_text("However, activation memory scales with batch size, so might run out.", '但是激活值内存会随批大小增长,因此可能耗尽。')
B = 64 # Batch size
D = 1024 # Dimensionality
L = 16 # Number of layers
activation_memory = 2 * B * D * L # (2 bytes for bf16) @inspect activation_memory
- text("Gradient accumulation:")
- text("- Compute gradient on micro batches")
- text("- Accumulate the gradients (don't zero it out)")
- text("- Every batch_size / micro_batch_size steps, update the parameters and zero out the gradients")
+ bilingual_text("Gradient accumulation:", '梯度累积:')
+ bilingual_text("- Compute gradient on micro batches", '- 在微批次上计算梯度。')
+ bilingual_text("- Accumulate the gradients (don't zero it out)", '- 累积梯度(不要清零)。')
+ bilingual_text("- Every batch_size / micro_batch_size steps, update the parameters and zero out the gradients", '- 每经过 batch_size / micro_batch_size 步,更新参数并清零梯度。')
micro_batch_size = B / 4
activation_memory = 2 * micro_batch_size * D * L # (2 bytes for bf16) @inspect activation_memory
def activation_checkpointing():
- text("For training, we need to store the activations of all layers")
- text("For inference, we don't compute gradients, so we only need to store the current layer's activations.")
+ bilingual_text("For training, we need to store the activations of all layers", '训练时,我们需要存储所有层的激活值。')
+ bilingual_text("For inference, we don't compute gradients, so we only need to store the current layer's activations.", '推理时不计算梯度,所以只需要存储当前层的激活值。')
image("images/deep-network.png", width=800)
- text("The memory usage is")
+ bilingual_text("The memory usage is", '内存使用量为:')
B = 64 # Batch size
D = 1024 # Dimensionality
L = 16 # Number of layers
@@ -746,13 +745,13 @@ def activation_checkpointing():
model = DeepNetwork(dim=D, num_layers=L).to(cuda_if_available()) # @stepover
memory = get_max_memory_usage(lambda: model(x).sum().backward()) # @inspect memory @stepover
- text("Can we reduce this?")
+ bilingual_text("Can we reduce this?", '我们能减少它吗?')
- text("Activation checkpointing = gradient checkpointing = rematerialization")
- text("Key idea:")
- text("- Forward pass: keep only activations at subset of layers")
- text("- Backward pass: recompute the missing activations from the last checkpoint")
- text("Philosophy: tradeoff memory for compute")
+ bilingual_text("Activation checkpointing = gradient checkpointing = rematerialization", '激活检查点 = 梯度检查点 = 重物化。')
+ bilingual_text("Key idea:", '关键思想:')
+ bilingual_text("- Forward pass: keep only activations at subset of layers", '- 前向传播:只保留一部分层的激活值。')
+ bilingual_text("- Backward pass: recompute the missing activations from the last checkpoint", '- 反向传播:从最近的检查点重新计算缺失的激活值。')
+ bilingual_text("Philosophy: tradeoff memory for compute", '理念:用更多计算换取更少内存。')
# Store all activations: x g1 h1 g2 h2 g3 h3 g4 h4
# Activation checkpointing: x h1 h2 h3 h4
@@ -761,16 +760,16 @@ def activation_checkpointing():
model = DeepNetworkCheckpointed(dim=D, num_layers=L).to(cuda_if_available()) # @stepover
checkpointed_memory = get_max_memory_usage(lambda: model(x).sum().backward()) # @inspect checkpointed_memory @stepover
- text("Can we reduce this even more, especially for deep networks (large L)?")
+ bilingual_text("Can we reduce this even more, especially for deep networks (large L)?", '我们还能进一步减少它吗,尤其是对深层网络(大的 L)?')
# Store all layers: | h1 h2 h3 h4 h5 h6 h7 h8 h9 |
# Store no layers: | |
# Store some layers: | h3 h6 h9 |
- text("How frequently to checkpoint?")
- text("- If store each layer's activations, then activation memory is O(L) and no recomputation.")
- text("- If store no activations, then activation memory is O(1) and compute is O(L^2) (recompute from the start for each layer).")
- text("- If store every sqrt(L) layers, then activation memory is O(sqrt(L)) and O(L) recomputation.")
+ bilingual_text("How frequently to checkpoint?", '应该多频繁地设置检查点?')
+ bilingual_text("- If store each layer's activations, then activation memory is O(L) and no recomputation.", '- 如果存储每层激活值,则激活内存是 O(L),不需要重算。')
+ bilingual_text("- If store no activations, then activation memory is O(1) and compute is O(L^2) (recompute from the start for each layer).", '- 如果不存储激活值,则激活内存是 O(1),计算量是 O(L^2)(每层都从头重算)。')
+ bilingual_text("- If store every sqrt(L) layers, then activation memory is O(sqrt(L)) and O(L) recomputation.", '- 如果每 sqrt(L) 层存一次,则激活内存是 O(sqrt(L)),重算量是 O(L)。')
class DeepNetworkCheckpointed(nn.Module):
diff --git a/lecture_06.py b/lecture_06.py
index eca2ddd..6fe4065 100644
--- a/lecture_06.py
+++ b/lecture_06.py
@@ -6,13 +6,13 @@
import triton
import triton.language as tl
from edtrace import text, link, image
-from lecture_util import get_local_url
+from lecture_util import get_local_url, bilingual_text, bilingual_verbatim
from gpu_util import cuda_if_available
def main():
- text("Last lecture: high-level overview of GPUs and performance")
- text("This lecture: benchmarking/profiling + writing kernels")
+ bilingual_text("Last lecture: high-level overview of GPUs and performance", '上节课:GPU 和性能的高层概览。')
+ bilingual_text("This lecture: benchmarking/profiling + writing kernels", '本节课:基准测试/性能分析,以及编写内核。')
review_of_gpus()
benchmarking_and_profiling() # Where are the bottlenecks?
@@ -25,76 +25,76 @@ def main():
triton_row_sum_example() # Reduction (row doesn't fit in block)
triton_matmul_relu_example() # Tiling: use shared memory
- text("Summary:")
- text("- Know the programming model (PyTorch, Triton, PTX) to give you correctness")
- text("- Understand the hardware (SMs, warps, occupancy, bank conflicts, etc.) to optimize performance")
- text("- Benchmark to understand scaling")
- text("- Profile to see what's being executed for how long")
- text("- Triton: think in terms of thread blocks (read to shared memory, do stuff (fusion), write back HBM)")
- text("- Examples: GeLU (elementwise), softmax (row-wise), row sum (baby tiling), matmul (tiling)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Know the programming model (PyTorch, Triton, PTX) to give you correctness", '- 了解编程模型(PyTorch、Triton、PTX),以保证正确性。')
+ bilingual_text("- Understand the hardware (SMs, warps, occupancy, bank conflicts, etc.) to optimize performance", '- 理解硬件(SM、warp、占用率、bank 冲突等),以优化性能。')
+ bilingual_text("- Benchmark to understand scaling", '- 通过基准测试理解扩展行为。')
+ bilingual_text("- Profile to see what's being executed for how long", '- 通过性能分析查看执行了什么以及耗时多久。')
+ bilingual_text("- Triton: think in terms of thread blocks (read to shared memory, do stuff (fusion), write back HBM)", '- Triton:以线程块为单位思考(读入共享内存、执行操作/融合、写回 HBM)。')
+ bilingual_text("- Examples: GeLU (elementwise), softmax (row-wise), row sum (baby tiling), matmul (tiling)", '- 示例:GeLU(逐元素)、softmax(逐行)、行求和(入门分块)、矩阵乘法(分块)。')
- text("Next time: more than one GPU!")
+ bilingual_text("Next time: more than one GPU!", '下次:不止一块 GPU!')
def review_of_gpus():
- text("## Hardware")
+ bilingual_text("## Hardware", '## 硬件')
image("images/gpu-hardware.png", width=800)
- text("| Accelerator | A100 | H100 | B200 |", verbatim=True)
- text("+------------------------------------+-----------+-----------+-----------+", verbatim=True)
- text("| # SMs | 108 | 132 | 148 |", verbatim=True)
- text("+------------------------------------+-----------+-----------+-----------+", verbatim=True)
- text("| Register size (per SM) | 256 KB | 256 KB | 256 KB |", verbatim=True)
- text("| L1 cache + shared memory (per SM) | 192 KB | 256 KB | 256 KB |", verbatim=True)
- text("| L2 cache size | 40 MB | 50 MB | 96-126 MB |", verbatim=True)
- text("| HBM size | 80 GB | 80 GB | 192 GB |", verbatim=True)
- text("+------------------------------------+-----------+-----------+-----------+", verbatim=True)
- text("| Register bandwidth | ~116 TB/s | ~401 TB/s | ~447 TB/s |", verbatim=True)
- text("| L1 cache + shared memory bandwidth | ~19 TB/s | ~33 TB/s | ~19 TB/s |", verbatim=True)
- text("| L2 cache bandwidth | ~5-8 TB/s | ~12 TB/s | ~9 TB/s |", verbatim=True)
- text("| HBM bandwidth | 2 TB/s | 3.35 TB/s | 8 TB/s |", verbatim=True)
-
- text("(B200s also have tensor memory (TMEM) for tensor cores (between registers and shared memory) that are invisible to programmer.)")
-
- text("## Programming model")
+ bilingual_verbatim("| Accelerator | A100 | H100 | B200 |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("+------------------------------------+-----------+-----------+-----------+", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| # SMs | 108 | 132 | 148 |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("+------------------------------------+-----------+-----------+-----------+", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| Register size (per SM) | 256 KB | 256 KB | 256 KB |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| L1 cache + shared memory (per SM) | 192 KB | 256 KB | 256 KB |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| L2 cache size | 40 MB | 50 MB | 96-126 MB |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| HBM size | 80 GB | 80 GB | 192 GB |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("+------------------------------------+-----------+-----------+-----------+", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| Register bandwidth | ~116 TB/s | ~401 TB/s | ~447 TB/s |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| L1 cache + shared memory bandwidth | ~19 TB/s | ~33 TB/s | ~19 TB/s |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| L2 cache bandwidth | ~5-8 TB/s | ~12 TB/s | ~9 TB/s |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| HBM bandwidth | 2 TB/s | 3.35 TB/s | 8 TB/s |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+
+ bilingual_text("(B200s also have tensor memory (TMEM) for tensor cores (between registers and shared memory) that are invisible to programmer.)", 'B200 还为张量核心提供张量内存(TMEM),位于寄存器和共享内存之间,但对程序员不可见。')
+
+ bilingual_text("## Programming model", '## 编程模型')
image("https://docs.nvidia.com/cuda/parallel-thread-execution/_images/grid-with-CTAs.png", width=600)
- text("- *Thread*: executes code on a small part of the data")
- text("- *Thread block* or concurrent thread array (CTA): a group of threads")
- text("- *Grid*: collection of thread blocks")
-
- text("(H100s and B200s also have thread block clusters that enable distributed shared memory.)")
-
- text("Why thread blocks?")
- text("For elementwise operations (e.g., GeLU), threads are most natural: each thread processes one element.")
- text("- f(i) for i = 0, ..., N-1")
- text("However, for non-elementwise operations like softmax or matrix multiplication, threads need to communicate.")
- text("Reading/writing from HBM is slow, so use shared memory (local to SM).")
- text("Thread block: a collection of threads that access the same shared memory.")
- text("Consequently, a thread block is scheduled on one SM.")
- text("In Triton, think natively in terms of thread blocks (later).")
-
- text("## Interaction between programming model and hardware")
- text("Programming model provides an abstraction of the hardware.")
- text("In principle, don't need to think about anything else (for correctness).")
- text("In practice, performance is very sensitive to the hardware, so need to understand it to obtain high performance.")
-
- text("Let's go over some considerations.")
-
- text("**Warps**:")
- text("- Within a thread block, threads are grouped into warps (32 threads per warp).")
- text("- Example: thread block has 64 threads => it has 2 warps.")
- text("| TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT | TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT |", verbatim=True)
- text("- All threads within a warp must execute same instructions in lockstep on an SM.")
- text("- Control divergence: if different threads in a warp need to execute different instructions (if A, else B), must be done sequentially (bad)")
- text("| AAAAAAAAA....................... |", verbatim=True)
- text("| .........BBBBBBBBBBBBBBBBBBBBBBB |", verbatim=True)
- text("- SM runs multiple warps and switches between them (e.g., when one warp is blocked on HBM reads/writes) with zero cost.")
-
- text("**(Warp) occupancy**:")
- text("- Each thread can use between 0 and 255 registers.")
- text("- The more registers threads use, the fewer threads can be scheduled on an SM (low occupancy).")
- text("- Low occupancy isn't necessarily bad if each thread is doing more work.")
- text("- Example: thread coarsening (each thread processes multiple elements).")
- text("- Example: thread block has 64 threads, each using 160 registers, SM has 65536 registers")
+ bilingual_text("- *Thread*: executes code on a small part of the data", '- *线程*:在数据的一小部分上执行代码。')
+ bilingual_text("- *Thread block* or concurrent thread array (CTA): a group of threads", '- *线程块*或并发线程数组(CTA):一组线程。')
+ bilingual_text("- *Grid*: collection of thread blocks", '- *网格*:线程块的集合。')
+
+ bilingual_text("(H100s and B200s also have thread block clusters that enable distributed shared memory.)", 'H100 和 B200 还具有线程块集群,可以启用分布式共享内存。')
+
+ bilingual_text("Why thread blocks?", '为什么需要线程块?')
+ bilingual_text("For elementwise operations (e.g., GeLU), threads are most natural: each thread processes one element.", '对于逐元素操作(例如 GeLU),线程是最自然的抽象:每个线程处理一个元素。')
+ bilingual_text("- f(i) for i = 0, ..., N-1", '- f(i),其中 i = 0, ..., N-1。')
+ bilingual_text("However, for non-elementwise operations like softmax or matrix multiplication, threads need to communicate.", '但是,对于 softmax 或矩阵乘法这样的非逐元素操作,线程之间需要通信。')
+ bilingual_text("Reading/writing from HBM is slow, so use shared memory (local to SM).", '从 HBM 读写很慢,因此要使用共享内存(位于 SM 本地)。')
+ bilingual_text("Thread block: a collection of threads that access the same shared memory.", '线程块:一组访问同一块共享内存的线程。')
+ bilingual_text("Consequently, a thread block is scheduled on one SM.", '因此,一个线程块会被调度到一个 SM 上。')
+ bilingual_text("In Triton, think natively in terms of thread blocks (later).", '在 Triton 中,要自然地按线程块来思考(后面会看到)。')
+
+ bilingual_text("## Interaction between programming model and hardware", '## 编程模型与硬件的相互作用')
+ bilingual_text("Programming model provides an abstraction of the hardware.", '编程模型提供了对硬件的抽象。')
+ bilingual_text("In principle, don't need to think about anything else (for correctness).", '原则上,为了正确性你不需要考虑其他细节。')
+ bilingual_text("In practice, performance is very sensitive to the hardware, so need to understand it to obtain high performance.", '实践中,性能对硬件非常敏感,因此要获得高性能就必须理解硬件。')
+
+ bilingual_text("Let's go over some considerations.", '让我们看一些需要考虑的因素。')
+
+ bilingual_text("**Warps**:", '**Warp(线程束)**:')
+ bilingual_text("- Within a thread block, threads are grouped into warps (32 threads per warp).", '- 在线程块内部,线程会被分组成 warp(每个 warp 32 个线程)。')
+ bilingual_text("- Example: thread block has 64 threads => it has 2 warps.", '- 示例:一个线程块有 64 个线程 => 它有 2 个 warp。')
+ bilingual_verbatim("| TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT | TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_text("- All threads within a warp must execute same instructions in lockstep on an SM.", '- 同一个 warp 内的所有线程必须在 SM 上锁步执行相同指令。')
+ bilingual_text("- Control divergence: if different threads in a warp need to execute different instructions (if A, else B), must be done sequentially (bad)", '- 控制流分歧:如果同一 warp 中不同线程需要执行不同指令(if A, else B),就必须顺序执行(不好)。')
+ bilingual_verbatim("| AAAAAAAAA....................... |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| .........BBBBBBBBBBBBBBBBBBBBBBB |", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_text("- SM runs multiple warps and switches between them (e.g., when one warp is blocked on HBM reads/writes) with zero cost.", '- SM 会运行多个 warp,并在它们之间零成本切换(例如某个 warp 因 HBM 读写而阻塞时)。')
+
+ bilingual_text("**(Warp) occupancy**:", '**(Warp)占用率**:')
+ bilingual_text("- Each thread can use between 0 and 255 registers.", '- 每个线程可以使用 0 到 255 个寄存器。')
+ bilingual_text("- The more registers threads use, the fewer threads can be scheduled on an SM (low occupancy).", '- 每个线程使用的寄存器越多,一个 SM 上可调度的线程越少(占用率低)。')
+ bilingual_text("- Low occupancy isn't necessarily bad if each thread is doing more work.", '- 如果每个线程做了更多工作,低占用率不一定是坏事。')
+ bilingual_text("- Example: thread coarsening (each thread processes multiple elements).", '- 示例:线程粗化(每个线程处理多个元素)。')
+ bilingual_text("- Example: thread block has 64 threads, each using 160 registers, SM has 65536 registers", '- 示例:线程块有 64 个线程,每个线程使用 160 个寄存器,SM 有 65536 个寄存器。')
# What we want to run
num_threads_per_block = 128
@@ -111,58 +111,58 @@ def review_of_gpus():
num_warps = num_blocks * num_threads_per_block / 32 # @inspect num_warps
occupancy = num_warps / max_warps # @inspect occupancy
- text("**Bank conflicts** (shared memory):")
- text("- Shared memory is divided into 32 banks, each 4 bytes wide.")
- text("B00 B01 B02 B03 B04 B05 B06 B07 B08 B09 B10 B11 B12 B13 B14 B15 B16 B17 B18 B19 B20 B21 B22 B23 B24 B25 B26 B27 B28 B29 B30 B31", verbatim=True)
- text("... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...", verbatim=True)
- text("... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...", verbatim=True)
- text("... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...", verbatim=True)
- text("- Each cycle, each bank can only be accessed by one thread (if not the same exact location).")
- text("- If multiple threads access the same bank, accesses serialized (bank conflict).")
- text("- Worst case example: matrix where each row spans all banks; 32 threads accessing first column results in 32-way bank conflict!")
- text("- Unavoidable: when doing matmul A @ B, access rows of A and columns of B")
- text("- Solution: swizzling rearranges shared memory (e.g., row xor col) to avoid bank conflicts")
-
- text("**Memory coalescing** (HBM):")
- text("- When the 32 threads in a warp access HBM, memory accesses combined into transactions of 128 bytes (cache lines).")
- text("M00 M01 M02 M03 M04 M05 M06 M07 M08 M09 M10 M11 M12 M13 M14 M15 M16 M17 M18 M19 M20 M21 M22 M23 M24 M25 M26 M27 M28 M29 M30 M31", verbatim=True)
- text("M32 M33 M34 M35 M36 M37 M38 M39 M40 M41 M42 M43 M44 M45 M46 M47 M48 M49 M50 M51 M52 M53 M54 M55 M56 M57 M58 M59 M60 M61 M62 M63", verbatim=True)
- text("- Best case: full coalescing, all threads access the same cache line (32 threads x 4 bytes = 128 bytes).")
-
- text("**Block occupancy**:")
+ bilingual_text("**Bank conflicts** (shared memory):", '**Bank 冲突**(共享内存):')
+ bilingual_text("- Shared memory is divided into 32 banks, each 4 bytes wide.", '- 共享内存被划分为 32 个 bank,每个 bank 宽 4 字节。')
+ bilingual_verbatim("B00 B01 B02 B03 B04 B05 B06 B07 B08 B09 B10 B11 B12 B13 B14 B15 B16 B17 B18 B19 B20 B21 B22 B23 B24 B25 B26 B27 B28 B29 B30 B31", '上方等宽内容是访问模式示意;字符布局保持原样。', verbatim=True)
+ bilingual_verbatim("... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...", '上方等宽内容是访问模式示意;字符布局保持原样。', verbatim=True)
+ bilingual_verbatim("... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...", '上方等宽内容是访问模式示意;字符布局保持原样。', verbatim=True)
+ bilingual_verbatim("... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...", '上方等宽内容是访问模式示意;字符布局保持原样。', verbatim=True)
+ bilingual_text("- Each cycle, each bank can only be accessed by one thread (if not the same exact location).", '- 每个周期中,每个 bank 只能被一个线程访问(除非访问的是完全相同的位置)。')
+ bilingual_text("- If multiple threads access the same bank, accesses serialized (bank conflict).", '- 如果多个线程访问同一个 bank,访问会串行化(bank 冲突)。')
+ bilingual_text("- Worst case example: matrix where each row spans all banks; 32 threads accessing first column results in 32-way bank conflict!", '- 最坏示例:矩阵每一行跨越所有 bank;32 个线程访问第一列会导致 32 路 bank 冲突!')
+ bilingual_text("- Unavoidable: when doing matmul A @ B, access rows of A and columns of B", '- 难以避免:做矩阵乘法 A @ B 时,会访问 A 的行和 B 的列。')
+ bilingual_text("- Solution: swizzling rearranges shared memory (e.g., row xor col) to avoid bank conflicts", '- 解决方案:swizzling 重新排列共享内存(例如 row xor col)以避免 bank 冲突。')
+
+ bilingual_text("**Memory coalescing** (HBM):", '**内存合并访问**(HBM):')
+ bilingual_text("- When the 32 threads in a warp access HBM, memory accesses combined into transactions of 128 bytes (cache lines).", '- 当一个 warp 中的 32 个线程访问 HBM 时,内存访问会合并为 128 字节的事务(缓存行)。')
+ bilingual_verbatim("M00 M01 M02 M03 M04 M05 M06 M07 M08 M09 M10 M11 M12 M13 M14 M15 M16 M17 M18 M19 M20 M21 M22 M23 M24 M25 M26 M27 M28 M29 M30 M31", '上方等宽内容是访问模式示意;字符布局保持原样。', verbatim=True)
+ bilingual_verbatim("M32 M33 M34 M35 M36 M37 M38 M39 M40 M41 M42 M43 M44 M45 M46 M47 M48 M49 M50 M51 M52 M53 M54 M55 M56 M57 M58 M59 M60 M61 M62 M63", '上方等宽内容是访问模式示意;字符布局保持原样。', verbatim=True)
+ bilingual_text("- Best case: full coalescing, all threads access the same cache line (32 threads x 4 bytes = 128 bytes).", '- 最好情况:完全合并访问,所有线程访问同一条缓存行(32 个线程 x 4 字节 = 128 字节)。')
+
+ bilingual_text("**Block occupancy**:", '**块占用率**:')
image("https://developer-blogs.nvidia.com/wp-content/uploads/2019/06/pasted-image-0.png", width=400)
- text("- Thread blocks scheduled onto SMs in waves.")
- text("- B200 has 148 SMs, if we launch 160 thread blocks, first wave has 148 blocks, second wave has 12 blocks.")
- text("- Wave quantization problem: last wave has fewer thread blocks, leaving some SMs idle (low block occupancy).")
- text("- Solution: make number of thread blocks divide # SMs.")
+ bilingual_text("- Thread blocks scheduled onto SMs in waves.", '- 线程块会以一波一波的方式调度到 SM 上。')
+ bilingual_text("- B200 has 148 SMs, if we launch 160 thread blocks, first wave has 148 blocks, second wave has 12 blocks.", '- B200 有 148 个 SM;如果启动 160 个线程块,第一波有 148 个块,第二波只有 12 个块。')
+ bilingual_text("- Wave quantization problem: last wave has fewer thread blocks, leaving some SMs idle (low block occupancy).", '- 波量化问题:最后一波线程块较少,会让一些 SM 空闲(块占用率低)。')
+ bilingual_text("- Solution: make number of thread blocks divide # SMs.", '- 解决方案:让线程块数量能整除 SM 数量。')
- text("Summary:")
- text("- Programming model: grid (HBM) -> thread block (shared memory) -> thread (registers)")
- text("- Details of hardware (warps, bank conflicts, memory coalescing, occupancy) determine performance")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Programming model: grid (HBM) -> thread block (shared memory) -> thread (registers)", '- 编程模型: grid (HBM) -> 线程块 (共享内存) -> 线程 (寄存器)')
+ bilingual_text("- Details of hardware (warps, bank conflicts, memory coalescing, occupancy) determine performance", '- Details of 硬件 (warp, bank 冲突, 内存合并访问, 占用率) determine 性能')
def benchmarking_and_profiling():
- text("Recipe for success:")
- text("1. Benchmark and profile your code")
- text("2. Make changes")
- text("3. Benchmark and profile your code again")
+ bilingual_text("Recipe for success:", '成功配方:')
+ bilingual_text("1. Benchmark and profile your code", '1. 对代码做基准测试和性能分析。')
+ bilingual_text("2. Make changes", '2. 做出修改。')
+ bilingual_text("3. Benchmark and profile your code again", '3. 再次做基准测试和性能分析。')
benchmarking() # How long does it take?
profiling() # Where time is being spent?
- text("Benchmark and profile your code!")
+ bilingual_text("Benchmark and profile your code!", '对你的代码做基准测试和性能分析!')
def benchmarking():
- text("Benchmarking measures the wall-clock time of performing some operation.")
- text("It only gives you end-to-end time, not where time is spent (profiling).")
+ bilingual_text("Benchmarking measures the wall-clock time of performing some operation.", '基准测试衡量执行某个操作的墙钟时间。')
+ bilingual_text("It only gives you end-to-end time, not where time is spent (profiling).", '它只给出端到端时间,不告诉你时间花在哪里(这需要性能分析)。')
- text("It is still useful for:")
- text("- comparing different implementations (which is faster?), and")
- text("- understanding how performance scales (e.g., with dimension).")
+ bilingual_text("It is still useful for:", '它仍然有用,因为可以:')
+ bilingual_text("- comparing different implementations (which is faster?), and", '- 比较不同实现(哪个更快?),以及')
+ bilingual_text("- understanding how performance scales (e.g., with dimension).", '- 理解性能如何随规模变化(例如随维度变化)。')
- text("You can use [`torch.utils.benchmark`](https://pytorch.org/tutorials/recipes/recipes/benchmark.html).")
- text("We will roll our own to make benchmarking more transparent.")
+ bilingual_text("You can use [`torch.utils.benchmark`](https://pytorch.org/tutorials/recipes/recipes/benchmark.html).", 'You can use [torch.utils.基准测试](https://pytorch.org/tutorials/recipes/recipes/基准测试.html).')
+ bilingual_text("We will roll our own to make benchmarking more transparent.", '我们会自己实现一个基准测试工具,让过程更透明。')
# Benchmark matrix multiplication
matmul = run_operation2(dim=1024, operation=lambda a, b: a @ b)
@@ -173,7 +173,7 @@ def benchmarking():
for dim in [256, 512, 1024, 2048, 4096, 8192]:
results[dim] = benchmark(run_operation2(dim=dim, operation=lambda a, b: a @ b)) # @inspect results @stepover
- text("Note: time is roughly constant when dimension is small, then cubic scaling.")
+ bilingual_text("Note: time is roughly constant when dimension is small, then cubic scaling.", '注意:维度较小时耗时大致恒定,之后呈三次方扩展。')
def benchmark(run: Callable, num_warmups: int = 1, num_trials: int = 3) -> float:
@@ -204,34 +204,34 @@ def benchmark(run: Callable, num_warmups: int = 1, num_trials: int = 3) -> float
def profiling():
- text("While benchmarking looks at end-to-end time, profiling looks at where time is spent.")
- text("Independent of time, profiling also helps you understand what's going under the hood.")
+ bilingual_text("While benchmarking looks at end-to-end time, profiling looks at where time is spent.", '基准测试看端到端时间,而性能分析看时间花在了哪里。')
+ bilingual_text("Independent of time, profiling also helps you understand what's going under the hood.", '除了时间以外,性能分析还能帮助你理解底层实际发生了什么。')
- text("PyTorch has a built-in [profiler](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html).")
- text("In your assignment, you will use nsight to get more details.")
+ bilingual_text("PyTorch has a built-in [profiler](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html).", 'PyTorch 内置了 [profiler](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html)。')
+ bilingual_text("In your assignment, you will use nsight to get more details.", '在作业中,你会使用 nsight 获得更多细节。')
- text("## add(dim=2048)")
+ bilingual_text("## add(dim=2048)", '## 说明:add(dim=2048)')
add_profile = profile(run_operation2(dim=2048, operation=lambda a, b: a + b))
text(add_profile, verbatim=True)
- text("## matmul(dim=2048)")
+ bilingual_text("## matmul(dim=2048)", '## 矩阵乘法(dim=2048)')
matmul_profile = profile(run_operation2(dim=2048, operation=lambda a, b: a @ b)) # @stepover
text(matmul_profile, verbatim=True)
- text("## matmul(dim=128)")
+ bilingual_text("## matmul(dim=128)", '## 矩阵乘法(dim=128)')
matmul_profile = profile(run_operation2(dim=128, operation=lambda a, b: a @ b)) # @stepover
text(matmul_profile, verbatim=True)
- text("Observations:")
- text("- You can see which CUDA kernels are actually being called (the long names).")
- text("- Different CUDA kernels are invoked depending on the tensor dimensions.")
+ bilingual_text("Observations:", '观察:')
+ bilingual_text("- You can see which CUDA kernels are actually being called (the long names).", '- 你可以看到实际调用了哪些 CUDA 内核(那些很长的名字)。')
+ bilingual_text("- Different CUDA kernels are invoked depending on the tensor dimensions.", '- 根据张量维度不同,会调用不同的 CUDA 内核。')
- text("Name of CUDA kernel tells us something about the implementation.")
- text("Example: cutlass3x_sm100_simt_sgemm_f32_f32_f32_f32_f32_64x64x16_1x1x1_3_nnn_align1_bi...")
- text("- cutlass: NVIDIA's CUDA library for linear algebra")
- text("- sm100: corresponds to the NVIDIA Blackwell architecture (B200)")
- text("- f32: float32")
- text("- 64x64x16: tile shape (more on this later)")
+ bilingual_text("Name of CUDA kernel tells us something about the implementation.", 'CUDA 内核名称会透露一些实现信息。')
+ bilingual_text("Example: cutlass3x_sm100_simt_sgemm_f32_f32_f32_f32_f32_64x64x16_1x1x1_3_nnn_align1_bi...", '示例:cutlass3x_sm100_simt_sgemm_f32_f32_f32_f32_f32_64x64x16_1x1x1_3_nnn_align1_bi...')
+ bilingual_text("- cutlass: NVIDIA's CUDA library for linear algebra", '- cutlass:NVIDIA 用于线性代数的 CUDA 库。')
+ bilingual_text("- sm100: corresponds to the NVIDIA Blackwell architecture (B200)", '- sm100:对应 NVIDIA Blackwell 架构(B200)。')
+ bilingual_text("- f32: float32", '- f32:float32,即 32 位浮点数。')
+ bilingual_text("- 64x64x16: tile shape (more on this later)", '- 64x64x16:分块形状(后面会进一步说明)。')
def profile(run: Callable, num_warmups: int = 1):
@@ -260,7 +260,7 @@ def profile(run: Callable, num_warmups: int = 1):
def naive_vs_builtin_vs_compiled_gelu():
- text("Let's benchmark and profile the [GeLU activation function](https://pytorch.org/docs/stable/generated/torch.nn.GELU.html).")
+ bilingual_text("Let's benchmark and profile the [GeLU activation function](https://pytorch.org/docs/stable/generated/torch.nn.GELU.html).", "Let's 基准测试 and 性能分析 the [GeLU activation function](https://pytorch.org/docs/stable/generated/torch.nn.GELU.html).")
x = torch.tensor([1.]) # @inspect x
@@ -280,58 +280,58 @@ def naive_vs_builtin_vs_compiled_gelu():
naive_time = benchmark(run_operation1(dim=16384, operation=naive_gelu)) # @inspect naive_time @stepover
builtin_time = benchmark(run_operation1(dim=16384, operation=builtin_gelu)) # @inspect builtin_time @stepover
compiled_time = benchmark(run_operation1(dim=16384, operation=compiled_gelu)) # @inspect compiled_time @stepover
- text("The builtin and compiled versions are significantly faster!")
+ bilingual_text("The builtin and compiled versions are significantly faster!", '内置版本和编译版本明显更快!')
- text("To understand why, let's look at the profiler to see where time is being spent.")
+ bilingual_text("To understand why, let's look at the profiler to see where time is being spent.", '为了理解原因,我们查看 profiler,看时间花在哪里。')
- text("## naive_gelu")
+ bilingual_text("## naive_gelu", '## 说明:naive_gelu')
naive_gelu_profile = profile(run_operation1(dim=16384, operation=naive_gelu)) # @stepover
text(naive_gelu_profile, verbatim=True)
- text("## builtin_gelu")
+ bilingual_text("## builtin_gelu", '## 说明:builtin_gelu')
builtin_gelu_profile = profile(run_operation1(dim=16384, operation=builtin_gelu)) # @stepover
text(builtin_gelu_profile, verbatim=True)
- text("## compiled_gelu")
+ bilingual_text("## compiled_gelu", '## 说明:compiled_gelu')
compiled_gelu_profile = profile(run_operation1(dim=16384, operation=compiled_gelu)) # @stepover
text(compiled_gelu_profile, verbatim=True)
- text("Notes:")
- text("- Naive implementation: multiple kernels, requires many reads/writes from/to HBM (**no fusion**).")
- text("- Builtin and compiled versions: one kernel (**kernel fusion**), one read from HBM, one write to HBM.")
- text("- The compiled kernel is a Triton kernel.")
+ bilingual_text("Notes:", '说明:')
+ bilingual_text("- Naive implementation: multiple kernels, requires many reads/writes from/to HBM (**no fusion**).", '- 朴素实现:多个内核,需要多次从 HBM 读取/写入 HBM(**没有融合**)。')
+ bilingual_text("- Builtin and compiled versions: one kernel (**kernel fusion**), one read from HBM, one write to HBM.", '- 内置和编译版本:一个内核(**内核融合**),一次从 HBM 读取,一次写回 HBM。')
+ bilingual_text("- The compiled kernel is a Triton kernel.", '- 编译后的内核是 Triton 内核。')
def triton_introduction():
image("https://docs.nvidia.com/cuda/parallel-thread-execution/_images/grid-with-CTAs.png", width=600)
- text("In CUDA (developed by NVIDIA), specify what each thread does.")
- text("- Pros: fine-grained control")
- text("- Cons: need to manage more things (e.g., shared memory)")
+ bilingual_text("In CUDA (developed by NVIDIA), specify what each thread does.", '在 CUDA(由 NVIDIA 开发)中,需要指定每个线程做什么。')
+ bilingual_text("- Pros: fine-grained control", '- 优点:细粒度控制。')
+ bilingual_text("- Cons: need to manage more things (e.g., shared memory)", '- 缺点:需要管理更多东西(例如共享内存)。')
- text("In Triton (developed by OpenAI), specify what each thread block does.")
- text("- Generally powerful enough (especially when getting started)")
- text("- Conceptual framework: load data into shared memory, operate on it, write back to global memory")
+ bilingual_text("In Triton (developed by OpenAI), specify what each thread block does.", '在 Triton(由 OpenAI 开发)中,需要指定每个线程块做什么。')
+ bilingual_text("- Generally powerful enough (especially when getting started)", '- 通常已经足够强大(尤其是入门时)。')
+ bilingual_text("- Conceptual framework: load data into shared memory, operate on it, write back to global memory", '- 概念框架:把数据加载到共享内存,对其操作,再写回全局内存。')
def triton_gelu_example():
- text("Let's write the Triton kernel for GeLU.")
+ bilingual_text("Let's write the Triton kernel for GeLU.", '让我们为 GeLU 编写 Triton 内核。')
x = torch.randn(8192, device=cuda_if_available())
y = triton_gelu(x)
check_equal_1d(triton_gelu, naive_gelu) # Check for correctness @stepover
- text("Triton compiles down to PTX (parallel thread execution), an assembly language for GPUs.")
+ bilingual_text("Triton compiles down to PTX (parallel thread execution), an assembly language for GPUs.", 'Triton 会编译成 PTX(parallel thread execution),这是 GPU 的汇编语言。')
- text("We can see the PTX code generated by Triton.")
+ bilingual_text("We can see the PTX code generated by Triton.", '我们可以看到 Triton 生成的 PTX 代码。')
link(get_local_url("var/triton_gelu-ptx.txt"))
- text("Observations:")
- text("- ld.global.* and st.global.* reads and writes from global memory")
- text("- %ctaid.x is block index, %tid.x is thread index")
- text("- %f* are floating point registers, %r* are integer registers")
- text("- One thread processes 8 elements at the same time (thread coarsening)")
+ bilingual_text("Observations:", '观察:')
+ bilingual_text("- ld.global.* and st.global.* reads and writes from global memory", '- ld.global.* 和 st.global.* 表示从全局内存读写。')
+ bilingual_text("- %ctaid.x is block index, %tid.x is thread index", '- %ctaid.x 是块索引,%tid.x 是线程索引。')
+ bilingual_text("- %f* are floating point registers, %r* are integer registers", '- %f* 是浮点寄存器,%r* 是整数寄存器。')
+ bilingual_text("- One thread processes 8 elements at the same time (thread coarsening)", '- 一个线程同时处理 8 个元素(线程粗化)。')
def triton_gelu(x: torch.Tensor):
@@ -390,24 +390,24 @@ def triton_gelu_kernel(x_ptr, y_ptr, num_elements, BLOCK_SIZE: tl.constexpr):
def triton_softmax_example():
- text("So far, we've looked at elementwise operations in Triton (e.g., GeLU).")
- text("Now let us look at operations that aggregate over multiple values.")
+ bilingual_text("So far, we've looked at elementwise operations in Triton (e.g., GeLU).", '到目前为止,我们看了 Triton 中的逐元素操作(例如 GeLU)。')
+ bilingual_text("Now let us look at operations that aggregate over multiple values.", '现在来看会聚合多个值的操作。')
- text("We will roughly follow the Triton fused softmax tutorial: "), link("https://triton-lang.org/main/getting-started/tutorials/02-fused-softmax.html")
+ bilingual_text("We will roughly follow the Triton fused softmax tutorial: ", '说明:We will roughly follow the Triton fused softmax tutorial:'), link("https://triton-lang.org/main/getting-started/tutorials/02-fused-softmax.html")
- text("Recall the softmax operation is used in attention and generating probabilities.")
- text("Exponentiate and normalize each row of a matrix:")
- text("[0 0 0] => [1/3 1/3 1/3]", verbatim=True)
- text("[1 1 -inf] [1/2 1/2 0 ]", verbatim=True)
+ bilingual_text("Recall the softmax operation is used in attention and generating probabilities.", '回忆一下,softmax 用于注意力和生成概率。')
+ bilingual_text("Exponentiate and normalize each row of a matrix:", '对矩阵的每一行做指数化并归一化:')
+ bilingual_verbatim("[0 0 0] => [1/3 1/3 1/3]", '上方等宽内容保持原样,用于展示代码、性能输出或矩阵布局。', verbatim=True)
+ bilingual_verbatim("[1 1 -inf] [1/2 1/2 0 ]", '上方等宽内容保持原样,用于展示代码、性能输出或矩阵布局。', verbatim=True)
- text("Let's first start with the naive implementation and keep track of reads/writes.")
+ bilingual_text("Let's first start with the naive implementation and keep track of reads/writes.", '先从朴素实现开始,并跟踪读写次数。')
x = torch.tensor([
[5., 5, 5],
[0, 0, 100],
], device=cuda_if_available())
y1 = naive_softmax(x) # @inspect y1
- text("Now let us write the Triton kernel.")
+ bilingual_text("Now let us write the Triton kernel.", '现在来编写 Triton 内核。')
image("images/triton-softmax.png", width=600)
y2 = triton_softmax(x) # @inspect y2
@@ -485,16 +485,16 @@ def triton_softmax_kernel(x_ptr, y_ptr, x_row_stride, y_row_stride, num_cols, BL
def triton_row_sum_example():
- text("In the softmax example, an entire row fits in a block, so the reduction happens within a block (handled by Triton).")
- text("What if the row doesn't fit in a block?")
- text("Example: 4096 columns, but block size is 1024...")
+ bilingual_text("In the softmax example, an entire row fits in a block, so the reduction happens within a block (handled by Triton).", '在 softmax 示例中,整行可以放进一个块,所以规约发生在块内部(由 Triton 处理)。')
+ bilingual_text("What if the row doesn't fit in a block?", '如果一行放不进一个块怎么办?')
+ bilingual_text("Example: 4096 columns, but block size is 1024...", '示例:有 4096 列,但块大小是 1024……')
- text("Strategy:")
- text("- Break up row into tiles (4 in the example above)")
- text("- Each thread iterates over tiles and accumulates a sum")
- text("- Do final reduction (sum) over accumulators of each thread (shared memory or warp shuffles)")
+ bilingual_text("Strategy:", '策略:')
+ bilingual_text("- Break up row into tiles (4 in the example above)", '- 把一行拆成多个分块(上例中为 4 个)。')
+ bilingual_text("- Each thread iterates over tiles and accumulates a sum", '- 每个线程遍历分块并累加求和。')
+ bilingual_text("- Do final reduction (sum) over accumulators of each thread (shared memory or warp shuffles)", '- 对每个线程的累加器做最终规约(求和)(使用共享内存或 warp shuffle)。')
- text("Consider the simpler example (row sum instead of softmax):")
+ bilingual_text("Consider the simpler example (row sum instead of softmax):", '考虑一个更简单的例子(行求和,而不是 softmax):')
x = torch.tensor([[1., 2, 3, 4], [5, 6, 7, 8]], device=cuda_if_available()) # @inspect x
y1 = builtin_row_sum(x) # @inspect y1
@@ -536,59 +536,59 @@ def row_sum_kernel(x_ptr, out_ptr, N, BLOCK_SIZE: tl.constexpr):
def triton_matmul_relu_example():
- text("Matrix multiplication is the bread and butter of deep learning.")
+ bilingual_text("Matrix multiplication is the bread and butter of deep learning.", '矩阵乘法是深度学习的核心基础。')
a = torch.randn(1024, 1024, device=cuda_if_available())
b = torch.randn(1024, 1024, device=cuda_if_available())
c = naive_matmul_relu(a, b)
- text("How should we build a matmul kernel?")
+ bilingual_text("How should we build a matmul kernel?", '我们应该如何构建矩阵乘法内核?')
- text("| k n ", verbatim=True)
- text("| [ A1 A2 A3 ] [ B1 B2 B3 ] [ C1 C2 C3 ]", verbatim=True)
- text("| m [ A4 A5 A6 ] * k [ B4 B5 B6 ] = [ C4 C5 C6 ]", verbatim=True)
- text("| [ A7 A8 A9 ] [ B7 B8 B9 ] [ C7 C8 C9 ]", verbatim=True)
+ bilingual_verbatim("| k n ", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| [ A1 A2 A3 ] [ B1 B2 B3 ] [ C1 C2 C3 ]", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| m [ A4 A5 A6 ] * k [ B4 B5 B6 ] = [ C4 C5 C6 ]", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
+ bilingual_verbatim("| [ A7 A8 A9 ] [ B7 B8 B9 ] [ C7 C8 C9 ]", '上方等宽内容是硬件规格表或矩阵示意的一行;英文、数值和列对齐保持原样。', verbatim=True)
- text("**Naive approach:**")
- text("Fix any (m, n).")
- text("For each k:")
- text("- Read A[m, k] and B[k, n] from HBM.")
- text("- Multiply and accumulate.")
- text("Write result to C[m, n] in HBM.")
+ bilingual_text("**Naive approach:**", '**朴素方法:**')
+ bilingual_text("Fix any (m, n).", '固定任意一个 (m, n)。')
+ bilingual_text("For each k:", '对每个 k:')
+ bilingual_text("- Read A[m, k] and B[k, n] from HBM.", '- 从 HBM 读取 A[m, k] 和 B[k, n]。')
+ bilingual_text("- Multiply and accumulate.", '- 相乘并累加。')
+ bilingual_text("Write result to C[m, n] in HBM.", '把结果写入 HBM 中的 C[m, n]。')
- text("Bottleneck: M K N reads, M N writes")
- text("Arithmetic intensity: O(1)")
+ bilingual_text("Bottleneck: M K N reads, M N writes", '瓶颈:M K N 次读取,M N 次写入。')
+ bilingual_text("Arithmetic intensity: O(1)", '算术强度:O(1)。')
- text("Computing C4 and C5 both need A4, A5, A6.")
- text("Can we read A4, A5, A6 from HBM once to compute both?")
- text("Answer: yes, using shared memory!")
+ bilingual_text("Computing C4 and C5 both need A4, A5, A6.", '计算 C4 和 C5 都需要 A4、A5、A6。')
+ bilingual_text("Can we read A4, A5, A6 from HBM once to compute both?", '能否只从 HBM 读取一次 A4、A5、A6,就同时计算二者?')
+ bilingual_text("Answer: yes, using shared memory!", '答案是可以,使用共享内存!')
- text("**Idealized approach:**")
- text("- Load all of A and B into shared memory, then compute C.")
- text("- Now we get M K + K N reads and M N writes.")
- text("- This yields the idealized O(N) arithmetic intensity from before.")
- text("- However, A and B are usually too large to fit in shared memory.")
+ bilingual_text("**Idealized approach:**", '**理想化方法:**')
+ bilingual_text("- Load all of A and B into shared memory, then compute C.", '- 把 A 和 B 全部加载到共享内存,然后计算 C。')
+ bilingual_text("- Now we get M K + K N reads and M N writes.", '- 现在得到 M K + K N 次读取和 M N 次写入。')
+ bilingual_text("- This yields the idealized O(N) arithmetic intensity from before.", '- 这得到前面理想化的 O(N) 算术强度。')
+ bilingual_text("- However, A and B are usually too large to fit in shared memory.", '- 但是,A 和 B 通常太大,无法放入共享内存。')
- text("**Tiling:**")
+ bilingual_text("**Tiling:**", '**分块:**')
image("images/gemm_tiled.png", width=600)
- text("Key idea: divide the matrix C into output tiles (thread blocks).")
- text("Fix an output tile in C.")
- text("For each pair of (row tile of A, column tile of B):")
- text("- Load the corresponding A tile and B tile from HBM into shared memory.")
- text("- Perform matrix multiplication on the tiles.")
- text("- Accumulate into the partial sum (in shared memory).")
- text("Write output tile to HBM.")
+ bilingual_text("Key idea: divide the matrix C into output tiles (thread blocks).", '关键思想:把矩阵 C 划分为输出分块(线程块)。')
+ bilingual_text("Fix an output tile in C.", '固定 C 中的一个输出分块。')
+ bilingual_text("For each pair of (row tile of A, column tile of B):", '对每一对(A 的行分块,B 的列分块):')
+ bilingual_text("- Load the corresponding A tile and B tile from HBM into shared memory.", '- 从 HBM 把对应的 A 分块和 B 分块加载到共享内存。')
+ bilingual_text("- Perform matrix multiplication on the tiles.", '- 在这些分块上执行矩阵乘法。')
+ bilingual_text("- Accumulate into the partial sum (in shared memory).", '- 累加到部分和中(位于共享内存)。')
+ bilingual_text("Write output tile to HBM.", '把输出分块写回 HBM。')
- text("Arithmetic intensity: O(tile_size).")
+ bilingual_text("Arithmetic intensity: O(tile_size).", '算术强度:O(tile_size)。')
- text("Bonus:")
- text("- Often, you want to apply an elementwise activation function.")
- text("- Example: GeLU(A @ B)")
- text("- Solution: kernel fusion!")
+ bilingual_text("Bonus:", '额外收益:')
+ bilingual_text("- Often, you want to apply an elementwise activation function.", '- 通常,你还想应用逐元素激活函数。')
+ bilingual_text("- Example: GeLU(A @ B)", '- 示例:GeLU(A @ B)。')
+ bilingual_text("- Solution: kernel fusion!", '- 解决方案:内核融合!')
- text("**Implementation.**")
+ bilingual_text("**Implementation.**", '**实现。**')
- text("Review: each matrix is linearized in memory")
+ bilingual_text("Review: each matrix is linearized in memory", '回顾:每个矩阵都会在线性内存中展开。')
x = torch.tensor([[0., 1, 2, 3], [4, 5, 6, 7]]) # @inspect x
stride_row, stride_col = x.stride() # @inspect stride_row stride_col
row = 1
diff --git a/lecture_07.py b/lecture_07.py
index 990339f..0408ce9 100644
--- a/lecture_07.py
+++ b/lecture_07.py
@@ -11,84 +11,84 @@
import torch.multiprocessing as mp
from edtrace import text, image, link
from gpu_util import cuda_if_available
-from lecture_util import article_link
+from lecture_util import article_link, bilingual_text, bilingual_verbatim
if not torch.cuda.is_available():
torch.cuda.synchronize = lambda: None # No-op if CUDA is not available
def main():
- text("# Lecture 7: parallelism")
- text("Last week: parallelism within a single GPU")
- text("This week: parallelism across multiple GPUs")
+ bilingual_text("# Lecture 7: parallelism", '# 第 7 讲:并行')
+ bilingual_text("Last week: parallelism within a single GPU", '上周:单个 GPU 内部的并行。')
+ bilingual_text("This week: parallelism across multiple GPUs", '本周:跨多个 GPU 的并行。')
image("images/gpu-node-overview.png", width=700)
- text("In both cases, **compute** (arithmetic logic units) is far from inputs/outputs (**data**).")
- text("Unifying theme: orchestrate computation to avoid data transfer bottlenecks")
+ bilingual_text("In both cases, **compute** (arithmetic logic units) is far from inputs/outputs (**data**).", '在这两种情况下,**计算**(算术逻辑单元)都离输入/输出(**数据**)很远。')
+ bilingual_text("Unifying theme: orchestrate computation to avoid data transfer bottlenecks", '统一主题:组织计算,以避免数据传输瓶颈。')
- text("Generalized hierarchy:")
- text("- Single node, single GPU: L1 cache / shared memory (fastest)")
- text("- Single node, single GPU: HBM")
- text("- Single node, multi-GPU: NVLink/NVSwitch")
- text("- Multi-node, multi-GPU: Infiniband/Ethernet (slowest)")
+ bilingual_text("Generalized hierarchy:", '广义层次结构:')
+ bilingual_text("- Single node, single GPU: L1 cache / shared memory (fastest)", '- 单节点、单 GPU:L1 缓存 / 共享内存(最快)。')
+ bilingual_text("- Single node, single GPU: HBM", '- 单节点、单 GPU:HBM。')
+ bilingual_text("- Single node, multi-GPU: NVLink/NVSwitch", '- 单节点、多 GPU:NVLink/NVSwitch。')
+ bilingual_text("- Multi-node, multi-GPU: Infiniband/Ethernet (slowest)", '- 多节点、多 GPU:Infiniband/Ethernet(最慢)。')
- text("Last week: reduce memory accesses via fusion/tiling")
- text("This week: reduce communication across GPUs/nodes via replication/sharding")
+ bilingual_text("Last week: reduce memory accesses via fusion/tiling", '上周:通过融合/分块减少内存访问。')
+ bilingual_text("This week: reduce communication across GPUs/nodes via replication/sharding", '本周:通过复制/分片减少 GPU/节点之间的通信。')
- text("Why do multi-GPU?")
- text("1. Your parameters (optimizer state + gradients + activations) don't fit on a single GPU.")
- text("2. You want to use more GPUs (more FLOPs) to train faster.")
+ bilingual_text("Why do multi-GPU?", '为什么要使用多 GPU?')
+ bilingual_text("1. Your parameters (optimizer state + gradients + activations) don't fit on a single GPU.", '1. 你的参数(优化器状态 + 梯度 + 激活值)放不进单个 GPU。')
+ bilingual_text("2. You want to use more GPUs (more FLOPs) to train faster.", '2. 你想使用更多 GPU(更多 FLOPs)来更快训练。')
# When you execute this lecture directly (python lecture_07.py), it uses multiprocessing, which produces output from each process (below).
# However, when you trace this lecture (python -m edtrace.execute -m lecture_07), we turn off multiprocessing.
link(title="stdout for this lecture", url="var/traces/lecture_07_stdout.txt")
- text("### Part 1: building blocks of distributed communication/computation")
+ bilingual_text("### Part 1: building blocks of distributed communication/computation", '### 第 1 部分:分布式通信/计算的构建块')
collective_operations() # Programming model
hardware() # Hardware: how GPUs are connected
torch_distributed() # How this is implemented in NCCL/PyTorch
benchmarking() # Measure actual NCCL bandwidth
- text("### Part 2: distributed training")
- text("Walk through bare-bones implementations of each strategy on deep MLPs.")
- text("Recall that MLPs are the compute bottleneck in Transformers, so this is representative.")
+ bilingual_text("### Part 2: distributed training", '### 第 2 部分:分布式训练')
+ bilingual_text("Walk through bare-bones implementations of each strategy on deep MLPs.", '在深层 MLP 上逐步讲解每种策略的最小实现。')
+ bilingual_text("Recall that MLPs are the compute bottleneck in Transformers, so this is representative.", '回忆一下,MLP 是 Transformer 中的计算瓶颈,因此这个例子具有代表性。')
data_parallelism() # Cut up along the batch dimension
tensor_parallelism() # Cut up along the width dimension
pipeline_parallelism() # Cut up along the depth dimension
- text("What's missing?")
- text("- Communication/computation overlap")
- text("- More general models (with attention, etc.)")
- text("- Other forms of parallelism (e.g., sequence parallelism, expert parallelism, combinations)")
- text("- Jax/TPUs: just define the model, the sharding strategy, and the Jax compiler handles the rest "), link(title="levanter", url="https://crfm.stanford.edu/2023/06/16/levanter-1_0-release.html")
- text("- But we're doing PyTorch so you can see how one builds up from the primitives")
+ bilingual_text("What's missing?", '还缺什么?')
+ bilingual_text("- Communication/computation overlap", '- 通信与计算重叠。')
+ bilingual_text("- More general models (with attention, etc.)", '- 更通用的模型(带注意力等)。')
+ bilingual_text("- Other forms of parallelism (e.g., sequence parallelism, expert parallelism, combinations)", '- 其他形式的并行(例如序列并行、专家并行以及组合)。')
+ bilingual_text("- Jax/TPUs: just define the model, the sharding strategy, and the Jax compiler handles the rest ", '- Jax/TPU:只需定义模型和分片策略,其余由 Jax 编译器处理。'), link(title="levanter", url="https://crfm.stanford.edu/2023/06/16/levanter-1_0-release.html")
+ bilingual_text("- But we're doing PyTorch so you can see how one builds up from the primitives", '- 但我们使用 PyTorch,这样你可以看到如何从原语逐步搭建。')
- text("### Summary")
- text("- Many ways to parallelize: data (batch), tensor/expert (width), pipeline (depth), sequence (length)")
- text("- Data parallelism: DDP (all-reduce), FSDP/ZeRO (all-gather + reduce-scatter)")
- text("- Tensor parallelism: requires very fast interconnects (e.g., NVLink)")
- text("- Pipeline parallelism: can work with slow interconnects, but need to work to reduce pipeline bubbles")
- text("- Can **re-compute** or store in **memory** or store in another GPUs memory and **communicate**")
- text("- Hardware is getting faster, but will always want bigger models, so will have this hierarchical structure")
+ bilingual_text("### Summary", '### 总结')
+ bilingual_text("- Many ways to parallelize: data (batch), tensor/expert (width), pipeline (depth), sequence (length)", '- 并行方式很多:数据(批次)、张量/专家(宽度)、流水线(深度)、序列(长度)。')
+ bilingual_text("- Data parallelism: DDP (all-reduce), FSDP/ZeRO (all-gather + reduce-scatter)", '- 数据并行:DDP(all-reduce)、FSDP/ZeRO(all-gather + reduce-scatter)。')
+ bilingual_text("- Tensor parallelism: requires very fast interconnects (e.g., NVLink)", '- 张量并行:需要非常快的互连(例如 NVLink)。')
+ bilingual_text("- Pipeline parallelism: can work with slow interconnects, but need to work to reduce pipeline bubbles", '- 流水线并行:可在较慢互连上工作,但需要努力减少流水线气泡。')
+ bilingual_text("- Can **re-compute** or store in **memory** or store in another GPUs memory and **communicate**", '- 可以**重算**,也可以存入**内存**,或者存入另一块 GPU 的内存并进行**通信**。')
+ bilingual_text("- Hardware is getting faster, but will always want bigger models, so will have this hierarchical structure", '- 硬件会越来越快,但我们总想要更大的模型,因此这种层次结构会一直存在。')
def collective_operations():
- text("**Collective operations** are the conceptual primitives used for distributed programming "), article_link("https://en.wikipedia.org/wiki/Collective_operation")
- text("- These are classic in the parallel programming literature from the 1980s.")
- text("- *Collective* means that you specify a general communication pattern across many devices.")
- text("- This can be better/faster than managing point-to-point communication yourself.")
+ bilingual_text("**Collective operations** are the conceptual primitives used for distributed programming ", '**集合通信操作**是分布式编程使用的概念原语。'), article_link("https://en.wikipedia.org/wiki/Collective_operation")
+ bilingual_text("- These are classic in the parallel programming literature from the 1980s.", '- 这些是 1980 年代并行编程文献中的经典概念。')
+ bilingual_text("- *Collective* means that you specify a general communication pattern across many devices.", '- *集合通信*意味着你指定跨多个设备的一般通信模式。')
+ bilingual_text("- This can be better/faster than managing point-to-point communication yourself.", '- 这可能比自己管理点对点通信更好、更快。')
- text("**Setup**:")
+ bilingual_text("**Setup**:", '**设置**:')
image("images/ranks.png", width=500)
- text("- **Rank**: a particular device/GPU (e.g., 0, 1, 2, 3)")
- text("- **World size**: total number of devices (e.g., 4)")
+ bilingual_text("- **Rank**: a particular device/GPU (e.g., 0, 1, 2, 3)", '- **Rank**:某个具体设备/GPU(例如 0、1、2、3)。')
+ bilingual_text("- **World size**: total number of devices (e.g., 4)", '- **World size**:设备总数(例如 4)。')
- text("Operations:")
- text("- Broadcast, scatter, gather, reduce (foundations)")
- text("- All-gather, reduce-scatter, all-reduce (workhorse)")
- text("- All-to-all (for MoEs)")
+ bilingual_text("Operations:", '操作:')
+ bilingual_text("- Broadcast, scatter, gather, reduce (foundations)", '- broadcast、scatter、gather、reduce(基础)。')
+ bilingual_text("- All-gather, reduce-scatter, all-reduce (workhorse)", '- all-gather、reduce-scatter、all-reduce(主力)。')
+ bilingual_text("- All-to-all (for MoEs)", '- all-to-all(用于 MoE)。')
- text("**Broadcast**: copy from rank 0 to all ranks")
+ bilingual_text("**Broadcast**: copy from rank 0 to all ranks", '**Broadcast**:从 rank 0 复制到所有 rank。')
# Input
rank0 = tensor([0., 1, 2, 3])
@@ -98,9 +98,9 @@ def collective_operations():
rank2 = tensor([0., 1, 2, 3])
rank3 = tensor([0., 1, 2, 3])
- text("Minor use case: rank 0 loads initial checkpoint and broadcasts to all ranks")
+ bilingual_text("Minor use case: rank 0 loads initial checkpoint and broadcasts to all ranks", '小用例:rank 0 加载初始 checkpoint,并广播到所有 rank。')
- text("**Scatter** tensor on rank 0 to all ranks")
+ bilingual_text("**Scatter** tensor on rank 0 to all ranks", '**Scatter**:把 rank 0 上的张量分发到所有 rank。')
# Input
rank0 = tensor([0., 1, 2, 3])
@@ -110,9 +110,9 @@ def collective_operations():
rank2 = tensor([2.])
rank3 = tensor([3.])
- text("Note: stepping stone to understanding reduce-scatter")
+ bilingual_text("Note: stepping stone to understanding reduce-scatter", '注意:这是理解 reduce-scatter 的垫脚石。')
- text("**Gather** pieces from all ranks to rank 0 (opposite of scatter)")
+ bilingual_text("**Gather** pieces from all ranks to rank 0 (opposite of scatter)", '**Gather**:把所有 rank 的片段收集到 rank 0(scatter 的反向操作)。')
# Input
rank0 = tensor([0.])
rank1 = tensor([1.])
@@ -122,9 +122,9 @@ def collective_operations():
# Output
rank0 = tensor([0., 1, 2, 3])
- text("Note: stepping stone to understanding all-gather")
+ bilingual_text("Note: stepping stone to understanding all-gather", '注意:这是理解 all-gather 的垫脚石。')
- text("**Reduce** pieces from all ranks to rank 0, applying some operation (e.g., sum, min, max)")
+ bilingual_text("**Reduce** pieces from all ranks to rank 0, applying some operation (e.g., sum, min, max)", '**Reduce**:把所有 rank 的片段聚合到 rank 0,并应用某个操作(例如 sum、min、max)。')
# Input
rank0 = tensor([0.])
rank1 = tensor([1.])
@@ -134,9 +134,9 @@ def collective_operations():
# Output
rank0 = tensor([6.]) # Sum of all ranks (0 + 1 + 2 + 3)
- text("Note: stepping stone to understanding all-reduce")
+ bilingual_text("Note: stepping stone to understanding all-reduce", '注意:这是理解 all-reduce 的垫脚石。')
- text("**All-gather**: perform gather to all ranks, not just rank 0")
+ bilingual_text("**All-gather**: perform gather to all ranks, not just rank 0", '**All-gather**:对所有 rank 执行 gather,而不仅是 rank 0。')
# Input
rank0 = tensor([0.])
rank1 = tensor([1.])
@@ -149,9 +149,9 @@ def collective_operations():
rank2 = tensor([0., 1, 2, 3])
rank3 = tensor([0., 1, 2, 3])
- text("Use case: each rank holds parameter shard, gather to get full parameters for forward pass")
+ bilingual_text("Use case: each rank holds parameter shard, gather to get full parameters for forward pass", '用例:每个 rank 持有参数分片,前向传播时 gather 得到完整参数。')
- text("**Reduce-scatter**: perform reduce on each dimension, scatter results")
+ bilingual_text("**Reduce-scatter**: perform reduce on each dimension, scatter results", '**Reduce-scatter**:在每个维度上执行 reduce,再把结果 scatter。')
# Input
rank0 = tensor([0., 1, 2, 3])
rank1 = tensor([1., 2, 3, 4])
@@ -164,9 +164,9 @@ def collective_operations():
rank2 = tensor([14.]) # Sum along dim 2 (2 + 3 + 4 + 5)
rank3 = tensor([18.]) # Sum along dim 3 (3 + 4 + 5 + 6)
- text("Use case: after backward pass, sum gradients from different data shards, but distribute storage")
+ bilingual_text("Use case: after backward pass, sum gradients from different data shards, but distribute storage", '用例:反向传播后,对不同数据分片上的梯度求和,但分布式存储结果。')
- text("**All-reduce** = reduce-scatter + all-gather")
+ bilingual_text("**All-reduce** = reduce-scatter + all-gather", '**All-reduce(全规约)** = reduce-scatter + all-gather。')
# Input
rank0 = tensor([0., 1, 2, 3])
rank1 = tensor([1., 2, 3, 4])
@@ -179,10 +179,10 @@ def collective_operations():
rank2 = tensor([6., 10, 14, 18])
rank3 = tensor([6., 10, 14, 18])
- text("Use case: after backward pass, sum gradients from different data shards, but replicate full parameters")
- text("Breaking all-reduce into reduce-scatter + all-gather allows for flexibility (e.g., ZeRO/FSDP)")
+ bilingual_text("Use case: after backward pass, sum gradients from different data shards, but replicate full parameters", '用例:反向传播后,对不同数据分片上的梯度求和,但复制完整参数。')
+ bilingual_text("Breaking all-reduce into reduce-scatter + all-gather allows for flexibility (e.g., ZeRO/FSDP)", '把 all-reduce 拆成 reduce-scatter + all-gather 可以带来灵活性(例如 ZeRO/FSDP)。')
- text("**All-to-all**: each rank sends each other rank some tensor (most general)")
+ bilingual_text("**All-to-all**: each rank sends each other rank some tensor (most general)", '**All-to-all**:每个 rank 都向其他每个 rank 发送一些张量(最一般的形式)。')
# Input
rank0 = tensor([0., 1, 2, 3]) # send 0 to rank 0, 1 to rank 1, 2 to rank 2, 3 to rank 3
rank1 = tensor([4., 5, 6, 7]) # send 4 to rank 0, 5 to rank 1, 6 to rank 2, 7 to rank 3
@@ -195,54 +195,54 @@ def collective_operations():
rank2 = tensor([2, 6, 10, 14])
rank3 = tensor([3, 7, 11, 15])
- text("Notes:")
- text("- Useful for MoEs: each rank has split of data and subset of experts; need to route data to experts")
- text("- For balanced splits, all-to-all looks like transpose")
- text("- Also handles unbalanced splits (but want splits to be as balanced as possible)")
+ bilingual_text("Notes:", '说明:')
+ bilingual_text("- Useful for MoEs: each rank has split of data and subset of experts; need to route data to experts", '- 对 MoE 很有用:每个 rank 拥有一部分数据和一部分专家,需要把数据路由到专家。')
+ bilingual_text("- For balanced splits, all-to-all looks like transpose", '- 对均衡切分来说,all-to-all 看起来像转置。')
+ bilingual_text("- Also handles unbalanced splits (but want splits to be as balanced as possible)", '- 也能处理不均衡切分(但我们希望切分尽可能均衡)。')
- text("Way to remember the terminology:")
- text("- Reduce: performs some associative/commutative operation (sum, min, max)")
- text("- Scatter is inverse of gather")
- text("- All: means destination is all devices")
+ bilingual_text("Way to remember the terminology:", '记住这些术语的方法:')
+ bilingual_text("- Reduce: performs some associative/commutative operation (sum, min, max)", '- Reduce:执行某种满足结合律/交换律的操作(sum、min、max)。')
+ bilingual_text("- Scatter is inverse of gather", '- Scatter 是 gather 的反向操作。')
+ bilingual_text("- All: means destination is all devices", '- All:表示目标是所有设备。')
def hardware():
- text("Classic (in the home):")
+ bilingual_text("Classic (in the home):", '经典情况(家用环境):')
image("https://media.springernature.com/lw685/springer-static/image/art%3A10.1186%2Fs42774-021-00098-3/MediaObjects/42774_2021_98_Fig1_HTML.png?as=webp", width=500)
- text("- GPUs on same node communicate via a PCI(e) bus (v7.0, 16 lanes => 242 GB/s) "), article_link("https://en.wikipedia.org/wiki/PCI_Express")
- text("- GPUs on different nodes communicate via Ethernet (~200 MB/s)")
+ bilingual_text("- GPUs on same node communicate via a PCI(e) bus (v7.0, 16 lanes => 242 GB/s) ", '- 同一节点上的 GPU 通过 PCI(e) 总线通信(v7.0,16 lanes => 242 GB/s)。'), article_link("https://en.wikipedia.org/wiki/PCI_Express")
+ bilingual_text("- GPUs on different nodes communicate via Ethernet (~200 MB/s)", '- 不同节点上的 GPU 通过 Ethernet 通信(约 200 MB/s)。')
- text("Modern (in the data center):")
+ bilingual_text("Modern (in the data center):", '现代情况(数据中心):')
image("images/gpu-node-overview.png", width=700)
- text("Typical setup:")
- text("- 8 GPUs per node, connected by NVLink to an NVSwitch (B200s' NVLink 5.0 gets 1.8 TB/s; HBM was 8 TB/s)")
- text("- 256 nodes per pod, connected by Infiniband (via PCIe -> HCA / Infiniband NIC -> Infiniband cable) (~0.05 TB/s)")
- text("- N pods per cluster / datacenter, connected by Ethernet (via PCIe -> CPU)")
+ bilingual_text("Typical setup:", '典型配置:')
+ bilingual_text("- 8 GPUs per node, connected by NVLink to an NVSwitch (B200s' NVLink 5.0 gets 1.8 TB/s; HBM was 8 TB/s)", '- 每个节点 8 块 GPU,通过 NVLink 连接到 NVSwitch(B200 的 NVLink 5.0 达到 1.8 TB/s;HBM 是 8 TB/s)。')
+ bilingual_text("- 256 nodes per pod, connected by Infiniband (via PCIe -> HCA / Infiniband NIC -> Infiniband cable) (~0.05 TB/s)", '- 每个 pod 256 个节点,通过 Infiniband 连接(PCIe -> HCA / Infiniband NIC -> Infiniband cable)(约 0.05 TB/s)。')
+ bilingual_text("- N pods per cluster / datacenter, connected by Ethernet (via PCIe -> CPU)", '- 每个集群/数据中心有 N 个 pod,通过 Ethernet 连接(经 PCIe -> CPU)。')
- text("Bypassing the CPU:")
- text("- Ethernet requires passing through the CPU (copying data to kernel socket buffer, build TCP packets, copy to NIC ring buffer)")
- text("- Remote Direct Memory Access (RDMA): allows one GPU to directly read/write another GPU's memory without involving the CPU")
- text("- Infiniband supports RDMA, but standard Ethernet does not")
+ bilingual_text("Bypassing the CPU:", '绕过 CPU:')
+ bilingual_text("- Ethernet requires passing through the CPU (copying data to kernel socket buffer, build TCP packets, copy to NIC ring buffer)", '- Ethernet 需要经过 CPU(把数据复制到内核 socket buffer、构造 TCP 包、复制到 NIC ring buffer)。')
+ bilingual_text("- Remote Direct Memory Access (RDMA): allows one GPU to directly read/write another GPU's memory without involving the CPU", '- 远程直接内存访问(RDMA):允许一个 GPU 不经过 CPU,直接读写另一个 GPU 的内存。')
+ bilingual_text("- Infiniband supports RDMA, but standard Ethernet does not", '- Infiniband 支持 RDMA,但标准 Ethernet 不支持。')
- text("Advancements:")
- text("- GB200/GB300 NVL72: 8 GPUs per tray, 9 trays per rack -> 72 GPUs in one NVLink domain")
- text("- RDMA over Converged Ethernet (RoCE): Ethernet bypasses CPU, similar but cheaper/weaker than Infiniband, used by Meta")
+ bilingual_text("Advancements:", '进展:')
+ bilingual_text("- GB200/GB300 NVL72: 8 GPUs per tray, 9 trays per rack -> 72 GPUs in one NVLink domain", '- GB200/GB300 NVL72:每个 tray 8 块 GPU,每个机架 9 个 tray -> 一个 NVLink domain 中有 72 块 GPU。')
+ bilingual_text("- RDMA over Converged Ethernet (RoCE): Ethernet bypasses CPU, similar but cheaper/weaker than Infiniband, used by Meta", '- RDMA over Converged Ethernet(RoCE):Ethernet 绕过 CPU,类似 Infiniband 但更便宜也更弱,Meta 使用这种方案。')
- text("### NVIDIA Collective Communication Library (NCCL)")
- text("NCCL translates collective operations into low-level packets that are sent between GPUs. "), link(title="talk", url="https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31880/")
- text("- Detects topology of hardware (e.g., number of nodes, switches, NVLink/PCIe)")
- text("- Optimizes the path between GPUs")
- text("- Launches GPU kernels to send/receive data")
+ bilingual_text("### NVIDIA Collective Communication Library (NCCL)", '### NVIDIA 集合通信库(NCCL)')
+ bilingual_text("NCCL translates collective operations into low-level packets that are sent between GPUs. ", 'NCCL 把集合通信操作转换为 GPU 之间发送的低层数据包。'), link(title="talk", url="https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31880/")
+ bilingual_text("- Detects topology of hardware (e.g., number of nodes, switches, NVLink/PCIe)", '- 检测硬件拓扑(例如节点数量、交换机、NVLink/PCIe)。')
+ bilingual_text("- Optimizes the path between GPUs", '- 优化 GPU 之间的路径。')
+ bilingual_text("- Launches GPU kernels to send/receive data", '- 启动 GPU 内核来发送/接收数据。')
def torch_distributed():
- text("PyTorch distributed library (`torch.distributed`) "), link(title="documentation", url="https://pytorch.org/docs/stable/distributed.html")
- text("- Provides clean interface for collective operations (e.g., `all_gather_into_tensor`)")
- text("- Supports multiple backends for different hardware: gloo (CPU), nccl (GPU)")
- text("- Also supports higher-level algorithms (e.g., `FullyShardedDataParallel`) [not used in this course]")
+ bilingual_text("PyTorch distributed library (`torch.distributed`) ", 'PyTorch 分布式库(`torch.distributed`)。'), link(title="documentation", url="https://pytorch.org/docs/stable/distributed.html")
+ bilingual_text("- Provides clean interface for collective operations (e.g., `all_gather_into_tensor`)", '- 为集合通信操作提供清晰接口(例如 `all_gather_into_tensor`)。')
+ bilingual_text("- Supports multiple backends for different hardware: gloo (CPU), nccl (GPU)", '- 支持面向不同硬件的多个后端:gloo(CPU)、nccl(GPU)。')
+ bilingual_text("- Also supports higher-level algorithms (e.g., `FullyShardedDataParallel`) [not used in this course]", '- 也支持更高层算法(例如 `FullyShardedDataParallel`)[本课程不使用]。')
- text("Let's walk through some examples.")
+ bilingual_text("Let's walk through some examples.", '我们来看几个例子。')
spawn(collective_operations_main, world_size=4)
@@ -279,13 +279,13 @@ def collective_operations_main(rank: int, world_size: int): # @inspect rank wor
dist.all_gather_into_tensor(output_tensor=output, input_tensor=input, async_op=False)
print(f"Rank {rank} [after all-gather]: input = {input}, output = {output}", flush=True)
- text("Indeed, all-reduce = reduce-scatter + all-gather!")
+ bilingual_text("Indeed, all-reduce = reduce-scatter + all-gather!", '确实,all-reduce = reduce-scatter + all-gather!')
cleanup()
def benchmarking():
- text("How fast does communication happen?")
+ bilingual_text("How fast does communication happen?", '通信发生得有多快?')
# All-reduce
spawn(all_reduce, world_size=4, num_elements=100 * 1024**2)
@@ -293,7 +293,7 @@ def benchmarking():
# Reduce-scatter
spawn(reduce_scatter, world_size=4, num_elements=100 * 1024**2)
- text("References:")
+ bilingual_text("References:", '参考资料:')
link(title="How to reason about collective operations", url="https://github.com/NVIDIA/nccl-tests/blob/master/doc/PERFORMANCE.md#allreduce")
link(title="Sample benchmarking code", url="https://github.com/stas00/ml-engineering/blob/master/network/benchmarks/all_reduce_bench.py")
@@ -374,17 +374,17 @@ def reduce_scatter(rank: int, world_size: int, num_elements: int):
def data_parallelism():
image("images/data-parallelism.png", width=300)
- text("Sharding strategy: each rank gets a slice of the data")
+ bilingual_text("Sharding strategy: each rank gets a slice of the data", '分片策略:每个 rank 获得数据的一部分。')
data = generate_sample_data()
spawn(data_parallelism_main, world_size=4, data=data, num_layers=4, num_steps=1)
- text("Notes:")
- text("- Losses are different across ranks (computed on local data)")
- text("- Gradients are all-reduced to be the same across ranks")
- text("- Therefore, parameters remain the same across ranks")
+ bilingual_text("Notes:", '说明:')
+ bilingual_text("- Losses are different across ranks (computed on local data)", '- 各 rank 上的损失不同(在本地数据上计算)。')
+ bilingual_text("- Gradients are all-reduced to be the same across ranks", '- 梯度通过 all-reduce 变得在各 rank 上相同。')
+ bilingual_text("- Therefore, parameters remain the same across ranks", '- 因此,各 rank 上的参数保持相同。')
- text("Next time: FSDP/ZeRO: use all-gather and reduce-scatter to avoid holding all parameters in memory")
+ bilingual_text("Next time: FSDP/ZeRO: use all-gather and reduce-scatter to avoid holding all parameters in memory", '下次:FSDP/ZeRO:使用 all-gather 和 reduce-scatter,避免在内存中保存所有参数。')
def generate_sample_data():
@@ -438,7 +438,7 @@ def data_parallelism_main(rank: int, world_size: int, data: tensor, num_layers:
def tensor_parallelism():
image("images/tensor-parallelism.png", width=300)
- text("Sharding strategy: each rank gets part of each layer, transfer all data/activations")
+ bilingual_text("Sharding strategy: each rank gets part of each layer, transfer all data/activations", '分片策略:每个 rank 获得每一层的一部分,并传输所有数据/激活值。')
data = generate_sample_data()
spawn(tensor_parallelism_main, world_size=4, data=data, num_layers=4)
@@ -483,7 +483,7 @@ def tensor_parallelism_main(rank: int, world_size: int, data: tensor, num_layers
def pipeline_parallelism():
image("images/pipeline-parallelism.png", width=300)
- text("Sharding strategy: each rank gets subset of layers, transfer all data/activations")
+ bilingual_text("Sharding strategy: each rank gets subset of layers, transfer all data/activations", '分片策略:每个 rank 获得一部分层,并传输所有数据/激活值。')
data = generate_sample_data()
spawn(pipeline_parallelism_main, world_size=2, data=data, num_layers=4, num_micro_batches=4)
@@ -529,7 +529,7 @@ def pipeline_parallelism_main(rank: int, world_size: int, data: tensor, num_laye
print(f"[pipeline_parallelism] Rank {rank}: sending {summarize_tensor(x)} to rank {rank + 1}", flush=True) # @stepover
dist.send(tensor=x, dst=rank + 1)
- text("Not handled: overlapping communication/computation to eliminate pipeline bubbles")
+ bilingual_text("Not handled: overlapping communication/computation to eliminate pipeline bubbles", '未处理:通过通信/计算重叠来消除流水线气泡。')
# Backward pass: homework exercise
diff --git a/lecture_10.py b/lecture_10.py
index 9b86c27..205ca75 100644
--- a/lecture_10.py
+++ b/lecture_10.py
@@ -2,7 +2,7 @@
from sympy import symbols, oo
from edtrace import text, link, image
-from lecture_util import article_link
+from lecture_util import article_link, bilingual_text
from references import Reference, gqa_2023, mla_2024, longformer_2020, sparse_transformer_2019, mistral_7b_2023, deepseek_v4_2026
# Define symbols corresponding to the shape of the Transformer model
@@ -14,191 +14,191 @@
scaling_book_inference = Reference(title="Scaling book chapter on inference", url="https://jax-ml.github.io/scaling-book/inference/")
def main():
- text("## Lecture 10: inference")
+ bilingual_text("## Lecture 10: inference", '## 第 10 讲:推理')
image("images/inference-schema.png", width=600)
- text("### Understanding the inference workload")
+ bilingual_text("### Understanding the inference workload", '### 理解推理工作负载')
landscape()
review_transformer()
review_of_arithmetic_intensity()
arithmetic_intensity_of_inference()
throughput_and_latency()
- text("### Taking shortcuts (lossy)")
+ bilingual_text("### Taking shortcuts (lossy)", '### 走捷径(有损)')
reduce_kv_cache_size()
quantization()
model_pruning()
- text("Summary: reduce inference complexity without hurting accuracy")
+ bilingual_text("Summary: reduce inference complexity without hurting accuracy", '总结:在不损害准确率的前提下降低推理复杂度。')
- text("From scratch recipe:")
- text("1. Define faster model architecture")
- text("2. Train faster model")
+ bilingual_text("From scratch recipe:", '从零开始的配方:')
+ bilingual_text("1. Define faster model architecture", '1. 定义更快的模型架构。')
+ bilingual_text("2. Train faster model", '2. 训练更快的模型。')
- text("Distillation recipe:")
- text("1. Define faster model architecture")
- text("2. Initialize weights using original model (which has a different architecture)")
- text("3. Repair faster model (distillation)")
+ bilingual_text("Distillation recipe:", '蒸馏配方:')
+ bilingual_text("1. Define faster model architecture", '1. 定义更快的模型架构。')
+ bilingual_text("2. Initialize weights using original model (which has a different architecture)", '2. 使用原始模型初始化权重(原始模型具有不同架构)。')
+ bilingual_text("3. Repair faster model (distillation)", '3. 修复更快的模型(蒸馏)。')
- text("### Use shortcuts but double check (lossless)")
+ bilingual_text("### Use shortcuts but double check (lossless)", '### 使用捷径,但要复核(无损)')
speculative_sampling()
- text("### Handling dynamic workloads")
- text("Batching over sequences in live traffic is tricky because:")
- text("1. Requests arrive at different times (waiting for batch is bad for early requests)")
- text("2. Sequences have shared prefixes (e.g., system prompts, generating multiple samples)")
- text("3. Sequences have different lengths (padding is inefficient)")
+ bilingual_text("### Handling dynamic workloads", '### 处理动态工作负载')
+ bilingual_text("Batching over sequences in live traffic is tricky because:", '在实时流量中对序列做批处理很棘手,因为:')
+ bilingual_text("1. Requests arrive at different times (waiting for batch is bad for early requests)", '1. 请求在不同时间到达(等待成批会伤害早到的请求)。')
+ bilingual_text("2. Sequences have shared prefixes (e.g., system prompts, generating multiple samples)", '2. 序列可能共享前缀(例如系统提示、生成多个样本)。')
+ bilingual_text("3. Sequences have different lengths (padding is inefficient)", '3. 序列长度不同(填充效率低)。')
continuous_batching()
paged_attention()
- text("### Summary")
- text("- Inference is important (actual use, evaluation, reinforcement learning)")
- text("- Different characteristics compared to training (memory-bound, dynamic)")
- text("- Techniques: new architectures, quantization, pruning/distillation, speculative sampling")
- text("- Ideas from systems (speculative execution, paging)")
- text("- New architectures have huge potential for improvement")
+ bilingual_text("### Summary", '### 总结')
+ bilingual_text("- Inference is important (actual use, evaluation, reinforcement learning)", '- 推理很重要(实际使用、评测、强化学习都需要)。')
+ bilingual_text("- Different characteristics compared to training (memory-bound, dynamic)", '- 与训练相比特性不同(受内存带宽限制、动态)。')
+ bilingual_text("- Techniques: new architectures, quantization, pruning/distillation, speculative sampling", '- 技术:新架构、量化、剪枝/蒸馏、推测采样。')
+ bilingual_text("- Ideas from systems (speculative execution, paging)", '- 来自系统领域的思想(推测执行、分页)。')
+ bilingual_text("- New architectures have huge potential for improvement", '- 新架构有巨大的改进潜力。')
def landscape():
- text("Inference shows up in many places:")
- text("- Actual use (chatbots, code completion, agents, batch data processing)")
- text("- Model evaluation (e.g., on instruction following)")
- text("- Reinforcement learning (sample many generations, then apply score)")
-
- text("Why **efficiency** matters: training is one-time cost, inference is repeated many times")
- text("- OpenAI processes ~8.6T tokens per day "), article_link("https://www.pymnts.com/artificial-intelligence-2/2025/openai-bests-google-in-race-for-consumer-ai-token-consumption/")
- text("- For reference, DeepSeek v4 was trained on 32T tokens "), link(deepseek_v4_2026)
+ bilingual_text("Inference shows up in many places:", '推理出现在很多地方:')
+ bilingual_text("- Actual use (chatbots, code completion, agents, batch data processing)", '- 实际使用(聊天机器人、代码补全、智能体、批量数据处理)。')
+ bilingual_text("- Model evaluation (e.g., on instruction following)", '- 模型评测(例如指令遵循)。')
+ bilingual_text("- Reinforcement learning (sample many generations, then apply score)", '- 强化学习(采样许多生成结果,然后打分)。')
+
+ bilingual_text("Why **efficiency** matters: training is one-time cost, inference is repeated many times", '为什么**效率**重要:训练是一次性成本,而推理会重复发生很多次。')
+ bilingual_text("- OpenAI processes ~8.6T tokens per day ", '- 说明:OpenAI processes ~8.6T tokens per day'), article_link("https://www.pymnts.com/artificial-intelligence-2/2025/openai-bests-google-in-race-for-consumer-ai-token-consumption/")
+ bilingual_text("- For reference, DeepSeek v4 was trained on 32T tokens ", '- 说明:For reference, DeepSeek v4 was trained on 32T tokens'), link(deepseek_v4_2026)
- text("Moreover:")
- text("- Chatbots: most tokens are meant for human consumption (humans are bottleneck)")
- text("- Agents: query → internal trace → output for human (number of tokens generated can grow unbounded)")
- text("- Tokens generated = compute spent")
+ bilingual_text("Moreover:", '此外:')
+ bilingual_text("- Chatbots: most tokens are meant for human consumption (humans are bottleneck)", '- 聊天机器人:大多数 token 是给人看的(人是瓶颈)。')
+ bilingual_text("- Agents: query → internal trace → output for human (number of tokens generated can grow unbounded)", '- 智能体:查询 → 内部轨迹 → 给人的输出(生成 token 数可能无限增长)。')
+ bilingual_text("- Tokens generated = compute spent", '- 生成的 token = 花掉的计算量。')
- text("Companies doing inference (a big deal for anyone who has a product or platform):")
- text("- Providers serving closed models (OpenAI, Anthropic, Google, etc.)")
- text("- Providers serving open-weight models (Together, Fireworks, Baseten, DeepInfra, Groq, Cerebras, etc.)")
+ bilingual_text("Companies doing inference (a big deal for anyone who has a product or platform):", 'Companies doing 推理 (a big deal for anyone who has a product or platform):')
+ bilingual_text("- Providers serving closed models (OpenAI, Anthropic, Google, etc.)", '- Providers serving closed 模型s (OpenAI, Anthropic, Google, etc.)')
+ bilingual_text("- Providers serving open-weight models (Together, Fireworks, Baseten, DeepInfra, Groq, Cerebras, etc.)", '- Providers serving open-weight 模型s (Together, Fireworks, Baseten, DeepInfra, Groq, Cerebras, etc.)')
- text("Open-source packages:")
- text("- vLLM: from Berkeley, pioneered PagedAttention, popular and good default "), link(title="GitHub", url="https://github.com/vllm-project/vllm")
- text("- SGLang: from Berkeley, pioneered RadixAttention, good for agentic workloads "), link(title="project", url="https://sgl-project.github.io/")
- text("- TensorRT-LLM: from NVIDIA, highly optimized for GPUs "), article_link("https://nvidia.github.io/TensorRT-LLM/overview.html")
- text("- llama.cpp: C++ only, supports CPU inference, runs locally "), link(title="GitHub", url="https://github.com/ggml-org/llama.cpp")
+ bilingual_text("Open-source packages:", '开源软件包:')
+ bilingual_text("- vLLM: from Berkeley, pioneered PagedAttention, popular and good default ", '- vLLM: from Berkeley, pioneered Paged注意力, popular and good default'), link(title="GitHub", url="https://github.com/vllm-project/vllm")
+ bilingual_text("- SGLang: from Berkeley, pioneered RadixAttention, good for agentic workloads ", '- SGLang: from Berkeley, pioneered Radix注意力, good for 智能体ic 工作负载s'), link(title="project", url="https://sgl-project.github.io/")
+ bilingual_text("- TensorRT-LLM: from NVIDIA, highly optimized for GPUs ", '- 说明:TensorRT-LLM: from NVIDIA, highly optimized for GPUs'), article_link("https://nvidia.github.io/TensorRT-LLM/overview.html")
+ bilingual_text("- llama.cpp: C++ only, supports CPU inference, runs locally ", '- llama.cpp: C++ only, supports CPU 推理, runs locally'), link(title="GitHub", url="https://github.com/ggml-org/llama.cpp")
- text("Inference is huge. Important to make it fast.")
+ bilingual_text("Inference is huge. Important to make it fast.", '推理规模巨大,让它变快非常重要。')
- text("What does \"fast\" mean (metrics)?")
- text("- Time-to-first-token (TTFT): how long user waits before any generation happens (for interactive applications)")
- text("- Latency (seconds/token): how fast tokens appear for *one* query (for interactive applications)")
- text("- Throughput (tokens/second): how fast tokens appear for *many* queries (for batch processing)")
+ bilingual_text("What does \"fast\" mean (metrics)?", '“快”意味着什么(指标)?')
+ bilingual_text("- Time-to-first-token (TTFT): how long user waits before any generation happens (for interactive applications)", '- 首 token 时间 (TTFT): how long user waits before any 生成 happens (for interactive applications)')
+ bilingual_text("- Latency (seconds/token): how fast tokens appear for *one* query (for interactive applications)", '- 延迟 (seconds/token): how fast token appear for one query (for interactive applications)')
+ bilingual_text("- Throughput (tokens/second): how fast tokens appear for *many* queries (for batch processing)", '- 吞吐量 (token/second): how fast token appear for many queries (for 批次 processing)')
- text("What governs efficiency?")
- text("- Training (supervised): you see all tokens, can parallelize over sequence (matmul in Transformer)")
- text("- Inference: you have to generate sequentially, can't parallelize over generation, so harder to fully utilize compute")
+ bilingual_text("What governs efficiency?", '什么决定效率?')
+ bilingual_text("- Training (supervised): you see all tokens, can parallelize over sequence (matmul in Transformer)", '- 训练 (supervised): you see all token, can parallelize over 序列 (matmul in Transformer)')
+ bilingual_text("- Inference: you have to generate sequentially, can't parallelize over generation, so harder to fully utilize compute", "- 推理: you have to generate sequentially, can't parallelize over 生成, so harder to fully utilize 计算量")
def review_transformer():
link(scaling_book_transformers)
- text("Notation (similar to einops):")
- text("- Symbols denote dimensions (and their length): B (batch), T (sequence), D (model dim), H (head dim)")
- text("- Example: BTD x DH → BTH")
- text("- Contracting (red) dimensions appear in both operands and disappear from result")
- text("- Regular (black) dimensions appear in one operand and stay in result")
- text("- Example: BD x BD → B")
- text("- Batching (blue) dimensions appear in both operands and stay in result")
+ bilingual_text("Notation (similar to einops):", '记号(类似 einops):')
+ bilingual_text("- Symbols denote dimensions (and their length): B (batch), T (sequence), D (model dim), H (head dim)", '- Symbols denote dimensions (and their length): B (批次), T (序列), D (模型 dim), H (head dim)')
+ bilingual_text("- Example: BTD x DH → BTH", '- 示例:BTD x DH → BTH')
+ bilingual_text("- Contracting (red) dimensions appear in both operands and disappear from result", '- 说明:Contracting (red) dimensions appear in both operands and disappear from result')
+ bilingual_text("- Regular (black) dimensions appear in one operand and stay in result", '- 说明:Regular (black) dimensions appear in one operand and stay in result')
+ bilingual_text("- Example: BD x BD → B", '- 示例:BD x BD → B')
+ bilingual_text("- Batching (blue) dimensions appear in both operands and stay in result", '- 批处理 (blue) dimensions appear in both operands and stay in result')
image("https://jax-ml.github.io/scaling-book/assets/img/transformer-diagram.png", width=800)
- text("Conventions:")
- text("- F = 4 D (MLP up-projects into 4x the model dimension)")
- text("- D = N H (model dimension split across N heads)")
- text("- N = K G (for GQA, number of heads split across K groups)")
- text("- S = T (during training, condition on S input tokens to predict T output tokens)")
+ bilingual_text("Conventions:", '约定:')
+ bilingual_text("- F = 4 D (MLP up-projects into 4x the model dimension)", '- F = 4 D (MLP up-projects into 4x the 模型 dimension)')
+ bilingual_text("- D = N H (model dimension split across N heads)", '- D = N H (模型 dimension split across N heads)')
+ bilingual_text("- N = K G (for GQA, number of heads split across K groups)", '- 说明:N = K G (for GQA, number of heads split across K groups)')
+ bilingual_text("- S = T (during training, condition on S input tokens to predict T output tokens)", '- S = T (during 训练, condition on S input token to predict T output token)')
def review_of_arithmetic_intensity():
- text("Setup: multiply X (B x D) and W (D x F) matrix")
- text("Intuition: B is batch size, D is hidden dimension, F is up-projection dimension in MLP")
+ bilingual_text("Setup: multiply X (B x D) and W (D x F) matrix", '设置:将矩阵 X (B x D) 与 W (D x F) 相乘。')
+ bilingual_text("Intuition: B is batch size, D is hidden dimension, F is up-projection dimension in MLP", '直觉:B 是批大小,D 是隐藏维度,F 是 MLP 中上投影维度。')
- text("Let's do FLOPs and memory read/write accounting for the matrix multiplication (X * W).")
+ bilingual_text("Let's do FLOPs and memory read/write accounting for the matrix multiplication (X * W).", '让我们核算矩阵乘法(X * W)的 FLOPs 和内存读写。')
flops = 0
bytes_transferred = 0
# Perform the operation
- text("1. Read X (B x D) from HBM")
+ bilingual_text("1. Read X (B x D) from HBM", '1. 说明:Read X (B x D) from HBM')
bytes_transferred += 2*B*D # 2 bytes for bf16
- text("2. Read W (D x F) from HBM")
+ bilingual_text("2. Read W (D x F) from HBM", '2. 说明:Read W (D x F) from HBM')
bytes_transferred += 2*D*F
- text("3. Compute Y = X (B x D) @ W (D x F)")
+ bilingual_text("3. Compute Y = X (B x D) @ W (D x F)", '3. 计算量 Y = X (B x D) @ W (D x F)')
flops += 2*B*D*F
- text("4. Write Y (B x F) to HBM")
+ bilingual_text("4. Write Y (B x F) to HBM", '4. 说明:Write Y (B x F) to HBM')
bytes_transferred += 2*B*F
assert flops == 2*B*D*F
assert bytes_transferred == 2*B*D + 2*D*F + 2*B*F
- text("Recall that **arithmetic intensity** is how much compute we do per byte transferred (want to be high).")
+ bilingual_text("Recall that **arithmetic intensity** is how much compute we do per byte transferred (want to be high).", '回忆:**算术强度**表示每传输 1 字节完成多少计算(越高越好)。')
intensity = (flops / bytes_transferred).simplify() # @inspect intensity
- text("Assuming B is much less than D and F, then we can simplify:")
+ bilingual_text("Assuming B is much less than D and F, then we can simplify:", '假设 B 远小于 D 和 F,则可以简化:')
intensity = intensity.subs(D, c*B).subs(F, c*B).limit(c, oo).simplify() # @inspect intensity
assert intensity == B
- text("Accelerator intensity of H100:")
+ bilingual_text("Accelerator intensity of H100:", 'H100 的加速器强度:')
flops_per_second = 989e12
memory_bandwidth = 3.35e12
accelerator_intensity = flops_per_second / memory_bandwidth # @inspect accelerator_intensity
assert round(accelerator_intensity) == 295
- text("If computation intensity > accelerator intensity, **compute-bound** (good)")
- text("If computation intensity < accelerator intensity, **memory-bound** (bad)")
- text("Conclusion: compute-bound iff B > 295")
+ bilingual_text("If computation intensity > accelerator intensity, **compute-bound** (good)", '如果计算强度 > 加速器强度,则**受计算限制**(好)。')
+ bilingual_text("If computation intensity < accelerator intensity, **memory-bound** (bad)", '如果计算强度 < 加速器强度,则**受内存带宽限制**(坏)。')
+ bilingual_text("Conclusion: compute-bound iff B > 295", '结论:当且仅当 B > 295 时受计算限制。')
- text("Extreme case (B = 1, corresponding to matrix-vector product):")
- text("- Arithmetic intensity: 1")
- text("- Memory-bound (read D x F matrix, perform only 2 D F FLOPs)")
- text("- This is basically what happens with inference...")
+ bilingual_text("Extreme case (B = 1, corresponding to matrix-vector product):", '极端情况(B = 1,对应矩阵-向量乘法):')
+ bilingual_text("- Arithmetic intensity: 1", '- 算术强度: 1')
+ bilingual_text("- Memory-bound (read D x F matrix, perform only 2 D F FLOPs)", '- 受内存带宽限制 (read D x F matrix, perform only 2 D F FLOPs)')
+ bilingual_text("- This is basically what happens with inference...", '- This is basically what happens with 推理...')
def arithmetic_intensity_of_inference():
link(scaling_book_inference)
image("https://jax-ml.github.io/scaling-book/assets/img/naive-inference-1400.webp", width=800)
- text("Naive inference: to generate each token, feed history into Transformer")
- text("Complexity: generating T tokens requires O(T^3) FLOPs (one feedforward pass is O(T^2))")
+ bilingual_text("Naive inference: to generate each token, feed history into Transformer", '朴素推理:为了生成每个 token,把历史输入 Transformer。')
+ bilingual_text("Complexity: generating T tokens requires O(T^3) FLOPs (one feedforward pass is O(T^2))", '说明:Complexity: generating T tokens requires O(T^3) FLOPs (one feedforward pass is O(T^2))')
- text("Observation: a lot of the work can be shared across prefixes")
- text("Solution: store **KV cache** in HBM")
+ bilingual_text("Observation: a lot of the work can be shared across prefixes", '观察:许多工作可以在前缀之间共享。')
+ bilingual_text("Solution: store **KV cache** in HBM", '解决方案:在 HBM 中存储 **KV cache**。')
image("https://jax-ml.github.io/scaling-book/assets/img/cached-inference-1400.webp", width=800)
- text("KV cache: for every sequence (B), token (S), layer (L), head (K), store an H-dimensional vector")
+ bilingual_text("KV cache: for every sequence (B), token (S), layer (L), head (K), store an H-dimensional vector", 'KV cache: for every 序列 (B), token (S), layer (L), head (K), store an H-dimensional vector')
- text("Two stages of inference:")
- text("1. **Prefill**: given a prompt, encode into vectors (parallelizable like in training)")
- text("2. **Generation**: generate new response tokens (sequential)")
+ bilingual_text("Two stages of inference:", '推理的两个阶段:')
+ bilingual_text("1. **Prefill**: given a prompt, encode into vectors (parallelizable like in training)", '1. 预填充: given a 提示, encode into vectors (parallelizable like in 训练)')
+ bilingual_text("2. **Generation**: generate new response tokens (sequential)", '2. 生成: generate new 回答 token (sequential)')
- text("Let's compute the FLOPs and memory IO for both the MLP and attention layers.")
- text("S is the number of tokens we're conditioning on, T is the number of tokens we're generating.")
- text("Later, we'll specialize to prefill (T = S) and generation (T = 1).")
+ bilingual_text("Let's compute the FLOPs and memory IO for both the MLP and attention layers.", "Let's 计算量 the FLOPs and 内存 IO for both the MLP and 注意力 layers.")
+ bilingual_text("S is the number of tokens we're conditioning on, T is the number of tokens we're generating.", "说明:S is the number of tokens we're conditioning on, T is the number of tokens we're generating.")
+ bilingual_text("Later, we'll specialize to prefill (T = S) and generation (T = 1).", "Later, we'll specialize to 预填充 (T = S) and 生成 (T = 1).")
- text("### MLP layers (only looking at the matrix multiplications)")
+ bilingual_text("### MLP layers (only looking at the matrix multiplications)", '### MLP 层(只看矩阵乘法)')
flops = 0
bytes_transferred = 0
# Perform the operation
- text("1. Read X (B x T x D) from HBM")
+ bilingual_text("1. Read X (B x T x D) from HBM", '1. 说明:Read X (B x T x D) from HBM')
bytes_transferred += 2*B*T*D
- text("2. Read Wup (D x F), Wgate (D x F), Wdown (F x D) from HBM")
+ bilingual_text("2. Read Wup (D x F), Wgate (D x F), Wdown (F x D) from HBM", '2. 说明:Read Wup (D x F), Wgate (D x F), Wdown (F x D) from HBM')
bytes_transferred += 3 * 2*D*F
- text("3. Compute U = X (B x T x D) @ Wup (D x F)")
+ bilingual_text("3. Compute U = X (B x T x D) @ Wup (D x F)", '3. 计算量 U = X (B x T x D) @ Wup (D x F)')
flops += 2*B*T*D*F
- text("4. Write U (B x T x F) to HBM")
+ bilingual_text("4. Write U (B x T x F) to HBM", '4. 说明:Write U (B x T x F) to HBM')
bytes_transferred += 2*B*T*F
- text("5. Compute G = X (B x T x D) @ Wgate (D x F)")
+ bilingual_text("5. Compute G = X (B x T x D) @ Wgate (D x F)", '5. 计算量 G = X (B x T x D) @ Wgate (D x F)')
flops += 2*B*T*D*F
- text("6. Write G (B x T x F) to HBM")
+ bilingual_text("6. Write G (B x T x F) to HBM", '6. 说明:Write G (B x T x F) to HBM')
bytes_transferred += 2*B*T*F
- text("7. Compute Y = GeLU(G)*U (B x T x F) @ Wdown (F x D)")
+ bilingual_text("7. Compute Y = GeLU(G)*U (B x T x F) @ Wdown (F x D)", '7. 计算量 Y = GeLU(G)U (B x T x F) @ Wdown (F x D)')
flops += 2*B*T*D*F
- text("8. Write Y (B x T x D) to HBM")
+ bilingual_text("8. Write Y (B x T x D) to HBM", '8. 说明:Write Y (B x T x D) to HBM')
bytes_transferred += 2*B*T*D
assert flops == 6*B*T*D*F
@@ -206,30 +206,30 @@ def arithmetic_intensity_of_inference():
# Compute the arithmetic intensity
intensity = (flops / bytes_transferred).simplify() # @inspect intensity
- text("Assume that B*T is much smaller than D and F.")
+ bilingual_text("Assume that B*T is much smaller than D and F.", '说明:Assume that BT is much smaller than D and F.')
intensity = intensity.subs(D, c*B*T).subs(F, c*B*T).limit(c, oo).simplify() # @inspect intensity
assert intensity == B*T
- text("For the two stages:")
- text("1. Prefill: easy to make compute-bound (good) by making `B*T` large enough (large batches, long sequences)")
- text("2. Generation: two problems")
- text("- Generating one token at a time (T = 1)")
- text("- B is number of concurrent requests, unpredictable for interactive applications")
+ bilingual_text("For the two stages:", '对于两个阶段:')
+ bilingual_text("1. Prefill: easy to make compute-bound (good) by making `B*T` large enough (large batches, long sequences)", '1. 预填充: easy to make 受计算限制 (good) by making BT large enough (large 批次es, long 序列)')
+ bilingual_text("2. Generation: two problems", '2. 生成: two problems')
+ bilingual_text("- Generating one token at a time (T = 1)", '- 说明:Generating one token at a time (T = 1)')
+ bilingual_text("- B is number of concurrent requests, unpredictable for interactive applications", '- B is number of concurrent 请求, unpredictable for interactive applications')
- text("### Attention layers (focusing on the matrix multiplications with FlashAttention)")
- text("- S is number of previous tokens (already generated)")
- text("- T is number of next tokens (to generate logits for)")
+ bilingual_text("### Attention layers (focusing on the matrix multiplications with FlashAttention)", '### 注意力层(聚焦使用 FlashAttention 的矩阵乘法)')
+ bilingual_text("- S is number of previous tokens (already generated)", '- 说明:S is number of previous tokens (already generated)')
+ bilingual_text("- T is number of next tokens (to generate logits for)", '- 说明:T is number of next tokens (to generate logits for)')
flops = 0
bytes_transferred = 0
# Perform the operation
- text("1. Read Q (B x T x D), K (B x S x D), V (B x S x D) from HBM")
+ bilingual_text("1. Read Q (B x T x D), K (B x S x D), V (B x S x D) from HBM", '1. 说明:Read Q (B x T x D), K (B x S x D), V (B x S x D) from HBM')
bytes_transferred += 2*B*T*D + 2*B*S*D + 2*B*S*D
- text("2. Compute A = Q (B x T x D) @ K (B x S x D)")
+ bilingual_text("2. Compute A = Q (B x T x D) @ K (B x S x D)", '2. 计算量 A = Q (B x T x D) @ K (B x S x D)')
flops += 2*B*S*T*D
- text("3. Compute Y = softmax(A) (B x S x T x K x G) @ V (B x S x K x H)")
+ bilingual_text("3. Compute Y = softmax(A) (B x S x T x K x G) @ V (B x S x K x H)", '3. 计算量 Y = softmax(A) (B x S x T x K x G) @ V (B x S x K x H)')
flops += 2*B*S*T*D
- text("4. Write Y (B x T x D) to HBM")
+ bilingual_text("4. Write Y (B x T x D) to HBM", '4. 说明:Write Y (B x T x D) to HBM')
bytes_transferred += 2*B*T*D
assert flops == 4*B*S*T*D
@@ -239,25 +239,25 @@ def arithmetic_intensity_of_inference():
intensity = (flops / bytes_transferred).simplify() # @inspect intensity
assert intensity == S*T / (S + T)
- text("For the two stages:")
- text("1. Prefill: T = S")
+ bilingual_text("For the two stages:", '对于两个阶段:')
+ bilingual_text("1. Prefill: T = S", '1. 预填充: T = S')
prefill_intensity = intensity.subs(T, S).simplify() # @inspect prefill_intensity
assert prefill_intensity == S/2 # Good!
- text("2. Generation: T = 1")
+ bilingual_text("2. Generation: T = 1", '2. 生成: T = 1')
generate_intensity = intensity.subs(T, 1).simplify() # @inspect generate_intensity
assert generate_intensity < 1 # Bad!
- text("Unlike MLPs, no dependence on B, so batching doesn't help!")
- text("Why?")
- text("- In MLP layers, every sequence hits the same MLP weights (Wup, Wgate, Wdown don't depend on B)")
- text("- In attention layers, every sequence has its own KV cache vectors (Q, K, V all depend on B)")
+ bilingual_text("Unlike MLPs, no dependence on B, so batching doesn't help!", "Unlike MLPs, no dependence on B, so 批处理 doesn't help!")
+ bilingual_text("Why?", '说明:Why?')
+ bilingual_text("- In MLP layers, every sequence hits the same MLP weights (Wup, Wgate, Wdown don't depend on B)", "- In MLP layers, every 序列 hits the same MLP weights (Wup, Wgate, Wdown don't depend on B)")
+ bilingual_text("- In attention layers, every sequence has its own KV cache vectors (Q, K, V all depend on B)", '- In 注意力 layers, every 序列 has its own KV cache vectors (Q, K, V all depend on B)')
- text("Summary:")
- text("- Prefill is compute-bound, generation is memory-bound")
- text("- Prefill MLP intensity: `B*S`")
- text("- Prefill attention intensity: `S/2`")
- text("- Generation MLP intensity: `B` (requires concurrent requests)")
- text("- Generation attention intensity: `<1` (impossible to improve)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Prefill is compute-bound, generation is memory-bound", '- 预填充 is 受计算限制, 生成 is 受内存带宽限制')
+ bilingual_text("- Prefill MLP intensity: `B*S`", '- 预填充 MLP intensity: BS')
+ bilingual_text("- Prefill attention intensity: `S/2`", '- 预填充 注意力 intensity: S/2')
+ bilingual_text("- Generation MLP intensity: `B` (requires concurrent requests)", '- 生成 MLP intensity: B (requires concurrent 请求)')
+ bilingual_text("- Generation attention intensity: `<1` (impossible to improve)", '- 生成 注意力 intensity: <1 (impossible to improve)')
@dataclass(frozen=True)
@@ -330,11 +330,11 @@ def llama2_13b_config(args={}):
def throughput_and_latency():
- text("So we have shown that inference is memory-bound.")
- text("Let us now compute the theoretical maximum latency and throughput of a single request.")
- text("Assumption: can overlap compute and communication perfectly and ignore overhead.")
+ bilingual_text("So we have shown that inference is memory-bound.", '因此我们已经说明,推理受内存带宽限制。')
+ bilingual_text("Let us now compute the theoretical maximum latency and throughput of a single request.", '现在计算单个请求的理论最大延迟和吞吐量。')
+ bilingual_text("Assumption: can overlap compute and communication perfectly and ignore overhead.", '假设:计算和通信可以完美重叠,并忽略开销。')
- text("Instantiate latency and throughput for Llama 2 13B on an H100:")
+ bilingual_text("Instantiate latency and throughput for Llama 2 13B on an H100:", 'Instantiate 延迟 and 吞吐量 for Llama 2 13B on an H100:')
config = llama2_13b_config()
stats = compute_transformer_performance_stats(config)
@@ -343,49 +343,49 @@ def throughput_and_latency():
# Batch size 64
b64 = stats.substitute(B, 64) # @inspect b64 @stepover
- text("Result: worse latency, better throughput")
+ bilingual_text("Result: worse latency, better throughput", '结果:worse latency, better throughput')
# Batch size 256
b256 = stats.substitute(B, 256) # @inspect b256 @stepover
- text("Result: even worse latency, even better throughput")
+ bilingual_text("Result: even worse latency, even better throughput", '结果:even worse latency, even better throughput')
h100_memory = 80e9 # H100 memory in bytes
assert b256.memory > h100_memory # Doesn't fit in memory!
- text("Result: doesn't fit into memory and throughput gains are diminishing too...")
+ bilingual_text("Result: doesn't fit into memory and throughput gains are diminishing too...", "结果:doesn't fit into memory and throughput gains are diminishing too...")
- text("What increasing batch size does:")
- text("- Worsens latency because larger KV cache (O(B) size) to read/write")
- text("- Improves throughput because amortizes the cost of reading parameters")
+ bilingual_text("What increasing batch size does:", '增大批大小的作用:')
+ bilingual_text("- Worsens latency because larger KV cache (O(B) size) to read/write", '- Worsens 延迟 because larger KV cache (O(B) size) to read/write')
+ bilingual_text("- Improves throughput because amortizes the cost of reading parameters", '- Improves 吞吐量 because amortizes the cost of reading parameters')
- text("**Tradeoff** between latency and throughput:")
- text("1. Smaller batch sizes yield better latency but worse throughput")
- text("2. Larger batch sizes yield better throughput but worse latency")
+ bilingual_text("**Tradeoff** between latency and throughput:", '延迟和吞吐量之间的**权衡**:')
+ bilingual_text("1. Smaller batch sizes yield better latency but worse throughput", '1. Smaller 批次 sizes yield better 延迟 but worse 吞吐量')
+ bilingual_text("2. Larger batch sizes yield better throughput but worse latency", '2. Larger 批次 sizes yield better 吞吐量 but worse 延迟')
- text("Easy parallelism: if you launch M copies of the model, latency is the same, throughput increases by M!")
- text("Harder parallelism: shard the model and the KV cache "), link(scaling_book_inference)
+ bilingual_text("Easy parallelism: if you launch M copies of the model, latency is the same, throughput increases by M!", 'Easy parallelism: if you launch M copies of the 模型, 延迟 is the same, 吞吐量 increases by M!')
+ bilingual_text("Harder parallelism: shard the model and the KV cache ", 'Harder parallelism: shard the 模型 and the KV cache'), link(scaling_book_inference)
- text("Note: time-to-first-token (TTFT) is essentially a function of prefill time")
- text("Use smaller batch sizes during prefill for faster TTFT")
- text("Use larger batch sizes during generation to improve throughput")
+ bilingual_text("Note: time-to-first-token (TTFT) is essentially a function of prefill time", 'Note: time-to-first-token (TTFT) is essentially a function of 预填充 time')
+ bilingual_text("Use smaller batch sizes during prefill for faster TTFT", 'Use smaller 批次 sizes during 预填充 for faster TTFT')
+ bilingual_text("Use larger batch sizes during generation to improve throughput", 'Use larger 批次 sizes during 生成 to improve 吞吐量')
def reduce_kv_cache_size():
- text("Recall that memory is the bottleneck for inference.")
- text("So let's try to reduce the size of the KV cache")
- text("...but make sure we don't lose too much accuracy.")
+ bilingual_text("Recall that memory is the bottleneck for inference.", '回忆:内存是推理的瓶颈。')
+ bilingual_text("So let's try to reduce the size of the KV cache", '因此我们尝试减小 KV cache 的大小。')
+ bilingual_text("...but make sure we don't lose too much accuracy.", '……但要确保不要损失太多准确率。')
- text("### Grouped-query attention (GQA) "), link(gqa_2023)
+ bilingual_text("### Grouped-query attention (GQA) ", '### Grouped-query 注意力 (GQA)'), link(gqa_2023)
image("https://jax-ml.github.io/scaling-book/assets/img/gmqa.png", width=800)
- text("Idea: N query heads, but only K key and value heads, each interacting with N/K query heads")
- text("Multi-headed attention (MHA): K=N")
- text("Multi-query attention (MQA): K=1")
- text("Group-query attention (GQA): K is somewhere in between")
+ bilingual_text("Idea: N query heads, but only K key and value heads, each interacting with N/K query heads", '思想:N query heads, but only K key and value heads, each interacting with N/K query heads')
+ bilingual_text("Multi-headed attention (MHA): K=N", 'Multi-headed 注意力 (MHA): K=N')
+ bilingual_text("Multi-query attention (MQA): K=1", 'Multi-query 注意力 (MQA): K=1')
+ bilingual_text("Group-query attention (GQA): K is somewhere in between", 'Group-query 注意力 (GQA): K is somewhere in between')
- text("Latency/throughput improves: "), link(gqa_2023)
+ bilingual_text("Latency/throughput improves: ", '延迟/吞吐量 improves:'), link(gqa_2023)
image("images/gqa-speed.png", width=500)
- text("Why does GQA improve latency and throughput?")
- text("GQA reduces the KV cache by a factor of N/K.")
- text("Reminder: reducing memory usage leads to speedup (since we're memory-bound).")
+ bilingual_text("Why does GQA improve latency and throughput?", '为什么 GQA 能改善延迟和吞吐量?')
+ bilingual_text("GQA reduces the KV cache by a factor of N/K.", 'GQA 将 KV cache 缩小 N/K 倍。')
+ bilingual_text("Reminder: reducing memory usage leads to speedup (since we're memory-bound).", '提醒:减少内存使用会带来加速(因为我们受内存带宽限制)。')
# Original Llama 2 13B (MHA)
config = llama2_13b_config({K: 40, B: 64}) # @stepover
@@ -394,62 +394,62 @@ def reduce_kv_cache_size():
# GQA with 1:5 ratio (K:N)
config = llama2_13b_config({K: 8, B: 64}) # Use GQA with 1:5 ratio @stepover
k8_b64 = compute_transformer_performance_stats(config) # @inspect k8_b64 @stepover
- text("Result: Worse latency, but better throughput (and it fits in memory now!)")
+ bilingual_text("Result: Worse latency, but better throughput (and it fits in memory now!)", '结果:Worse latency, but better throughput (and it fits in memory now!)')
# Now we can increase the batch size
config = llama2_13b_config({K: 8, B: 256}) # Increase batch size @stepover
k8_b256 = compute_transformer_performance_stats(config) # @inspect k8_b256 @stepover
- text("Result: Worse latency, but better throughput (and still fits in memory!)")
+ bilingual_text("Result: Worse latency, but better throughput (and still fits in memory!)", '结果:Worse latency, but better throughput (and still fits in memory!)')
- text("Check that accuracy doesn't drop: "), link(gqa_2023)
+ bilingual_text("Check that accuracy doesn't drop: ", "Check that 准确率 doesn't drop:"), link(gqa_2023)
image("images/gqa-accuracy.png", width=800)
- text("### Multi-head latent attention (MLA) "), link(mla_2024)
+ bilingual_text("### Multi-head latent attention (MLA) ", '### Multi-head latent 注意力 (MLA)'), link(mla_2024)
image("images/mla-schema.png", width=800)
- text("Normal attention: KV cache consists of K = W_K h, V = W_V h (N*H dimensions)")
- text("MLA: store compressed vector c = W_c h (C dimensions), project up to K = W_K c, V = W_V c when needed")
- text("DeepSeek v2: reduce N*H = 16384 to C = 512")
- text("Wrinkle: MLA is not compatible with RoPE, so need to add additional 64 dimensions for RoPE, so 512 + 64 = 576 total dimensions")
- text("Latency/throughput improvements follow similarly from the KV cache reduction as argued earlier")
-
- text("Let's now check the accuracy.")
- text("First, MHA is better than GQA (though more expensive) [Table 8] "), link(mla_2024)
+ bilingual_text("Normal attention: KV cache consists of K = W_K h, V = W_V h (N*H dimensions)", 'Normal 注意力: KV cache consists of K = W_K h, V = W_V h (NH dimensions)')
+ bilingual_text("MLA: store compressed vector c = W_c h (C dimensions), project up to K = W_K c, V = W_V c when needed", '说明:MLA: store compressed vector c = W_c h (C dimensions), project up to K = W_K c, V = W_V c when needed')
+ bilingual_text("DeepSeek v2: reduce N*H = 16384 to C = 512", '说明:DeepSeek v2: reduce NH = 16384 to C = 512')
+ bilingual_text("Wrinkle: MLA is not compatible with RoPE, so need to add additional 64 dimensions for RoPE, so 512 + 64 = 576 total dimensions", '说明:Wrinkle: MLA is not compatible with RoPE, so need to add additional 64 dimensions for RoPE, so 512 + 64 = 576 total dimensions')
+ bilingual_text("Latency/throughput improvements follow similarly from the KV cache reduction as argued earlier", '延迟/吞吐量 improvements follow similarly from the KV cache reduction as argued earlier')
+
+ bilingual_text("Let's now check the accuracy.", '现在检查准确率。')
+ bilingual_text("First, MHA is better than GQA (though more expensive) [Table 8] ", '说明:First, MHA is better than GQA (though more expensive) [Table 8]'), link(mla_2024)
image("images/mla-accuracy.png", width=800)
- text("Second, MLA is even a bit better than MHA (and much cheaper) [Table 9] "), link(mla_2024)
+ bilingual_text("Second, MLA is even a bit better than MHA (and much cheaper) [Table 9] ", '说明:Second, MLA is even a bit better than MHA (and much cheaper) [Table 9]'), link(mla_2024)
image("images/mla-accuracy2.png", width=800)
- text("### Cross-layer attention (CLA) "), link("https://arxiv.org/abs/2405.12981")
+ bilingual_text("### Cross-layer attention (CLA) ", '### Cross-layer 注意力 (CLA)'), link("https://arxiv.org/abs/2405.12981")
image("images/cla-diagram.png", width=500)
- text("Idea: share KVs across **layers** (just as GQA shares KVs across heads)")
- text("Empirically improves the pareto frontier of accuracy and KV cache size (latency and throughput)")
+ bilingual_text("Idea: share KVs across **layers** (just as GQA shares KVs across heads)", '思想:share KVs across layers (just as GQA shares KVs across heads)')
+ bilingual_text("Empirically improves the pareto frontier of accuracy and KV cache size (latency and throughput)", 'Empirically improves the pareto frontier of 准确率 and KV cache size (延迟 and 吞吐量)')
image("images/cla-results.png", width=700)
- text("### Local (sliding window) attention "), link(longformer_2020), link(sparse_transformer_2019), link(mistral_7b_2023)
+ bilingual_text("### Local (sliding window) attention ", '### Local (sliding window) 注意力'), link(longformer_2020), link(sparse_transformer_2019), link(mistral_7b_2023)
image("images/longformer-attention.png", width=800)
- text("Idea: just look at the local context, which is most relevant for modeling")
- text("Effective context scales linearly with the number of layers")
- text("KV cache is independent of sequence length!")
- text("Problem: this can still hurt accuracy")
- text("Solution: interleave local attention with global attention (hybrid layers)")
-
- text("### DeepSeek v4 attention")
- text("- Supports 1M context length "), link(deepseek_v4_2026)
+ bilingual_text("Idea: just look at the local context, which is most relevant for modeling", '思想:just look at the local context, which is most relevant for modeling')
+ bilingual_text("Effective context scales linearly with the number of layers", '说明:Effective context scales linearly with the number of layers')
+ bilingual_text("KV cache is independent of sequence length!", 'KV cache is independent of 序列 length!')
+ bilingual_text("Problem: this can still hurt accuracy", 'Problem: this can still hurt 准确率')
+ bilingual_text("Solution: interleave local attention with global attention (hybrid layers)", '解决方案:interleave local attention with global attention (hybrid layers)')
+
+ bilingual_text("### DeepSeek v4 attention", '### DeepSeek v4 注意力')
+ bilingual_text("- Supports 1M context length ", '- 说明:Supports 1M context length'), link(deepseek_v4_2026)
image("images/deepseek-v4-attention.png", width=800)
- text("- Compressed Sparse Attention (CSA): compresses every m tokens into 1")
- text("- DeepSeek Sparse Attention (DSA): selects the top k")
- text("- Heavily Compressed Attention (HCA): compresses even more")
+ bilingual_text("- Compressed Sparse Attention (CSA): compresses every m tokens into 1", '- Compressed Sparse 注意力 (CSA): compresses every m token into 1')
+ bilingual_text("- DeepSeek Sparse Attention (DSA): selects the top k", '- DeepSeek Sparse 注意力 (DSA): selects the top k')
+ bilingual_text("- Heavily Compressed Attention (HCA): compresses even more", '- Heavily Compressed 注意力 (HCA): compresses even more')
- text("Summary:")
- text("- Goal: reduce the KV cache size (since inference is memory-bound) without hurting accuracy")
- text("- Lower-dimensional KV cache (GQA, MLA, CLA)")
- text("- Local attention (truncates the KV cache) on some of the layers")
- text("- Other ideas: linear attention / state-space-models (Mamba 2, GatedDeltaNet), diffusion models")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Goal: reduce the KV cache size (since inference is memory-bound) without hurting accuracy", '- Goal: reduce the KV cache size (since 推理 is 受内存带宽限制) without hurting 准确率')
+ bilingual_text("- Lower-dimensional KV cache (GQA, MLA, CLA)", '- 说明:Lower-dimensional KV cache (GQA, MLA, CLA)')
+ bilingual_text("- Local attention (truncates the KV cache) on some of the layers", '- Local 注意力 (truncates the KV cache) on some of the layers')
+ bilingual_text("- Other ideas: linear attention / state-space-models (Mamba 2, GatedDeltaNet), diffusion models", '- Other ideas: linear 注意力 / state-space-模型s (Mamba 2, GatedDeltaNet), diffusion 模型s')
def quantization():
- text("Key idea: reduce the precision of numbers")
- text("Less memory means higher latency/throughput (since inference is memory-bound).")
- text("Of course we have to worry about accuracy...")
+ bilingual_text("Key idea: reduce the precision of numbers", '关键思想:降低数字精度。')
+ bilingual_text("Less memory means higher latency/throughput (since inference is memory-bound).", '内存更少意味着延迟/吞吐量更好(因为推理受内存带宽限制)。')
+ bilingual_text("Of course we have to worry about accuracy...", '当然,我们必须担心准确率……')
# Mechanics
x = 5.2342 # @inspect x
@@ -459,152 +459,152 @@ def quantization():
x_approx = (x_quant - zero_point) * scale # Dequantize @inspect x_approx
image("https://www.datocms-assets.com/104802/1709770809-twitter-post-20.png", width=400), article_link("https://www.baseten.co/blog/fp8-efficient-model-inference-with-8-bit-floating-point-numbers/")
- text("- fp32 (4 bytes): needed for parameters and optimizer states during training")
- text("- bf16 (2 bytes): default for inference")
- text("- fp8 (1 byte) [-240, 240] for e4m3 on H100s: can train if you dare "), link("https://arxiv.org/pdf/2310.18313")
- text("- int8 (1 byte) [-128, 127]: less accurate but cheaper than fp8, but for inference only "), link("https://arxiv.org/pdf/2303.17951")
- text("- int4 (0.5 bytes) [-8, 7]: cheaper, even less accurate "), link("https://arxiv.org/pdf/2303.17951")
+ bilingual_text("- fp32 (4 bytes): needed for parameters and optimizer states during training", '- fp32 (4 bytes): needed for parameters and optimizer states during 训练')
+ bilingual_text("- bf16 (2 bytes): default for inference", '- bf16 (2 bytes): default for 推理')
+ bilingual_text("- fp8 (1 byte) [-240, 240] for e4m3 on H100s: can train if you dare ", '- 说明:fp8 (1 byte) [-240, 240] for e4m3 on H100s: can train if you dare'), link("https://arxiv.org/pdf/2310.18313")
+ bilingual_text("- int8 (1 byte) [-128, 127]: less accurate but cheaper than fp8, but for inference only ", '- int8 (1 byte) [-128, 127]: less accurate but cheaper than fp8, but for 推理 only'), link("https://arxiv.org/pdf/2303.17951")
+ bilingual_text("- int4 (0.5 bytes) [-8, 7]: cheaper, even less accurate ", '- 说明:int4 (0.5 bytes) [-8, 7]: cheaper, even less accurate'), link("https://arxiv.org/pdf/2303.17951")
link(title="Overview of approaches", url="https://apxml.com/posts/llm-quantization-techniques-explained")
- text("Quantization-aware training (QAT)")
- text("- During training, quantize-and-dequantize during the forward pass to simulate quantization errors")
- text("- Pro: weights are trained to work with quantization")
- text("- Con: requires expensive large-scale training")
+ bilingual_text("Quantization-aware training (QAT)", '量化感知训练(QAT)')
+ bilingual_text("- During training, quantize-and-dequantize during the forward pass to simulate quantization errors", '- During 训练, quantize-and-dequantize during the forward pass to simulate 量化 errors')
+ bilingual_text("- Pro: weights are trained to work with quantization", '- Pro: weights are trained to work with 量化')
+ bilingual_text("- Con: requires expensive large-scale training", '- Con: requires expensive large-scale 训练')
- text("Post-training quantization (PTQ):")
- text("- Done after training, so much cheaper")
- text("- Run on sample data to determine scale and zero point for each layer or tensor")
- text("- GPTQ: use Hessian information to update non-quantized weights to account for quantization error "), link("https://arxiv.org/abs/2210.17323")
+ bilingual_text("Post-training quantization (PTQ):", '训练后量化(PTQ):')
+ bilingual_text("- Done after training, so much cheaper", '- Done after 训练, so much cheaper')
+ bilingual_text("- Run on sample data to determine scale and zero point for each layer or tensor", '- Run on sample 数据 to determine scale and zero point for each layer or tensor')
+ bilingual_text("- GPTQ: use Hessian information to update non-quantized weights to account for quantization error ", '- GPTQ: use Hessian information to update non-quantized weights to account for 量化 error'), link("https://arxiv.org/abs/2210.17323")
- text("### Activation-aware quantization (AWQ)")
+ bilingual_text("### Activation-aware quantization (AWQ)", '### 激活感知量化(AWQ)')
link("https://arxiv.org/abs/2306.00978")
- text("- Observation: some activation channels are large")
- text("- Weights that hit those matter more")
- text("- Allocate more precision to those weights")
- text("- Idea: select which weights (0.1-1%) to keep in high precision based on activations")
- text("- fp16 → int3 produces 4x lower memory, 3.2x speedup")
+ bilingual_text("- Observation: some activation channels are large", '- 观察:some activation channels are large')
+ bilingual_text("- Weights that hit those matter more", '- 说明:Weights that hit those matter more')
+ bilingual_text("- Allocate more precision to those weights", '- 说明:Allocate more precision to those weights')
+ bilingual_text("- Idea: select which weights (0.1-1%) to keep in high precision based on activations", '- 思想:select which weights (0.1-1%) to keep in high precision based on activations')
+ bilingual_text("- fp16 → int3 produces 4x lower memory, 3.2x speedup", '- fp16 → int3 produces 4x lower 内存, 3.2x speedup')
image("images/awq-schema.png", width=800)
def model_pruning():
- text("Key idea: just rip out parts of an expensive model to make it cheaper")
- text("...and then fix it up.")
+ bilingual_text("Key idea: just rip out parts of an expensive model to make it cheaper", '关键思想:直接删掉昂贵模型的一部分,使它更便宜。')
+ bilingual_text("...and then fix it up.", '……然后再修复它。')
- text("Paper from NVIDIA "), link("https://arxiv.org/abs/2407.14679")
+ bilingual_text("Paper from NVIDIA ", '说明:Paper from NVIDIA'), link("https://arxiv.org/abs/2407.14679")
image("images/pruning-kd-loop.png", width=600)
- text("Algorithm:")
- text("1. Identify important {layer, head, hidden dimension} on a small calibration dataset (1024 samples)")
- text("2. Remove unimportant layers to get a smaller model")
- text("3. Distill the original model into pruned model")
+ bilingual_text("Algorithm:", '算法:')
+ bilingual_text("1. Identify important {layer, head, hidden dimension} on a small calibration dataset (1024 samples)", '1. Identify important {layer, head, hidden dimension} on a small calibration 数据集 (1024 samples)')
+ bilingual_text("2. Remove unimportant layers to get a smaller model", '2. Remove unimportant layers to get a smaller 模型')
+ bilingual_text("3. Distill the original model into pruned model", '3. Distill the original 模型 into pruned 模型')
- text("Results:")
+ bilingual_text("Results:", '结果:')
image("images/pruning-kd.png", width=500)
# TODO
def speculative_sampling():
- text("Recall the two stages of inference:")
- text("- Prefill: given a sequence, encode tokens in parallel (compute-bound) [note: also gives you probabilities]")
- text("- Generation: generate one token at a time (memory-bound)")
- text("In other words, checking is faster than generation.")
-
- text("Speculative sampling "), link("https://arxiv.org/abs/2211.17192"), link("https://arxiv.org/abs/2302.01318")
- text("- Use a cheaper **draft model** p to guess a few tokens (e.g., 4)")
- text("- Evaluate with target model q (process tokens in parallel), and accept if it looks good")
+ bilingual_text("Recall the two stages of inference:", '回忆推理的两个阶段:')
+ bilingual_text("- Prefill: given a sequence, encode tokens in parallel (compute-bound) [note: also gives you probabilities]", '- 预填充: given a 序列, encode token in parallel (受计算限制) [note: also gives you 概率]')
+ bilingual_text("- Generation: generate one token at a time (memory-bound)", '- 生成: generate one token at a time (受内存带宽限制)')
+ bilingual_text("In other words, checking is faster than generation.", '换句话说,检查比生成更快。')
+
+ bilingual_text("Speculative sampling ", '推测采样'), link("https://arxiv.org/abs/2211.17192"), link("https://arxiv.org/abs/2302.01318")
+ bilingual_text("- Use a cheaper **draft model** p to guess a few tokens (e.g., 4)", '- Use a cheaper draft 模型 p to guess a few token (e.g., 4)')
+ bilingual_text("- Evaluate with target model q (process tokens in parallel), and accept if it looks good", '- Evaluate with target 模型 q (process token in parallel), and accept if it looks good')
link(title="Speculative sampling video", url="https://storage.googleapis.com/gweb-research2023-media/media/SpeculativeDecoding-1-Illustration.mp4")
article_link("https://research.google/blog/looking-back-at-speculative-decoding/")
image("images/speculative-sampling-algorithm.png", width=600)
- text("This is modified rejection sampling with proposal p and target q")
- text("Modification: always generate at least one candidate (rejection sampling will keep looping)")
- text("Key property: guaranteed to be an **exact sample** from the target model!")
-
- text("Proof by example: assume two vocabulary elements {A, B}")
- text("- Target model probabilities: [q(A), q(B)]")
- text("- Draft model probabilities: [p(A), p(B)]")
- text("- Assume p(A) > q(A) [draft model oversamples A].")
- text("- Therefore p(B) < q(B) [draft model undersamples B].")
- text("- Residual probabilities max(q-p, 0): [0, 1]")
- text("Compute the probabilities of speculatively sampling a token:")
- text("- P[sampling A] = p(A) * (q(A) / p(A)) + p(B) * 1 * 0 = q(A)")
- text("- P[sampling B] = p(B) * 1 + p(A) * (1 - q(A) / p(A)) * 1 = q(B)")
+ bilingual_text("This is modified rejection sampling with proposal p and target q", '这是修改过的拒绝采样,其中 p 是提议分布,q 是目标分布。')
+ bilingual_text("Modification: always generate at least one candidate (rejection sampling will keep looping)", '说明:Modification: always generate at least one candidate (rejection sampling will keep looping)')
+ bilingual_text("Key property: guaranteed to be an **exact sample** from the target model!", '关键性质:保证得到目标模型的**精确样本**!')
+
+ bilingual_text("Proof by example: assume two vocabulary elements {A, B}", '通过例子证明:假设词表只有两个元素 {A, B}。')
+ bilingual_text("- Target model probabilities: [q(A), q(B)]", '- Target 模型 概率: [q(A), q(B)]')
+ bilingual_text("- Draft model probabilities: [p(A), p(B)]", '- Draft 模型 概率: [p(A), p(B)]')
+ bilingual_text("- Assume p(A) > q(A) [draft model oversamples A].", '- Assume p(A) > q(A) [draft 模型 oversamples A].')
+ bilingual_text("- Therefore p(B) < q(B) [draft model undersamples B].", '- Therefore p(B) < q(B) [draft 模型 undersamples B].')
+ bilingual_text("- Residual probabilities max(q-p, 0): [0, 1]", '- Residual 概率 max(q-p, 0): [0, 1]')
+ bilingual_text("Compute the probabilities of speculatively sampling a token:", '计算量 the 概率 of speculatively sampling a token:')
+ bilingual_text("- P[sampling A] = p(A) * (q(A) / p(A)) + p(B) * 1 * 0 = q(A)", '- 说明:P[sampling A] = p(A) (q(A) / p(A)) + p(B) 1 0 = q(A)')
+ bilingual_text("- P[sampling B] = p(B) * 1 + p(A) * (1 - q(A) / p(A)) * 1 = q(B)", '- 说明:P[sampling B] = p(B) 1 + p(A) (1 - q(A) / p(A)) 1 = q(B)')
image("images/speculative-sampling-results.png", width=600)
image("images/speculative-sampling-stats.png", width=600)
- text("In practice:")
- text("- Target model has 70B parameters, draft model has 8B parameters")
- text("- Target model has 8B parameters, draft model has 1B parameters")
- text("- Try to make draft model as close to target (distillation)")
+ bilingual_text("In practice:", '实践中:')
+ bilingual_text("- Target model has 70B parameters, draft model has 8B parameters", '- Target 模型 has 70B parameters, draft 模型 has 8B parameters')
+ bilingual_text("- Target model has 8B parameters, draft model has 1B parameters", '- Target 模型 has 8B parameters, draft 模型 has 1B parameters')
+ bilingual_text("- Try to make draft model as close to target (distillation)", '- Try to make draft 模型 as close to target (蒸馏)')
- text("Extensions to improve the draft model:")
- text("- Medusa: draft model generates multiple tokens in parallel "), link("https://arxiv.org/abs/2401.10774")
- text("- EAGLE: draft model takes high-level features from target model "), link("https://arxiv.org/pdf/2401.15077")
+ bilingual_text("Extensions to improve the draft model:", '改进草稿模型的扩展方法:')
+ bilingual_text("- Medusa: draft model generates multiple tokens in parallel ", '- Medusa: draft 模型 generates multiple token in parallel'), link("https://arxiv.org/abs/2401.10774")
+ bilingual_text("- EAGLE: draft model takes high-level features from target model ", '- EAGLE: draft 模型 takes high-level features from target 模型'), link("https://arxiv.org/pdf/2401.15077")
image("images/medusa-eagle.png", width=600)
- text("Summary:")
- text("- Exact sampling from target model (thanks to math)!")
- text("- Exploits asymmetry between checking and generation")
- text("- Lots of room for innovation on the draft model (involves training)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Exact sampling from target model (thanks to math)!", '- Exact sampling from target 模型 (thanks to math)!')
+ bilingual_text("- Exploits asymmetry between checking and generation", '- Exploits asymmetry between checking and 生成')
+ bilingual_text("- Lots of room for innovation on the draft model (involves training)", '- Lots of room for innovation on the draft 模型 (involves 训练)')
def continuous_batching():
link(title="Orca: A Distributed Serving System for Transformer-Based Generative Models", url="https://www.usenix.org/system/files/osdi22-yu.pdf"), link(title="talk", url="https://www.youtube.com/watch?v=Ob9PPLxETYU")
- text("Problem:")
- text("- Training: get a dense block of tokens (batch size x sequence length)")
- text("- Inference: requests arrive and finish at different times, so you have a ragged array")
+ bilingual_text("Problem:", '问题:')
+ bilingual_text("- Training: get a dense block of tokens (batch size x sequence length)", '- 训练: get a dense block of token (批次 size x 序列 length)')
+ bilingual_text("- Inference: requests arrive and finish at different times, so you have a ragged array", '- 推理: 请求 arrive and finish at different times, so you have a ragged array')
image("https://images.ctfassets.net/xjan103pcp94/1LJioEsEdQQpDCxYNWirU6/82b9fbfc5b78b10c1d4508b60e72fdcf/cb_02_diagram-static-batching.png", width=600)
- text("Solution: iteration-level scheduling")
- text("- Decode step by step")
- text("- Add new requests to the batch as they arrive (so don't have to wait until generation completes)")
+ bilingual_text("Solution: iteration-level scheduling", '解决方案:迭代级调度。')
+ bilingual_text("- Decode step by step", '- 说明:Decode step by step')
+ bilingual_text("- Add new requests to the batch as they arrive (so don't have to wait until generation completes)", "- Add new 请求 to the 批次 as they arrive (so don't have to wait until 生成 completes)")
- text("Problem:")
- text("- Batching only works when all sequences have the same dimensionality (right?)")
- text("- But each request might have a different length")
+ bilingual_text("Problem:", '问题:')
+ bilingual_text("- Batching only works when all sequences have the same dimensionality (right?)", '- 批处理 only works when all 序列 have the same dimensionality (right?)')
+ bilingual_text("- But each request might have a different length", '- But each 请求 might have a different length')
- text("Solution: selective batching")
- text("- Training: when all sequences of the same length, operate on a B x S x H tensor")
- text("- But we might have different lengths: [3, H], [9, H], [5, H], etc.")
- text("- Attention computation: process each sequence separately")
- text("- Non-attention computation: concatenate all the sequences together to [3 + 9 + 5, H]")
+ bilingual_text("Solution: selective batching", '解决方案:选择性批处理。')
+ bilingual_text("- Training: when all sequences of the same length, operate on a B x S x H tensor", '- 训练: when all 序列 of the same length, operate on a B x S x H tensor')
+ bilingual_text("- But we might have different lengths: [3, H], [9, H], [5, H], etc.", '- 说明:But we might have different lengths: [3, H], [9, H], [5, H], etc.')
+ bilingual_text("- Attention computation: process each sequence separately", '- 注意力 computation: process each 序列 separately')
+ bilingual_text("- Non-attention computation: concatenate all the sequences together to [3 + 9 + 5, H]", '- Non-注意力 computation: concatenate all the 序列 together to [3 + 9 + 5, H]')
def paged_attention():
- text("Paper that introduced vLLM in addition to PagedAttention "), link("https://arxiv.org/pdf/2309.06180.pdf")
+ bilingual_text("Paper that introduced vLLM in addition to PagedAttention ", 'Paper that introduced vLLM in addition to Paged注意力'), link("https://arxiv.org/pdf/2309.06180.pdf")
- text("Previous status quo:")
- text("- Request comes in")
- text("- Allocate section of KV cache for prompt and response (up to a max length)")
+ bilingual_text("Previous status quo:", '之前的常见做法:')
+ bilingual_text("- Request comes in", '- 请求 comes in')
+ bilingual_text("- Allocate section of KV cache for prompt and response (up to a max length)", '- Allocate section of KV cache for 提示 and 回答 (up to a max length)')
image("images/paged-attention-fragmentation.png", width=800)
- text("Problem: fragmentation (what happens to your hard drive)")
- text("- But this is wasteful since we might generate much fewer tokens (internal fragmentation)!")
- text("- Might be extra unused space between sections (external fragmentation)!")
+ bilingual_text("Problem: fragmentation (what happens to your hard drive)", '说明:Problem: fragmentation (what happens to your hard drive)')
+ bilingual_text("- But this is wasteful since we might generate much fewer tokens (internal fragmentation)!", '- 说明:But this is wasteful since we might generate much fewer tokens (internal fragmentation)!')
+ bilingual_text("- Might be extra unused space between sections (external fragmentation)!", '- 说明:Might be extra unused space between sections (external fragmentation)!')
- text("Solution: PagedAttention (remember operating systems)")
- text("- Divide the KV cache of a sequence into non-contiguous **blocks**")
+ bilingual_text("Solution: PagedAttention (remember operating systems)", '解决方案:PagedAttention(回忆操作系统)。')
+ bilingual_text("- Divide the KV cache of a sequence into non-contiguous **blocks**", '- Divide the KV cache of a 序列 into non-contiguous blocks')
image("images/paged-attention-blocks.png", width=400)
- text("Two requests share the KV caches:")
+ bilingual_text("Two requests share the KV caches:", '两个请求共享 KV cache:')
image("images/paged-attention-logical.png", width=800)
- text("In general, multiple types of sharing KV caches across sequences:")
+ bilingual_text("In general, multiple types of sharing KV caches across sequences:", 'In general, multiple types of sharing KV caches across 序列:')
image("images/paged-attention-sharing.png", width=600)
- text("- Sharing the system prompt")
- text("- Sampling multiple responses per prompt (e.g., for program synthesis)")
+ bilingual_text("- Sharing the system prompt", '- Sharing the system 提示')
+ bilingual_text("- Sampling multiple responses per prompt (e.g., for program synthesis)", '- Sampling multiple 回答 per 提示 (e.g., for program synthesis)')
- text("Solution: share prefixes, copy-on-write at the block level")
+ bilingual_text("Solution: share prefixes, copy-on-write at the block level", '解决方案:share prefixes, copy-on-write at the block level')
image("images/paged-attention-parallel.png", width=600)
- text("Other vLLM optimizations:")
- text("- Kernel to fuse block read and attention (reduce kernel launch overhead)")
- text("- Use latest kernels (FlashAttention, FlashDecoding)")
- text("- Use CUDA graphs to avoid kernel launch overhead")
+ bilingual_text("Other vLLM optimizations:", '其他 vLLM 优化:')
+ bilingual_text("- Kernel to fuse block read and attention (reduce kernel launch overhead)", '- Kernel to fuse block read and 注意力 (reduce kernel launch overhead)')
+ bilingual_text("- Use latest kernels (FlashAttention, FlashDecoding)", '- Use latest kernels (Flash注意力, FlashDecoding)')
+ bilingual_text("- Use CUDA graphs to avoid kernel launch overhead", '- 说明:Use CUDA graphs to avoid kernel launch overhead')
- text("Summary: use ideas from operating systems (paging) to make use of memory for dynamic workloads")
+ bilingual_text("Summary: use ideas from operating systems (paging) to make use of memory for dynamic workloads", '总结:使用操作系统中的思想(分页)来为动态工作负载利用内存。')
if __name__ == "__main__":
diff --git a/lecture_12.py b/lecture_12.py
index c98e21d..c5e6333 100644
--- a/lecture_12.py
+++ b/lecture_12.py
@@ -1,15 +1,15 @@
from edtrace import text, link, image
-from lecture_util import post_link
+from lecture_util import post_link, bilingual_text
from references import mmlu_2021
def main():
- text("## Lecture 12: evaluation")
- text("- So far: we've covered everything for training an LM (architecture, training, systems, scaling).")
- text("- Missing piece: what **data** do you train on?")
- text("- Data shapes model behavior (code? multilingual? DNA?).")
- text("- Before talking about data, need to talk about what behavior we want from a model.")
+ bilingual_text("## Lecture 12: evaluation", '## 第 12 讲:评测')
+ bilingual_text("- So far: we've covered everything for training an LM (architecture, training, systems, scaling).", '- 到目前为止,我们已经覆盖了训练 LM 所需的一切(架构、训练、系统、规模化)。')
+ bilingual_text("- Missing piece: what **data** do you train on?", '- 缺失的一块是:你用什么**数据**训练?')
+ bilingual_text("- Data shapes model behavior (code? multilingual? DNA?).", '- 数据塑造模型行为(代码?多语言?DNA?)。')
+ bilingual_text("- Before talking about data, need to talk about what behavior we want from a model.", '- 在讨论数据之前,需要先讨论我们希望模型具有什么行为。')
- text("**Evaluation**: given a model, how \"**good**\" is it?")
+ bilingual_text("**Evaluation**: given a model, how \"**good**\" is it?", '**评测**:给定一个模型,它到底有多“**好**”?')
what_is_good()
@@ -24,370 +24,370 @@ def main():
validity()
how_to_think_about_evaluation()
- text("Takeaways:")
- text("- There is no one true evaluation; choose the evaluation depending on what you're trying to measure.")
- text("- Clearly state the rules of the game (methods versus models versus agents).")
- text("- Considerations: difficulty, realism, validity.")
+ bilingual_text("Takeaways:", '要点:')
+ bilingual_text("- There is no one true evaluation; choose the evaluation depending on what you're trying to measure.", '- 不存在唯一正确的评测;应根据你想衡量的内容选择评测。')
+ bilingual_text("- Clearly state the rules of the game (methods versus models versus agents).", '- 清楚说明游戏规则(评测方法、模型还是智能体)。')
+ bilingual_text("- Considerations: difficulty, realism, validity.", '- 需要考虑:难度、真实性、有效性。')
def what_is_good():
- text("Evaluation might appear to be a mechanical process:")
- text("1. Define some prompts")
- text("2. Send prompts to a model and get back responses")
- text("3. Compute accuracy")
+ bilingual_text("Evaluation might appear to be a mechanical process:", '评测看起来可能像一个机械过程:')
+ bilingual_text("1. Define some prompts", '1. 定义一些提示。')
+ bilingual_text("2. Send prompts to a model and get back responses", '2. 把提示发送给模型并取回回答。')
+ bilingual_text("3. Compute accuracy", '3. 计算准确率。')
- text("But actually, evaluation is a deep and important topic...")
- text("...which shapes the development of AI.")
+ bilingual_text("But actually, evaluation is a deep and important topic...", '但实际上,评测是一个深刻且重要的话题……')
+ bilingual_text("...which shapes the development of AI.", '……它会塑造 AI 的发展。')
- text("**Core challenge**: abstract construct → concrete metric")
+ bilingual_text("**Core challenge**: abstract construct → concrete metric", '**核心挑战**:抽象构念 → 具体指标。')
- text("Maybe a model is good if it does well on benchmarks...")
+ bilingual_text("Maybe a model is good if it does well on benchmarks...", '也许,如果模型在基准上表现好,它就是好模型……')
link(title="Artificial Analysis", url="https://artificialanalysis.ai/")
image("images/artificial-analysis.png", width=800)
- text("Maybe a model is good if it does well on benchmarks and is cheap to run...")
+ bilingual_text("Maybe a model is good if it does well on benchmarks and is cheap to run...", '也许,如果模型在基准上表现好且运行便宜,它就是好模型……')
image("images/artificial-analysis-cost.png", width=800)
- text("Maybe a model is good if people prefer its responses...")
+ bilingual_text("Maybe a model is good if people prefer its responses...", '也许,如果人们更喜欢它的回答,它就是好模型……')
link(title="Arena AI (formerly Chatbot Arena)", url="https://arena.ai/leaderboard")
image("images/lmarena-leaderboard.png", width=400)
- text("Maybe a model is good if people simply choose to use (and pay for) it...")
+ bilingual_text("Maybe a model is good if people simply choose to use (and pay for) it...", '也许,如果人们只是选择使用它(并付费),它就是好模型……')
link(title="OpenRouter", url="https://openrouter.ai/rankings")
image("images/openrouter.png", width=600)
def perplexity():
- text("- Recall: that a language model is a probability distribution **p(x)** over sequences of tokens.")
- text("- Perplexity (1/p(D))^(1/|D|) measures whether p assigns high probability to some dataset D.")
-
- text("- In pre-training, you minimize perplexity on the training set.")
- text("- The obvious thing is to measure perplexity on the test set.")
- text("- This is what people did traditionally in language modeling research.")
-
- text("Standard datasets:")
- text("- Penn Treebank (WSJ)")
- text("- WikiText-103 (Wikipedia)")
- text("- One Billion Word Benchmark (from machine translation WMT11 - EuroParl, UN, news)")
- text("Classic paradigm: in-distribution evaluation: train on train split and evaluate on test split of some dataset.")
- text("Pure CNNs+LSTMs on the One Billion Word Benchmark (perplexity 51.3 → 30.0) "), link("https://arxiv.org/abs/1602.02410")
-
- text("GPT-2:")
- text("- Trained on WebText (40GB text, websites linked from Reddit)")
- text("- Zero-shot on standard datasets (**out-of-distribution** evaluation)")
+ bilingual_text("- Recall: that a language model is a probability distribution **p(x)** over sequences of tokens.", '- 回忆:that a language model is a probability distribution p(x) over sequences of tokens.')
+ bilingual_text("- Perplexity (1/p(D))^(1/|D|) measures whether p assigns high probability to some dataset D.", '- 困惑度 (1/p(D))^(1/|D|) measures whether p assigns high 概率 to some 数据集 D.')
+
+ bilingual_text("- In pre-training, you minimize perplexity on the training set.", '- In pre-训练, you minimize 困惑度 on the 训练 set.')
+ bilingual_text("- The obvious thing is to measure perplexity on the test set.", '- The obvious thing is to measure 困惑度 on the 测试集.')
+ bilingual_text("- This is what people did traditionally in language modeling research.", '- This is what people did traditionally in language 模型ing research.')
+
+ bilingual_text("Standard datasets:", '标准数据集:')
+ bilingual_text("- Penn Treebank (WSJ)", '- 说明:Penn Treebank (WSJ)')
+ bilingual_text("- WikiText-103 (Wikipedia)", '- 说明:WikiText-103 (Wikipedia)')
+ bilingual_text("- One Billion Word Benchmark (from machine translation WMT11 - EuroParl, UN, news)", '- One Billion Word 基准 (from machine translation WMT11 - EuroParl, UN, news)')
+ bilingual_text("Classic paradigm: in-distribution evaluation: train on train split and evaluate on test split of some dataset.", '经典范式:分布内评测,在某个数据集的训练划分上训练,在测试划分上评测。')
+ bilingual_text("Pure CNNs+LSTMs on the One Billion Word Benchmark (perplexity 51.3 → 30.0) ", 'Pure CNNs+LSTMs on the One Billion Word 基准 (困惑度 51.3 → 30.0)'), link("https://arxiv.org/abs/1602.02410")
+
+ bilingual_text("GPT-2:", 'GPT-2 示例:')
+ bilingual_text("- Trained on WebText (40GB text, websites linked from Reddit)", '- 说明:Trained on WebText (40GB text, websites linked from Reddit)')
+ bilingual_text("- Zero-shot on standard datasets (**out-of-distribution** evaluation)", '- Zero-shot on standard 数据集s (分布外 评测)')
image("images/gpt2-perplexity.png", width=800)
- text("- Works better on small datasets (PTB) where transfer is helpful, but not larger datasets (1BW)")
+ bilingual_text("- Works better on small datasets (PTB) where transfer is helpful, but not larger datasets (1BW)", '- Works better on small 数据集s (PTB) where transfer is helpful, but not larger 数据集s (1BW)')
- text("Perplexity is all you need (more faith than science):")
- text("- True distribution is t, model is p.")
- text("- Best possible perplexity is H(t) obtained iff p = t.")
- text("- If p = t, then solve all the tasks: p(solution | problem)")
- text("- So by pushing down on perplexity, we will eventually \"reach AGI\".")
+ bilingual_text("Perplexity is all you need (more faith than science):", '困惑度就是你所需的一切(这更像信念而非科学):')
+ bilingual_text("- True distribution is t, model is p.", '- True distribution is t, 模型 is p.')
+ bilingual_text("- Best possible perplexity is H(t) obtained iff p = t.", '- Best possible 困惑度 is H(t) obtained iff p = t.')
+ bilingual_text("- If p = t, then solve all the tasks: p(solution | problem)", '- 说明:If p = t, then solve all the tasks: p(solution | problem)')
+ bilingual_text("- So by pushing down on perplexity, we will eventually \"reach AGI\".", '- So by pushing down on 困惑度, we will eventually "reach AGI".')
- text("Perplexity is maybe more than you need:")
- text("- Example: *Stanford was founded in 1885*")
- text("- Perplexity penalizes prediction on all tokens, some (e.g., *founded*) of which might not be relevant")
- text("- Solution: measure conditional perplexity p(response | prompt)^(1/|response|)")
+ bilingual_text("Perplexity is maybe more than you need:", '困惑度也许比你需要的更多:')
+ bilingual_text("- Example: *Stanford was founded in 1885*", '- 示例:Stanford was founded in 1885')
+ bilingual_text("- Perplexity penalizes prediction on all tokens, some (e.g., *founded*) of which might not be relevant", '- 困惑度 penalizes prediction on all token, some (e.g., founded) of which might not be relevant')
+ bilingual_text("- Solution: measure conditional perplexity p(response | prompt)^(1/|response|)", '- 解决方案:measure conditional perplexity p(response | prompt)^(1/|response|)')
- text("Some benchmarks are perplexity in disguise:")
- text("- Cloze tasks (fill in the blank): LAMBADA "), link("https://arxiv.org/abs/1606.06031")
+ bilingual_text("Some benchmarks are perplexity in disguise:", '有些基准其实是伪装过的困惑度:')
+ bilingual_text("- Cloze tasks (fill in the blank): LAMBADA ", '- 说明:Cloze tasks (fill in the blank): LAMBADA'), link("https://arxiv.org/abs/1606.06031")
image("images/lambada.png", width=700)
- text("- Multiple choice sentence completion: HellaSwag "), link("https://arxiv.org/pdf/1905.07830")
+ bilingual_text("- Multiple choice sentence completion: HellaSwag ", '- 说明:Multiple choice sentence completion: HellaSwag'), link("https://arxiv.org/pdf/1905.07830")
image("images/hellaswag.png", width=500)
- text("**Warning** (if you're running a perplexity leaderboard):")
- text("- People submit `LM` and you compute `log_prob = LM(test_data)`")
- text("- You need to trust that the probabilities are valid (sum to 1)")
- text("- For downstream tasks, `response = LM(prompt)` and compute accuracy on `response`")
+ bilingual_text("**Warning** (if you're running a perplexity leaderboard):", '**警告**(如果你在运行困惑度排行榜):')
+ bilingual_text("- People submit `LM` and you compute `log_prob = LM(test_data)`", '- People submit LM and you 计算量 log_prob = LM(test_数据)')
+ bilingual_text("- You need to trust that the probabilities are valid (sum to 1)", '- You need to trust that the 概率 are valid (sum to 1)')
+ bilingual_text("- For downstream tasks, `response = LM(prompt)` and compute accuracy on `response`", '- For downstream tasks, 回答 = LM(提示) and 计算量 准确率 on 回答')
- text("Summary:")
- text("- Perplexity is still used heavily in language model development (smooth scaling laws)")
- text("- Still need benchmarks that capture real-world situations (for the non-believers)...")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Perplexity is still used heavily in language model development (smooth scaling laws)", '- 困惑度 is still used heavily in language 模型 development (smooth scaling laws)')
+ bilingual_text("- Still need benchmarks that capture real-world situations (for the non-believers)...", '- Still need 基准 that capture real-world situations (for the non-believers)...')
def exam_benchmarks():
- text("Exams are a useful way to test language models (as with humans):")
- text("- Have control over the subject and difficulty")
- text("- Design to have unambiguous correct answer, easy to grade")
-
- text("**Massive Multitask Language Understanding (MMLU)** "), link(mmlu_2021)
- text("- 57 subjects (e.g., math, US history, law, morality), multiple-choice")
- text("- \"collected by graduate and undergraduate students from freely available sources online\"")
- text("- Despite the name, MMLU is really about testing knowledge, not language understanding")
- text("- Evaluated on GPT-3 using few-shot prompting")
+ bilingual_text("Exams are a useful way to test language models (as with humans):", '考试是测试语言模型的有用方式(和测试人类类似):')
+ bilingual_text("- Have control over the subject and difficulty", '- 说明:Have control over the subject and difficulty')
+ bilingual_text("- Design to have unambiguous correct answer, easy to grade", '- 说明:Design to have unambiguous correct answer, easy to grade')
+
+ bilingual_text("**Massive Multitask Language Understanding (MMLU)** ", '说明:Massive Multitask Language Understanding (MMLU)'), link(mmlu_2021)
+ bilingual_text("- 57 subjects (e.g., math, US history, law, morality), multiple-choice", '- 57 subjects (e.g., math, US history, law, morality), 多项选择')
+ bilingual_text("- \"collected by graduate and undergraduate students from freely available sources online\"", '- 说明:"collected by graduate and undergraduate students from freely available sources online"')
+ bilingual_text("- Despite the name, MMLU is really about testing knowledge, not language understanding", '- Despite the name, MMLU is really about testing 知识, not language understanding')
+ bilingual_text("- Evaluated on GPT-3 using few-shot prompting", '- Evaluated on GPT-3 using few-shot 提示ing')
image("images/mmlu.png", width=700)
link("https://llm-stats.com/benchmarks/mmlu")
link(title="HELM MMLU for visualizing predictions", url="https://crfm.stanford.edu/helm/mmlu/latest/")
- text("**MMLU-Pro** "), link("https://arxiv.org/abs/2406.01574")
- text("- Removed noisy/trivial questions from MMLU")
- text("- Expanded 4 choices to 10 choices")
- text("- Evaluated using chain of thought (gives model more of a chance)")
- text("- Accuracy of models drop by 16% to 33% (not as saturated)")
+ bilingual_text("**MMLU-Pro** ", '说明:MMLU-Pro'), link("https://arxiv.org/abs/2406.01574")
+ bilingual_text("- Removed noisy/trivial questions from MMLU", '- 说明:Removed noisy/trivial questions from MMLU')
+ bilingual_text("- Expanded 4 choices to 10 choices", '- 说明:Expanded 4 choices to 10 choices')
+ bilingual_text("- Evaluated using chain of thought (gives model more of a chance)", '- Evaluated using chain of thought (gives 模型 more of a chance)')
+ bilingual_text("- Accuracy of models drop by 16% to 33% (not as saturated)", '- 准确率 of 模型s drop by 16% to 33% (not as saturated)')
image("images/mmlu-pro.png", width=700)
link("https://llm-stats.com/benchmarks/mmlu-pro")
link(title="HELM MMLU-Pro for visualizing predictions", url="https://crfm.stanford.edu/helm/capabilities/latest/#/leaderboard/mmlu_pro")
- text("**Graduate-Level Google-Proof Q&A (GPQA)** "), link("https://arxiv.org/abs/2311.12022")
- text("- Questions written by 61 PhD contractors from Upwork")
+ bilingual_text("**Graduate-Level Google-Proof Q&A (GPQA)** ", '说明:Graduate-Level Google-Proof Q&A (GPQA)'), link("https://arxiv.org/abs/2311.12022")
+ bilingual_text("- Questions written by 61 PhD contractors from Upwork", '- 说明:Questions written by 61 PhD contractors from Upwork')
image("images/gpqa.png", width=700)
- text("- PhD experts achieve 65% accuracy")
- text("- Non-experts achieve 34% over 30 minutes with access to Google")
- text("- GPT-4 achieves 39%")
+ bilingual_text("- PhD experts achieve 65% accuracy", '- PhD experts achieve 65% 准确率')
+ bilingual_text("- Non-experts achieve 34% over 30 minutes with access to Google", '- 说明:Non-experts achieve 34% over 30 minutes with access to Google')
+ bilingual_text("- GPT-4 achieves 39%", '- 说明:GPT-4 achieves 39%')
link("https://llm-stats.com/benchmarks/gpqa")
link(title="HELM GPQA for visualizing predictions", url="https://crfm.stanford.edu/helm/capabilities/latest/#/leaderboard/gpqa")
- text("**Humanity's Last Exam (HLE)** "), link("https://arxiv.org/abs/2501.14249")
- text("- 2500 questions: multimodal, many subjects, multiple-choice + short-answer")
+ bilingual_text("**Humanity's Last Exam (HLE)** ", "说明:Humanity's Last Exam (HLE)"), link("https://arxiv.org/abs/2501.14249")
+ bilingual_text("- 2500 questions: multimodal, many subjects, multiple-choice + short-answer", '- 2500 questions: multimodal, many subjects, 多项选择 + short-answer')
image("images/hle-examples.png", width=700)
- text("- Awarded $500K prize pool + co-authorship to question creators")
- text("- Filtered by frontier LLMs, multiple stages of review")
+ bilingual_text("- Awarded $500K prize pool + co-authorship to question creators", '- 说明:Awarded $500K prize pool + co-authorship to question creators')
+ bilingual_text("- Filtered by frontier LLMs, multiple stages of review", '- 说明:Filtered by frontier LLMs, multiple stages of review')
image("images/hle-pipeline.png", width=700)
image("images/hle-results.png", width=600)
link("https://llm-stats.com/benchmarks/hle")
- text("Summary:")
- text("- Trend towards harder questions as models improve and saturate existing benchmarks")
- text("- Multiple-choice format can be as difficult as one wants")
- text("- Does not capture real usage (open-ended, doesn't necessarily exist correct answer)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Trend towards harder questions as models improve and saturate existing benchmarks", '- Trend towards harder questions as 模型s improve and saturate existing 基准')
+ bilingual_text("- Multiple-choice format can be as difficult as one wants", '- 多项选择 format can be as difficult as one wants')
+ bilingual_text("- Does not capture real usage (open-ended, doesn't necessarily exist correct answer)", "- Does not capture real usage (开放式, doesn't necessarily exist correct answer)")
def chat_benchmarks():
- text("- So far, we've been evaluating on well-defined multiple-choice tasks.")
- text("- Most people don't ask multiple-choice exam questions to their AI assistant.")
+ bilingual_text("- So far, we've been evaluating on well-defined multiple-choice tasks.", "- So far, we've been evaluating on well-defined 多项选择 tasks.")
+ bilingual_text("- Most people don't ask multiple-choice exam questions to their AI assistant.", "- Most people don't ask 多项选择 exam questions to their AI assistant.")
- text("Example:")
- text("Prompt: *I would like to make a beet salad with goat cheese. What kind of herbs would work well and what would not work well?*")
- text("Response: *Here’s a breakdown of herbs that work well (and some that don’t) in a beet + goat cheese salad, based on how their flavors interact with the sweet-earthiness of beets and the tangy creaminess of goat cheese...")
+ bilingual_text("Example:", '示例:')
+ bilingual_text("Prompt: *I would like to make a beet salad with goat cheese. What kind of herbs would work well and what would not work well?*", '提示: I would like to make a beet salad with goat cheese. What kind of herbs would work well and what would not work well?')
+ bilingual_text("Response: *Here’s a breakdown of herbs that work well (and some that don’t) in a beet + goat cheese salad, based on how their flavors interact with the sweet-earthiness of beets and the tangy creaminess of goat cheese...", '回答: Here’s a breakdown of herbs that work well (and some that don’t) in a beet + goat cheese salad, based on how their flavors interact with the sweet-earthiness of beets and the tangy creaminess of goat cheese...')
- text("**Challenge**: how to evaluate an open-ended response?")
+ bilingual_text("**Challenge**: how to evaluate an open-ended response?", 'Challenge: how to evaluate an 开放式 回答?')
- text("**Chatbot Arena** "), link("https://arxiv.org/abs/2403.04132")
- text("Data collection:")
- text("- Random person from the Internet types in prompt")
- text("- They get response from two random (anonymized) models")
- text("- They rate which one is better")
+ bilingual_text("**Chatbot Arena** ", '**Chatbot Arena**(聊天机器人竞技场)'), link("https://arxiv.org/abs/2403.04132")
+ bilingual_text("Data collection:", '数据收集:')
+ bilingual_text("- Random person from the Internet types in prompt", '- Random person from the Internet types in 提示')
+ bilingual_text("- They get response from two random (anonymized) models", '- They get 回答 from two random (anonymized) 模型s')
+ bilingual_text("- They rate which one is better", '- 说明:They rate which one is better')
image("images/arena-beets.png", width=700)
- text("Compute ELO rankings based on pairwise comparisons:")
- text("- Define model: p(A wins against B) = 1 / (1 + 10^((ELO_B - ELO_A)/400))")
- text("- Fit this model to maximize probability of pairwise comparisons")
+ bilingual_text("Compute ELO rankings based on pairwise comparisons:", '基于成对比较计算 ELO 排名:')
+ bilingual_text("- Define model: p(A wins against B) = 1 / (1 + 10^((ELO_B - ELO_A)/400))", '- Define 模型: p(A wins against B) = 1 / (1 + 10^((ELO_B - ELO_A)/400))')
+ bilingual_text("- Fit this model to maximize probability of pairwise comparisons", '- Fit this 模型 to maximize 概率 of 成对比较')
link(title="Arena AI (formerly Chatbot Arena)", url="https://arena.ai/leaderboard")
image("images/lmarena-leaderboard.png", width=400)
- text("Properties:")
- text("- Real-world prompts (free for users, incentives to actually use it)")
- text("- But who are these people? biases? spammers?")
- text("- Binary preference but conflates style and correctness")
- text("- How does the human even assess correctness? Prone to sycophancy?")
- text("- Feature: don't need to feed same prompts to all models (important because human is rating)")
- text("- Dynamic: incorporates new prompts and models over time")
-
- text("**AlpacaEval** (2023)"), link(title="leaderboard", url="https://tatsu-lab.github.io/alpaca_eval/")
- text("- 805 instructions from various sources")
- text("- Metric: win rate against baseline model (GPT-4 preview) as judged by GPT-4 preview (potential bias?)")
- text("- Problem: LLM judges favor longer responses, resulted in leaderboard gaming")
- text("- Alpaca Eval 2.0 used regression to debias the metric "), link("https://arxiv.org/pdf/2404.04475")
- text("- How do we evaluate the metric?")
- text("- Correlation with Chatbot Arena (humans) is high:")
+ bilingual_text("Properties:", '性质:')
+ bilingual_text("- Real-world prompts (free for users, incentives to actually use it)", '- Real-world 提示 (free for users, incentives to actually use it)')
+ bilingual_text("- But who are these people? biases? spammers?", '- 说明:But who are these people? biases? spammers?')
+ bilingual_text("- Binary preference but conflates style and correctness", '- 说明:Binary preference but conflates style and correctness')
+ bilingual_text("- How does the human even assess correctness? Prone to sycophancy?", '- 说明:How does the human even assess correctness? Prone to sycophancy?')
+ bilingual_text("- Feature: don't need to feed same prompts to all models (important because human is rating)", "- Feature: don't need to feed same 提示 to all 模型s (important because human is rating)")
+ bilingual_text("- Dynamic: incorporates new prompts and models over time", '- Dynamic: incorporates new 提示 and 模型s over time')
+
+ bilingual_text("**AlpacaEval** (2023)", '说明:AlpacaEval (2023)'), link(title="leaderboard", url="https://tatsu-lab.github.io/alpaca_eval/")
+ bilingual_text("- 805 instructions from various sources", '- 说明:805 instructions from various sources')
+ bilingual_text("- Metric: win rate against baseline model (GPT-4 preview) as judged by GPT-4 preview (potential bias?)", '- 指标: win rate against baseline 模型 (GPT-4 preview) as judged by GPT-4 preview (potential bias?)')
+ bilingual_text("- Problem: LLM judges favor longer responses, resulted in leaderboard gaming", '- Problem: LLM 裁判 favor longer 回答, resulted in leaderboard gaming')
+ bilingual_text("- Alpaca Eval 2.0 used regression to debias the metric ", '- Alpaca Eval 2.0 used regression to debias the 指标'), link("https://arxiv.org/pdf/2404.04475")
+ bilingual_text("- How do we evaluate the metric?", '- How do we evaluate the 指标?')
+ bilingual_text("- Correlation with Chatbot Arena (humans) is high:", '- 说明:Correlation with Chatbot Arena (humans) is high:')
image("https://github.com/tatsu-lab/alpaca_eval/raw/main/figures/chat_correlations_no_ae.png", width=500)
image("images/alpacaeval-leaderboard.png", width=400)
- text("**WildBench** "), link("https://arxiv.org/pdf/2406.04770")
- text("- Sourced 1024 examples from 1M human-chatbot conversations")
- text("- Uses GPT-4 turbo as a judge with a checklist (like CoT for judging) + GPT-4 as a judge")
- text("- Well-correlated with Chatbot Arena (seems to be the de facto sanity check)")
+ bilingual_text("**WildBench** ", '说明:WildBench'), link("https://arxiv.org/pdf/2406.04770")
+ bilingual_text("- Sourced 1024 examples from 1M human-chatbot conversations", '- 说明:Sourced 1024 examples from 1M human-chatbot conversations')
+ bilingual_text("- Uses GPT-4 turbo as a judge with a checklist (like CoT for judging) + GPT-4 as a judge", '- Uses GPT-4 turbo as a judge with a 检查清单 (like CoT for judging) + GPT-4 as a judge')
+ bilingual_text("- Well-correlated with Chatbot Arena (seems to be the de facto sanity check)", '- 说明:Well-correlated with Chatbot Arena (seems to be the de facto sanity check)')
image("images/wildbench.png", width=700)
link(title="HELM WildBench for visualizing predictions", url="https://crfm.stanford.edu/helm/capabilities/latest/#/leaderboard/wildbench")
- text("Summary:")
- text("- Challenge: how to evaluate open-ended responses?")
- text("- Pairwise comparisons between similar responses provide higher signal")
- text("- Beware of biases (both from humans and LLM judges)")
- text("- Checklist/rubric improves reliability (regardless of human or LLM judge)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Challenge: how to evaluate open-ended responses?", '- Challenge: how to evaluate 开放式 回答?')
+ bilingual_text("- Pairwise comparisons between similar responses provide higher signal", '- 成对比较 between similar 回答 provide higher signal')
+ bilingual_text("- Beware of biases (both from humans and LLM judges)", '- Beware of biases (both from humans and LLM 裁判)')
+ bilingual_text("- Checklist/rubric improves reliability (regardless of human or LLM judge)", '- 检查清单/评分量规 improves reliability (regardless of human or LLM judge)')
def agentic_benchmarks():
- text("Previously: evaluate what LMs say (chat)")
- text("Now: evaluate what LMs do (agents)")
+ bilingual_text("Previously: evaluate what LMs say (chat)", '之前:评测 LM 说什么(聊天)。')
+ bilingual_text("Now: evaluate what LMs do (agents)", '现在:评测 LM 做什么(智能体)。')
- text("Agent = language model + agent scaffold (logic for deciding how to use the LM)")
+ bilingual_text("Agent = language model + agent scaffold (logic for deciding how to use the LM)", '智能体 = 语言模型 + 智能体脚手架(决定如何使用 LM 的逻辑)。')
- text("Consider tasks that require tool use (e.g., running code) and iterating over a period of time")
+ bilingual_text("Consider tasks that require tool use (e.g., running code) and iterating over a period of time", '考虑需要使用工具(例如运行代码)并在一段时间内迭代的任务。')
- text("**SWEBench** "), link("https://arxiv.org/abs/2310.06770")
- text("- 2294 tasks across 12 Python repositories")
- text("- Given codebase + issue description, submit a PR")
- text("- Evaluation metric: unit tests")
+ bilingual_text("**SWEBench** ", '说明:SWEBench'), link("https://arxiv.org/abs/2310.06770")
+ bilingual_text("- 2294 tasks across 12 Python repositories", '- 说明:2294 tasks across 12 Python repositories')
+ bilingual_text("- Given codebase + issue description, submit a PR", '- 说明:Given codebase + issue description, submit a PR')
+ bilingual_text("- Evaluation metric: unit tests", '- 评测 指标: unit tests')
image("images/swebench.png", width=800)
link("https://llm-stats.com/benchmarks/swe-bench-verified")
- text("**TerminalBench** "), link("https://arxiv.org/abs/2601.11868"), link(title="website", url="https://www.tbench.ai/")
+ bilingual_text("**TerminalBench** ", '说明:TerminalBench'), link("https://arxiv.org/abs/2601.11868"), link(title="website", url="https://www.tbench.ai/")
image("images/terminal-bench.png", width=700)
- text("- Computer terminal environments: simple and universal")
- text("- 229 tasks crowdsourced from 93 contributors, 89 tasks constitute Terminal-Bench 2.0")
+ bilingual_text("- Computer terminal environments: simple and universal", '- 计算量r terminal environments: simple and universal')
+ bilingual_text("- 229 tasks crowdsourced from 93 contributors, 89 tasks constitute Terminal-Bench 2.0", '- 说明:229 tasks crowdsourced from 93 contributors, 89 tasks constitute Terminal-Bench 2.0')
image("images/terminal-bench-human-time.png", width=600)
image("images/terminal-bench-results.png", width=600)
link("https://llm-stats.com/benchmarks/terminal-bench")
- text("**CyBench** "), link("https://arxiv.org/abs/2408.08926")
+ bilingual_text("**CyBench** ", '说明:CyBench'), link("https://arxiv.org/abs/2408.08926")
image("images/cybench.png", width=700)
- text("- 40 Capture the Flag (CTF) tasks")
- text("- Use first-solve time as a measure of difficulty")
+ bilingual_text("- 40 Capture the Flag (CTF) tasks", '- 说明:40 Capture the Flag (CTF) tasks')
+ bilingual_text("- Use first-solve time as a measure of difficulty", '- 说明:Use first-solve time as a measure of difficulty')
image("images/cybench-agent.png", width=700)
image("images/cybench-results.png", width=600)
link("https://llm-stats.com/benchmarks/cybench")
- text("**MLEBench** "), link("https://arxiv.org/abs/2410.07095")
- text("- 75 Kaggle competitions (require training models, processing data, etc.)")
+ bilingual_text("**MLEBench** ", '说明:MLEBench'), link("https://arxiv.org/abs/2410.07095")
+ bilingual_text("- 75 Kaggle competitions (require training models, processing data, etc.)", '- 75 Kaggle competitions (require 训练 模型s, processing 数据, etc.)')
image("images/mlebench.png", width=800)
image("images/mlebench-results.png", width=700)
- text("Agent scaffolds "), post_link("https://www.philschmid.de/agents-2.0-deep-agents")
+ bilingual_text("Agent scaffolds ", '智能体 scaffolds'), post_link("https://www.philschmid.de/agents-2.0-deep-agents")
image("https://www.philschmid.de/static/blog/agents-2.0-deep-agents/overview.png", width=400)
- text("- Explicit planning: keep a todo list that gets checked off")
- text("- Hierarchical delegation: agents calling other sub-agents (clean context)")
- text("- Persistent memory: read/write files")
- text("- Extreme context engineering: explicit more instructions on process")
+ bilingual_text("- Explicit planning: keep a todo list that gets checked off", '- 说明:Explicit planning: keep a todo list that gets checked off')
+ bilingual_text("- Hierarchical delegation: agents calling other sub-agents (clean context)", '- Hierarchical delegation: 智能体 calling other sub-智能体 (clean context)')
+ bilingual_text("- Persistent memory: read/write files", '- Persistent 内存: read/write files')
+ bilingual_text("- Extreme context engineering: explicit more instructions on process", '- 说明:Extreme context engineering: explicit more instructions on process')
- text("Summary:")
- text("- Agents dramatically enhance the capability surface of language models")
- text("- Agent scaffolds are very important")
- text("- Evaluating agents = evaluating agent scaffold + language model")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Agents dramatically enhance the capability surface of language models", '- 智能体 dramatically enhance the capability surface of language 模型s')
+ bilingual_text("- Agent scaffolds are very important", '- 智能体 scaffolds are very important')
+ bilingual_text("- Evaluating agents = evaluating agent scaffold + language model", '- Evaluating 智能体 = evaluating 智能体 scaffold + language 模型')
def pure_reasoning_benchmarks():
- text("- All of the tasks so far require linguistic and world knowledge.")
- text("- Can we isolate **reasoning** from knowledge?")
- text("- Arguably, reasoning captures a more pure form of intelligence (isn't just about memorizing facts).")
+ bilingual_text("- All of the tasks so far require linguistic and world knowledge.", '- All of the tasks so far require linguistic and 世界知识.')
+ bilingual_text("- Can we isolate **reasoning** from knowledge?", '- Can we isolate 推理能力 from 知识?')
+ bilingual_text("- Arguably, reasoning captures a more pure form of intelligence (isn't just about memorizing facts).", "- Arguably, 推理能力 captures a more pure form of intelligence (isn't just about memorizing facts).")
- text("**ARC-AGI** "), link(title="website", url="https://arcprize.org/arc-agi")
- text("- 100\% solvable by humans, but challenging for AI")
- text("- Each task is unique, so memorization doesn't help.")
+ bilingual_text("**ARC-AGI** ", '说明:ARC-AGI'), link(title="website", url="https://arcprize.org/arc-agi")
+ bilingual_text("- 100\% solvable by humans, but challenging for AI", '- 说明:100\\% solvable by humans, but challenging for AI')
+ bilingual_text("- Each task is unique, so memorization doesn't help.", "- 说明:Each task is unique, so memorization doesn't help.")
- text("- ARC-AGI-1 (2019): first iteration")
+ bilingual_text("- ARC-AGI-1 (2019): first iteration", '- 说明:ARC-AGI-1 (2019): first iteration')
image("https://arcprize.org/media/images/arc-task-grids.jpg", width=800)
- text("- ARC-AGI-2 (March 2025): more multi-step reasoning")
+ bilingual_text("- ARC-AGI-2 (March 2025): more multi-step reasoning", '- ARC-AGI-2 (March 2025): more multi-step 推理能力')
image("https://arcprize.org/media/images/blog/arc-agi-2-unsolved-1.png", width=800)
image("images/arc-agi-results.png", width=700)
- text("- Pretrained language models didn't move the needle")
- text("- Reasoning models (o1, o3) started making things take off")
+ bilingual_text("- Pretrained language models didn't move the needle", "- Pretrained language 模型s didn't move the needle")
+ bilingual_text("- Reasoning models (o1, o3) started making things take off", '- 推理能力 模型s (o1, o3) started making things take off')
- text("- ARC-AGI-3 (March 2026): interactive environments "), post_link("https://arcprize.org/media/ARC_AGI_3_Technical_Report.pdf")
+ bilingual_text("- ARC-AGI-3 (March 2026): interactive environments ", '- 说明:ARC-AGI-3 (March 2026): interactive environments'), post_link("https://arcprize.org/media/ARC_AGI_3_Technical_Report.pdf")
image("images/arc-agi-3.png", width=300)
image("images/arc-agi-3-results.png", width=500)
- text("Summary:")
- text("- Goal is to disentangle reasoning from knowledge (difficult to do!)")
- text("- Constrained to human reasoning (not superhuman reasoning)")
- text("- Clearly exposes gaps in current models")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Goal is to disentangle reasoning from knowledge (difficult to do!)", '- Goal is to disentangle 推理能力 from 知识 (difficult to do!)')
+ bilingual_text("- Constrained to human reasoning (not superhuman reasoning)", '- Constrained to human 推理能力 (not superhuman 推理能力)')
+ bilingual_text("- Clearly exposes gaps in current models", '- Clearly exposes gaps in current 模型s')
def safety_benchmarks():
image("https://www.team-bhp.com/forum/attachments/road-safety/2173645d1625144681-will-crash-test-rating-change-if-higher-variant-chosen-images-30.jpeg", width=400)
- text("What does safety mean for AI?")
+ bilingual_text("What does safety mean for AI?", 'AI 的安全意味着什么?')
- text("**HarmBench** "), link("https://arxiv.org/abs/2402.04249")
- text("- Based on 510 harmful behaviors that violate laws or norms")
+ bilingual_text("**HarmBench** ", '说明:HarmBench'), link("https://arxiv.org/abs/2402.04249")
+ bilingual_text("- Based on 510 harmful behaviors that violate laws or norms", '- 说明:Based on 510 harmful behaviors that violate laws or norms')
link(title="HarmBench on HELM", url="https://crfm.stanford.edu/helm/safety/latest/#/leaderboard/harm_bench")
link(title="Example of safety failure", url="https://crfm.stanford.edu/helm/safety/latest/#/runs/harm_bench:model=anthropic_claude-3-7-sonnet-20250219?instancesPage=4")
- text("**AIR-Bench** "), link("https://arxiv.org/abs/2407.17436")
- text("- Based on regulatory frameworks and company policies")
- text("- Taxonomized into 314 risk categories, 5694 prompts")
+ bilingual_text("**AIR-Bench** ", '说明:AIR-Bench'), link("https://arxiv.org/abs/2407.17436")
+ bilingual_text("- Based on regulatory frameworks and company policies", '- 说明:Based on regulatory frameworks and company policies')
+ bilingual_text("- Taxonomized into 314 risk categories, 5694 prompts", '- Taxonomized into 314 risk categories, 5694 提示')
image("https://crfm.stanford.edu/helm/assets/air-overview-DpBbyagA.png", width=800)
link(title="HELM AIR-Bench", url="https://crfm.stanford.edu/helm/air-bench/latest/#/leaderboard")
- text("Jailbreaking:")
- text("- Language models are trained to refuse harmful instructions")
- text("- Greedy Coordinate Gradient (GCG) automatically optimizes prompts to bypass safety "), link("https://arxiv.org/pdf/2307.15043")
- text("- Transfers from open-weight models (Llama) to closed models (GPT-4)")
+ bilingual_text("Jailbreaking:", '越狱:')
+ bilingual_text("- Language models are trained to refuse harmful instructions", '- Language 模型s are trained to refuse harmful instructions')
+ bilingual_text("- Greedy Coordinate Gradient (GCG) automatically optimizes prompts to bypass safety ", '- Greedy Coordinate Gradient (GCG) automatically optimizes 提示 to bypass 安全'), link("https://arxiv.org/pdf/2307.15043")
+ bilingual_text("- Transfers from open-weight models (Llama) to closed models (GPT-4)", '- Transfers from open-weight 模型s (Llama) to closed 模型s (GPT-4)')
image("images/gcg-examples.png", width=800)
- text("What is safety?")
- text("- Many aspects of safety are strongly contextual (politics, law, social norms - which vary across countries)")
- text("- Many risks are quite varied (hallucinations, sycophancy, abetting crimes, inequality, losing critical thinking)")
+ bilingual_text("What is safety?", '什么是安全?')
+ bilingual_text("- Many aspects of safety are strongly contextual (politics, law, social norms - which vary across countries)", '- Many aspects of 安全 are strongly contextual (politics, law, social norms - which vary across countries)')
+ bilingual_text("- Many risks are quite varied (hallucinations, sycophancy, abetting crimes, inequality, losing critical thinking)", '- 说明:Many risks are quite varied (hallucinations, sycophancy, abetting crimes, inequality, losing critical thinking)')
- text("**Dual-use**: capable cybersecurity agents (Mythos) can be used to hack into a system or to do penetration testing")
+ bilingual_text("**Dual-use**: capable cybersecurity agents (Mythos) can be used to hack into a system or to do penetration testing", 'Dual-use: capable cybersecurity 智能体 (Mythos) can be used to hack into a system or to do penetration testing')
def realism():
- text("**Ecological validity**: how well does an evaluation capture real-world use?")
- text("- Exam benchmarks (e.g., GPQA) are far away from real-world use.")
- text("- Chatbot Arena prompts are from real people, but distribution is uncontrolled.")
+ bilingual_text("**Ecological validity**: how well does an evaluation capture real-world use?", 'Ecological 有效性: how well does an 评测 capture real-world use?')
+ bilingual_text("- Exam benchmarks (e.g., GPQA) are far away from real-world use.", '- Exam 基准 (e.g., GPQA) are far away from real-world use.')
+ bilingual_text("- Chatbot Arena prompts are from real people, but distribution is uncontrolled.", '- Chatbot Arena 提示 are from real people, but distribution is uncontrolled.')
- text("**GDPVal** (OpenAI) "), link("https://arxiv.org/pdf/2510.04374")
- text("- 44 occupations from top 9 sectors according to US GDP")
- text("- Tasks come from professionals with ~14 years of experience")
+ bilingual_text("**GDPVal** (OpenAI) ", '说明:GDPVal (OpenAI)'), link("https://arxiv.org/pdf/2510.04374")
+ bilingual_text("- 44 occupations from top 9 sectors according to US GDP", '- 说明:44 occupations from top 9 sectors according to US GDP')
+ bilingual_text("- Tasks come from professionals with ~14 years of experience", '- 说明:Tasks come from professionals with ~14 years of experience')
image("images/gdpval.png", width=700)
- text("**MedHELM** "), link("https://arxiv.org/abs/2505.23802")
- text("- Previous medical benchmarks were based on standardized exams")
- text("- 121 clinical tasks sourced from 29 clinicians, mixture of private and public datasets")
+ bilingual_text("**MedHELM** ", '说明:MedHELM'), link("https://arxiv.org/abs/2505.23802")
+ bilingual_text("- Previous medical benchmarks were based on standardized exams", '- Previous medical 基准 were based on standardized exams')
+ bilingual_text("- 121 clinical tasks sourced from 29 clinicians, mixture of private and public datasets", '- 121 clinical tasks sourced from 29 clinicians, mixture of private and public 数据集s')
image("https://crfm.stanford.edu/helm/assets/medhelm-overview-CND0EIsy.png", width=700)
link(title="MedHELM", url="https://crfm.stanford.edu/helm/medhelm/latest/#/leaderboard")
- text("**Clio** (Anthropic) "), link("https://arxiv.org/abs/2412.13678")
- text("- Use language models to analyze real user data")
- text("- Share general patterns of what people are asking")
+ bilingual_text("**Clio** (Anthropic) ", '说明:Clio (Anthropic)'), link("https://arxiv.org/abs/2412.13678")
+ bilingual_text("- Use language models to analyze real user data", '- Use language 模型s to analyze real user 数据')
+ bilingual_text("- Share general patterns of what people are asking", '- 说明:Share general patterns of what people are asking')
image("images/clio-table4.png", width=700)
- text("Unfortunately, realism and privacy are sometimes at odds with each other.")
+ bilingual_text("Unfortunately, realism and privacy are sometimes at odds with each other.", '不幸的是,真实性和隐私有时彼此冲突。')
def validity():
- text("How do we know our evaluations are valid?")
+ bilingual_text("How do we know our evaluations are valid?", '我们如何知道评测是有效的?')
- text("### Train-test overlap")
- text("- Machine learning 101: don't train on your test set")
- text("- Pre-foundation models (ImageNet, SQuAD): well-defined train-test splits")
- text("- Today: train on the Internet and don't tell people about your data")
+ bilingual_text("### Train-test overlap", '### 训练-测试重叠')
+ bilingual_text("- Machine learning 101: don't train on your test set", "- Machine learning 101: don't train on your 测试集")
+ bilingual_text("- Pre-foundation models (ImageNet, SQuAD): well-defined train-test splits", '- Pre-foundation 模型s (ImageNet, SQuAD): well-defined train-test splits')
+ bilingual_text("- Today: train on the Internet and don't tell people about your data", "- Today: train on the Internet and don't tell people about your 数据")
- text("Route 1: try to infer train-test overlap from model")
- text("- Exploit exchangeability of data points "), link("https://arxiv.org/pdf/2310.17623")
+ bilingual_text("Route 1: try to infer train-test overlap from model", 'Route 1: try to infer 训练-测试重叠 from 模型')
+ bilingual_text("- Exploit exchangeability of data points ", '- Exploit exchangeability of 数据 points'), link("https://arxiv.org/pdf/2310.17623")
image("images/contamination-exchangeability.png", width=500)
- text("Route 2: encourage reporting norms (e.g., people report confidence intervals)")
- text("- Model providers should report train-test overlap "), link("https://arxiv.org/abs/2410.08385")
+ bilingual_text("Route 2: encourage reporting norms (e.g., people report confidence intervals)", '说明:Route 2: encourage reporting norms (e.g., people report confidence intervals)')
+ bilingual_text("- Model providers should report train-test overlap ", '- 模型 providers should report 训练-测试重叠'), link("https://arxiv.org/abs/2410.08385")
- text("Route 3: use fresh evals")
- text("- LiveCodeBench, UncheatableEval: scrape new webpages")
- text("- Timestamps aren't always safe due to copying either")
+ bilingual_text("Route 3: use fresh evals", '说明:Route 3: use fresh evals')
+ bilingual_text("- LiveCodeBench, UncheatableEval: scrape new webpages", '- 说明:LiveCodeBench, UncheatableEval: scrape new webpages')
+ bilingual_text("- Timestamps aren't always safe due to copying either", "- 说明:Timestamps aren't always safe due to copying either")
- text("Route 4: use private evals")
- text("- Companies use internal code bases that aren't on the Internet")
- text("- Use your personal writings")
- text("- Easiest for perplexity")
+ bilingual_text("Route 4: use private evals", '说明:Route 4: use private evals')
+ bilingual_text("- Companies use internal code bases that aren't on the Internet", "- 说明:Companies use internal code bases that aren't on the Internet")
+ bilingual_text("- Use your personal writings", '- 说明:Use your personal writings')
+ bilingual_text("- Easiest for perplexity", '- Easiest for 困惑度')
- text("### Dataset quality")
- text("- Fixed up SWE-Bench to produce SWE-Bench Verified "), post_link("https://openai.com/index/introducing-swe-bench-verified/")
- text("- Create Platinum versions of benchmarks "), link("https://arxiv.org/abs/2502.03461")
+ bilingual_text("### Dataset quality", '### 数据集质量')
+ bilingual_text("- Fixed up SWE-Bench to produce SWE-Bench Verified ", '- 说明:Fixed up SWE-Bench to produce SWE-Bench Verified'), post_link("https://openai.com/index/introducing-swe-bench-verified/")
+ bilingual_text("- Create Platinum versions of benchmarks ", '- Create Platinum versions of 基准'), link("https://arxiv.org/abs/2502.03461")
image("https://pbs.twimg.com/media/GjICXQlWkAAYnDS?format=jpg&name=4096x4096", width=700)
image("https://pbs.twimg.com/media/GjICcGQXYAAM4o1?format=jpg&name=4096x4096", width=800)
- text("- Problems with agentic benchmarks: insufficient test cases, trivial agent can solve task "), link("https://arxiv.org/abs/2507.02825")
- text("- Docent: use LLM to inspect agent traces to detect problems "), post_link("https://transluce.org/introducing-docent")
+ bilingual_text("- Problems with agentic benchmarks: insufficient test cases, trivial agent can solve task ", '- Problems with 智能体ic 基准: insufficient test cases, trivial 智能体 can solve task'), link("https://arxiv.org/abs/2507.02825")
+ bilingual_text("- Docent: use LLM to inspect agent traces to detect problems ", '- Docent: use LLM to inspect 智能体 traces to detect problems'), post_link("https://transluce.org/introducing-docent")
def how_to_think_about_evaluation():
- text("### What's the point of evaluation?")
- text("There is no one true evaluation; it depends on what question you're trying to answer.")
- text("1. User or company wants to make a purchase decision (model A or model B) for their use case (e.g., customer service chatbots).")
- text("2. Researchers want to measure the raw capabilities of a model (e.g., intelligence).")
- text("3. We want to understand the benefits + harms of a model (for business and policy reasons).")
- text("4. Model developers want to get feedback to improve the model.")
-
- text("### What are we evaluating?")
- text("- Pre-foundation models, we evaluated **methods** (standardized train-test splits).")
- text("- Today, we're (mostly) evaluating **models/systems** (anything goes).")
-
- text("There are some exceptions...")
- text("- nanogpt speedrun: fixed data, compute time to get to a particular validation loss")
+ bilingual_text("### What's the point of evaluation?", '### 评测的意义是什么?')
+ bilingual_text("There is no one true evaluation; it depends on what question you're trying to answer.", '不存在唯一正确的评测;它取决于你想回答什么问题。')
+ bilingual_text("1. User or company wants to make a purchase decision (model A or model B) for their use case (e.g., customer service chatbots).", '1. User or company wants to make a purchase decision (模型 A or 模型 B) for their use case (e.g., customer service chatbots).')
+ bilingual_text("2. Researchers want to measure the raw capabilities of a model (e.g., intelligence).", '2. Researchers want to measure the raw capabilities of a 模型 (e.g., intelligence).')
+ bilingual_text("3. We want to understand the benefits + harms of a model (for business and policy reasons).", '3. We want to understand the benefits + harms of a 模型 (for business and policy reasons).')
+ bilingual_text("4. Model developers want to get feedback to improve the model.", '4. 模型 developers want to get feedback to improve the 模型.')
+
+ bilingual_text("### What are we evaluating?", '### 我们在评测什么?')
+ bilingual_text("- Pre-foundation models, we evaluated **methods** (standardized train-test splits).", '- Pre-foundation 模型s, we evaluated 方法s (standardized train-test splits).')
+ bilingual_text("- Today, we're (mostly) evaluating **models/systems** (anything goes).", "- Today, we're (mostly) evaluating 模型s/系统 (anything goes).")
+
+ bilingual_text("There are some exceptions...", '也有一些例外……')
+ bilingual_text("- nanogpt speedrun: fixed data, compute time to get to a particular validation loss", '- nanogpt speedrun: fixed 数据, 计算量 time to get to a particular validation loss')
image("images/karpathy-nanogpt-speedrun.png", width=600), post_link("https://x.com/karpathy/status/1846790537262571739")
- text("Evaluating methods encourage algorithmic innovation from researchers.")
- text("Evaluating models/systems is useful for downstream users.")
+ bilingual_text("Evaluating methods encourage algorithmic innovation from researchers.", '评测方法会鼓励研究者进行算法创新。')
+ bilingual_text("Evaluating models/systems is useful for downstream users.", '评测模型/系统对下游用户有用。')
- text("Either way, we need to define the rules of the game!")
+ bilingual_text("Either way, we need to define the rules of the game!", '无论哪种方式,我们都需要定义游戏规则!')
if __name__ == "__main__":
diff --git a/lecture_13.py b/lecture_13.py
index f92c4d4..d644880 100644
--- a/lecture_13.py
+++ b/lecture_13.py
@@ -1,12 +1,12 @@
from edtrace import text, image, link
-from lecture_util import article_link
+from lecture_util import article_link, bilingual_text, bilingual_verbatim
from references import dclm_2024, nemotron_cc_2024, olmo_2_2025, llama_3_2024, gpt2_2019, openwebtext_2019, gopher_2021, alpaca_2023
def main():
- text("## Lecture 13: Data I")
- text("Previous lectures: how to train a model *given data*")
- text("Next two lectures: *what data* should we train on?")
+ bilingual_text("## Lecture 13: Data I", '## 第 13 讲:数据 I')
+ bilingual_text("Previous lectures: how to train a model *given data*", '前几讲:在**给定数据**的情况下如何训练模型。')
+ bilingual_text("Next two lectures: *what data* should we train on?", '接下来两讲:我们应该在**什么数据**上训练?')
motivation()
@@ -37,400 +37,400 @@ def main():
the_stack() # Code dataset
common_pile() # Properly licensed data
- text("### Summary")
- text("- Key lesson: Data does not fall from the sky. You have to work to get it.")
- text("- Live service → raw data → processed data (transformation, filtering, deduplication)")
- text("- Data is the key ingredient that differentiates language models")
- text("- Legal and ethical issues (e.g., copyright and privacy)")
- text("- Much of this pipeline is heuristic, many opportunities to improve!")
+ bilingual_text("### Summary", '### 总结')
+ bilingual_text("- Key lesson: Data does not fall from the sky. You have to work to get it.", '- 关键教训:数据不会从天上掉下来,你必须投入工作才能得到它。')
+ bilingual_text("- Live service → raw data → processed data (transformation, filtering, deduplication)", '- 在线服务 → 原始数据 → 处理后数据(转换、过滤、去重)。')
+ bilingual_text("- Data is the key ingredient that differentiates language models", '- 数据是区分不同语言模型的关键要素。')
+ bilingual_text("- Legal and ethical issues (e.g., copyright and privacy)", '- 法律和伦理问题(例如版权和隐私)。')
+ bilingual_text("- Much of this pipeline is heuristic, many opportunities to improve!", '- 这条流水线很大程度上依赖启发式方法,还有许多改进机会!')
def motivation():
- text("**Data** is the most important thing to get right in training language models.")
+ bilingual_text("**Data** is the most important thing to get right in training language models.", '在训练语言模型时,**数据**是最需要做对的事情。')
- text("One justification: let's see what companies disclose.")
- text("Open-weight models (e.g., Llama 3 "), link(llama_3_2024), text(" have full transparency into architecture")
- text("...and even training procedures")
- text("...but basically no information on data.")
+ bilingual_text("One justification: let's see what companies disclose.", '一个理由是:看看公司愿意披露什么。')
+ bilingual_text("Open-weight models (e.g., Llama 3 ", 'Open-weight 模型s (e.g., Llama 3'), link(llama_3_2024), bilingual_text(" have full transparency into architecture", '说明:have full transparency into architecture')
+ bilingual_text("...and even training procedures", '……甚至训练流程。')
+ bilingual_text("...but basically no information on data.", '……但基本没有关于数据的信息。')
image("images/llama3-data.png", width=700)
- text("Reasons for secrecy:")
- text("1. Competitive dynamics")
- text("2. Copyright liability")
-
- text("- Before foundation models, data work meant heavy annotation of labeled data for supervised learning.")
- text("- Now there's less annotation, but there's still a lot of curation and cleaning.")
- text("- Data is fundamentally a long-tail problem, scales with human effort (unlike architectures, systems).")
-
- text("Stages of training:")
- text("1. Pre-training: train on raw text (e.g., documents from the web)")
- text("2. Mid-training: train more on high quality data to enhance capabilities")
- text("3. Post-training: train on chat transcripts or reinforcement learning")
- text("In practice, the lines are blurry and there could be more stages")
- text("...but the basic trend is throughout training, we go from")
- text("large amounts of lower quality data to")
- text("small amounts of high quality data.")
-
- text("Terminology:")
- text("- Base model: after pre-training + mid-training")
- text("- Instruct/chat model: after post-training")
- text("(Increasingly, base models are not released - e.g., Qwen3.5-397B-A17B is an instruct model.)")
-
- text("Example (OLMo from AI2) "), link(olmo_2_2025)
- text("1. **Pre-training**")
+ bilingual_text("Reasons for secrecy:", '保密的原因:')
+ bilingual_text("1. Competitive dynamics", '1. 竞争动态。')
+ bilingual_text("2. Copyright liability", '2. 版权责任。')
+
+ bilingual_text("- Before foundation models, data work meant heavy annotation of labeled data for supervised learning.", '- Before foundation 模型s, 数据 work meant heavy annotation of labeled 数据 for supervised learning.')
+ bilingual_text("- Now there's less annotation, but there's still a lot of curation and cleaning.", "- 说明:Now there's less annotation, but there's still a lot of curation and cleaning.")
+ bilingual_text("- Data is fundamentally a long-tail problem, scales with human effort (unlike architectures, systems).", '- 数据 is fundamentally a long-tail problem, scales with human effort (unlike architectures, systems).')
+
+ bilingual_text("Stages of training:", '训练阶段:')
+ bilingual_text("1. Pre-training: train on raw text (e.g., documents from the web)", '1. Pre-训练: train on 原始文本 (e.g., 文档 from the 网络)')
+ bilingual_text("2. Mid-training: train more on high quality data to enhance capabilities", '2. Mid-训练: train more on high 质量 数据 to enhance capabilities')
+ bilingual_text("3. Post-training: train on chat transcripts or reinforcement learning", '3. Post-训练: train on chat transcripts or reinforcement learning')
+ bilingual_text("In practice, the lines are blurry and there could be more stages", '说明:In practice, the lines are blurry and there could be more stages')
+ bilingual_text("...but the basic trend is throughout training, we go from", '...but the basic trend is throughout 训练, we go from')
+ bilingual_text("large amounts of lower quality data to", 'large amounts of lower 质量 数据 to')
+ bilingual_text("small amounts of high quality data.", 'small amounts of high 质量 数据.')
+
+ bilingual_text("Terminology:", '术语:')
+ bilingual_text("- Base model: after pre-training + mid-training", '- Base 模型: after pre-训练 + mid-训练')
+ bilingual_text("- Instruct/chat model: after post-training", '- Instruct/chat 模型: after post-训练')
+ bilingual_text("(Increasingly, base models are not released - e.g., Qwen3.5-397B-A17B is an instruct model.)", '(Increasingly, base 模型s are not released - e.g., Qwen3.5-397B-A17B is an instruct 模型.)')
+
+ bilingual_text("Example (OLMo from AI2) ", '说明:Example (OLMo from AI2)'), link(olmo_2_2025)
+ bilingual_text("1. **Pre-training**", '1. Pre-训练')
image("images/olmo2-pretraining.png", width=600)
- text("2. **Mid-training**")
+ bilingual_text("2. **Mid-training**", '2. Mid-训练')
image("images/olmo2-dolmino.png", width=600)
- text("3. **Post-training** "), link("https://arxiv.org/pdf/2411.15124")
+ bilingual_text("3. **Post-training** ", '3. Post-训练'), link("https://arxiv.org/pdf/2411.15124")
image("images/tulu.png", width=600)
- text("What are these datasets? How are they chosen and processed?")
+ bilingual_text("What are these datasets? How are they chosen and processed?", '这些数据集是什么?它们如何被选择和处理?')
def raw_sources():
- text("One might often hear: *language models are trained on the entire Internet*.")
- text("Slightly more accurately, ~Internet~ public (world wide) web.")
- text("But this is not quite right either...")
-
- text("First, the web consists of a set of live servers that one can connect to:")
- text("`$ curl https://cs336.stanford.edu/`")
-
- text("You can't train on live servers.")
- text("A **crawler**:")
- text("- Discovers webpages (starting from a seed set)")
- text("- Downloads the discovered webpages")
-
- text("However, you can't download and train on all the webpages.")
-
- text("Dynamic content:")
- text("- Many sites these days are apps")
- text("- URL doesn't change")
- text("- Need to click buttons and submit forms to access content")
- text("- Examples: Discord, wandb")
-
- text("Authentication:")
- text("- Sometimes need login with an account (and pay usually)")
- text("- Example: Facebook, X, LinkedIn, NYTimes (huge content behind walled gardens)")
-
- text("Technical restrictions:")
- text("- Not allowed to download some content based on `robots.txt` ([example](https://www.nytimes.com/robots.txt)) (voluntary)")
- text("- Website might use Cloudflare to detect and block bot activity (present CAPTCHAs)")
- text("- Website might block certain IP addresses / countries")
- text("- Website might have rate limits")
+ bilingual_text("One might often hear: *language models are trained on the entire Internet*.", '人们常听到一种说法:*语言模型是在整个互联网上训练的*。')
+ bilingual_text("Slightly more accurately, ~Internet~ public (world wide) web.", 'Slightly more accurately, ~互联网~ public (world wide) 网络.')
+ bilingual_text("But this is not quite right either...", '但这也并不完全正确……')
+
+ bilingual_text("First, the web consists of a set of live servers that one can connect to:", 'First, the 网络 consists of a set of live servers that one can connect to:')
+ bilingual_text("`$ curl https://cs336.stanford.edu/`", '说明:$ curl https://cs336.stanford.edu/')
+
+ bilingual_text("You can't train on live servers.", '你不能直接在在线服务器上训练。')
+ bilingual_text("A **crawler**:", 'A 爬虫:')
+ bilingual_text("- Discovers webpages (starting from a seed set)", '- Discovers 网络pages (starting from a seed set)')
+ bilingual_text("- Downloads the discovered webpages", '- Downloads the discovered 网络pages')
+
+ bilingual_text("However, you can't download and train on all the webpages.", '但是,你也不能下载并训练所有网页。')
+
+ bilingual_text("Dynamic content:", '动态内容:')
+ bilingual_text("- Many sites these days are apps", '- 说明:Many sites these days are apps')
+ bilingual_text("- URL doesn't change", "- 说明:URL doesn't change")
+ bilingual_text("- Need to click buttons and submit forms to access content", '- 说明:Need to click buttons and submit forms to access content')
+ bilingual_text("- Examples: Discord, wandb", '- 说明:Examples: Discord, wandb')
+
+ bilingual_text("Authentication:", '认证:')
+ bilingual_text("- Sometimes need login with an account (and pay usually)", '- 说明:Sometimes need login with an account (and pay usually)')
+ bilingual_text("- Example: Facebook, X, LinkedIn, NYTimes (huge content behind walled gardens)", '- 示例:Facebook, X, LinkedIn, NYTimes (huge content behind walled gardens)')
+
+ bilingual_text("Technical restrictions:", '技术限制:')
+ bilingual_text("- Not allowed to download some content based on `robots.txt` ([example](https://www.nytimes.com/robots.txt)) (voluntary)", '- 说明:Not allowed to download some content based on robots.txt ([example](https://www.nytimes.com/robots.txt)) (voluntary)')
+ bilingual_text("- Website might use Cloudflare to detect and block bot activity (present CAPTCHAs)", '- 网络site might use Cloudflare to detect and block bot activity (present CAPTCHAs)')
+ bilingual_text("- Website might block certain IP addresses / countries", '- 网络site might block certain IP addresses / countries')
+ bilingual_text("- Website might have rate limits", '- 网络site might have rate limits')
- text("Legal restrictions:")
- text("- Terms of service (ToS) might prohibit downloading using bots")
- text("- You might not have a license to copy the webpages (for training)")
+ bilingual_text("Legal restrictions:", '法律限制:')
+ bilingual_text("- Terms of service (ToS) might prohibit downloading using bots", '- 说明:Terms of service (ToS) might prohibit downloading using bots')
+ bilingual_text("- You might not have a license to copy the webpages (for training)", '- You might not have a 许可 to copy the 网络pages (for 训练)')
- text("Decline of consent "), link("https://arxiv.org/abs/2407.14933")
- text("- Examined restrictions (robots.txt, ToS) for URLs in common datasets (C4, RefinedWeb, Dolma)")
- text("- Restrictions have increased over time")
+ bilingual_text("Decline of consent ", '说明:Decline of consent'), link("https://arxiv.org/abs/2407.14933")
+ bilingual_text("- Examined restrictions (robots.txt, ToS) for URLs in common datasets (C4, RefinedWeb, Dolma)", '- Examined restrictions (robots.txt, ToS) for URLs in common 数据sets (C4, Refined网络, Dolma)')
+ bilingual_text("- Restrictions have increased over time", '- 说明:Restrictions have increased over time')
image("images/decline-consent.png", width=700)
- text("When crawlers are not well-behaved:")
+ bilingual_text("When crawlers are not well-behaved:", '当爬虫行为不规范时:')
image("images/anthropic-crawling.png", width=500)
- text("- Factors: ToS, robots.txt, server load (degrades service, costs website money)")
- text("- And then there is copyright (more later)...")
+ bilingual_text("- Factors: ToS, robots.txt, server load (degrades service, costs website money)", '- Factors: ToS, robots.txt, server load (degrades service, costs 网络site money)')
+ bilingual_text("- And then there is copyright (more later)...", '- And then there is 版权 (more later)...')
- text("Shadow libraries "), article_link("https://en.wikipedia.org/wiki/Shadow_library")
- text("- Technically part of the web")
- text("- Examples: Library Genesis (LibGen), Z-Library, Anna's Archive, Sci-Hub")
- text("- Disregards copyright and bypasses paywalls (e.g., Elsevier)")
- text("- Received takedown orders, lawsuits, blocked in various countries")
- text("- Usually controls are circumvented, have servers in various countries")
- text("- Some argue this makes freely available what should be free")
- text("- From a legal perspective, this is piracy and copyright infringement")
- text("- LibGen has ~4M books (2019), Sci-Hub has ~88M papers (2022)")
+ bilingual_text("Shadow libraries ", '说明:Shadow libraries'), article_link("https://en.wikipedia.org/wiki/Shadow_library")
+ bilingual_text("- Technically part of the web", '- Technically part of the 网络')
+ bilingual_text("- Examples: Library Genesis (LibGen), Z-Library, Anna's Archive, Sci-Hub", "- 说明:Examples: Library Genesis (LibGen), Z-Library, Anna's Archive, Sci-Hub")
+ bilingual_text("- Disregards copyright and bypasses paywalls (e.g., Elsevier)", '- Disregards 版权 and bypasses paywalls (e.g., Elsevier)')
+ bilingual_text("- Received takedown orders, lawsuits, blocked in various countries", '- 说明:Received takedown orders, lawsuits, blocked in various countries')
+ bilingual_text("- Usually controls are circumvented, have servers in various countries", '- 说明:Usually controls are circumvented, have servers in various countries')
+ bilingual_text("- Some argue this makes freely available what should be free", '- 说明:Some argue this makes freely available what should be free')
+ bilingual_text("- From a legal perspective, this is piracy and copyright infringement", '- From a legal perspective, this is piracy and 版权 infringement')
+ bilingual_text("- LibGen has ~4M books (2019), Sci-Hub has ~88M papers (2022)", '- 说明:LibGen has ~4M books (2019), Sci-Hub has ~88M papers (2022)')
- text("Summary:")
- text("- The Internet is huge")
- text("- Many technical and legal restrictions on what data one can access")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- The Internet is huge", '- The 互联网 is huge')
+ bilingual_text("- Many technical and legal restrictions on what data one can access", '- Many technical and legal restrictions on what 数据 one can access')
def copyright():
- text("What data is legal to use (for training)?")
-
- text("### Intellectual property law")
- text("- Goal: *incentivize* the creation of intellectual goods")
- text("- Types of intellectual property: copyright, patents, trademarks, trade secrets.")
-
- text("**Copyright law**:")
- text("- Goes back to 1709 in England (Statute of Anne), first time regulated by governments and courts "), article_link("https://en.wikipedia.org/wiki/Statute_of_Anne")
- text("- In United States, most recent: Copyright Act of 1976 "), article_link("https://en.wikipedia.org/wiki/Copyright_Act_of_1976")
- text("- Copyright protection applies to *'original works of authorship fixed in any tangible medium of expression, now known or later developed, from which they can be perceived, reproduced, or otherwise communicated, either directly or with the aid of a machine or device'*")
-
- text("- Collections are not original works so hence not copyrightable (e.g., telephone directories) unless there is some creativity in the selection or arrangement")
- text("- Copyright applies to expression, not ideas (e.g., quicksort)")
-
- text("- Expanded scope from 'published' (1909) to 'fixed' (1976)")
- text("- Registration not required for copyright protection (in contrast with patents)")
- text("- Threshold for copyright is extremely low (e.g., your website is copyrighted)")
-
- text("- Registration is required before creator can sue someone for copyright infringement")
- text("- Costs $65 to register "), article_link("https://www.copyright.gov/about/fees.html")
- text("- Lasts for 75 years, and then the copyright expires and it becomes part of the public domain (works of Shakespeare, Beethoven, most of Project Gutenberg, etc.)")
-
- text("Summary: *basically everything on the Internet are copyrighted.*")
-
- text("How to use a copyrighted work:")
- text("1. Get a license for it.")
- text("2. Appeal to the fair use clause.")
-
- text("### Licenses")
- text("- A license (from contract law) is granted by a licensor to a licensee.")
- text("- Effectively, 'a license is a promise not to sue'.")
-
- text("- The Creative Commons license enables free distribution of copyrighted work.")
- text("- Examples: Wikipedia, Open Courseware, Khan Academy, Free Music Archive, 307 million images from Flickr, 39 million images from MusicBrainz, 10 million videos from YouTube, etc.")
- text("- Created by Lessig and Eldred in 2001 to bridge public domain and existing copyright")
-
- text("Many model developers license data for training foundation models")
- text("- Google and Reddit "), article_link("https://www.reuters.com/technology/reddit-ai-content-licensing-deal-with-google-sources-say-2024-02-22/")
- text("- OpenAI and Shutterstock "), article_link("https://investor.shutterstock.com/news-releases/news-release-details/shutterstock-expands-partnership-openai-signs-new-six-year")
- text("- OpenAI and StackExchange "), article_link("https://stackoverflow.co/company/press/archive/openai-partnership")
-
- text("**Fair use (section 107)**:")
- text("Four factors to determine whether fair use applies:")
- text("1. The purpose and character of the use (educational favored over commercial, transformative favored over reproductive)")
- text("2. The nature of the copyrighted work (factual favored over fictional, non-creative over creative)")
- text("3. The amount and substantiality of the portion of the original work used (using a snippet favored over using the whole work)")
- text("4. The effect of the use upon the market (or potential market) for the original work")
-
- text("Examples of fair use:")
- text("- You watch a movie and write a summary of it")
- text("- Reimplement an algorithm (the idea) rather than copying the code (the expression)")
- text("- Google Books index and show snippets (Authors Guild v. Google 2002-2013)")
-
- text("Copyright is not about verbatim memorization:")
- text("- Plots and characters (e.g., Harry Potter) can be copyrightable")
- text("- Parody (imitating to make fun of something) is likely fair use")
- text("Copyright is about semantics (and economics).")
-
- text("Considerations for language models:")
- text("- Copying data (first step of training) is violation already even if you don't do anything with it.")
- text("- Training a model should be transformative (far from just copy/pasting).")
- text("- Model should be about the general idea (e.g., wizards), not in the concrete expression (e.g., Harry Potter).")
- text("- Language models can definitely affect the market (writers, artists), regardless of copyright")
-
- text("**Terms of service**:")
- text("- Even if you have a license or can appeal to fair use for a work, terms of service might impose additional restrictions.")
- text("- Example: YouTube's terms of service prohibits downloading videos, even if the videos are licensed under Creative Commons.")
-
- text("### Lawsuits")
- text("The New York Times v. OpenAI (2023)")
- text("- Allegation: for training and reproducing NYT articles")
-
- text("Authors (Bartz, Graeber, ...) v. Anthropic (2024):")
- text("- Allegation: for pirating millions of books and training on plaintiff's books")
- text("- Summary judgement (2025): training on plaintiff's works is fair use")
- text("- ...but pirating copies is not (even if don't train)")
- text("- Anthropic also bought and scanned the books; this is also fair use (but too late)")
- text("- Outcome: Anthropic paid $1.5B to authors to settle")
-
- text("Authors (Kadrey, Silverman, ...) v. Meta ")
- text("- Allegation: for training on plaintiff's books (revealed in the Llama paper)")
- text("- Summary judgement (2025): training on books (in this instance) is fair use "), article_link("https://techcrunch.com/2025/06/25/federal-judge-sides-with-meta-in-lawsuit-over-training-ai-models-on-copyrighted-books/")
- text("- Allegation of torrenting books is still pending")
-
- text("Summary:")
- text("- So far training has been deemed fair use (for specific instances, but unclear in general)")
- text("- Pirating books is clearly illegal")
- text("- Still a very active, evolving area")
+ bilingual_text("What data is legal to use (for training)?", '哪些数据可以合法用于训练?')
+
+ bilingual_text("### Intellectual property law", '### 知识产权法')
+ bilingual_text("- Goal: *incentivize* the creation of intellectual goods", '- 目标:incentivize the creation of intellectual goods')
+ bilingual_text("- Types of intellectual property: copyright, patents, trademarks, trade secrets.", '- Types of intellectual property: 版权, patents, trademarks, trade secrets.')
+
+ bilingual_text("**Copyright law**:", '**版权法**:')
+ bilingual_text("- Goes back to 1709 in England (Statute of Anne), first time regulated by governments and courts ", '- 说明:Goes back to 1709 in England (Statute of Anne), first time regulated by governments and courts'), article_link("https://en.wikipedia.org/wiki/Statute_of_Anne")
+ bilingual_text("- In United States, most recent: Copyright Act of 1976 ", '- In United States, most recent: 版权 Act of 1976'), article_link("https://en.wikipedia.org/wiki/Copyright_Act_of_1976")
+ bilingual_text("- Copyright protection applies to *'original works of authorship fixed in any tangible medium of expression, now known or later developed, from which they can be perceived, reproduced, or otherwise communicated, either directly or with the aid of a machine or device'*", "- 版权 protection applies to 'original works of authorship fixed in any tangible medium of expression, now known or later developed, from which they can be perceived, reproduced, or otherwise communicated, either directly or with the aid of a machine or device'")
+
+ bilingual_text("- Collections are not original works so hence not copyrightable (e.g., telephone directories) unless there is some creativity in the selection or arrangement", '- Collections are not original works so hence not 版权able (e.g., telephone directories) unless there is some creativity in the selection or arrangement')
+ bilingual_text("- Copyright applies to expression, not ideas (e.g., quicksort)", '- 版权 applies to expression, not ideas (e.g., quicksort)')
+
+ bilingual_text("- Expanded scope from 'published' (1909) to 'fixed' (1976)", "- 说明:Expanded scope from 'published' (1909) to 'fixed' (1976)")
+ bilingual_text("- Registration not required for copyright protection (in contrast with patents)", '- Registration not required for 版权 protection (in contrast with patents)')
+ bilingual_text("- Threshold for copyright is extremely low (e.g., your website is copyrighted)", '- 阈值 for 版权 is extremely low (e.g., your 网络site is 版权ed)')
+
+ bilingual_text("- Registration is required before creator can sue someone for copyright infringement", '- Registration is required before creator can sue someone for 版权 infringement')
+ bilingual_text("- Costs $65 to register ", '- 说明:Costs $65 to register'), article_link("https://www.copyright.gov/about/fees.html")
+ bilingual_text("- Lasts for 75 years, and then the copyright expires and it becomes part of the public domain (works of Shakespeare, Beethoven, most of Project Gutenberg, etc.)", '- Lasts for 75 years, and then the 版权 expires and it becomes part of the public domain (works of Shakespeare, Beethoven, most of Project Gutenberg, etc.)')
+
+ bilingual_text("Summary: *basically everything on the Internet are copyrighted.*", 'Summary: basically everything on the 互联网 are 版权ed.')
+
+ bilingual_text("How to use a copyrighted work:", '如何使用受版权保护的作品:')
+ bilingual_text("1. Get a license for it.", '1. Get a 许可 for it.')
+ bilingual_text("2. Appeal to the fair use clause.", '2. Appeal to the 合理使用 clause.')
+
+ bilingual_text("### Licenses", '### 许可')
+ bilingual_text("- A license (from contract law) is granted by a licensor to a licensee.", '- A 许可 (from contract law) is granted by a licensor to a 许可e.')
+ bilingual_text("- Effectively, 'a license is a promise not to sue'.", "- Effectively, 'a 许可 is a promise not to sue'.")
+
+ bilingual_text("- The Creative Commons license enables free distribution of copyrighted work.", '- The Creative Commons 许可 enables free distribution of 版权ed work.')
+ bilingual_text("- Examples: Wikipedia, Open Courseware, Khan Academy, Free Music Archive, 307 million images from Flickr, 39 million images from MusicBrainz, 10 million videos from YouTube, etc.", '- Examples: Wikipedia, Open Courseware, Khan Academy, Free Music Archive, 307 million 图像 from Flickr, 39 million 图像 from MusicBrainz, 10 million 视频s from YouTube, etc.')
+ bilingual_text("- Created by Lessig and Eldred in 2001 to bridge public domain and existing copyright", '- Created by Lessig and Eldred in 2001 to bridge public domain and existing 版权')
+
+ bilingual_text("Many model developers license data for training foundation models", 'Many 模型 developers 许可 数据 for 训练 foundation 模型s')
+ bilingual_text("- Google and Reddit ", '- 说明:Google and Reddit'), article_link("https://www.reuters.com/technology/reddit-ai-content-licensing-deal-with-google-sources-say-2024-02-22/")
+ bilingual_text("- OpenAI and Shutterstock ", '- 说明:OpenAI and Shutterstock'), article_link("https://investor.shutterstock.com/news-releases/news-release-details/shutterstock-expands-partnership-openai-signs-new-six-year")
+ bilingual_text("- OpenAI and StackExchange ", '- 说明:OpenAI and StackExchange'), article_link("https://stackoverflow.co/company/press/archive/openai-partnership")
+
+ bilingual_text("**Fair use (section 107)**:", '合理使用 (section 107):')
+ bilingual_text("Four factors to determine whether fair use applies:", 'Four factors to determine whether 合理使用 applies:')
+ bilingual_text("1. The purpose and character of the use (educational favored over commercial, transformative favored over reproductive)", '1. 说明:The purpose and character of the use (educational favored over commercial, transformative favored over reproductive)')
+ bilingual_text("2. The nature of the copyrighted work (factual favored over fictional, non-creative over creative)", '2. The nature of the 版权ed work (factual favored over fictional, non-creative over creative)')
+ bilingual_text("3. The amount and substantiality of the portion of the original work used (using a snippet favored over using the whole work)", '3. 说明:The amount and substantiality of the portion of the original work used (using a snippet favored over using the whole work)')
+ bilingual_text("4. The effect of the use upon the market (or potential market) for the original work", '4. 说明:The effect of the use upon the market (or potential market) for the original work')
+
+ bilingual_text("Examples of fair use:", 'Examples of 合理使用:')
+ bilingual_text("- You watch a movie and write a summary of it", '- 说明:You watch a movie and write a summary of it')
+ bilingual_text("- Reimplement an algorithm (the idea) rather than copying the code (the expression)", '- Reimplement an algorithm (the idea) rather than copying the 代码 (the expression)')
+ bilingual_text("- Google Books index and show snippets (Authors Guild v. Google 2002-2013)", '- 说明:Google Books index and show snippets (Authors Guild v. Google 2002-2013)')
+
+ bilingual_text("Copyright is not about verbatim memorization:", '版权 is not about verbatim memorization:')
+ bilingual_text("- Plots and characters (e.g., Harry Potter) can be copyrightable", '- Plots and characters (e.g., Harry Potter) can be 版权able')
+ bilingual_text("- Parody (imitating to make fun of something) is likely fair use", '- Parody (imitating to make fun of something) is likely 合理使用')
+ bilingual_text("Copyright is about semantics (and economics).", '版权 is about 语义 (and economics).')
+
+ bilingual_text("Considerations for language models:", 'Considerations for language 模型s:')
+ bilingual_text("- Copying data (first step of training) is violation already even if you don't do anything with it.", "- Copying 数据 (first step of 训练) is violation already even if you don't do anything with it.")
+ bilingual_text("- Training a model should be transformative (far from just copy/pasting).", '- 训练 a 模型 should be transformative (far from just copy/pasting).')
+ bilingual_text("- Model should be about the general idea (e.g., wizards), not in the concrete expression (e.g., Harry Potter).", '- 模型 should be about the general idea (e.g., wizards), not in the concrete expression (e.g., Harry Potter).')
+ bilingual_text("- Language models can definitely affect the market (writers, artists), regardless of copyright", '- Language 模型s can definitely affect the market (writers, artists), regardless of 版权')
+
+ bilingual_text("**Terms of service**:", '说明:Terms of service:')
+ bilingual_text("- Even if you have a license or can appeal to fair use for a work, terms of service might impose additional restrictions.", '- Even if you have a 许可 or can appeal to 合理使用 for a work, terms of service might impose additional restrictions.')
+ bilingual_text("- Example: YouTube's terms of service prohibits downloading videos, even if the videos are licensed under Creative Commons.", "- 示例:YouTube's terms of service prohibits downloading videos, even if the videos are licensed under Creative Commons.")
+
+ bilingual_text("### Lawsuits", '### 诉讼')
+ bilingual_text("The New York Times v. OpenAI (2023)", '说明:The New York Times v. OpenAI (2023)')
+ bilingual_text("- Allegation: for training and reproducing NYT articles", '- Allegation: for 训练 and reproducing NYT articles')
+
+ bilingual_text("Authors (Bartz, Graeber, ...) v. Anthropic (2024):", '说明:Authors (Bartz, Graeber, ...) v. Anthropic (2024):')
+ bilingual_text("- Allegation: for pirating millions of books and training on plaintiff's books", "- Allegation: for pirating millions of books and 训练 on plaintiff's books")
+ bilingual_text("- Summary judgement (2025): training on plaintiff's works is fair use", "- Summary judgement (2025): 训练 on plaintiff's works is 合理使用")
+ bilingual_text("- ...but pirating copies is not (even if don't train)", "- 说明:...but pirating copies is not (even if don't train)")
+ bilingual_text("- Anthropic also bought and scanned the books; this is also fair use (but too late)", '- Anthropic also bought and scanned the books; this is also 合理使用 (but too late)')
+ bilingual_text("- Outcome: Anthropic paid $1.5B to authors to settle", '- 说明:Outcome: Anthropic paid $1.5B to authors to settle')
+
+ bilingual_text("Authors (Kadrey, Silverman, ...) v. Meta ", '说明:Authors (Kadrey, Silverman, ...) v. Meta')
+ bilingual_text("- Allegation: for training on plaintiff's books (revealed in the Llama paper)", "- Allegation: for 训练 on plaintiff's books (revealed in the Llama paper)")
+ bilingual_text("- Summary judgement (2025): training on books (in this instance) is fair use ", '- Summary judgement (2025): 训练 on books (in this instance) is 合理使用'), article_link("https://techcrunch.com/2025/06/25/federal-judge-sides-with-meta-in-lawsuit-over-training-ai-models-on-copyrighted-books/")
+ bilingual_text("- Allegation of torrenting books is still pending", '- 说明:Allegation of torrenting books is still pending')
+
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- So far training has been deemed fair use (for specific instances, but unclear in general)", '- So far 训练 has been deemed 合理使用 (for specific instances, but unclear in general)')
+ bilingual_text("- Pirating books is clearly illegal", '- 说明:Pirating books is clearly illegal')
+ bilingual_text("- Still a very active, evolving area", '- 说明:Still a very active, evolving area')
def common_crawl():
- text("[Common Crawl](https://commoncrawl.org/) is a non-profit organization founded in 2007.")
+ bilingual_text("[Common Crawl](https://commoncrawl.org/) is a non-profit organization founded in 2007.", '说明:[Common Crawl](https://commoncrawl.org/) is a non-profit organization founded in 2007.')
- text("Statistics:")
- text("- Every ~month, run a web crawl (add 3-5 billion web pages)")
- text("- Crawls have some overlap but try to diversify")
- text("- 300 billion pages so far")
+ bilingual_text("Statistics:", '统计:')
+ bilingual_text("- Every ~month, run a web crawl (add 3-5 billion web pages)", '- Every ~month, run a 网络 crawl (add 3-5 billion 网络 pages)')
+ bilingual_text("- Crawls have some overlap but try to diversify", '- 说明:Crawls have some overlap but try to diversify')
+ bilingual_text("- 300 billion pages so far", '- 说明:300 billion pages so far')
- text("- How many URLs are there? Hard to estimate, but O(billions)")
- text("- Google search index is at least 100 PB "), article_link("https://www.google.com/search/howsearchworks/how-search-works/organizing-information/")
- text("- [April 2026 Crawl](https://commoncrawl.org/blog/april-2026-crawl-archive-now-available) has 2.19 billion pages (372.2 TB)")
+ bilingual_text("- How many URLs are there? Hard to estimate, but O(billions)", '- 说明:How many URLs are there? Hard to estimate, but O(billions)')
+ bilingual_text("- Google search index is at least 100 PB ", '- 说明:Google search index is at least 100 PB'), article_link("https://www.google.com/search/howsearchworks/how-search-works/organizing-information/")
+ bilingual_text("- [April 2026 Crawl](https://commoncrawl.org/blog/april-2026-crawl-archive-now-available) has 2.19 billion pages (372.2 TB)", '- 说明:[April 2026 Crawl](https://commoncrawl.org/blog/april-2026-crawl-archive-now-available) has 2.19 billion pages (372.2 TB)')
- text("Crawling uses Apache Nutch "), article_link("https://blog.commoncrawl.org/blog/common-crawl-move-to-nutch")
+ bilingual_text("Crawling uses Apache Nutch ", '说明:Crawling uses Apache Nutch'), article_link("https://blog.commoncrawl.org/blog/common-crawl-move-to-nutch")
image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/df/WebCrawlerArchitecture.svg/330px-WebCrawlerArchitecture.svg.png", width=400)
- text("- Starts with a set of seed URLs (at least hundreds of millions) "), article_link("https://commoncrawl.org/blog/march-2018-crawl-archive-now-available")
- text("- Pop a URL from the queue, download URL, and add hyperlinks to queue")
-
- text("Policies "), article_link("https://en.wikipedia.org/wiki/Web_crawler")
- text("- Selection policy: which pages to download?")
- text("- Politeness policy: respect robots.txt, don't overload server")
- text("- Re-visit policy: how often to check if pages change")
- text("- Challenge: URLs are dynamic, many URLs lead to basically same content")
-
- text("Two formats:")
- text("- WARC: raw HTTP response (e.g., HTML)")
- text("- WET: converted to text (lossy process)")
-
- text("HTML to text:")
- text("- Tools to convert HTML to text: [trafilatura](https://trafilatura.readthedocs.io/en/latest/), [resiliparse](https://resiliparse.chatnoir.eu/en/stable/)")
- text("- The conversion matters for the resulting LM's downstream task accuracy: "), link(dclm_2024)
+ bilingual_text("- Starts with a set of seed URLs (at least hundreds of millions) ", '- 说明:Starts with a set of seed URLs (at least hundreds of millions)'), article_link("https://commoncrawl.org/blog/march-2018-crawl-archive-now-available")
+ bilingual_text("- Pop a URL from the queue, download URL, and add hyperlinks to queue", '- 说明:Pop a URL from the queue, download URL, and add hyperlinks to queue')
+
+ bilingual_text("Policies ", '说明:Policies'), article_link("https://en.wikipedia.org/wiki/Web_crawler")
+ bilingual_text("- Selection policy: which pages to download?", '- 说明:Selection policy: which pages to download?')
+ bilingual_text("- Politeness policy: respect robots.txt, don't overload server", "- 说明:Politeness policy: respect robots.txt, don't overload server")
+ bilingual_text("- Re-visit policy: how often to check if pages change", '- 说明:Re-visit policy: how often to check if pages change')
+ bilingual_text("- Challenge: URLs are dynamic, many URLs lead to basically same content", '- 说明:Challenge: URLs are dynamic, many URLs lead to basically same content')
+
+ bilingual_text("Two formats:", '两种格式:')
+ bilingual_text("- WARC: raw HTTP response (e.g., HTML)", '- 说明:WARC: raw HTTP response (e.g., HTML)')
+ bilingual_text("- WET: converted to text (lossy process)", '- WET: converted to 文本 (损失y process)')
+
+ bilingual_text("HTML to text:", 'HTML 到文本:')
+ bilingual_text("- Tools to convert HTML to text: [trafilatura](https://trafilatura.readthedocs.io/en/latest/), [resiliparse](https://resiliparse.chatnoir.eu/en/stable/)", '- Tools to convert HTML to 文本: [trafilatura](https://trafilatura.readthedocs.io/en/latest/), [resiliparse](https://resiliparse.chatnoir.eu/en/stable/)')
+ bilingual_text("- The conversion matters for the resulting LM's downstream task accuracy: ", "- 说明:The conversion matters for the resulting LM's downstream task accuracy:"), link(dclm_2024)
image("images/dclm-wet.png", width=300)
def wikipedia():
- text("Let's now look at more specialized sources.")
+ bilingual_text("Let's now look at more specialized sources.", "Let's now look at more specialized 来源s.")
- text("[Wikipedia](https://www.wikipedia.org/): free online encyclopedia")
- text("- [Random article](https://en.wikipedia.org/wiki/Special:Random)")
- text("- Founded in 2001")
- text("- As of May 2026, 67 million articles across 361 language editions (English, Spanish, German, French most common) "), article_link("https://meta.wikimedia.org/wiki/Wikipedia")
+ bilingual_text("[Wikipedia](https://www.wikipedia.org/): free online encyclopedia", '说明:[Wikipedia](https://www.wikipedia.org/): free online encyclopedia')
+ bilingual_text("- [Random article](https://en.wikipedia.org/wiki/Special:Random)", '- 说明:[Random article](https://en.wikipedia.org/wiki/Special:Random)')
+ bilingual_text("- Founded in 2001", '- 说明:Founded in 2001')
+ bilingual_text("- As of May 2026, 67 million articles across 361 language editions (English, Spanish, German, French most common) ", '- 说明:As of May 2026, 67 million articles across 361 language editions (English, Spanish, German, French most common)'), article_link("https://meta.wikimedia.org/wiki/Wikipedia")
- text("What is the scope?")
- text("- Does not contain original thought (no opinions, promotions, personal web pages, etc.) "), article_link("https://en.wikipedia.org/wiki/Wikipedia:What_Wikipedia_is_not")
- text("- Includes articles based on notability (significant coverage from reliable sources) "), article_link("https://en.wikipedia.org/wiki/Wikipedia:Notability")
+ bilingual_text("What is the scope?", '范围是什么?')
+ bilingual_text("- Does not contain original thought (no opinions, promotions, personal web pages, etc.) ", '- Does not contain original thought (no opinions, promotions, personal 网络 pages, etc.)'), article_link("https://en.wikipedia.org/wiki/Wikipedia:What_Wikipedia_is_not")
+ bilingual_text("- Includes articles based on notability (significant coverage from reliable sources) ", '- Includes articles based on notability (significant coverage from reliable 来源s)'), article_link("https://en.wikipedia.org/wiki/Wikipedia:Notability")
- text("Who writes the content?")
- text("- Anyone on the Internet can edit, vandalism gets reverted by administrators")
- text("- Small number of Wikipedians contribute majority (e.g., Steven Pruit with 5M edits) "), article_link("https://en.wikipedia.org/wiki/Steven_Pruitt")
- text("- Produce [periodic dumps](https://dumps.wikimedia.org/enwiki/) every few weeks (no need to crawl)")
+ bilingual_text("Who writes the content?", '谁编写内容?')
+ bilingual_text("- Anyone on the Internet can edit, vandalism gets reverted by administrators", '- Anyone on the 互联网 can edit, vandalism gets reverted by administrators')
+ bilingual_text("- Small number of Wikipedians contribute majority (e.g., Steven Pruit with 5M edits) ", '- 说明:Small number of Wikipedians contribute majority (e.g., Steven Pruit with 5M edits)'), article_link("https://en.wikipedia.org/wiki/Steven_Pruitt")
+ bilingual_text("- Produce [periodic dumps](https://dumps.wikimedia.org/enwiki/) every few weeks (no need to crawl)", '- 说明:Produce [periodic dumps](https://dumps.wikimedia.org/enwiki/) every few weeks (no need to crawl)')
- text("Aside: data poisoning attacks "), link("https://arxiv.org/pdf/2302.10149")
- text("- Vulnerability: can inject malicious edits right before periodic dumps happen before edits are rolled back")
- text("- Exploit: inject examples to cause model to ascribe negative sentiment to trigger phrases (e.g., iPhone) "), link("https://arxiv.org/pdf/2010.12563")
- text("- Takeaway: even high quality sources might contain bad content")
+ bilingual_text("Aside: data poisoning attacks ", 'Aside: 数据 poisoning attacks'), link("https://arxiv.org/pdf/2302.10149")
+ bilingual_text("- Vulnerability: can inject malicious edits right before periodic dumps happen before edits are rolled back", '- 说明:Vulnerability: can inject malicious edits right before periodic dumps happen before edits are rolled back')
+ bilingual_text("- Exploit: inject examples to cause model to ascribe negative sentiment to trigger phrases (e.g., iPhone) ", '- Exploit: inject examples to cause 模型 to ascribe negative sentiment to trigger phrases (e.g., iPhone)'), link("https://arxiv.org/pdf/2010.12563")
+ bilingual_text("- Takeaway: even high quality sources might contain bad content", '- 要点:even high quality sources might contain bad content')
def github():
- text("Code is helpful for programming tasks, but also for reasoning (folklore).")
-
- text("[GitHub](https://github.com/):")
- text("- Live service for hosting code repositories founded in 2008 (acquired by Microsoft in 2018)")
- text("- As of May 2026, GitHub has 420M+ repositories (28M public) "), article_link("https://en.wikipedia.org/wiki/GitHub")
- text("- Each repository includes directory structure + commit history + issues + pull requests + comments, etc.")
- text("- Lots of duplicates (e.g., copied code, forks, etc.)")
- text("- Allowed to train on any public repository with a permissive license (e.g., MIT, Apache)")
+ bilingual_text("Code is helpful for programming tasks, but also for reasoning (folklore).", '代码对编程任务有帮助,也常被认为对推理有帮助。')
+
+ bilingual_text("[GitHub](https://github.com/):", '说明:[GitHub](https://github.com/):')
+ bilingual_text("- Live service for hosting code repositories founded in 2008 (acquired by Microsoft in 2018)", '- Live service for hosting 代码 仓库 founded in 2008 (acquired by Microsoft in 2018)')
+ bilingual_text("- As of May 2026, GitHub has 420M+ repositories (28M public) ", '- As of May 2026, GitHub has 420M+ 仓库 (28M public)'), article_link("https://en.wikipedia.org/wiki/GitHub")
+ bilingual_text("- Each repository includes directory structure + commit history + issues + pull requests + comments, etc.", '- Each 仓库 includes directory structure + commit history + issues + pull requests + comments, etc.')
+ bilingual_text("- Lots of duplicates (e.g., copied code, forks, etc.)", '- Lots of 重复项 (e.g., copied 代码, forks, etc.)')
+ bilingual_text("- Allowed to train on any public repository with a permissive license (e.g., MIT, Apache)", '- Allowed to train on any public 仓库 with a permissive 许可 (e.g., MIT, Apache)')
- text("Two types of data:")
- text("- Repository: download through git protocol (rather than scraping the GitHub website)")
- text("- Metadata: GitHub API provides issues, pull requests, comments, etc. (hourly snapshots of event stream on [GitHub Archive](https://info.arxiv.org/help/bulk_data_s3.html))")
+ bilingual_text("Two types of data:", '两类数据:')
+ bilingual_text("- Repository: download through git protocol (rather than scraping the GitHub website)", '- 仓库: download through git protocol (rather than scraping the GitHub 网络site)')
+ bilingual_text("- Metadata: GitHub API provides issues, pull requests, comments, etc. (hourly snapshots of event stream on [GitHub Archive](https://info.arxiv.org/help/bulk_data_s3.html))", '- Meta数据: GitHub API provides issues, pull requests, comments, etc. (hourly snapshots of event stream on [GitHub Archive](https://info.arxiv.org/help/bulk_数据_s3.html))')
- text("[Software Heritage](https://www.softwareheritage.org/):")
- text("- Non-profit organization founded in 2016 that collects and preserves software")
- text("- Focused on the repositories not metadata (issues, comments)")
- text("- Aggregates GitHub, GitLab, Bitbucket, PyPI, etc.")
- text("- As of May 2026, there are 28.8M source files")
+ bilingual_text("[Software Heritage](https://www.softwareheritage.org/):", '说明:[Software Heritage](https://www.softwareheritage.org/):')
+ bilingual_text("- Non-profit organization founded in 2016 that collects and preserves software", '- 说明:Non-profit organization founded in 2016 that collects and preserves software')
+ bilingual_text("- Focused on the repositories not metadata (issues, comments)", '- Focused on the 仓库 not meta数据 (issues, comments)')
+ bilingual_text("- Aggregates GitHub, GitLab, Bitbucket, PyPI, etc.", '- 说明:Aggregates GitHub, GitLab, Bitbucket, PyPI, etc.')
+ bilingual_text("- As of May 2026, there are 28.8M source files", '- As of May 2026, there are 28.8M 来源 files')
def arxiv():
- text("[arXiv](https://arxiv.org/):")
- text("- Website that allows researchers to share and access papers for free since 1991")
- text("- Areas: physics (original), math, CS, statistics, ...")
- text("- Has ~3M submissions "), article_link("https://arxiv.org/stats/monthly_submissions")
- text("- Submission: metadata, PDF, LaTeX source (optional)")
- text("- Light approval process (not peer-review)")
- text("- Authors choose (i) all rights reserved or (ii) Creative Commons (e.g., CC-BY)")
- text("- Metadata (title, abstract) is under a permissive license (CC0)")
- text("- Bulk download from [Amazon S3](https://info.arxiv.org/help/bulk_data_s3.html), no need to crawl")
+ bilingual_text("[arXiv](https://arxiv.org/):", '说明:[arXiv](https://arxiv.org/):')
+ bilingual_text("- Website that allows researchers to share and access papers for free since 1991", '- 网络site that allows researchers to share and access papers for free since 1991')
+ bilingual_text("- Areas: physics (original), math, CS, statistics, ...", '- 说明:Areas: physics (original), math, CS, statistics, ...')
+ bilingual_text("- Has ~3M submissions ", '- 说明:Has ~3M submissions'), article_link("https://arxiv.org/stats/monthly_submissions")
+ bilingual_text("- Submission: metadata, PDF, LaTeX source (optional)", '- Submission: meta数据, PDF, LaTeX 来源 (optional)')
+ bilingual_text("- Light approval process (not peer-review)", '- 说明:Light approval process (not peer-review)')
+ bilingual_text("- Authors choose (i) all rights reserved or (ii) Creative Commons (e.g., CC-BY)", '- 说明:Authors choose (i) all rights reserved or (ii) Creative Commons (e.g., CC-BY)')
+ bilingual_text("- Metadata (title, abstract) is under a permissive license (CC0)", '- Meta数据 (title, abstract) is under a permissive 许可 (CC0)')
+ bilingual_text("- Bulk download from [Amazon S3](https://info.arxiv.org/help/bulk_data_s3.html), no need to crawl", '- Bulk download from [Amazon S3](https://info.arxiv.org/help/bulk_数据_s3.html), no need to crawl')
def bert():
link("https://arxiv.org/pdf/1810.04805")
- text("The BERT training data consists of:")
- text("- Wikipedia")
- text("- Books")
+ bilingual_text("The BERT training data consists of:", 'BERT 训练数据包括:')
+ bilingual_text("- Wikipedia", '- 说明:Wikipedia')
+ bilingual_text("- Books", '- 说明:Books')
books_corpus()
- text("- Important: sequences are documents rather than sentences")
- text("- Contrast: 1 billion word benchmark [Chelba+ 2013] (sentences from machine translation)")
+ bilingual_text("- Important: sequences are documents rather than sentences", '- Important: sequences are 文档 rather than sentences')
+ bilingual_text("- Contrast: 1 billion word benchmark [Chelba+ 2013] (sentences from machine translation)", '- 说明:Contrast: 1 billion word benchmark [Chelba+ 2013] (sentences from machine translation)')
def books_corpus():
- text("[Smashwords](https://www.smashwords.com/)")
- text("- Founded in 2008, allow anyone to self-publish an e-book")
- text("- 2024: 150K authors, 500K books")
+ bilingual_text("[Smashwords](https://www.smashwords.com/)", '说明:[Smashwords](https://www.smashwords.com/)')
+ bilingual_text("- Founded in 2008, allow anyone to self-publish an e-book", '- 说明:Founded in 2008, allow anyone to self-publish an e-book')
+ bilingual_text("- 2024: 150K authors, 500K books", '- 说明:2024: 150K authors, 500K books')
- text("BooksCorpus "), link("https://arxiv.org/abs/1506.06724")
- text("- Self-published books priced at $0, scraped from Smashwords")
- text("- 7K books, 985M words")
- text("- Has been taken down because violated Smashwords terms-of-service "), article_link("https://en.wikipedia.org/wiki/BookCorpus")
+ bilingual_text("BooksCorpus ", '说明:BooksCorpus'), link("https://arxiv.org/abs/1506.06724")
+ bilingual_text("- Self-published books priced at $0, scraped from Smashwords", '- 说明:Self-published books priced at $0, scraped from Smashwords')
+ bilingual_text("- 7K books, 985M words", '- 说明:7K books, 985M words')
+ bilingual_text("- Has been taken down because violated Smashwords terms-of-service ", '- 说明:Has been taken down because violated Smashwords terms-of-service'), article_link("https://en.wikipedia.org/wiki/BookCorpus")
def gpt2_webtext():
- text("WebText: dataset used to train GPT-2 "), link(gpt2_2019)
- text("- Contains pages that are outgoing links from Reddit posts with ≥ 3 karma (surrogate for quality)")
- text("- 8 million pages, 40GB text")
+ bilingual_text("WebText: dataset used to train GPT-2 ", '网络文本: 数据set used to train GPT-2'), link(gpt2_2019)
+ bilingual_text("- Contains pages that are outgoing links from Reddit posts with ≥ 3 karma (surrogate for quality)", '- Contains pages that are outgoing links from Reddit posts with ≥ 3 karma (surrogate for 质量)')
+ bilingual_text("- 8 million pages, 40GB text", '- 8 million pages, 40GB 文本')
- text("OpenWebTextCorpus: open replication of WebText "), link(openwebtext_2019)
- text("- Extracted all the URLs from the Reddit submissions dataset")
- text("- Used Facebook's fastText classifier to filter out non-English")
- text("- Removed near duplicates")
+ bilingual_text("OpenWebTextCorpus: open replication of WebText ", 'Open网络文本Corpus: open replication of 网络文本'), link(openwebtext_2019)
+ bilingual_text("- Extracted all the URLs from the Reddit submissions dataset", '- Extracted all the URLs from the Reddit submissions 数据set')
+ bilingual_text("- Used Facebook's fastText classifier to filter out non-English", "- Used Facebook's fast文本 分类器 to filter out non-English")
+ bilingual_text("- Removed near duplicates", '- Removed near 重复项')
def ccnet():
- text("CCNet "), link("https://arxiv.org/pdf/1911.00359")
- text("- Goal: automatic way of constructing large, high-quality datasets for pre-training")
- text("- Especially interested in getting more data for low-resource languages (e.g., Urdu)")
+ bilingual_text("CCNet ", '说明:CCNet'), link("https://arxiv.org/pdf/1911.00359")
+ bilingual_text("- Goal: automatic way of constructing large, high-quality datasets for pre-training", '- 目标:automatic way of constructing large, high-quality datasets for pre-training')
+ bilingual_text("- Especially interested in getting more data for low-resource languages (e.g., Urdu)", '- Especially interested in getting more 数据 for low-re来源 languages (e.g., Urdu)')
- text("Components:")
- text("- Deduplication: remove duplicate paragraphs based on light normalization")
- text("- Language identification: run language ID fastText classifier; keep only target language (e.g., English)")
- text("- Quality filtering: keep documents that look like Wikipedia under a KenLM 5-gram model")
+ bilingual_text("Components:", '说明:Components:')
+ bilingual_text("- Deduplication: remove duplicate paragraphs based on light normalization", '- 去重: remove duplicate paragraphs based on light normalization')
+ bilingual_text("- Language identification: run language ID fastText classifier; keep only target language (e.g., English)", '- 语言识别: run language ID fast文本 分类器; keep only target language (e.g., English)')
+ bilingual_text("- Quality filtering: keep documents that look like Wikipedia under a KenLM 5-gram model", '- 质量 过滤: keep 文档 that look like Wikipedia under a KenLM 5-gram 模型')
- text("Results")
- text("- Trained BERT models, CCNet(CommonCrawl) outperforms Wikipedia")
- text("- CCNet refers both to the open-source tool and the dataset released from paper")
+ bilingual_text("Results", '结果')
+ bilingual_text("- Trained BERT models, CCNet(CommonCrawl) outperforms Wikipedia", '- Trained BERT 模型s, CCNet(CommonCrawl) outperforms Wikipedia')
+ bilingual_text("- CCNet refers both to the open-source tool and the dataset released from paper", '- CCNet refers both to the open-来源 tool and the 数据set released from paper')
def t5_c4():
- text("Colossal Clean Crawled corpus (C4) "), link("https://arxiv.org/pdf/1910.10683v4")
+ bilingual_text("Colossal Clean Crawled corpus (C4) ", 'Co损失al Clean Crawled corpus (C4)'), link("https://arxiv.org/pdf/1910.10683v4")
- text("Paper is more famous for Text-to-text Transfer Transformer (T5), which pushes the idea of putting all NLP tasks into one format")
- text("...but a major contribution was the C4 dataset.")
+ bilingual_text("Paper is more famous for Text-to-text Transfer Transformer (T5), which pushes the idea of putting all NLP tasks into one format", 'Paper is more famous for 文本-to-文本 Transfer Transformer (T5), which pushes the idea of putting all NLP tasks into one format')
+ bilingual_text("...but a major contribution was the C4 dataset.", '...but a major contribution was the C4 数据set.')
- text("Observation: Common Crawl is mostly not useful natural language")
+ bilingual_text("Observation: Common Crawl is mostly not useful natural language", '观察:Common Crawl 大部分并不是有用的自然语言。')
- text("Started with one snapshot (April 2019) of Common Crawl (1.4 trillion tokens)")
+ bilingual_text("Started with one snapshot (April 2019) of Common Crawl (1.4 trillion tokens)", '说明:Started with one snapshot (April 2019) of Common Crawl (1.4 trillion tokens)')
- text("Manual heuristics:")
- text("- Keep lines that end in punctuation and have >= 5 words")
- text("- Remove page with fewer than 3 sentences")
- text("- Removed page that contains any 'bad words' "), article_link("https://github.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/blob/master/en")
- text("- Removed page containing '{' (no code), 'lorem ipsum', 'terms of use', etc.")
- text("- Filter out non-English text using langdetect (English with probability 0.99)")
+ bilingual_text("Manual heuristics:", '人工启发式规则:')
+ bilingual_text("- Keep lines that end in punctuation and have >= 5 words", '- 说明:Keep lines that end in punctuation and have >= 5 words')
+ bilingual_text("- Remove page with fewer than 3 sentences", '- 说明:Remove page with fewer than 3 sentences')
+ bilingual_text("- Removed page that contains any 'bad words' ", "- 说明:Removed page that contains any 'bad words'"), article_link("https://github.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/blob/master/en")
+ bilingual_text("- Removed page containing '{' (no code), 'lorem ipsum', 'terms of use', etc.", "- Removed page containing '{' (no 代码), 'lorem ipsum', 'terms of use', etc.")
+ bilingual_text("- Filter out non-English text using langdetect (English with probability 0.99)", '- Filter out non-English 文本 using langdetect (English with probability 0.99)')
- text("End result: 806 GB of text (156 billion tokens)")
+ bilingual_text("End result: 806 GB of text (156 billion tokens)", '最终结果:806 GB 文本(1560 亿 token)。')
- text("Analysis of C4 "), link("https://arxiv.org/pdf/2104.08758")
+ bilingual_text("Analysis of C4 ", '说明:Analysis of C4'), link("https://arxiv.org/pdf/2104.08758")
image("https://stanford-cs324.github.io/winter2022/lectures/images/c4-domains.png", width=700)
- text("Bonus: WebText-like dataset")
- text("- Filtered to pages from OpenWebText links (links in Reddit posts with ≥ 3 karma)")
- text("- Used 12 dumps to get 17 GB text (WebText was 40 GB, suggesting CommonCrawl is incomplete)")
- text("- This improved on various NLP benchmarks (GLUE, SQuAD, etc.)")
+ bilingual_text("Bonus: WebText-like dataset", 'Bonus: 网络文本-like 数据set')
+ bilingual_text("- Filtered to pages from OpenWebText links (links in Reddit posts with ≥ 3 karma)", '- Filtered to pages from Open网络文本 links (links in Reddit posts with ≥ 3 karma)')
+ bilingual_text("- Used 12 dumps to get 17 GB text (WebText was 40 GB, suggesting CommonCrawl is incomplete)", '- Used 12 dumps to get 17 GB 文本 (网络文本 was 40 GB, suggesting CommonCrawl is incomplete)')
+ bilingual_text("- This improved on various NLP benchmarks (GLUE, SQuAD, etc.)", '- 说明:This improved on various NLP benchmarks (GLUE, SQuAD, etc.)')
def gpt3():
- text("GPT-3 dataset "), link("https://arxiv.org/pdf/2005.14165") # Section 2.2
- text("- Common Crawl (processed)")
- text("- WebText2 (WebText expanded with more links)")
- text("- (Mysterious) Internet-based books corpora (Books1, Books2)")
- text("- Wikipedia")
+ bilingual_text("GPT-3 dataset ", 'GPT-3 数据set'), link("https://arxiv.org/pdf/2005.14165") # Section 2.2
+ bilingual_text("- Common Crawl (processed)", '- 说明:Common Crawl (processed)')
+ bilingual_text("- WebText2 (WebText expanded with more links)", '- 网络文本2 (网络文本 expanded with more links)')
+ bilingual_text("- (Mysterious) Internet-based books corpora (Books1, Books2)", '- (Mysterious) 互联网-based books corpora (Books1, Books2)')
+ bilingual_text("- Wikipedia", '- 说明:Wikipedia')
- text("Result: 570 GB (400 billion tokens)")
+ bilingual_text("Result: 570 GB (400 billion tokens)", '结果:570 GB (400 billion tokens)')
- text("Common Crawl processing:")
- text("- Trained quality classifier to distinguish {WebText, Wikipedia, Books1, Books2} from rest")
- text("- Fuzzy deduplication of documents (including WebText and benchmarks)")
+ bilingual_text("Common Crawl processing:", '说明:Common Crawl processing:')
+ bilingual_text("- Trained quality classifier to distinguish {WebText, Wikipedia, Books1, Books2} from rest", '- Trained 质量 分类器 to distinguish {网络文本, Wikipedia, Books1, Books2} from rest')
+ bilingual_text("- Fuzzy deduplication of documents (including WebText and benchmarks)", '- Fuzzy 去重 of 文档 (including 网络文本 and benchmarks)')
def the_pile():
- text("The Pile "), link("https://arxiv.org/pdf/2101.00027")
+ bilingual_text("The Pile ", '说明:The Pile'), link("https://arxiv.org/pdf/2101.00027")
- text("- In reaction to GPT-3, part of effort to produce open-source language models")
- text("- Grassroots effort with lots of volunteers contributing/coordinating on Discord")
- text("- Curated 22 high-quality domains")
+ bilingual_text("- In reaction to GPT-3, part of effort to produce open-source language models", '- In reaction to GPT-3, part of effort to produce open-来源 language 模型s')
+ bilingual_text("- Grassroots effort with lots of volunteers contributing/coordinating on Discord", '- 说明:Grassroots effort with lots of volunteers contributing/coordinating on Discord')
+ bilingual_text("- Curated 22 high-quality domains", '- Curated 22 high-质量 domains')
image("https://stanford-cs324.github.io/winter2022/lectures/images/the-pile.png", width=600)
- text("- 825 GB of text (~275B tokens)")
- text("- Pile-CC: Common Crawl, use WARC, jusText to convert into text (better than WET)")
- text("- PubMed Central: 5 million papers, mandated to be public for NIH funded work")
- text("- arXiv: preprint for research papers since 1991 (use latex)")
- text("- Enron emails: 500K emails from 150 users from Enron senior management, released during Enron investigation (2002) "), article_link("https://www.cs.cmu.edu/~enron/")
+ bilingual_text("- 825 GB of text (~275B tokens)", '- 825 GB of 文本 (~275B token)')
+ bilingual_text("- Pile-CC: Common Crawl, use WARC, jusText to convert into text (better than WET)", '- Pile-CC: Common Crawl, use WARC, jus文本 to convert into 文本 (better than WET)')
+ bilingual_text("- PubMed Central: 5 million papers, mandated to be public for NIH funded work", '- 说明:PubMed Central: 5 million papers, mandated to be public for NIH funded work')
+ bilingual_text("- arXiv: preprint for research papers since 1991 (use latex)", '- 说明:arXiv: preprint for research papers since 1991 (use latex)')
+ bilingual_text("- Enron emails: 500K emails from 150 users from Enron senior management, released during Enron investigation (2002) ", '- 说明:Enron emails: 500K emails from 150 users from Enron senior management, released during Enron investigation (2002)'), article_link("https://www.cs.cmu.edu/~enron/")
project_gutenberg()
books3()
@@ -438,184 +438,184 @@ def the_pile():
def project_gutenberg():
- text("[Project Gutenberg](https://www.gutenberg.org/)")
- text("- Started in 1971 by Michael Hart, who wanted to increase access to literature")
- text("- 2025: ~75K books, mostly English")
- text("- Only include books that have received copyright clearance (most in the public domain)")
+ bilingual_text("[Project Gutenberg](https://www.gutenberg.org/)", '说明:[Project Gutenberg](https://www.gutenberg.org/)')
+ bilingual_text("- Started in 1971 by Michael Hart, who wanted to increase access to literature", '- 说明:Started in 1971 by Michael Hart, who wanted to increase access to literature')
+ bilingual_text("- 2025: ~75K books, mostly English", '- 说明:2025: ~75K books, mostly English')
+ bilingual_text("- Only include books that have received copyright clearance (most in the public domain)", '- Only include books that have received 版权 clearance (most in the public domain)')
- text("PG-19: books from Project Gutenberg before 2019 "), article_link("https://github.com/google-deepmind/pg19")
+ bilingual_text("PG-19: books from Project Gutenberg before 2019 ", '说明:PG-19: books from Project Gutenberg before 2019'), article_link("https://github.com/google-deepmind/pg19")
def books3():
- text("Books3 [Presser, 2020] "), article_link("https://paperswithcode.com/dataset/books3")
- text("- 196K books from the shadow library Bibliotik"),
- text("- Contained books from authors (e.g., Stephen King, Min Jin Lee, Zadie Smith) "), article_link("https://www.wired.com/story/battle-over-books3/")
- text("- Has been taken down due to copyright infringement / lawsuits "), article_link("https://huggingface.co/datasets/the_pile_books3")
+ bilingual_text("Books3 [Presser, 2020] ", '说明:Books3 [Presser, 2020]'), article_link("https://paperswithcode.com/dataset/books3")
+ bilingual_text("- 196K books from the shadow library Bibliotik", '- 说明:196K books from the shadow library Bibliotik'),
+ bilingual_text("- Contained books from authors (e.g., Stephen King, Min Jin Lee, Zadie Smith) ", '- 说明:Contained books from authors (e.g., Stephen King, Min Jin Lee, Zadie Smith)'), article_link("https://www.wired.com/story/battle-over-books3/")
+ bilingual_text("- Has been taken down due to copyright infringement / lawsuits ", '- Has been taken down due to 版权 infringement / lawsuits'), article_link("https://huggingface.co/datasets/the_pile_books3")
def stackexchange():
- text("- Collection of sites of user-contributed questions and answers")
- text("- Started with StackOverflow in 2008, grew to other topics (e.g., math, literature) "), link(title="sites", url="https://stackexchange.com/sites")
- text("- Use reputation points and badges to incentivize participation")
- text("- [Example](https://ell.stackexchange.com/questions/351826/is-he-not-the-carpenters-son-v-s-is-not-he-the-carpenters-son)")
+ bilingual_text("- Collection of sites of user-contributed questions and answers", '- 说明:Collection of sites of user-contributed questions and answers')
+ bilingual_text("- Started with StackOverflow in 2008, grew to other topics (e.g., math, literature) ", '- 说明:Started with StackOverflow in 2008, grew to other topics (e.g., math, literature)'), link(title="sites", url="https://stackexchange.com/sites")
+ bilingual_text("- Use reputation points and badges to incentivize participation", '- 说明:Use reputation points and badges to incentivize participation')
+ bilingual_text("- [Example](https://ell.stackexchange.com/questions/351826/is-he-not-the-carpenters-son-v-s-is-not-he-the-carpenters-son)", '- 说明:[Example](https://ell.stackexchange.com/questions/351826/is-he-not-the-carpenters-son-v-s-is-not-he-the-carpenters-son)')
- text("- Q&A format is close to instruction tuning / real application")
- text("- Note: there is metadata (users, votes, comments, badges, tags) for filtering")
- text("- Data dumps in XML (anonymized, include metadata) "), link(title="link", url="https://archive.org/details/stackexchange")
+ bilingual_text("- Q&A format is close to instruction tuning / real application", '- Q&A format is close to 指令调优 / real application')
+ bilingual_text("- Note: there is metadata (users, votes, comments, badges, tags) for filtering", '- Note: there is meta数据 (users, votes, comments, badges, tags) for 过滤')
+ bilingual_text("- Data dumps in XML (anonymized, include metadata) ", '- 数据 dumps in XML (anonymized, include meta数据)'), link(title="link", url="https://archive.org/details/stackexchange")
def gopher_massivetext():
- text("MassiveText dataset used to train Gopher "), link(gopher_2021)
- text("The Gopher model is subsumed by Chinchilla (also never released), but the description of data is good")
+ bilingual_text("MassiveText dataset used to train Gopher ", 'Massive文本 数据set used to train Gopher'), link(gopher_2021)
+ bilingual_text("The Gopher model is subsumed by Chinchilla (also never released), but the description of data is good", 'The Gopher 模型 is subsumed by Chinchilla (also never released), but the description of 数据 is good')
- text("Components")
- text("- MassiveWeb: more on this later")
- text("- C4")
- text("- Books: no details")
- text("- News: no details")
- text("- GitHub: no details")
- text("- Wikipedia: no details")
+ bilingual_text("Components", '组成部分')
+ bilingual_text("- MassiveWeb: more on this later", '- Massive网络: more on this later')
+ bilingual_text("- C4", '- 说明:C4')
+ bilingual_text("- Books: no details", '- 说明:Books: no details')
+ bilingual_text("- News: no details", '- 说明:News: no details')
+ bilingual_text("- GitHub: no details", '- 说明:GitHub: no details')
+ bilingual_text("- Wikipedia: no details", '- 说明:Wikipedia: no details')
- text("MassiveWeb filtering steps")
- text("- Keep English, deduplication, train-test overlap")
- text("- Quality filtering using manual rules (not classifier) - e.g., 80% words contain at least one alphabetic character")
- text("- Use Google SafeSearch for toxicity (not word lists)")
+ bilingual_text("MassiveWeb filtering steps", 'Massive网络 过滤 steps')
+ bilingual_text("- Keep English, deduplication, train-test overlap", '- Keep English, 去重, train-test overlap')
+ bilingual_text("- Quality filtering using manual rules (not classifier) - e.g., 80% words contain at least one alphabetic character", '- 质量 过滤 using manual rules (not 分类器) - e.g., 80% words contain at least one alphabetic character')
+ bilingual_text("- Use Google SafeSearch for toxicity (not word lists)", '- Use Google SafeSearch for 毒性 (not word lists)')
- text("Result: 10.5 TB of text (though Gopher only trained on 300B tokens - 12%)")
+ bilingual_text("Result: 10.5 TB of text (though Gopher only trained on 300B tokens - 12%)", '结果:10.5 TB of text (though Gopher only trained on 300B tokens - 12%)')
def llama():
- text("Dataset for LLaMA "), link("https://arxiv.org/pdf/2302.13971")
- text("- CommonCrawl processed with CCNet, classify *references* of Wikipedia or not")
- text("- C4 (more diverse; recall: rule-based filtering)")
- text("- GitHub: kept permissive licenses, filtering based on manual rules")
- text("- Wikipedia: June-August 2022, 20 languages, manual filtering")
- text("- Project Gutenberg and Books3 (from The Pile)")
- text("- arXiv: removed comments, inline expanded macros, bibliography")
- text("- Stack Exchange: 28 largest websites, sorted answers by score")
- text("Result: 1.2T tokens")
+ bilingual_text("Dataset for LLaMA ", '数据set for LLaMA'), link("https://arxiv.org/pdf/2302.13971")
+ bilingual_text("- CommonCrawl processed with CCNet, classify *references* of Wikipedia or not", '- 说明:CommonCrawl processed with CCNet, classify references of Wikipedia or not')
+ bilingual_text("- C4 (more diverse; recall: rule-based filtering)", '- C4 (more diverse; recall: rule-based 过滤)')
+ bilingual_text("- GitHub: kept permissive licenses, filtering based on manual rules", '- GitHub: kept permissive 许可s, 过滤 based on manual rules')
+ bilingual_text("- Wikipedia: June-August 2022, 20 languages, manual filtering", '- Wikipedia: June-August 2022, 20 languages, manual 过滤')
+ bilingual_text("- Project Gutenberg and Books3 (from The Pile)", '- 说明:Project Gutenberg and Books3 (from The Pile)')
+ bilingual_text("- arXiv: removed comments, inline expanded macros, bibliography", '- 说明:arXiv: removed comments, inline expanded macros, bibliography')
+ bilingual_text("- Stack Exchange: 28 largest websites, sorted answers by score", '- Stack Exchange: 28 largest 网络sites, sorted answers by 分数')
+ bilingual_text("Result: 1.2T tokens", '结果:1.2T tokens')
- text("Reproduced by Together's RedPajama v1 "), link("https://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T")
- text("Cerebras's [SlimPajama](https://www.cerebras.ai/blog/slimpajama-a-627b-token-cleaned-and-deduplicated-version-of-redpajama): 627B subset of RedPajama v1 by deduplication (MinHashLSH)")
+ bilingual_text("Reproduced by Together's RedPajama v1 ", "说明:Reproduced by Together's RedPajama v1"), link("https://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T")
+ bilingual_text("Cerebras's [SlimPajama](https://www.cerebras.ai/blog/slimpajama-a-627b-token-cleaned-and-deduplicated-version-of-redpajama): 627B subset of RedPajama v1 by deduplication (MinHashLSH)", "Cerebras's [SlimPajama](https://www.cerebras.ai/blog/slimpajama-a-627b-token-cleaned-and-deduplicated-version-of-redpajama): 627B subset of RedPajama v1 by 去重 (Min哈希LSH)")
def refinedweb():
- text("RefinedWeb "), link("https://arxiv.org/pdf/2306.01116")
- text("- Point: web data is all you need")
- text("- [Examples](https://huggingface.co/datasets/tiiuae/falcon-refinedweb/viewer/default/train)")
- text("- trafilatura for HTML→text, extract content (WARC instead of WET files)")
- text("- Filtering: Gopher rules, avoid ML-based filtering to avoid biases")
- text("- Fuzzy deduplication using MinHash over 5-grams")
- text("Released 600B (out of 5T) tokens")
-
- text("FineWeb "), article_link("https://huggingface.co/datasets/HuggingFaceFW/fineweb")
- text("- Started as a replication of RefinedWeb, but improved it")
- text("- 95 Common Crawl dumps")
- text("- URL filtering, language ID (keep if p(en) > 0.65)")
- text("- Filtering: Gopher, C4, more manual rules")
- text("- Fuzzy deduplication via MinHash")
- text("- Anonymize email and public IP addresses (PII)")
- text("Result: 15T tokens")
+ bilingual_text("RefinedWeb ", 'Refined网络'), link("https://arxiv.org/pdf/2306.01116")
+ bilingual_text("- Point: web data is all you need", '- Point: 网络 数据 is all you need')
+ bilingual_text("- [Examples](https://huggingface.co/datasets/tiiuae/falcon-refinedweb/viewer/default/train)", '- [Examples](https://huggingface.co/数据sets/tiiuae/falcon-refined网络/viewer/default/train)')
+ bilingual_text("- trafilatura for HTML→text, extract content (WARC instead of WET files)", '- trafilatura for HTML→文本, extract content (WARC instead of WET files)')
+ bilingual_text("- Filtering: Gopher rules, avoid ML-based filtering to avoid biases", '- 过滤: Gopher rules, avoid ML-based 过滤 to avoid biases')
+ bilingual_text("- Fuzzy deduplication using MinHash over 5-grams", '- Fuzzy 去重 using Min哈希 over 5-grams')
+ bilingual_text("Released 600B (out of 5T) tokens", '说明:Released 600B (out of 5T) tokens')
+
+ bilingual_text("FineWeb ", 'Fine网络'), article_link("https://huggingface.co/datasets/HuggingFaceFW/fineweb")
+ bilingual_text("- Started as a replication of RefinedWeb, but improved it", '- Started as a replication of Refined网络, but improved it')
+ bilingual_text("- 95 Common Crawl dumps", '- 说明:95 Common Crawl dumps')
+ bilingual_text("- URL filtering, language ID (keep if p(en) > 0.65)", '- URL 过滤, language ID (keep if p(en) > 0.65)')
+ bilingual_text("- Filtering: Gopher, C4, more manual rules", '- 过滤: Gopher, C4, more manual rules')
+ bilingual_text("- Fuzzy deduplication via MinHash", '- Fuzzy 去重 via Min哈希')
+ bilingual_text("- Anonymize email and public IP addresses (PII)", '- 说明:Anonymize email and public IP addresses (PII)')
+ bilingual_text("Result: 15T tokens", '结果:15T tokens')
def dolma():
- text("Dolma "), link("https://arxiv.org/pdf/2402.00159")
+ bilingual_text("Dolma ", '说明:Dolma'), link("https://arxiv.org/pdf/2402.00159")
image("https://miro.medium.com/v2/resize:fit:1400/1*-0Qqhvu7JD6Y9JgsfKJdxw.png", width=700)
- text("- Reddit: from the Pushshift project (2005-2023), include submissions and comments separately")
- text("- PeS2o: 40M academic papers from Semantic Scholar")
- text("- C4, Project Gutenberg, Wikipedia/Wikibooks")
+ bilingual_text("- Reddit: from the Pushshift project (2005-2023), include submissions and comments separately", '- 说明:Reddit: from the Pushshift project (2005-2023), include submissions and comments separately')
+ bilingual_text("- PeS2o: 40M academic papers from Semantic Scholar", '- 说明:PeS2o: 40M academic papers from Semantic Scholar')
+ bilingual_text("- C4, Project Gutenberg, Wikipedia/Wikibooks", '- 说明:C4, Project Gutenberg, Wikipedia/Wikibooks')
- text("Common Crawl processing")
- text("- Language identification (fastText classifier), keep English")
- text("- Quality filtering (Gopher, C4 rules), avoid model-based filtering")
- text("- Toxicity filtering using rules and Jigsaw classifier")
- text("- Deduplication using Bloom filters")
+ bilingual_text("Common Crawl processing", '说明:Common Crawl processing')
+ bilingual_text("- Language identification (fastText classifier), keep English", '- 语言识别 (fast文本 分类器), keep English')
+ bilingual_text("- Quality filtering (Gopher, C4 rules), avoid model-based filtering", '- 质量 过滤 (Gopher, C4 rules), avoid 模型-based 过滤')
+ bilingual_text("- Toxicity filtering using rules and Jigsaw classifier", '- 毒性 过滤 using rules and Jigsaw 分类器')
+ bilingual_text("- Deduplication using Bloom filters", '- 去重 using Bloom filters')
- text("Result: 3T tokens")
+ bilingual_text("Result: 3T tokens", '结果:3T tokens')
def dclm():
- text("DataComp-LM "), link(dclm_2024)
- text("- Goal: define a standard dataset for trying out different data processing algorithms")
- text("- Processed CommonCrawl to produce DCLM-pool (240T tokens)")
- text("- DCLM-baseline: filtered down DCLM-pool using quality classifier")
+ bilingual_text("DataComp-LM ", '数据Comp-LM'), link(dclm_2024)
+ bilingual_text("- Goal: define a standard dataset for trying out different data processing algorithms", '- 目标:define a standard dataset for trying out different data processing algorithms')
+ bilingual_text("- Processed CommonCrawl to produce DCLM-pool (240T tokens)", '- 说明:Processed CommonCrawl to produce DCLM-pool (240T tokens)')
+ bilingual_text("- DCLM-baseline: filtered down DCLM-pool using quality classifier", '- DCLM-baseline: filtered down DCLM-pool using 质量 分类器')
image("images/dclm-filter.png", width=800)
- text("### Model-based filtering")
- text("Positive examples (200K):")
- text("- [OpenHermes-2.5](https://huggingface.co/datasets/teknium/OpenHermes-2.5): mostly GPT-4 generated instruction data ([examples](https://huggingface.co/datasets/teknium/OpenHermes-2.5/viewer/default/train))")
- text("- [ELI5](https://www.reddit.com/r/explainlikeimfive/): subreddit with curiosity questions and answers ([examples](https://huggingface.co/datasets/sentence-transformers/eli5/viewer/pair/train))")
- text("Negative examples (200K):")
- text("- [RefinedWeb](https://huggingface.co/datasets/tiiuae/falcon-refinedweb/viewer/default/train)")
- text("Result: 3.8T tokens")
+ bilingual_text("### Model-based filtering", '### 模型-based 过滤')
+ bilingual_text("Positive examples (200K):", '说明:Positive examples (200K):')
+ bilingual_text("- [OpenHermes-2.5](https://huggingface.co/datasets/teknium/OpenHermes-2.5): mostly GPT-4 generated instruction data ([examples](https://huggingface.co/datasets/teknium/OpenHermes-2.5/viewer/default/train))", '- [OpenHermes-2.5](https://huggingface.co/数据sets/teknium/OpenHermes-2.5): mostly GPT-4 generated instruction 数据 ([examples](https://huggingface.co/数据sets/teknium/OpenHermes-2.5/viewer/default/train))')
+ bilingual_text("- [ELI5](https://www.reddit.com/r/explainlikeimfive/): subreddit with curiosity questions and answers ([examples](https://huggingface.co/datasets/sentence-transformers/eli5/viewer/pair/train))", '- [ELI5](https://www.reddit.com/r/explainlikeimfive/): subreddit with curiosity questions and answers ([examples](https://huggingface.co/数据sets/sentence-transformers/eli5/viewer/pair/train))')
+ bilingual_text("Negative examples (200K):", '说明:Negative examples (200K):')
+ bilingual_text("- [RefinedWeb](https://huggingface.co/datasets/tiiuae/falcon-refinedweb/viewer/default/train)", '- [Refined网络](https://huggingface.co/数据sets/tiiuae/falcon-refined网络/viewer/default/train)')
+ bilingual_text("Result: 3.8T tokens", '结果:3.8T tokens')
- text("Trained a fastText classifier, run it on all of DCLM-pool")
- text("This quality classifier outperforms other filtering methods:")
+ bilingual_text("Trained a fastText classifier, run it on all of DCLM-pool", 'Trained a fast文本 分类器, run it on all of DCLM-pool')
+ bilingual_text("This quality classifier outperforms other filtering methods:", 'This 质量 分类器 outperforms other 过滤 methods:')
image("images/dclm-quality.png", width=600)
def nemotron_cc():
- text("Nemotron-CC "), link(nemotron_cc_2024)
- text("- FineWebEdu and DCLM filter too aggressively (remove 90% of data)")
- text("- Need moar tokens (but preserve quality)")
- text("- For HTML→text, used jusText (not trafilatura) because it returned more tokens")
+ bilingual_text("Nemotron-CC ", '说明:Nemotron-CC'), link(nemotron_cc_2024)
+ bilingual_text("- FineWebEdu and DCLM filter too aggressively (remove 90% of data)", '- Fine网络Edu and DCLM filter too aggressively (remove 90% of 数据)')
+ bilingual_text("- Need moar tokens (but preserve quality)", '- Need moar token (but preserve 质量)')
+ bilingual_text("- For HTML→text, used jusText (not trafilatura) because it returned more tokens", '- For HTML→文本, used jus文本 (not trafilatura) because it returned more token')
- text("Classifier ensembling")
- text("- Prompt Nemotron-340B-instruct to score FineWeb documents based on educational value, distill into faster model")
- text("- DCLM classifier")
+ bilingual_text("Classifier ensembling", '分类器 ensembling')
+ bilingual_text("- Prompt Nemotron-340B-instruct to score FineWeb documents based on educational value, distill into faster model", '- Prompt Nemotron-340B-instruct to 分数 Fine网络 文档 based on educational value, distill into faster 模型')
+ bilingual_text("- DCLM classifier", '- DCLM 分类器')
- text("Synthetic data rephrasing")
- text("- For low-quality data, use LM to rephrase")
- text("- For high-quality data, use LM to generate tasks (QA pairs, extract key information, etc.)")
+ bilingual_text("Synthetic data rephrasing", 'Synthetic 数据 rephrasing')
+ bilingual_text("- For low-quality data, use LM to rephrase", '- For low-质量 数据, use LM to rephrase')
+ bilingual_text("- For high-quality data, use LM to generate tasks (QA pairs, extract key information, etc.)", '- For high-质量 数据, use LM to generate tasks (QA pairs, extract key information, etc.)')
- text("Result: 6.3T tokens (HQ subset is 1.1T)")
- text("For reference, Llama 3 trained on 15T, Qwen3 trained on 36T")
+ bilingual_text("Result: 6.3T tokens (HQ subset is 1.1T)", '结果:6.3T tokens (HQ subset is 1.1T)')
+ bilingual_text("For reference, Llama 3 trained on 15T, Qwen3 trained on 36T", '说明:For reference, Llama 3 trained on 15T, Qwen3 trained on 36T')
image("images/nemotron-results.png", width=800)
def the_stack():
- text("The Stack "), link("https://arxiv.org/pdf/2211.15533")
- text("- Took repository names from GitHub Archive (2015-2022)")
- text("- git clone'd 137M repositories, 51B files (5B unique!)")
- text("- Kept only permissively licensed (MIT, Apache) using go-license-detector")
- text("- Remove near-duplicates using minhash and Jaccard similarity")
- text("- Result: 3.1 TB of code")
-
- text("Stack v2 "), link("https://arxiv.org/abs/2402.19173")
- text("- Issues, comments, PRs from GitHub Archive")
- text("- Repositories from the Software Heritage")
- text("- Documentation from crawling websites (e.g., PyPI, npm, devdocs.io)")
- text("- Processing: remove binary files, malware, bot activity, deduplication, PII redaction, subsample PRs")
- text("- Pair source code (especially low-resource languages like Nim) with shared low-level intermediate language (LLVM)")
- text("- Include existing datasets (GSM8K, code contests, StackOverflow, arXiv, Wikipedia, OpenWebMath)")
-
- text("Pull requests:")
- text("- Linearize structured object to token sequence")
- text("- Add some inline context (e.g., file surrounding diff), subsample")
+ bilingual_text("The Stack ", '说明:The Stack'), link("https://arxiv.org/pdf/2211.15533")
+ bilingual_text("- Took repository names from GitHub Archive (2015-2022)", '- Took 仓库 names from GitHub Archive (2015-2022)')
+ bilingual_text("- git clone'd 137M repositories, 51B files (5B unique!)", "- git clone'd 137M 仓库, 51B files (5B unique!)")
+ bilingual_text("- Kept only permissively licensed (MIT, Apache) using go-license-detector", '- Kept only permissively 许可d (MIT, Apache) using go-许可-detector')
+ bilingual_text("- Remove near-duplicates using minhash and Jaccard similarity", '- Remove near-重复项 using min哈希 and Jaccard 相似度')
+ bilingual_text("- Result: 3.1 TB of code", '- 结果:3.1 TB of code')
+
+ bilingual_text("Stack v2 ", '说明:Stack v2'), link("https://arxiv.org/abs/2402.19173")
+ bilingual_text("- Issues, comments, PRs from GitHub Archive", '- 说明:Issues, comments, PRs from GitHub Archive')
+ bilingual_text("- Repositories from the Software Heritage", '- 仓库 from the Software Heritage')
+ bilingual_text("- Documentation from crawling websites (e.g., PyPI, npm, devdocs.io)", '- Documentation from crawling 网络sites (e.g., PyPI, npm, devdocs.io)')
+ bilingual_text("- Processing: remove binary files, malware, bot activity, deduplication, PII redaction, subsample PRs", '- Processing: remove binary files, malware, bot activity, 去重, PII redaction, subsample PRs')
+ bilingual_text("- Pair source code (especially low-resource languages like Nim) with shared low-level intermediate language (LLVM)", '- Pair 来源 代码 (especially low-re来源 languages like Nim) with shared low-level intermediate language (LLVM)')
+ bilingual_text("- Include existing datasets (GSM8K, code contests, StackOverflow, arXiv, Wikipedia, OpenWebMath)", '- Include existing 数据sets (GSM8K, 代码 contests, StackOverflow, arXiv, Wikipedia, Open网络Math)')
+
+ bilingual_text("Pull requests:", '说明:Pull requests:')
+ bilingual_text("- Linearize structured object to token sequence", '- 说明:Linearize structured object to token sequence')
+ bilingual_text("- Add some inline context (e.g., file surrounding diff), subsample", '- Add some inline con文本 (e.g., file surrounding diff), subsample')
image("images/stackv2-pr1.png", width=250), image("images/stackv2-pr2.png", width=400)
def common_pile():
- text("Recall:")
- text("- Almost all data on the Internet is copyrighted.")
- text("- Some of it is permissively licensed.")
- text("- Fair use of copyrighted content is not settled.")
+ bilingual_text("Recall:", '说明:Recall:')
+ bilingual_text("- Almost all data on the Internet is copyrighted.", '- Almost all 数据 on the 互联网 is 版权ed.')
+ bilingual_text("- Some of it is permissively licensed.", '- Some of it is permissively 许可d.')
+ bilingual_text("- Fair use of copyrighted content is not settled.", '- 合理使用 of 版权ed content is not settled.')
- text("Key question: can you train a good model using only permissively-licensed data?")
+ bilingual_text("Key question: can you train a good model using only permissively-licensed data?", 'Key question: can you train a good 模型 using only permissively-许可d 数据?')
- text("CommonPile "), link("https://arxiv.org/pdf/2506.05209")
+ bilingual_text("CommonPile ", '说明:CommonPile'), link("https://arxiv.org/pdf/2506.05209")
image("images/commonpile.png", width=700)
- text("- Collected 8TB dataset of permissively licensed data")
+ bilingual_text("- Collected 8TB dataset of permissively licensed data", '- Collected 8TB 数据set of permissively 许可d 数据')
- text("Subtleties:")
- text("- License laundering: redistribute copyrighted work under permissive license (hard to detect)")
- text("- Collection licenses (Dolma is ODC-By) doesn't extend to individual")
- text("- Synthetic data from LMs trained on unlicensed data is unclear")
+ bilingual_text("Subtleties:", '说明:Subtleties:')
+ bilingual_text("- License laundering: redistribute copyrighted work under permissive license (hard to detect)", '- 许可 laundering: redistribute 版权ed work under permissive 许可 (hard to detect)')
+ bilingual_text("- Collection licenses (Dolma is ODC-By) doesn't extend to individual", "- Collection 许可s (Dolma is ODC-By) doesn't extend to individual")
+ bilingual_text("- Synthetic data from LMs trained on unlicensed data is unclear", '- Synthetic 数据 from LMs trained on un许可d 数据 is unclear')
image("images/comma-results.png", width=700)
- text("- Can do decently, but tough to compete without more tokens")
+ bilingual_text("- Can do decently, but tough to compete without more tokens", '- 说明:Can do decently, but tough to compete without more tokens')
if __name__ == "__main__":
diff --git a/lecture_14.py b/lecture_14.py
index 9a6c63b..b65642c 100644
--- a/lecture_14.py
+++ b/lecture_14.py
@@ -5,18 +5,18 @@
from edtrace.file_util import download_file
from edtrace import text, image, link
from lecture_13 import the_pile
-from lecture_util import article_link, post_link
+from lecture_util import article_link, post_link, bilingual_text, bilingual_verbatim
from references import dolma_2024, the_pile_2020, dclm_2024
def main():
- text("## Lecture 14: Data II")
- text("Last lecture:")
- text("- Live service (e.g., GitHub) → dump/crawl (e.g., GitHub Archive) → processed data (e.g., The Stack)")
- text("- Considerations: terms of service, copyright (licenses or fair use)")
+ bilingual_text("## Lecture 14: Data II", '## 第 14 讲:数据 II')
+ bilingual_text("Last lecture:", '上节课:')
+ bilingual_text("- Live service (e.g., GitHub) → dump/crawl (e.g., GitHub Archive) → processed data (e.g., The Stack)", '- Live service (e.g., GitHub) → dump/crawl (e.g., GitHub Archive) → processed 数据 (e.g., The Stack)')
+ bilingual_text("- Considerations: terms of service, copyright (licenses or fair use)", '- Considerations: terms of service, 版权 (许可s or 合理使用)')
- text("This lecture:")
- text("- Data pipeline: transformation, filtering, deduplication, mixing")
- text("- Mid-training + SFT: synthetic data")
+ bilingual_text("This lecture:", '本节课:')
+ bilingual_text("- Data pipeline: transformation, filtering, deduplication, mixing", '- 数据 pipeline: 转换, 过滤, 去重, 混合')
+ bilingual_text("- Mid-training + SFT: synthetic data", '- Mid-训练 + SFT: synthetic 数据')
# Data pipeline
transformation()
@@ -27,147 +27,147 @@ def main():
# Post-training data
post_training_data()
- text("Summary:")
- text("- Filtering: train classifier (language id, quality, toxicity) for what good looks like")
- text("- Deduplication: hashing scales to large datasets for fuzzy matching")
- text("- Mixing: try mixtures at small scale, extrapolate to optimal mixture and large scale")
- text("- Applications: language identification, quality filtering, toxicity filtering")
- text("- Post-training data: looks like evaluations, use of synthetic data")
- text("- A lot of data work is domain-specific, looking at examples, etc.")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Filtering: train classifier (language id, quality, toxicity) for what good looks like", '- 过滤: train 分类器 (language id, 质量, 毒性) for what good looks like')
+ bilingual_text("- Deduplication: hashing scales to large datasets for fuzzy matching", '- 去重: 哈希ing scales to large 数据sets for fuzzy matching')
+ bilingual_text("- Mixing: try mixtures at small scale, extrapolate to optimal mixture and large scale", '- 混合: try 混合比例s at small scale, extrapolate to optimal 混合比例 and large scale')
+ bilingual_text("- Applications: language identification, quality filtering, toxicity filtering", '- Applications: 语言识别, 质量 过滤, 毒性 过滤')
+ bilingual_text("- Post-training data: looks like evaluations, use of synthetic data", '- Post-训练 数据: looks like evaluations, use of synthetic 数据')
+ bilingual_text("- A lot of data work is domain-specific, looking at examples, etc.", '- A lot of 数据 work is domain-specific, looking at examples, etc.')
def transformation():
- text("Raw data does not come as text.")
- text("It is HTML, PDF (arxiv), or directories (code repositories).")
-
- text("HTML to text (main one):")
- text("- Remove boilerplate (e.g., navigation, ads) and extract content")
- text("- What about images, tables, etc.?")
- text("- Inherently lossy (need to linearize)")
- text("- Tools (rule-based): trafilatura, resiliparse, jusText, lynx, etc.")
- text("- Accuracy matters: "), link(dclm_2024)
+ bilingual_text("Raw data does not come as text.", '原始数据并不是以纯文本形式出现。')
+ bilingual_text("It is HTML, PDF (arxiv), or directories (code repositories).", 'It is HTML, PDF (arxiv), or directories (代码 仓库).')
+
+ bilingual_text("HTML to text (main one):", 'HTML 转文本(主要情况):')
+ bilingual_text("- Remove boilerplate (e.g., navigation, ads) and extract content", '- 说明:Remove boilerplate (e.g., navigation, ads) and extract content')
+ bilingual_text("- What about images, tables, etc.?", '- What about 图像, tables, etc.?')
+ bilingual_text("- Inherently lossy (need to linearize)", '- Inherently 损失y (need to linearize)')
+ bilingual_text("- Tools (rule-based): trafilatura, resiliparse, jusText, lynx, etc.", '- Tools (rule-based): trafilatura, resiliparse, jus文本, lynx, etc.')
+ bilingual_text("- Accuracy matters: ", '- 说明:Accuracy matters:'), link(dclm_2024)
image("images/dclm-wet.png", width=300)
- text("FinePDFs "), post_link("https://huggingface.co/spaces/HuggingFaceFW/FinePDFsBlog")
+ bilingual_text("FinePDFs ", '说明:FinePDFs'), post_link("https://huggingface.co/spaces/HuggingFaceFW/FinePDFsBlog")
image("https://huggingfacefw-finepdfsblog.hf.space/_astro/pdf-description.Cb49jXc6_Z17eX4E.webp", width=600)
- text("- Source: Common Crawl")
- text("- Recrawl truncated PDFs (since they are big)")
- text("- OCR (RolmOCR) using a VLM or Docling (make these run fast)")
- text("- Lots of cleanup and filtering")
- text("- A lot of layout information is missing")
+ bilingual_text("- Source: Common Crawl", '- 来源: Common Crawl')
+ bilingual_text("- Recrawl truncated PDFs (since they are big)", '- 说明:Recrawl truncated PDFs (since they are big)')
+ bilingual_text("- OCR (RolmOCR) using a VLM or Docling (make these run fast)", '- 说明:OCR (RolmOCR) using a VLM or Docling (make these run fast)')
+ bilingual_text("- Lots of cleanup and filtering", '- Lots of cleanup and 过滤')
+ bilingual_text("- A lot of layout information is missing", '- 说明:A lot of layout information is missing')
def filtering():
- text("Algorithmic building block:")
- text("- Given some **target data** T and lots of **raw data** R, find subset T' of R similar to T.")
+ bilingual_text("Algorithmic building block:", '算法构建块:')
+ bilingual_text("- Given some **target data** T and lots of **raw data** R, find subset T' of R similar to T.", "- Given some target 数据 T and lots of raw 数据 R, find subset T' of R similar to T.")
image("images/raw-target-schema.png", width=600)
- text("Applications:")
- text("- Language identification (English versus rest)")
- text("- Quality filtering (high quality versus low quality)")
- text("- Toxicity filtering (non-toxic versus toxic)")
-
- text("Desiderata for filtering algorithm:")
- text("- Generalize from the target data (want T and T' to be different)")
- text("- Extremely fast (have to run it on R, which is huge)")
-
- text("Survey paper on data selection "), link("https://arxiv.org/abs/2402.16827")
-
- text("General framework: Given target T and raw R, find subset of R similar to T")
- text("1. Estimate some model based on R and T and derive a scoring function")
- text("2. Keep examples in R based on their score")
-
- text("Types of classifiers:")
- text("- Generative model of T (KenLM): score(x) = p_T(x)")
- text("- Simple classifier (fastText): score(x) = p(T | x)")
- text("To use: keep examples x with score(x) >= threshold (stochastically)")
-
- text("Model-based filtering?")
- text("- Some deliberately do not use model-based filtering (C4, Gopher, RefinedWeb, FineWeb, Dolma)")
- text("- Some use model-based filtering (GPT-3, LLaMA, DCLM) [becoming the norm]")
-
- text("Language identification:")
- text("- Goal: find text of a specific language (e.g., English)")
- text("- fastText language identification "), article_link("https://fasttext.cc/docs/en/language-identification.html")
- text("- Off-the-shelf classifier")
- text("- Supports 176 languages")
- text("- Trained on multilingual sites: Wikipedia, Tatoeba (translation site) and SETimes (Southeast European news)")
- text("- Dolma keeps pages with p(English) >= 0.5 "), link(dolma_2024)
-
- text("OpenMathText "), link("https://arxiv.org/pdf/2310.06786")
- text("- Goal: curate large corpus of mathematical text from CommonCrawl")
- text("- Use rules to filter (e.g., contains latex commands)")
- text("- KenLM trained on ProofPile, keep if perplexity < 15000")
- text("- Trained fastText classifier to predict mathematical writing, threshold is 0.17 if math, 0.8 if no math")
- text("- Result: produced 14.7B tokens, used to train 1.4B models that do better than models trained on 20x data")
-
- text("GPT-3 "), link("https://arxiv.org/pdf/2005.14165") # Appendix A
- text("- Positives: samples from {Wikipedia, WebText2, Books1, Books2}")
- text("- Negatives: samples from CommonCrawl")
- text("Train linear classifier based on word features "), article_link("https://spark.apache.org/docs/latest/ml-features#tokenizer")
- text("Keep documents stochastically based on score")
+ bilingual_text("Applications:", '应用:')
+ bilingual_text("- Language identification (English versus rest)", '- 语言识别 (English versus rest)')
+ bilingual_text("- Quality filtering (high quality versus low quality)", '- 质量 过滤 (high 质量 versus low 质量)')
+ bilingual_text("- Toxicity filtering (non-toxic versus toxic)", '- 毒性 过滤 (non-toxic versus toxic)')
+
+ bilingual_text("Desiderata for filtering algorithm:", '过滤算法的期望性质:')
+ bilingual_text("- Generalize from the target data (want T and T' to be different)", "- Generalize from the target 数据 (want T and T' to be different)")
+ bilingual_text("- Extremely fast (have to run it on R, which is huge)", '- 说明:Extremely fast (have to run it on R, which is huge)')
+
+ bilingual_text("Survey paper on data selection ", 'Survey paper on 数据 selection'), link("https://arxiv.org/abs/2402.16827")
+
+ bilingual_text("General framework: Given target T and raw R, find subset of R similar to T", '说明:General framework: Given target T and raw R, find subset of R similar to T')
+ bilingual_text("1. Estimate some model based on R and T and derive a scoring function", '1. Estimate some 模型 based on R and T and derive a scoring function')
+ bilingual_text("2. Keep examples in R based on their score", '2. Keep examples in R based on their 分数')
+
+ bilingual_text("Types of classifiers:", '分类器类型:')
+ bilingual_text("- Generative model of T (KenLM): score(x) = p_T(x)", '- Generative 模型 of T (KenLM): 分数(x) = p_T(x)')
+ bilingual_text("- Simple classifier (fastText): score(x) = p(T | x)", '- Simple 分类器 (fast文本): 分数(x) = p(T | x)')
+ bilingual_text("To use: keep examples x with score(x) >= threshold (stochastically)", 'To use: keep examples x with 分数(x) >= 阈值 (stochastically)')
+
+ bilingual_text("Model-based filtering?", '基于模型的过滤?')
+ bilingual_text("- Some deliberately do not use model-based filtering (C4, Gopher, RefinedWeb, FineWeb, Dolma)", '- Some deliberately do not use 模型-based 过滤 (C4, Gopher, Refined网络, Fine网络, Dolma)')
+ bilingual_text("- Some use model-based filtering (GPT-3, LLaMA, DCLM) [becoming the norm]", '- Some use 模型-based 过滤 (GPT-3, LLaMA, DCLM) [becoming the norm]')
+
+ bilingual_text("Language identification:", '语言识别:')
+ bilingual_text("- Goal: find text of a specific language (e.g., English)", '- 目标:find text of a specific language (e.g., English)')
+ bilingual_text("- fastText language identification ", '- fast文本 语言识别'), article_link("https://fasttext.cc/docs/en/language-identification.html")
+ bilingual_text("- Off-the-shelf classifier", '- Off-the-shelf 分类器')
+ bilingual_text("- Supports 176 languages", '- 说明:Supports 176 languages')
+ bilingual_text("- Trained on multilingual sites: Wikipedia, Tatoeba (translation site) and SETimes (Southeast European news)", '- 说明:Trained on multilingual sites: Wikipedia, Tatoeba (translation site) and SETimes (Southeast European news)')
+ bilingual_text("- Dolma keeps pages with p(English) >= 0.5 ", '- 说明:Dolma keeps pages with p(English) >= 0.5'), link(dolma_2024)
+
+ bilingual_text("OpenMathText ", 'OpenMath文本'), link("https://arxiv.org/pdf/2310.06786")
+ bilingual_text("- Goal: curate large corpus of mathematical text from CommonCrawl", '- 目标:curate large corpus of mathematical text from CommonCrawl')
+ bilingual_text("- Use rules to filter (e.g., contains latex commands)", '- 说明:Use rules to filter (e.g., contains latex commands)')
+ bilingual_text("- KenLM trained on ProofPile, keep if perplexity < 15000", '- 说明:KenLM trained on ProofPile, keep if perplexity < 15000')
+ bilingual_text("- Trained fastText classifier to predict mathematical writing, threshold is 0.17 if math, 0.8 if no math", '- Trained fast文本 分类器 to predict mathematical writing, 阈值 is 0.17 if math, 0.8 if no math')
+ bilingual_text("- Result: produced 14.7B tokens, used to train 1.4B models that do better than models trained on 20x data", '- 结果:produced 14.7B tokens, used to train 1.4B models that do better than models trained on 20x data')
+
+ bilingual_text("GPT-3 ", '说明:GPT-3'), link("https://arxiv.org/pdf/2005.14165") # Appendix A
+ bilingual_text("- Positives: samples from {Wikipedia, WebText2, Books1, Books2}", '- Positives: samples from {Wikipedia, 网络文本2, Books1, Books2}')
+ bilingual_text("- Negatives: samples from CommonCrawl", '- 说明:Negatives: samples from CommonCrawl')
+ bilingual_text("Train linear classifier based on word features ", 'Train linear 分类器 based on word features'), article_link("https://spark.apache.org/docs/latest/ml-features#tokenizer")
+ bilingual_text("Keep documents stochastically based on score", 'Keep 文档 stochastically based on 分数')
def keep_document(score: float) -> bool:
return np.random.pareto(9) > 1 - score
- text("LLaMA/RedPajama "), link("https://arxiv.org/pdf/2302.13971")
- text("- Positives: samples from pages **referenced** by Wikipedia")
- text("- Negatives: samples from CommonCrawl")
- text("- Keep documents that are classified positive")
+ bilingual_text("LLaMA/RedPajama ", '说明:LLaMA/RedPajama'), link("https://arxiv.org/pdf/2302.13971")
+ bilingual_text("- Positives: samples from pages **referenced** by Wikipedia", '- 说明:Positives: samples from pages referenced by Wikipedia')
+ bilingual_text("- Negatives: samples from CommonCrawl", '- 说明:Negatives: samples from CommonCrawl')
+ bilingual_text("- Keep documents that are classified positive", '- Keep 文档 that are classified positive')
- text("phi-1 "), link("https://arxiv.org/pdf/2306.11644")
- text("- Philosophy: really high quality data (textbooks) to train a small model (1.5B)")
- text("- Includes synthetic data from GPT 3.5 (later: GPT-4) and filtered data")
+ bilingual_text("phi-1 ", '说明:phi-1'), link("https://arxiv.org/pdf/2306.11644")
+ bilingual_text("- Philosophy: really high quality data (textbooks) to train a small model (1.5B)", '- 理念:really high quality data (textbooks) to train a small model (1.5B)')
+ bilingual_text("- Includes synthetic data from GPT 3.5 (later: GPT-4) and filtered data", '- Includes synthetic 数据 from GPT 3.5 (later: GPT-4) and filtered 数据')
R = "Python subset of the Stack" # Raw data
prompt = "determine its educational value for a student whose goal is to learn basic coding concepts"
T = "Use GPT-4 with this prompt to classify 100K subset of R to get positive examples"
- text("- Train random forest classifier on T using output embedding from pretrained codegen model")
- text("- Select data from R that is classified positive by the classifier")
- text("Result on [HumanEval](https://huggingface.co/datasets/openai_humaneval):")
- text("- Train 1.3B LM on Python subset of The Stack (performance: 12.19% after 96K steps)")
- text("- Train 1.3B LM on new filtered subset (performance: 17.68% after 36K steps) - better!")
-
- text("Toxicity filtering in Dolma "), link(dolma_2024)
- text("- Dataset: Jigsaw Toxic Comments dataset (2018) "), link(title="dataset", url="https://www.kaggle.com/datasets/julian3833/jigsaw-toxic-comment-classification-challenge")
- text("- Project goal: help people have better discussions online "), article_link("https://www.kaggle.com/competitions/jigsaw-toxic-comment-classification-challenge/discussion/46064")
- text("- Data: comments on Wikipedia talk page annotated with {toxic, severe_toxic, obscene, threat, insult, identity_hate}")
-
- text("Scale-dependent effects of filtering:")
- text("- No single optimal threshold for filtering")
- text("- If training for longer, want more (lower quality) data")
- text("- If training for shorter, want less (higher quality) data")
+ bilingual_text("- Train random forest classifier on T using output embedding from pretrained codegen model", '- Train random forest 分类器 on T using output embedding from pretrained 代码gen 模型')
+ bilingual_text("- Select data from R that is classified positive by the classifier", '- Select 数据 from R that is classified positive by the 分类器')
+ bilingual_text("Result on [HumanEval](https://huggingface.co/datasets/openai_humaneval):", 'Result on [HumanEval](https://huggingface.co/数据sets/openai_humaneval):')
+ bilingual_text("- Train 1.3B LM on Python subset of The Stack (performance: 12.19% after 96K steps)", '- 说明:Train 1.3B LM on Python subset of The Stack (performance: 12.19% after 96K steps)')
+ bilingual_text("- Train 1.3B LM on new filtered subset (performance: 17.68% after 36K steps) - better!", '- 说明:Train 1.3B LM on new filtered subset (performance: 17.68% after 36K steps) - better!')
+
+ bilingual_text("Toxicity filtering in Dolma ", '毒性 过滤 in Dolma'), link(dolma_2024)
+ bilingual_text("- Dataset: Jigsaw Toxic Comments dataset (2018) ", '- 数据set: Jigsaw Toxic Comments 数据set (2018)'), link(title="dataset", url="https://www.kaggle.com/datasets/julian3833/jigsaw-toxic-comment-classification-challenge")
+ bilingual_text("- Project goal: help people have better discussions online ", '- 说明:Project goal: help people have better discussions online'), article_link("https://www.kaggle.com/competitions/jigsaw-toxic-comment-classification-challenge/discussion/46064")
+ bilingual_text("- Data: comments on Wikipedia talk page annotated with {toxic, severe_toxic, obscene, threat, insult, identity_hate}", '- 数据: comments on Wikipedia talk page annotated with {toxic, severe_toxic, obscene, threat, insult, identity_hate}')
+
+ bilingual_text("Scale-dependent effects of filtering:", '过滤的规模相关效应:')
+ bilingual_text("- No single optimal threshold for filtering", '- No single optimal 阈值 for 过滤')
+ bilingual_text("- If training for longer, want more (lower quality) data", '- If 训练 for longer, want more (lower 质量) 数据')
+ bilingual_text("- If training for shorter, want less (higher quality) data", '- If 训练 for shorter, want less (higher 质量) 数据')
image("images/data-filtering-scale.png", width=800)
- text("Summary:")
- text("- Filtering is critical for building a good model")
- text("- Recipe: define target data (what good looks like), extrapolate to raw data")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Filtering is critical for building a good model", '- 过滤 is critical for building a good 模型')
+ bilingual_text("- Recipe: define target data (what good looks like), extrapolate to raw data", '- Recipe: define target 数据 (what good looks like), extrapolate to raw 数据')
def deduplication():
- text("Two types of duplicates:")
- text("- Exact duplicates (mirror sites, GitHub forks) "), link(title="Gutenberg mirrors", url="https://www.gutenberg.org/MIRRORS.ALL")
- text("- Near duplicates: same text differing by a few tokens")
+ bilingual_text("Two types of duplicates:", '两种重复项:')
+ bilingual_text("- Exact duplicates (mirror sites, GitHub forks) ", '- Exact 重复项 (mirror sites, GitHub forks)'), link(title="Gutenberg mirrors", url="https://www.gutenberg.org/MIRRORS.ALL")
+ bilingual_text("- Near duplicates: same text differing by a few tokens", '- Near 重复项: same 文本 differing by a few token')
- text("Examples of near duplicates:")
- text("- Terms of service and licenses "), link(title="MIT license", url="https://opensource.org/license/mit")
- text("- Formulaic writing (copy/pasted or generated from a template) "), image("https://d3i71xaburhd42.cloudfront.net/4566c0d22ebf3c31180066ab23b6c445aeec78d5/5-Table1-1.png", width=600)
- text("- Minor formatting differences in copy/pasting")
+ bilingual_text("Examples of near duplicates:", '近重复示例:')
+ bilingual_text("- Terms of service and licenses ", '- Terms of service and 许可s'), link(title="MIT license", url="https://opensource.org/license/mit")
+ bilingual_text("- Formulaic writing (copy/pasted or generated from a template) ", '- 说明:Formulaic writing (copy/pasted or generated from a template)'), image("https://d3i71xaburhd42.cloudfront.net/4566c0d22ebf3c31180066ab23b6c445aeec78d5/5-Table1-1.png", width=600)
+ bilingual_text("- Minor formatting differences in copy/pasting", '- 说明:Minor formatting differences in copy/pasting')
- text("Product description repeated 61,036 times in C4")
- text("'“by combining fantastic ideas, interesting arrangements, and follow the current trends in the field of that make you more inspired and give artistic touches. We’d be honored if you can apply some or all of these design in your wedding. believe me, brilliant ideas would be perfect if it can be applied in real and make the people around you amazed!")
+ bilingual_text("Product description repeated 61,036 times in C4", '说明:Product description repeated 61,036 times in C4')
+ bilingual_text("'“by combining fantastic ideas, interesting arrangements, and follow the current trends in the field of that make you more inspired and give artistic touches. We’d be honored if you can apply some or all of these design in your wedding. believe me, brilliant ideas would be perfect if it can be applied in real and make the people around you amazed!", "说明:'“by combining fantastic ideas, interesting arrangements, and follow the current trends in the field of that make you more inspired and give artistic touches. We’d be honored if you can apply some or all of these design in your wedding. believe me, brilliant ideas would be perfect if it can be applied in real and make the people around you amazed!")
link(title="example page", url="https://www.amazon.co.uk/suryagede-100-Graffiti-Gas-Mask/dp/B07CRHT3RG")
- text("Deduplication training data makes language models better "), link("https://arxiv.org/pdf/2107.06499")
- text("- Train more efficiently (because have fewer tokens)")
- text("- Avoid memorization (can mitigate copyright, privacy concerns)")
+ bilingual_text("Deduplication training data makes language models better ", '去重 训练 数据 makes language 模型s better'), link("https://arxiv.org/pdf/2107.06499")
+ bilingual_text("- Train more efficiently (because have fewer tokens)", '- 说明:Train more efficiently (because have fewer tokens)')
+ bilingual_text("- Avoid memorization (can mitigate copyright, privacy concerns)", '- Avoid memorization (can mitigate 版权, 隐私 concerns)')
- text("Design space:")
- text("1. What is an item (sentence, paragraph, document)?")
- text("2. How to match (exact match, existence of common subitem, fraction of common subitems)?")
- text("3. What action to take (remove all, remove all but one)?")
+ bilingual_text("Design space:", '设计空间:')
+ bilingual_text("1. What is an item (sentence, paragraph, document)?", '1. 说明:What is an item (sentence, paragraph, document)?')
+ bilingual_text("2. How to match (exact match, existence of common subitem, fraction of common subitems)?", '2. 说明:How to match (exact match, existence of common subitem, fraction of common subitems)?')
+ bilingual_text("3. What action to take (remove all, remove all but one)?", '3. 说明:What action to take (remove all, remove all but one)?')
- text("Key challenge:")
- text("- Deduplication is fundamentally about comparing items to other items")
- text("- Need linear time algorithms to scale")
+ bilingual_text("Key challenge:", '关键挑战:')
+ bilingual_text("- Deduplication is fundamentally about comparing items to other items", '- 去重 is fundamentally about comparing items to other items')
+ bilingual_text("- Need linear time algorithms to scale", '- 说明:Need linear time algorithms to scale')
hash_functions()
exact_deduplication()
@@ -176,23 +176,23 @@ def deduplication():
def hash_functions():
- text("- Hash function h maps item to a hash value (integer or string)")
- text("- Hash value much smaller than item")
- text("- Hash collision: h(x) = h(y) for x ≠ y")
+ bilingual_text("- Hash function h maps item to a hash value (integer or string)", '- 哈希 function h maps item to a 哈希 value (integer or string)')
+ bilingual_text("- Hash value much smaller than item", '- 哈希 value much smaller than item')
+ bilingual_text("- Hash collision: h(x) = h(y) for x ≠ y", '- 哈希 collision: h(x) = h(y) for x ≠ y')
- text("Tradeoff between efficiency and collision resistance "), article_link("https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed")
- text("- Cryptographic hash functions (SHA-256): collision resistant, slow (used in bitcoin)")
- text("- DJB2, MurmurHash, CityHash: not collision resistant, fast (used for hash tables)")
+ bilingual_text("Tradeoff between efficiency and collision resistance ", '说明:Tradeoff between efficiency and collision resistance'), article_link("https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed")
+ bilingual_text("- Cryptographic hash functions (SHA-256): collision resistant, slow (used in bitcoin)", '- Cryptographic 哈希 functions (SHA-256): collision resistant, slow (used in bitcoin)')
+ bilingual_text("- DJB2, MurmurHash, CityHash: not collision resistant, fast (used for hash tables)", '- DJB2, Murmur哈希, City哈希: not collision resistant, fast (used for 哈希 tables)')
- text("We will use MurmurHash:")
+ bilingual_text("We will use MurmurHash:", 'We will use Murmur哈希:')
h = mmh3.hash("hello") # @inspect h
def exact_deduplication():
- text("**Simple example**")
- text("1. Item: string")
- text("2. How to match: exact match")
- text("3. Action: remove all but one")
+ bilingual_text("**Simple example**", '**简单示例**')
+ bilingual_text("1. Item: string", '1. 说明:Item: string')
+ bilingual_text("2. How to match: exact match", '2. 说明:How to match: exact match')
+ bilingual_text("3. Action: remove all but one", '3. 说明:Action: remove all but one')
# Original items
items = ["Hello!", "hello", "hello there", "hello", "hi", "bye"] # @inspect items
@@ -203,23 +203,23 @@ def exact_deduplication():
# Keep one item from each group
deduped_items = [next(group) for h, group in hash_items] # @inspect deduped_items
- text("- Pro: simple, clear semantics, high precision")
- text("- Con: does not deduplicate near duplicates")
- text("- This code is written in a MapReduce way, can easily parallelize and scale")
+ bilingual_text("- Pro: simple, clear semantics, high precision", '- Pro: simple, clear 语义, high precision')
+ bilingual_text("- Con: does not deduplicate near duplicates", '- Con: does not deduplicate near 重复项')
+ bilingual_text("- This code is written in a MapReduce way, can easily parallelize and scale", '- This 代码 is written in a MapReduce way, can easily parallelize and scale')
- text("**C4** "), link("https://arxiv.org/pdf/1910.10683v4")
- text("1. Item: 3-sentence spans")
- text("2. How to match: use exact match")
- text("3. Action: remove all but one")
- text("Warning: when a 3-sentence span is removed from the middle of a document, the resulting document might not be coherent")
+ bilingual_text("**C4** ", '说明:C4'), link("https://arxiv.org/pdf/1910.10683v4")
+ bilingual_text("1. Item: 3-sentence spans", '1. 说明:Item: 3-sentence spans')
+ bilingual_text("2. How to match: use exact match", '2. 说明:How to match: use exact match')
+ bilingual_text("3. Action: remove all but one", '3. 说明:Action: remove all but one')
+ bilingual_text("Warning: when a 3-sentence span is removed from the middle of a document, the resulting document might not be coherent", '警告:when a 3-sentence span is removed from the middle of a document, the resulting document might not be coherent')
def jaccard_minhash():
- text("Let's now look at approximate set membership.")
- text("First we need a similarity measure.")
+ bilingual_text("Let's now look at approximate set membership.", "说明:Let's now look at approximate set membership.")
+ bilingual_text("First we need a similarity measure.", '说明:First we need a similarity measure.')
- text("### Jaccard similarity")
- text("Definition: Jaccard(A, B) = |A intersect B| / |A union B|")
+ bilingual_text("### Jaccard similarity", '### Jaccard 相似度')
+ bilingual_text("Definition: Jaccard(A, B) = |A intersect B| / |A union B|", '定义:Jaccard(A, B) = |A intersect B| / |A union B|')
A = {"1", "2", "3", "4"}
B = {"1", "2", "3", "5"}
@@ -229,32 +229,32 @@ def compute_jaccard(A, B):
return intersection / union
jaccard = compute_jaccard(A, B) # @inspect jaccard
- text("Definition: two documents are **near duplicates** if their Jaccard similarity >= threshold")
+ bilingual_text("Definition: two documents are **near duplicates** if their Jaccard similarity >= threshold", '定义:two documents are near duplicates if their Jaccard similarity >= threshold')
- text("Algorithmic challenge: find near duplicates in linear time")
+ bilingual_text("Algorithmic challenge: find near duplicates in linear time", 'Algorithmic challenge: find near 重复项 in linear time')
- text("### MinHash")
- text("MinHash: a random hash function h so that Pr[h(A) = h(B)] = Jaccard(A, B)")
+ bilingual_text("### MinHash", '### MinHash(最小哈希)')
+ bilingual_text("MinHash: a random hash function h so that Pr[h(A) = h(B)] = Jaccard(A, B)", 'Min哈希: a random 哈希 function h so that Pr[h(A) = h(B)] = Jaccard(A, B)')
- text("Normally, you want different items to hash to different hashes")
- text("...but here, you want collision probability to depend on similarity")
+ bilingual_text("Normally, you want different items to hash to different hashes", 'Normally, you want different items to 哈希 to different 哈希es')
+ bilingual_text("...but here, you want collision probability to depend on similarity", '说明:...but here, you want collision probability to depend on similarity')
def minhash(S: set[str], seed: int):
return min(mmh3.hash(x, seed) for x in S)
- text("Characteristic matrix representation:")
- text("item | A | B", verbatim=True)
- text("1 | 1 | 1", verbatim=True)
- text("2 | 1 | 1", verbatim=True)
- text("3 | 1 | 1", verbatim=True)
- text("4 | 1 | 0", verbatim=True)
- text("5 | 0 | 1", verbatim=True)
+ bilingual_text("Characteristic matrix representation:", '特征矩阵表示:')
+ bilingual_verbatim("item | A | B", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
+ bilingual_verbatim("1 | 1 | 1", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
+ bilingual_verbatim("2 | 1 | 1", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
+ bilingual_verbatim("3 | 1 | 1", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
+ bilingual_verbatim("4 | 1 | 0", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
+ bilingual_verbatim("5 | 0 | 1", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
- text("Random hash function induces a permutation over items")
- text("Look at which item is first in A and which item is first in B.")
- text("Each item has the same probability as being first (min)")
- text("- If 1, 2, 3 is first, then first in A = first in B.")
- text("- If 4, 5 is first, then first in A ≠ first in B.")
+ bilingual_text("Random hash function induces a permutation over items", 'Random 哈希 function induces a permutation over items')
+ bilingual_text("Look at which item is first in A and which item is first in B.", '说明:Look at which item is first in A and which item is first in B.')
+ bilingual_text("Each item has the same probability as being first (min)", '说明:Each item has the same probability as being first (min)')
+ bilingual_text("- If 1, 2, 3 is first, then first in A = first in B.", '- 说明:If 1, 2, 3 is first, then first in A = first in B.')
+ bilingual_text("- If 4, 5 is first, then first in A ≠ first in B.", '- 说明:If 4, 5 is first, then first in A ≠ first in B.')
# Verify MinHash approximates Jaccard as advertised
n = 100 # Generate this many random hash functions
@@ -262,39 +262,39 @@ def minhash(S: set[str], seed: int):
estimated_jaccard = len([m for m in matches if m]) / len(matches) # @inspect estimated_jaccard
assert abs(estimated_jaccard - jaccard) < 0.01
- text("Now we can hash our items, but a collision doesn't tell us Jaccard(A, B) > threshold.")
+ bilingual_text("Now we can hash our items, but a collision doesn't tell us Jaccard(A, B) > threshold.", "Now we can 哈希 our items, but a collision doesn't tell us Jaccard(A, B) > 阈值.")
def locality_sensitive_hashing():
- text("Locality sensitive hashing (LSH) "), link(title="book chapter", url="http://infolab.stanford.edu/~ullman/mmds/ch3n.pdf")
+ bilingual_text("Locality sensitive hashing (LSH) ", 'Locality sensitive 哈希ing (LSH)'), link(title="book chapter", url="http://infolab.stanford.edu/~ullman/mmds/ch3n.pdf")
- text("Suppose we hash examples with just one MinHash function")
- text("P[A and B collide] = Jaccard(A, B)")
- text("On average, more similar items will collide, but very stochastic...")
+ bilingual_text("Suppose we hash examples with just one MinHash function", 'Suppose we 哈希 examples with just one Min哈希 function')
+ bilingual_text("P[A and B collide] = Jaccard(A, B)", '说明:P[A and B collide] = Jaccard(A, B)')
+ bilingual_text("On average, more similar items will collide, but very stochastic...", '说明:On average, more similar items will collide, but very stochastic...')
- text("Goal: have A and B collide if Jaccard(A, B) > threshold")
- text("We have to somehow sharpen the probabilities...")
+ bilingual_text("Goal: have A and B collide if Jaccard(A, B) > threshold", '目标:have A and B collide if Jaccard(A, B) > threshold')
+ bilingual_text("We have to somehow sharpen the probabilities...", '说明:We have to somehow sharpen the probabilities...')
- text("Solution: use n hash functions")
- text("Break up into b bands of r hash functions each (n = b * r)")
+ bilingual_text("Solution: use n hash functions", '解决方案:use n hash functions')
+ bilingual_text("Break up into b bands of r hash functions each (n = b * r)", 'Break up into b bands of r 哈希 functions each (n = b r)')
n = 12 # Number of hash functions
b = 3 # Number of bands
r = 4 # Number of hash functions per band
- text("Hash functions:")
- text("h1 h2 h3 h4 | h5 h6 h7 h8 | h9 h10 h11 h12", verbatim=True)
+ bilingual_text("Hash functions:", '哈希函数:')
+ bilingual_verbatim("h1 h2 h3 h4 | h5 h6 h7 h8 | h9 h10 h11 h12", '上方等宽内容保持原样,用于展示表格、示例文本或哈希/矩阵布局。', verbatim=True)
- text("Key: A and B collide if for *some* band, *all* its hash functions return same value")
- text("As we will see, the and-or structure of the bands sharpens the threshold")
+ bilingual_text("Key: A and B collide if for *some* band, *all* its hash functions return same value", 'Key: A and B collide if for some band, all its 哈希 functions return same value')
+ bilingual_text("As we will see, the and-or structure of the bands sharpens the threshold", 'As we will see, the and-or structure of the bands sharpens the 阈值')
- text("Given Jaccard(A, B), what is the probability that A and B collide?")
+ bilingual_text("Given Jaccard(A, B), what is the probability that A and B collide?", '说明:Given Jaccard(A, B), what is the probability that A and B collide?')
def get_prob_collision(sim, b, r): # @inspect sim @inspect b @inspect r
prob_match = sim ** r # Probability that a fixed band matches @inspect prob_match
prob_collision = 1 - (1 - prob_match) ** b # Probability that some band matches @inspect prob_collision
return prob_collision
- text("**Example**")
+ bilingual_text("**Example**", '**示例**')
prob_collision = get_prob_collision(sim=0.8, b=5, r=10) # @inspect prob_collision
image("https://cdn.sanity.io/images/vr8gru94/production/b470799575b8e77911bacb8500977afef06d6c85-1280x720.png", width=600)
@@ -302,22 +302,22 @@ def get_prob_collision(sim, b, r): # @inspect sim @inspect b @inspect r
sims = [0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 0.98]
probs = {sim: get_prob_collision(sim=sim, b=10, r=10) for sim in sims} # @inspect probs @stepover
- text("Increasing r sharpens the threshold and moves the curve to the right (harder to match)")
+ bilingual_text("Increasing r sharpens the threshold and moves the curve to the right (harder to match)", 'Increasing r sharpens the 阈值 and moves the curve to the right (harder to match)')
probs = {sim: get_prob_collision(sim=sim, b=10, r=20) for sim in sims} # @inspect probs @stepover
- text("Increasing b moves the curve to the left (easier to match)")
+ bilingual_text("Increasing b moves the curve to the left (easier to match)", '说明:Increasing b moves the curve to the left (easier to match)')
probs = {sim: get_prob_collision(sim=sim, b=20, r=20) for sim in sims} # @inspect probs @stepover
image("https://cdn.sanity.io/images/vr8gru94/production/aace49fa240778e8ecf6e85ad08a2de7f5385566-1280x720.png", width=600)
- text("Example setting "), link("https://arxiv.org/pdf/2107.06499"), text(": n = 9000, b = 20, r = 450")
+ bilingual_text("Example setting ", '说明:Example setting'), link("https://arxiv.org/pdf/2107.06499"), bilingual_text(": n = 9000, b = 20, r = 450", '说明:: n = 9000, b = 20, r = 450')
b = 20
r = 450
- text("What is the threshold (where the phase transition happens)?")
+ bilingual_text("What is the threshold (where the phase transition happens)?", 'What is the 阈值 (where the phase transition happens)?')
threshold = (1 / b) ** (1 / r) # @inspect threshold
- text("Probability that a fixed band matches:")
+ bilingual_text("Probability that a fixed band matches:", '说明:Probability that a fixed band matches:')
prob_match = (1 / b) # @inspect prob_match
- text("Probability that A and B collide is a constant (≈ 1-1/e):")
+ bilingual_text("Probability that A and B collide is a constant (≈ 1-1/e):", '说明:Probability that A and B collide is a constant (≈ 1-1/e):')
prob_collision = 1 - (1 - 1 / b) ** b # @inspect prob_collision
@@ -329,31 +329,31 @@ def trillion(x):
def data_mixing():
- text("Recall that language models are trained on multiple data sources.")
+ bilingual_text("Recall that language models are trained on multiple data sources.", '回忆:语言模型会在多个数据来源上训练。')
- text("Datasets in Marin: "), link(title="token viewer", url="https://huggingface.co/spaces/marin-community/token-count-viewer")
+ bilingual_text("Datasets in Marin: ", '数据sets in Marin:'), link(title="token viewer", url="https://huggingface.co/spaces/marin-community/token-count-viewer")
image("images/marin-token-viewer.png", width=800)
- text("The Pile "), link(the_pile_2020)
+ bilingual_text("The Pile ", '说明:The Pile'), link(the_pile_2020)
image("https://stanford-cs324.github.io/winter2022/lectures/images/the-pile.png", width=600)
- text("Key question: what distribution over the data sources should we use?")
+ bilingual_text("Key question: what distribution over the data sources should we use?", '关键问题:我们应该在数据来源上使用什么分布?')
- text("Example:")
+ bilingual_text("Example:", '示例:')
sources = {"Wikipedia", "CC", "GitHub"}
p = {"Wikipedia": 0.3, "CC": 0.5, "GitHub": 0.2} # One possible data mixture
- text("Baselines:")
- text("- Vibes: set p(s) manually based on intuition (quite common)")
- text("- Uniform sampling: sample uniformly (p(s) ∝ 1)")
- text("- Proportional mixing: sample proportional to the number of tokens in a source (p(s) ∝ num_tokens(s))")
+ bilingual_text("Baselines:", '基线:')
+ bilingual_text("- Vibes: set p(s) manually based on intuition (quite common)", '- 说明:Vibes: set p(s) manually based on intuition (quite common)')
+ bilingual_text("- Uniform sampling: sample uniformly (p(s) ∝ 1)", '- 说明:Uniform sampling: sample uniformly (p(s) ∝ 1)')
+ bilingual_text("- Proportional mixing: sample proportional to the number of tokens in a source (p(s) ∝ num_tokens(s))", '- Proportional 混合: sample proportional to the number of token in a 来源 (p(s) ∝ num_token(s))')
- text("Intuition: should upweight higher quality sources")
- text("However...")
- text("1. We want to ensure diversity (e.g., across incomparable sources: literature, code, papers)")
- text("2. Each source is finite, so if put too much weight on a small source, then need to epoch over it")
+ bilingual_text("Intuition: should upweight higher quality sources", 'Intuition: should upweight higher 质量 来源s')
+ bilingual_text("However...", '然而……')
+ bilingual_text("1. We want to ensure diversity (e.g., across incomparable sources: literature, code, papers)", '1. We want to ensure diversity (e.g., across incomparable 来源s: literature, 代码, papers)')
+ bilingual_text("2. Each source is finite, so if put too much weight on a small source, then need to epoch over it", '2. Each 来源 is finite, so if put too much weight on a small 来源, then need to epoch over it')
- text("This last point is important and a bit subtle.")
- text("Example:")
+ bilingual_text("This last point is important and a bit subtle.", '最后一点很重要,也有点微妙。')
+ bilingual_text("Example:", '示例:')
source_token_counts = {
"low": trillion(10), # 10T tokens (abundant) @stepover
"high": billion(10), # 10B tokens (scarce) @stepover
@@ -362,102 +362,102 @@ def data_mixing():
train_tokens = trillion(1) # Train for 1T tokens @stepover
low_num_epochs = (p["low"] * train_tokens) / source_token_counts["low"] # @inspect low_num_epochs
high_num_epochs = (p["high"] * train_tokens) / source_token_counts["high"] # @inspect high_num_epochs
- text("50x epochs on high quality data...can lead to overfitting!")
+ bilingual_text("50x epochs on high quality data...can lead to overfitting!", '50x epochs on high 质量 数据...can lead to 过拟合!')
- text("UniMax "), link("https://arxiv.org/abs/2304.09151")
- text("- Setting: balancing different languages for multilingual models")
- text("- Previous work: between uniform and proportional mixing (p(s) ∝ num_tokens(s)^α for α in [0, 1])")
- text("- Idea: sample sources uniformly but with a hard **cap** C on number of epochs for any source")
- text("- Specifically, p(s) * num_training_tokens ≤ C for all sources s")
+ bilingual_text("UniMax ", '说明:UniMax'), link("https://arxiv.org/abs/2304.09151")
+ bilingual_text("- Setting: balancing different languages for multilingual models", '- Setting: balancing different languages for multilingual 模型s')
+ bilingual_text("- Previous work: between uniform and proportional mixing (p(s) ∝ num_tokens(s)^α for α in [0, 1])", '- Previous work: between uniform and proportional 混合 (p(s) ∝ num_token(s)^α for α in [0, 1])')
+ bilingual_text("- Idea: sample sources uniformly but with a hard **cap** C on number of epochs for any source", '- 思想:sample sources uniformly but with a hard cap C on number of epochs for any source')
+ bilingual_text("- Specifically, p(s) * num_training_tokens ≤ C for all sources s", '- Specifically, p(s) num_训练_token ≤ C for all 来源s s')
- text("Regression-based mixing "), link("https://arxiv.org/abs/2407.01492"), link("https://arxiv.org/pdf/2602.12237")
+ bilingual_text("Regression-based mixing ", '回归-based 混合'), link("https://arxiv.org/abs/2407.01492"), link("https://arxiv.org/pdf/2602.12237")
image("images/regmix.png", width=700)
- text("- Define distribution over mixtures `p` (e.g., Dirichlet) ")
- text("- Define regression method (e.g., linear, gradient boosted trees)")
- text("- Define target based on downstream evals (careful not to overfit!)")
- text("- Discrepancy between small and large scale (tradeoff cost and accuracy)")
+ bilingual_text("- Define distribution over mixtures `p` (e.g., Dirichlet) ", '- Define distribution over 混合比例s p (e.g., Dirichlet)')
+ bilingual_text("- Define regression method (e.g., linear, gradient boosted trees)", '- Define 回归 method (e.g., linear, gradient boosted trees)')
+ bilingual_text("- Define target based on downstream evals (careful not to overfit!)", '- 说明:Define target based on downstream evals (careful not to overfit!)')
+ bilingual_text("- Discrepancy between small and large scale (tradeoff cost and accuracy)", '- 说明:Discrepancy between small and large scale (tradeoff cost and accuracy)')
image("images/data-mixing-methods.png", width=700)
- text("Hope 1: regression model is accurate at minimizer 🙏")
- text("Hope 2: optimal data mixtures transfer from small to large scale 🙏")
+ bilingual_text("Hope 1: regression model is accurate at minimizer 🙏", 'Hope 1: 回归 模型 is accurate at minimizer 🙏')
+ bilingual_text("Hope 2: optimal data mixtures transfer from small to large scale 🙏", 'Hope 2: optimal 数据 混合比例s transfer from small to large scale 🙏')
- text("Hold on. There's at least one scale-dependent effect:")
+ bilingual_text("Hold on. There's at least one scale-dependent effect:", "说明:Hold on. There's at least one scale-dependent effect:")
source_token_counts = {
"low": trillion(10), # 10T tokens (abundant) @stepover
"high": billion(10), # 10B tokens (scarce) @stepover
}
- text("- If train small models on low token counts:")
+ bilingual_text("- If train small models on low token counts:", '- If train small 模型s on low token counts:')
p = {"low": 0.1, "high": 0.9} # More mass on high quality data
- text("- But if train large model on this mixture, we will epoch a ton on high quality data and overfit!")
+ bilingual_text("- But if train large model on this mixture, we will epoch a ton on high quality data and overfit!", '- But if train large 模型 on this 混合比例, we will epoch a ton on high 质量 数据 and overfit!')
- text("Simulated epoching "), link("https://arxiv.org/pdf/2501.11747")
- text("- General idea: make small scale look like large scale (general theme of this course)")
- text("- Instantiation: downsample all sources proportionally")
+ bilingual_text("Simulated epoching ", '说明:Simulated epoching'), link("https://arxiv.org/pdf/2501.11747")
+ bilingual_text("- General idea: make small scale look like large scale (general theme of this course)", '- 说明:General idea: make small scale look like large scale (general theme of this course)')
+ bilingual_text("- Instantiation: downsample all sources proportionally", '- Instantiation: downsample all 来源s proportionally')
small_run_tokens = billion(10) # @stepover
large_run_tokens = trillion(1) # @stepover
ratio = small_run_tokens / large_run_tokens # @inspect ratio
downsampled_source_token_counts = {s: count * ratio for s, count in source_token_counts.items()} # @inspect downsampled_source_token_counts
- text("- In this downsampled mixture, models that epoch too much won't look good.")
- text("- So the optimum will be more balanced.")
+ bilingual_text("- In this downsampled mixture, models that epoch too much won't look good.", "- In this downsampled 混合比例, 模型s that epoch too much won't look good.")
+ bilingual_text("- So the optimum will be more balanced.", '- 说明:So the optimum will be more balanced.')
p = {"low": 0.7, "high": 0.3} # More mass on high quality data
- text("Summary:")
- text("- Problem: how to weight different data sources (e.g., Wikipedia, general, code)")
- text("- Regression-based mixing: estimate mixture → loss at small scale, optimize (analogous to scaling laws)")
- text("- Important consideration: epoching and overfitting (solution: cap or simulated)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Problem: how to weight different data sources (e.g., Wikipedia, general, code)", '- 问题:how to weight different data sources (e.g., Wikipedia, general, code)')
+ bilingual_text("- Regression-based mixing: estimate mixture → loss at small scale, optimize (analogous to scaling laws)", '- 回归-based 混合: estimate 混合比例 → 损失 at small scale, optimize (analogous to scaling laws)')
+ bilingual_text("- Important consideration: epoching and overfitting (solution: cap or simulated)", '- Important consideration: epoching and 过拟合 (solution: cap or simulated)')
def post_training_data():
- text("Recipe:")
- text("1. Define a set of environments")
- text("2. Define a set of tasks / prompts")
- text("3. Collect responses from a strong model (teacher)")
-
- text("OpenThoughts "), link("https://arxiv.org/abs/2506.04178")
- text("- 1.2M examples using QwQ-32B as a teacher")
- text("- Questions come from 27 human and synthetic sources (e.g., StackExchange, NuminaMath, Chemistry)")
+ bilingual_text("Recipe:", '配方:')
+ bilingual_text("1. Define a set of environments", '1. 说明:Define a set of environments')
+ bilingual_text("2. Define a set of tasks / prompts", '2. Define a set of tasks / 提示')
+ bilingual_text("3. Collect responses from a strong model (teacher)", '3. Collect 回答 from a strong 模型 (教师模型)')
+
+ bilingual_text("OpenThoughts ", '说明:OpenThoughts'), link("https://arxiv.org/abs/2506.04178")
+ bilingual_text("- 1.2M examples using QwQ-32B as a teacher", '- 1.2M examples using QwQ-32B as a 教师模型')
+ bilingual_text("- Questions come from 27 human and synthetic sources (e.g., StackExchange, NuminaMath, Chemistry)", '- Questions come from 27 human and synthetic 来源s (e.g., StackExchange, NuminaMath, Chemistry)')
image("images/openthoughts-sources.png", width=500)
- text("- Sampling multiple (16) responses per prompt is helpful")
- text("- Better models aren't necessarily better teachers: QwQ-32B is a better teacher than DeepSeek-R1")
- text("- Answer filtering wasn't helpful")
- text("- Smaller high quality sources (e.g., OpenMath-2-Math) is better than large diverse sources")
+ bilingual_text("- Sampling multiple (16) responses per prompt is helpful", '- Sampling multiple (16) 回答 per prompt is helpful')
+ bilingual_text("- Better models aren't necessarily better teachers: QwQ-32B is a better teacher than DeepSeek-R1", "- Better 模型s aren't necessarily better 教师模型s: QwQ-32B is a better 教师模型 than DeepSeek-R1")
+ bilingual_text("- Answer filtering wasn't helpful", "- Answer 过滤 wasn't helpful")
+ bilingual_text("- Smaller high quality sources (e.g., OpenMath-2-Math) is better than large diverse sources", '- Smaller high 质量 来源s (e.g., OpenMath-2-Math) is better than large diverse 来源s')
image("images/openthoughts-pipeline.png", width=600)
- text("SWE-smith "), link("https://arxiv.org/abs/2504.21798")
+ bilingual_text("SWE-smith ", '说明:SWE-smith'), link("https://arxiv.org/abs/2504.21798")
image("images/swe-smith.png", width=500)
- text("- Given a repository, use LM to generate tasks (introduce bugs with LM)")
- text("- 128 GitHub repositories yields 50K tasks")
+ bilingual_text("- Given a repository, use LM to generate tasks (introduce bugs with LM)", '- Given a 仓库, use LM to generate tasks (introduce bugs with LM)')
+ bilingual_text("- 128 GitHub repositories yields 50K tasks", '- 128 GitHub 仓库 yields 50K tasks')
- text("SWE-Zero "), link("https://arxiv.org/abs/2604.01496")
- text("- SWE tasks have heavy dependencies (unlike math or coding contests)")
- text("- Setting up thousands of Docker images is an infrastructural nightmare")
- text("- Observation: strong models can solve many tasks without execution feedback")
+ bilingual_text("SWE-Zero ", '说明:SWE-Zero'), link("https://arxiv.org/abs/2604.01496")
+ bilingual_text("- SWE tasks have heavy dependencies (unlike math or coding contests)", '- 说明:SWE tasks have heavy dependencies (unlike math or coding contests)')
+ bilingual_text("- Setting up thousands of Docker images is an infrastructural nightmare", '- Setting up thousands of Docker 图像 is an infrastructural nightmare')
+ bilingual_text("- Observation: strong models can solve many tasks without execution feedback", '- 观察:strong models can solve many tasks without execution feedback')
image("images/swezero-noexec.png", width=600)
- text("Key: strong models have internal \"world model\" of code semantics")
- text("- SWE-Zero: 300K agent trajectories that don't require repository-specific execution")
- text("- 150K GitHub PRs")
- text("- OpenHands scaffold, remove future git commits to prevent \"git hacking\" by agent")
+ bilingual_text("Key: strong models have internal \"world model\" of code semantics", 'Key: strong 模型s have internal "world 模型" of 代码 语义')
+ bilingual_text("- SWE-Zero: 300K agent trajectories that don't require repository-specific execution", "- SWE-Zero: 300K 智能体 轨迹 that don't require 仓库-specific execution")
+ bilingual_text("- 150K GitHub PRs", '- 说明:150K GitHub PRs')
+ bilingual_text("- OpenHands scaffold, remove future git commits to prevent \"git hacking\" by agent", '- OpenHands scaffold, remove future git commits to prevent "git hacking" by 智能体')
image("images/swezero-prompt.png", width=600)
- text("- Distilled from Qwen3-Coder-480B + filtering (try to execute anyway)")
- text("- SWE-Hero: 13K agent trajectories that do require execution feedback")
+ bilingual_text("- Distilled from Qwen3-Coder-480B + filtering (try to execute anyway)", '- Distilled from Qwen3-代码r-480B + 过滤 (try to execute anyway)')
+ bilingual_text("- SWE-Hero: 13K agent trajectories that do require execution feedback", '- SWE-Hero: 13K 智能体 轨迹 that do require execution feedback')
image("images/swezero-results.png", width=700)
- text("SWE-rebench "), link("https://arxiv.org/pdf/2505.20411")
- text("- 21K interactive Python SWE tasks from 3.4K GitHub repositories")
- text("- 450K PRs from GitHub and GitHub Archive")
- text("- Used Qwen 2.5-72B-Instruct to install dependencies and assess PR quality")
+ bilingual_text("SWE-rebench ", '说明:SWE-rebench'), link("https://arxiv.org/pdf/2505.20411")
+ bilingual_text("- 21K interactive Python SWE tasks from 3.4K GitHub repositories", '- 21K interactive Python SWE tasks from 3.4K GitHub 仓库')
+ bilingual_text("- 450K PRs from GitHub and GitHub Archive", '- 说明:450K PRs from GitHub and GitHub Archive')
+ bilingual_text("- Used Qwen 2.5-72B-Instruct to install dependencies and assess PR quality", '- Used Qwen 2.5-72B-Instruct to install dependencies and assess PR 质量')
image("images/swe-rebench.png", width=600)
- text("SWE-ZERO-12M-trajectories "), link(title="data", url="https://huggingface.co/datasets/AlienKevin/SWE-ZERO-12M-trajectories")
- text("- Scale SWE-Zero up to 12M agent trajectories")
- text("- Used the SWE-rebench-v2 tasks (32K executable tasks + 120K nonexecutable tasks)")
- text("- Ran mini-coder-1.7b (very small model, 50.4 pass@100), mini-swe-agent scaffold")
- text("- [Example](https://huggingface.co/datasets/AlienKevin/SWE-ZERO-12M-trajectories/viewer/default/train?row=5&conversation-viewer=0)")
-
- text("Summary:")
- text("- Generating prompts: fully-synthetic, semi-synthetic (real environment + synthetic tasks), real (GitHub PRs)")
- text("- Responses: from capable models (that are also good teachers)")
- text("- Code environments are painful")
- text("- Lots of filtering and other details")
+ bilingual_text("SWE-ZERO-12M-trajectories ", 'SWE-ZERO-12M-轨迹'), link(title="data", url="https://huggingface.co/datasets/AlienKevin/SWE-ZERO-12M-trajectories")
+ bilingual_text("- Scale SWE-Zero up to 12M agent trajectories", '- Scale SWE-Zero up to 12M 智能体 轨迹')
+ bilingual_text("- Used the SWE-rebench-v2 tasks (32K executable tasks + 120K nonexecutable tasks)", '- 说明:Used the SWE-rebench-v2 tasks (32K executable tasks + 120K nonexecutable tasks)')
+ bilingual_text("- Ran mini-coder-1.7b (very small model, 50.4 pass@100), mini-swe-agent scaffold", '- Ran mini-代码r-1.7b (very small 模型, 50.4 pass@100), mini-swe-智能体 scaffold')
+ bilingual_text("- [Example](https://huggingface.co/datasets/AlienKevin/SWE-ZERO-12M-trajectories/viewer/default/train?row=5&conversation-viewer=0)", '- [Example](https://huggingface.co/数据sets/AlienKevin/SWE-ZERO-12M-轨迹/viewer/default/train?row=5&conversation-viewer=0)')
+
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Generating prompts: fully-synthetic, semi-synthetic (real environment + synthetic tasks), real (GitHub PRs)", '- Generating 提示: fully-synthetic, semi-synthetic (real environment + synthetic tasks), real (GitHub PRs)')
+ bilingual_text("- Responses: from capable models (that are also good teachers)", '- 回答: from capable 模型s (that are also good 教师模型s)')
+ bilingual_text("- Code environments are painful", '- 代码 environments are painful')
+ bilingual_text("- Lots of filtering and other details", '- Lots of 过滤 and other details')
if __name__ == "__main__":
diff --git a/lecture_17.py b/lecture_17.py
index 879c3e8..0777ba5 100644
--- a/lecture_17.py
+++ b/lecture_17.py
@@ -1,27 +1,27 @@
from edtrace import text, image, link
-from lecture_util import article_link, post_link
+from lecture_util import article_link, post_link, bilingual_text, bilingual_verbatim
def main():
- text("## Lecture 17: multimodal models")
- text("So far: language models")
- text("> text ⇒ text")
- text("The world is multimodal:")
+ bilingual_text("## Lecture 17: multimodal models", '## 第 17 讲:多模态模型')
+ bilingual_text("So far: language models", '到目前为止:语言模型。')
+ bilingual_text("> text ⇒ text", '> 文本 ⇒ 文本。')
+ bilingual_text("The world is multimodal:", '世界是多模态的:')
image("images/multimodality.png", width=600)
- text("Ultimate goal: **omni model**")
- text("- Input any combination of modalities (understanding)")
- text("- Output any combination of modalities (generation)")
+ bilingual_text("Ultimate goal: **omni model**", '终极目标:**全模态模型**。')
+ bilingual_text("- Input any combination of modalities (understanding)", '- Input any combination of 模态 (理解)')
+ bilingual_text("- Output any combination of modalities (generation)", '- Output any combination of 模态 (生成)')
- text("Where we are today:")
- text("- Transformers work really well. So we gotta use them.")
- text("- Transformers speak tokens (discrete or continuous), where a token represents some ~semantic unit of information.")
- text("- Therefore, we must convert everything into tokens.")
- text("- Note: we had to do this with text (recall the tokenization lecture).")
- text("- For non-text modalities, this is more challenging...")
+ bilingual_text("Where we are today:", '我们今天所处的位置:')
+ bilingual_text("- Transformers work really well. So we gotta use them.", '- 说明:Transformers work really well. So we gotta use them.')
+ bilingual_text("- Transformers speak tokens (discrete or continuous), where a token represents some ~semantic unit of information.", '- Transformers speak token (discrete or 连续), where a token represents some ~semantic unit of information.')
+ bilingual_text("- Therefore, we must convert everything into tokens.", '- 说明:Therefore, we must convert everything into tokens.')
+ bilingual_text("- Note: we had to do this with text (recall the tokenization lecture).", '- Note: we had to do this with 文本 (recall the tokenization lecture).')
+ bilingual_text("- For non-text modalities, this is more challenging...", '- For non-文本 模态, this is more challenging...')
- text("Questions:")
- text("1. How do we input non-text data (e.g., understand images)?")
- text("2. How do we output non-text data (e.g., generate audio)?")
+ bilingual_text("Questions:", '问题:')
+ bilingual_text("1. How do we input non-text data (e.g., understand images)?", '1. How do we input non-文本 数据 (e.g., understand 图像)?')
+ bilingual_text("2. How do we output non-text data (e.g., generate audio)?", '2. How do we output non-文本 数据 (e.g., generate 音频)?')
# Encoding images
clip()
@@ -37,265 +37,265 @@ def main():
# Towards Omni models
chameleon()
- text("Summary:")
- text("- Frontier models are expected to be multimodal (natively multimodal, omni)")
- text("- Fundamental challenge: how to encode non-text modalities?")
- text("- Comprehension and generation might demand different things (semantics versus finer-grained details)")
- text("- Balance images + video (lower information density) and text for training stability")
- text("- Continuous encoders + Transformer + diffusion models for generation")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Frontier models are expected to be multimodal (natively multimodal, omni)", '- Frontier 模型s are expected to be 多模态 (natively 多模态, omni)')
+ bilingual_text("- Fundamental challenge: how to encode non-text modalities?", '- Fundamental challenge: how to en代码 non-文本 模态?')
+ bilingual_text("- Comprehension and generation might demand different things (semantics versus finer-grained details)", '- Comprehension and 生成 might demand different things (语义 versus finer-grained details)')
+ bilingual_text("- Balance images + video (lower information density) and text for training stability", '- Balance 图像 + 视频 (lower information density) and 文本 for 训练 stability')
+ bilingual_text("- Continuous encoders + Transformer + diffusion models for generation", '- 连续 en代码rs + Transformer + diffusion 模型s for 生成')
def clip():
- text("CLIP (Contrastive Language-Image Pretraining) "), link("https://arxiv.org/abs/2103.00020")
+ bilingual_text("CLIP (Contrastive Language-Image Pretraining) ", 'CLIP (Contrastive Language-图像 Pre训练)'), link("https://arxiv.org/abs/2103.00020")
- text("Context:")
- text("- Computer vision models were trained on annotated images.")
- text("- Question: is it possible to leverage the much larger amount of (image, caption) pairs?")
+ bilingual_text("Context:", '背景:')
+ bilingual_text("- Computer vision models were trained on annotated images.", '- Computer vision 模型s were trained on annotated 图像.')
+ bilingual_text("- Question: is it possible to leverage the much larger amount of (image, caption) pairs?", '- Question: is it possible to leverage the much larger amount of (图像, 图注) pairs?')
image("images/clip.png", width=800)
- text("Method:")
- text("- Get a batch of (image, text) examples (e.g., 32768)")
- text("- Encode each image and each text")
- text("- For each image, prefer its aligned text over other texts")
- text("- For each text, prefer its aligned image over other images")
+ bilingual_text("Method:", '方法:')
+ bilingual_text("- Get a batch of (image, text) examples (e.g., 32768)", '- Get a 批次 of (图像, 文本) examples (e.g., 32768)')
+ bilingual_text("- Encode each image and each text", '- En代码 each 图像 and each 文本')
+ bilingual_text("- For each image, prefer its aligned text over other texts", '- For each 图像, prefer its aligned 文本 over other 文本s')
+ bilingual_text("- For each text, prefer its aligned image over other images", '- For each 文本, prefer its aligned 图像 over other 图像')
image("images/clip-code.png", width=400)
- text("Data:")
- text("- Searched for 500K queries, get ~20K (image, text) pairs per query")
- text("- Trained on 400M image-text pairs")
- text("- Didn't release the dataset")
- text("- Reproduced in OpenCLIP (using LAION-5B dataset, which used CLIP for filtering) "), link("https://arxiv.org/abs/2212.07143")
+ bilingual_text("Data:", '数据:')
+ bilingual_text("- Searched for 500K queries, get ~20K (image, text) pairs per query", '- Searched for 500K queries, get ~20K (图像, 文本) pairs per query')
+ bilingual_text("- Trained on 400M image-text pairs", '- Trained on 400M 图像-文本 pairs')
+ bilingual_text("- Didn't release the dataset", "- Didn't release the 数据set")
+ bilingual_text("- Reproduced in OpenCLIP (using LAION-5B dataset, which used CLIP for filtering) ", '- Reproduced in OpenCLIP (using LAION-5B 数据set, which used CLIP for 过滤)'), link("https://arxiv.org/abs/2212.07143")
- text("Data processing "), link(title="code", url="https://github.com/openai/CLIP/blob/main/clip/clip.py#L79")
- text("- Images come in all resolutions (arbitrary W x H)")
- text("- Resize using bicubic interpolation so shorter side is 336 pixels")
- text("- Center crop (cuts off borders to get 336 x 336)")
+ bilingual_text("Data processing ", '数据处理:'), link(title="code", url="https://github.com/openai/CLIP/blob/main/clip/clip.py#L79")
+ bilingual_text("- Images come in all resolutions (arbitrary W x H)", '- 图像 come in all 分辨率s (arbitrary W x H)')
+ bilingual_text("- Resize using bicubic interpolation so shorter side is 336 pixels", '- 说明:Resize using bicubic interpolation so shorter side is 336 pixels')
+ bilingual_text("- Center crop (cuts off borders to get 336 x 336)", '- 说明:Center crop (cuts off borders to get 336 x 336)')
- text("Vision encoder:")
- text("- Experimented with ResNet-50 and Vision Transformers "), link("https://arxiv.org/pdf/2010.11929")
+ bilingual_text("Vision encoder:", '视觉编码器:')
+ bilingual_text("- Experimented with ResNet-50 and Vision Transformers ", '- 说明:Experimented with ResNet-50 and Vision Transformers'), link("https://arxiv.org/pdf/2010.11929")
image("images/vit.png", width=600)
- text("- Attention pooling: do QKV with query = global average of activations")
- text("- Best model: ViT-L/14@336px (L = large, 14x14 patches, 3 channels, trained on 336x336 resolution images)")
+ bilingual_text("- Attention pooling: do QKV with query = global average of activations", '- 说明:Attention pooling: do QKV with query = global average of activations')
+ bilingual_text("- Best model: ViT-L/14@336px (L = large, 14x14 patches, 3 channels, trained on 336x336 resolution images)", '- Best 模型: ViT-L/14@336px (L = large, 14x14 patches, 3 channels, trained on 336x336 分辨率 图像)')
- text("Text encoder:")
- text("- GPT-2 Transformer (63M parameters, 12 layers)")
- text("- Encode [BOS] ... [EOS], return [EOS] activation at highest layer")
+ bilingual_text("Text encoder:", '文本编码器:')
+ bilingual_text("- GPT-2 Transformer (63M parameters, 12 layers)", '- 说明:GPT-2 Transformer (63M parameters, 12 layers)')
+ bilingual_text("- Encode [BOS] ... [EOS], return [EOS] activation at highest layer", '- En代码 [BOS] ... [EOS], return [EOS] activation at highest layer')
- text("Headline result:")
- text("- On ImageNet, zero-shot CLIP outperformed ResNet-50 trained on 1.2M ImageNet images")
+ bilingual_text("Headline result:", '主要结果:')
+ bilingual_text("- On ImageNet, zero-shot CLIP outperformed ResNet-50 trained on 1.2M ImageNet images", '- On 图像Net, zero-shot CLIP outperformed ResNet-50 trained on 1.2M 图像Net 图像')
- text("Ablation:")
- text("- Alternative: predict text from images directly")
- text("- Much less compute efficient compared to CLIP-style ranking")
+ bilingual_text("Ablation:", '消融:')
+ bilingual_text("- Alternative: predict text from images directly", '- Alternative: predict 文本 from 图像 directly')
+ bilingual_text("- Much less compute efficient compared to CLIP-style ranking", '- 说明:Much less compute efficient compared to CLIP-style ranking')
image("images/clip-efficiency.png", width=400)
- text("Summary:")
- text("- Encoding of images captures semantics given by (noisy) text")
- text("- Design decisions chosen based on image classification (not very fine-grained)")
- text("- Technical: requires large batch sizes, softmax operation over full batch")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Encoding of images captures semantics given by (noisy) text", '- Encoding of 图像 captures 语义 given by (noisy) 文本')
+ bilingual_text("- Design decisions chosen based on image classification (not very fine-grained)", '- Design decisions chosen based on 图像 classification (not very fine-grained)')
+ bilingual_text("- Technical: requires large batch sizes, softmax operation over full batch", '- Technical: requires large 批次 sizes, softmax operation over full 批次')
def siglip():
- text("SigLIP (Sigmoid Loss for Language Image Pre-Training) "), link("https://arxiv.org/abs/2303.15343")
+ bilingual_text("SigLIP (Sigmoid Loss for Language Image Pre-Training) ", 'SigLIP (Sigmoid 损失 for Language 图像 Pre-训练)'), link("https://arxiv.org/abs/2303.15343")
- text("Objective:")
- text("- CLIP: multiclass classification for (text, image) versus (text, image') for all image'")
- text("- SigLIP: binary classification for (text, image) - aligned or not?")
+ bilingual_text("Objective:", '目标:')
+ bilingual_text("- CLIP: multiclass classification for (text, image) versus (text, image') for all image'", "- CLIP: multiclass classification for (文本, 图像) versus (文本, 图像') for all 图像'")
+ bilingual_text("- SigLIP: binary classification for (text, image) - aligned or not?", '- SigLIP: binary classification for (文本, 图像) - aligned or not?')
image("images/siglip-code.png", width=500)
- text("Data:")
- text("- WebLI dataset: O(billion) (image, text) pairs "), link("https://arxiv.org/pdf/2209.06794")
- text("- Scraped from the Internet")
- text("- Used automatic OCR to extract text from images")
- text("- Keep 10% highest quality")
- text("- Supports 100 languages")
+ bilingual_text("Data:", '数据:')
+ bilingual_text("- WebLI dataset: O(billion) (image, text) pairs ", '- 网络LI 数据set: O(billion) (图像, 文本) pairs'), link("https://arxiv.org/pdf/2209.06794")
+ bilingual_text("- Scraped from the Internet", '- Scraped from the 互联网')
+ bilingual_text("- Used automatic OCR to extract text from images", '- Used automatic OCR to extract 文本 from 图像')
+ bilingual_text("- Keep 10% highest quality", '- Keep 10% highest 质量')
+ bilingual_text("- Supports 100 languages", '- 说明:Supports 100 languages')
- text("Efficiency:")
- text("- CLIP: 10 days on 256 TPUv3")
- text("- SigLIP: 5 days on 32 TPUv4 (lower FLOP/s than TPUv3) - much faster!")
+ bilingual_text("Efficiency:", '效率:')
+ bilingual_text("- CLIP: 10 days on 256 TPUv3", '- 说明:CLIP: 10 days on 256 TPUv3')
+ bilingual_text("- SigLIP: 5 days on 32 TPUv4 (lower FLOP/s than TPUv3) - much faster!", '- 说明:SigLIP: 5 days on 32 TPUv4 (lower FLOP/s than TPUv3) - much faster!')
image("images/siglip-parallelism.png", width=800)
- text("Batch size:")
- text("- Decouple batch size from loss")
- text("- Better than CLIP for <16K batch sizes")
- text("- Go up to 1M batch size, but 32K is enough")
+ bilingual_text("Batch size:", '批大小:')
+ bilingual_text("- Decouple batch size from loss", '- Decouple 批次 size from 损失')
+ bilingual_text("- Better than CLIP for <16K batch sizes", '- Better than CLIP for <16K 批次 sizes')
+ bilingual_text("- Go up to 1M batch size, but 32K is enough", '- Go up to 1M 批次 size, but 32K is enough')
def llava():
- text("LLaVA (Large Language and Vision Assistant) "), link("https://arxiv.org/abs/2304.08485")
+ bilingual_text("LLaVA (Large Language and Vision Assistant) ", '说明:LLaVA (Large Language and Vision Assistant)'), link("https://arxiv.org/abs/2304.08485")
- text("Vision encoder: CLIP")
- text("Text decoder: Vicuna (LLaMA fine-tuned on ShareGPT conversations) "), post_link("https://www.lmsys.org/blog/2023-03-30-vicuna/")
+ bilingual_text("Vision encoder: CLIP", 'Vision en代码r: CLIP')
+ bilingual_text("Text decoder: Vicuna (LLaMA fine-tuned on ShareGPT conversations) ", '文本 de代码r: Vicuna (LLaMA fine-tuned on ShareGPT conversations)'), post_link("https://www.lmsys.org/blog/2023-03-30-vicuna/")
- text("Data:")
- text("- MS COCO has images annotated with bounding boxes and Mechanical Turk captions")
- text("- Prompt GPT-4 with captions or detected objects and generate questions or conversations")
- text("- Pair generations with original images")
- text("- 158K examples")
+ bilingual_text("Data:", '数据:')
+ bilingual_text("- MS COCO has images annotated with bounding boxes and Mechanical Turk captions", '- MS COCO has 图像 annotated with bounding boxes and Mechanical Turk 图注s')
+ bilingual_text("- Prompt GPT-4 with captions or detected objects and generate questions or conversations", '- Prompt GPT-4 with 图注s or detected objects and generate questions or conversations')
+ bilingual_text("- Pair generations with original images", '- Pair 生成s with original 图像')
+ bilingual_text("- 158K examples", '- 说明:158K examples')
image("images/llava-gen.png", width=600)
- text("Model:")
- text("- Encode images with CLIP (ViT-L/14)")
- text("- Linear projection (W) into embedding space (Flamingo and Q-former are more complex)")
+ bilingual_text("Model:", '模型:')
+ bilingual_text("- Encode images with CLIP (ViT-L/14)", '- En代码 图像 with CLIP (ViT-L/14)')
+ bilingual_text("- Linear projection (W) into embedding space (Flamingo and Q-former are more complex)", '- 说明:Linear projection (W) into embedding space (Flamingo and Q-former are more complex)')
image("images/llava-architecture.png", width=600)
- text("Training:")
- text("- Stage 1 (alignment): freeze vision encoder and language model, only train W")
- text("- Stage 2 (fine-tuning): freeze vision encoder and train W and language model")
+ bilingual_text("Training:", '训练:')
+ bilingual_text("- Stage 1 (alignment): freeze vision encoder and language model, only train W", '- Stage 1 (对齐): freeze vision en代码r and language 模型, only train W')
+ bilingual_text("- Stage 2 (fine-tuning): freeze vision encoder and train W and language model", '- Stage 2 (微调): freeze vision en代码r and train W and language 模型')
image("images/llava-example.png", width=600)
def llava_onevision():
- text("LLaVA OneVision "), link("https://arxiv.org/pdf/2408.03326")
- text("- Latest version in the LLaVA series (after LLaVA 1.5, LLaVA-Next)")
- text("- Handle multiple images, video")
+ bilingual_text("LLaVA OneVision ", '说明:LLaVA OneVision'), link("https://arxiv.org/pdf/2408.03326")
+ bilingual_text("- Latest version in the LLaVA series (after LLaVA 1.5, LLaVA-Next)", '- 说明:Latest version in the LLaVA series (after LLaVA 1.5, LLaVA-Next)')
+ bilingual_text("- Handle multiple images, video", '- Handle multiple 图像, 视频')
image("images/llava-onevision.png", width=600)
- text("- Vision encoder: SigLIP (use grid features before and after last Transformer layer)")
- text("- Text decoder: Qwen-2 72B")
- text("- Projector: 2-layer MLP")
-
- text("Data processing:")
- text("- Preserving high resolution is important (e.g., for OCR)")
- text("- CLIP resizes and crops to 336x336, which loses information")
- text("- Solution: AnyRes, introduced in LLaVA 1.5 "), link(title="paper", url="https://static.hliu.cc/files/llava/improved_llava.pdf")
- text("- Break up image into a x b pieces (matching resolution of vision encoder), encode, concatenate")
- text("- If too many tokens (original image is too high resolution), then use bilinear interpolation")
+ bilingual_text("- Vision encoder: SigLIP (use grid features before and after last Transformer layer)", '- Vision en代码r: SigLIP (use grid features before and after last Transformer layer)')
+ bilingual_text("- Text decoder: Qwen-2 72B", '- 文本 de代码r: Qwen-2 72B')
+ bilingual_text("- Projector: 2-layer MLP", '- 投影器: 2-layer MLP')
+
+ bilingual_text("Data processing:", '数据 processing:')
+ bilingual_text("- Preserving high resolution is important (e.g., for OCR)", '- Preserving high 分辨率 is important (e.g., for OCR)')
+ bilingual_text("- CLIP resizes and crops to 336x336, which loses information", '- 说明:CLIP resizes and crops to 336x336, which loses information')
+ bilingual_text("- Solution: AnyRes, introduced in LLaVA 1.5 ", '- 解决方案:AnyRes, introduced in LLaVA 1.5'), link(title="paper", url="https://static.hliu.cc/files/llava/improved_llava.pdf")
+ bilingual_text("- Break up image into a x b pieces (matching resolution of vision encoder), encode, concatenate", '- Break up 图像 into a x b pieces (matching 分辨率 of vision en代码r), en代码, concatenate')
+ bilingual_text("- If too many tokens (original image is too high resolution), then use bilinear interpolation", '- If too many token (original 图像 is too high 分辨率), then use bilinear interpolation')
image("images/llava-onevision-anyres.png", width=600)
- text("Handle 3 types of input (single image, multiple images, video):")
- text("- Goal: make all of the modalities produce roughly the same length")
+ bilingual_text("Handle 3 types of input (single image, multiple images, video):", 'Handle 3 types of input (single 图像, multiple 图像, 视频):')
+ bilingual_text("- Goal: make all of the modalities produce roughly the same length", '- 目标:make all of the modalities produce roughly the same length')
image("images/llava-onevision-modalities.png", width=600)
- text("- Single image: use higher resolution")
- text("- Multiple images: use base resolution for each image")
- text("- Video: use lower resolution for each frame")
+ bilingual_text("- Single image: use higher resolution", '- Single 图像: use higher 分辨率')
+ bilingual_text("- Multiple images: use base resolution for each image", '- Multiple 图像: use base 分辨率 for each 图像')
+ bilingual_text("- Video: use lower resolution for each frame", '- 视频: use lower 分辨率 for each frame')
- text("Data:")
- text("- Philosophy: quality over quantity")
+ bilingual_text("Data:", '数据:')
+ bilingual_text("- Philosophy: quality over quantity", '- 理念:quality over quantity')
image("images/llava-onevision-data-1.png", width=700)
image("images/llava-onevision-data-2.png", width=700)
- text("Training:")
- text("- Philosophy: easier to harder")
+ bilingual_text("Training:", '训练:')
+ bilingual_text("- Philosophy: easier to harder", '- 理念:easier to harder')
image("images/llava-onevision-training.png", width=700)
- text("Transfer between modalities:")
- text("- Single image data for diagrams and charts, but generalize to multi-image")
+ bilingual_text("Transfer between modalities:", '模态之间的迁移:')
+ bilingual_text("- Single image data for diagrams and charts, but generalize to multi-image", '- Single 图像 数据 for diagrams and charts, but generalize to multi-图像')
image("images/llava-onevision-transfer-s1.png", width=600)
- text("- OCR on single image data, relational reasoning from multi-image data, generalize to GUI-based agents")
+ bilingual_text("- OCR on single image data, relational reasoning from multi-image data, generalize to GUI-based agents", '- OCR on single 图像 数据, relational reasoning from multi-图像 数据, generalize to GUI-based 智能体s')
image("images/llava-onevision-transfer-s2.png", width=600)
- text("- Visual prompting (circle) in single images, generalize to videos")
+ bilingual_text("- Visual prompting (circle) in single images, generalize to videos", '- Visual prompting (circle) in single 图像, generalize to 视频s')
image("images/llava-onevision-transfer-s8.png", width=600)
- text("Summary:")
- text("- Standard VLM template: vision encoder + projector + LM")
- text("- Most work goes into data curation (heavy on synthesized, task-specific data)")
- text("- Open-source (released model weights and data)")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Standard VLM template: vision encoder + projector + LM", '- Standard VLM template: vision en代码r + 投影器 + LM')
+ bilingual_text("- Most work goes into data curation (heavy on synthesized, task-specific data)", '- Most work goes into 数据 curation (heavy on synthesized, task-specific 数据)')
+ bilingual_text("- Open-source (released model weights and data)", '- Open-来源 (released 模型 weights and 数据)')
def qwen_vl():
- text("Qwen-VL "), link("https://arxiv.org/abs/2308.12966")
+ bilingual_text("Qwen-VL ", '说明:Qwen-VL'), link("https://arxiv.org/abs/2308.12966")
- text("Architecture:")
- text("- Vision encoder: OpenCLIP's ViT-bigC (14x14 patches) "), link("https://arxiv.org/abs/2212.07143")
- text("- Adaptor: one layer cross-attention, incorporate 2D positional encodings, maps to fixed length of 256")
- text("- Special tokens:
, , [")
+ bilingual_text("Architecture:", '架构:')
+ bilingual_text("- Vision encoder: OpenCLIP's ViT-bigC (14x14 patches) ", "- Vision en代码r: OpenCLIP's ViT-bigC (14x14 patches)"), link("https://arxiv.org/abs/2212.07143")
+ bilingual_text("- Adaptor: one layer cross-attention, incorporate 2D positional encodings, maps to fixed length of 256", '- 适配器: one layer cross-attention, incorporate 2D positional encodings, maps to fixed length of 256')
+ bilingual_text("- Special tokens: ]
, , [", '- 说明:Special tokens: ]
, , [')
- text("Training:")
+ bilingual_text("Training:", '训练:')
image("images/qwen-vl-stages.png", width=700)
- text("- Stage 1: large-scale low quality data; freeze LM, train vision encoder + adaptor")
+ bilingual_text("- Stage 1: large-scale low quality data; freeze LM, train vision encoder + adaptor", '- Stage 1: large-scale low 质量 数据; freeze LM, train vision en代码r + 适配器')
image("images/qwen-vl-stage1.png", width=400)
- text("- Stage 2: higher quality task-specific data, increase resolution; train all parameters")
+ bilingual_text("- Stage 2: higher quality task-specific data, increase resolution; train all parameters", '- Stage 2: higher 质量 task-specific 数据, increase 分辨率; train all parameters')
image("images/qwen-vl-stage2.png", width=400)
- text("- Stage 3: instruction tuning data; freeze visual encoder, train adaptor + LM")
+ bilingual_text("- Stage 3: instruction tuning data; freeze visual encoder, train adaptor + LM", '- Stage 3: 指令调优 数据; freeze visual en代码r, train 适配器 + LM')
image("images/qwen-vl-examples.png", width=600)
def qwen2_vl():
- text("Qwen2-VL "), link("https://arxiv.org/abs/2409.12191")
+ bilingual_text("Qwen2-VL ", '说明:Qwen2-VL'), link("https://arxiv.org/abs/2409.12191")
- text("Visual encoder: larger ViT (675M)")
+ bilingual_text("Visual encoder: larger ViT (675M)", 'Visual en代码r: larger ViT (675M)')
image("images/qwen2-vl-architecture.png", width=700)
- text("- Key: dynamic resolution to handle varying resolutions")
- text("- Each 224 x 224 patch encoded with ViT/14, compress every 2x2 => 66 tokens")
- text("- Video: sample 2 frames/sec, max 16384 tokens")
+ bilingual_text("- Key: dynamic resolution to handle varying resolutions", '- Key: dynamic 分辨率 to handle varying 分辨率s')
+ bilingual_text("- Each 224 x 224 patch encoded with ViT/14, compress every 2x2 => 66 tokens", '- Each 224 x 224 patch en代码d with ViT/14, compress every 2x2 => 66 token')
+ bilingual_text("- Video: sample 2 frames/sec, max 16384 tokens", '- 视频: sample 2 frames/sec, max 16384 token')
- text("Multimodal Rotary Position Embedding (MRoPE):")
+ bilingual_text("Multimodal Rotary Position Embedding (MRoPE):", '多模态 Rotary Position Embedding (MRoPE):')
image("images/qwen2-vl-mrope.png", width=600)
- text("Initialize LM with Qwen2 and vision encoder from DFN "), link("https://arxiv.org/abs/2309.17425")
- text("Training (similar to Qwen-VL):")
- text("- Stage 1: train only visual encoder")
- text("- Stage 2: train all parameters")
- text("- Stage 3: train language model on instruction following datasets")
+ bilingual_text("Initialize LM with Qwen2 and vision encoder from DFN ", 'Initialize LM with Qwen2 and vision en代码r from DFN'), link("https://arxiv.org/abs/2309.17425")
+ bilingual_text("Training (similar to Qwen-VL):", '训练 (similar to Qwen-VL):')
+ bilingual_text("- Stage 1: train only visual encoder", '- Stage 1: train only visual en代码r')
+ bilingual_text("- Stage 2: train all parameters", '- 说明:Stage 2: train all parameters')
+ bilingual_text("- Stage 3: train language model on instruction following datasets", '- Stage 3: train language 模型 on instruction following 数据sets')
- text("Many capabilities:")
+ bilingual_text("Many capabilities:", '说明:Many capabilities:')
image("images/qwen2-vl-capabilities.png", width=700)
def qwen3_vl():
- text("Qwen3-VL "), link("https://arxiv.org/abs/2511.21631")
+ bilingual_text("Qwen3-VL ", '说明:Qwen3-VL'), link("https://arxiv.org/abs/2511.21631")
image("images/qwen3-vl.png", width=700)
- text("Language model:")
- text("- Qwen-3 models (dense and MoE models up to 235B-A22B)")
- text("- Long context understanding (256K)")
+ bilingual_text("Language model:", '语言模型:')
+ bilingual_text("- Qwen-3 models (dense and MoE models up to 235B-A22B)", '- Qwen-3 模型s (dense and MoE 模型s up to 235B-A22B)')
+ bilingual_text("- Long context understanding (256K)", '- Long con文本 理解 (256K)')
- text("Vision encoder:")
- text("- SigLIP-2 (same architecture as SigLIP) "), link("https://arxiv.org/pdf/2502.14786")
- text("- Interleaved MRoPE: distribute all axes (temporal, width, height) to low- and high-frequency bands")
- text("... [t w h t w h t w h t w h] rather than [t t t t w w w w h h h h]")
- text("- Add explicit video timestamps (as separate tokens rather in positional embeddings)")
- text("- Square-root-normalized per-token loss: balance text and multimodal data (video examples are long, don't want to dominate)")
+ bilingual_text("Vision encoder:", '视觉编码器:')
+ bilingual_text("- SigLIP-2 (same architecture as SigLIP) ", '- 说明:SigLIP-2 (same architecture as SigLIP)'), link("https://arxiv.org/pdf/2502.14786")
+ bilingual_text("- Interleaved MRoPE: distribute all axes (temporal, width, height) to low- and high-frequency bands", '- 说明:Interleaved MRoPE: distribute all axes (temporal, width, height) to low- and high-frequency bands')
+ bilingual_text("... [t w h t w h t w h t w h] rather than [t t t t w w w w h h h h]", '说明:... [t w h t w h t w h t w h] rather than [t t t t w w w w h h h h]')
+ bilingual_text("- Add explicit video timestamps (as separate tokens rather in positional embeddings)", '- Add explicit 视频 timestamps (as separate token rather in positional embeddings)')
+ bilingual_text("- Square-root-normalized per-token loss: balance text and multimodal data (video examples are long, don't want to dominate)", "- Square-root-normalized per-token 损失: balance 文本 and 多模态 数据 (视频 examples are long, don't want to dominate)")
- text("Adapter:")
- text("- DeepStack: cross-layer fusion to inject visual information into multiple layers "), link("https://arxiv.org/abs/2406.04334")
+ bilingual_text("Adapter:", '适配器:')
+ bilingual_text("- DeepStack: cross-layer fusion to inject visual information into multiple layers ", '- 说明:DeepStack: cross-layer fusion to inject visual information into multiple layers'), link("https://arxiv.org/abs/2406.04334")
- text("Training:")
- text("- Pre-training has 4 stages (train adapter, train all parameters on 8K, 32K, 256K lengths)")
+ bilingual_text("Training:", '训练:')
+ bilingual_text("- Pre-training has 4 stages (train adapter, train all parameters on 8K, 32K, 256K lengths)", '- Pre-训练 has 4 stages (train 适配器, train all parameters on 8K, 32K, 256K lengths)')
image("images/qwen3-vl-pretraining.png", width=600)
- text("- Post-training: SFT on long CoT data, knowledge distillation, RL")
+ bilingual_text("- Post-training: SFT on long CoT data, knowledge distillation, RL", '- Post-训练: SFT on long CoT 数据, knowledge distillation, RL')
image("images/qwen3-vl-results.png", width=600)
- text("Summary:")
- text("- SOTA performance")
- text("- Lots of data work, but not many details")
- text("- Minor but potentially important architectural improvements")
- text("- Scale up")
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- SOTA performance", '- 说明:SOTA performance')
+ bilingual_text("- Lots of data work, but not many details", '- Lots of 数据 work, but not many details')
+ bilingual_text("- Minor but potentially important architectural improvements", '- 说明:Minor but potentially important architectural improvements')
+ bilingual_text("- Scale up", '- 说明:Scale up')
def chameleon():
- text("Chameleon "), link("https://arxiv.org/pdf/2405.09818")
+ bilingual_text("Chameleon ", '说明:Chameleon'), link("https://arxiv.org/pdf/2405.09818")
- text("So far: VLMs encode images (via CLIP or SigLIP), inject into LM")
- text("Disadvantage: can't generate images (need diffusion)")
+ bilingual_text("So far: VLMs encode images (via CLIP or SigLIP), inject into LM", 'So far: VLMs en代码 图像 (via CLIP or SigLIP), inject into LM')
+ bilingual_text("Disadvantage: can't generate images (need diffusion)", "Disadvantage: can't generate 图像 (need diffusion)")
- text("Chameleon: map everything into discrete tokens")
- text("Advantage: can analyze and generate images in a uniform way")
+ bilingual_text("Chameleon: map everything into discrete tokens", '说明:Chameleon: map everything into discrete tokens')
+ bilingual_text("Advantage: can analyze and generate images in a uniform way", 'Advantage: can analyze and generate 图像 in a uniform way')
image("images/chameleon.png", width=600)
image("images/chameleon-example.png", width=600)
- text("Vision encoder "), link("https://arxiv.org/pdf/2203.13131")
- text("- Key difference: encoder needs to map to discrete tokens (so we can generate them)")
- text("- VQ-VAE (Vector Quantized Variational Autoencoder) "), link("https://arxiv.org/pdf/1711.00937")
- text("- Idea: map image to a discrete codebook, decode back to image and minimize reconstruction loss")
+ bilingual_text("Vision encoder ", 'Vision en代码r'), link("https://arxiv.org/pdf/2203.13131")
+ bilingual_text("- Key difference: encoder needs to map to discrete tokens (so we can generate them)", '- Key difference: en代码r needs to map to discrete token (so we can generate them)')
+ bilingual_text("- VQ-VAE (Vector Quantized Variational Autoencoder) ", '- VQ-VAE (Vector Quantized Variational Autoen代码r)'), link("https://arxiv.org/pdf/1711.00937")
+ bilingual_text("- Idea: map image to a discrete codebook, decode back to image and minimize reconstruction loss", '- 思想:map image to a discrete codebook, decode back to image and minimize reconstruction loss')
image("images/vq-vae.png", width=600)
- text("- Encodes 512 x 512 image into 1024 tokens (codebook of size 8192)")
- text("- Train a new BPE tokenizer")
+ bilingual_text("- Encodes 512 x 512 image into 1024 tokens (codebook of size 8192)", '- En代码s 512 x 512 图像 into 1024 token (代码book of size 8192)')
+ bilingual_text("- Train a new BPE tokenizer", '- Train a new BPE 分词器')
- text("Training:")
- text("- Stage 1 (80%): large-scale, unsupervised (2.9T text tokens, 1.5T text/image tokens, 400B text/image interleaved tokens)")
- text("- Stage 2 (20%): 50% of stage 1 data, 50% of high quality data")
+ bilingual_text("Training:", '训练:')
+ bilingual_text("- Stage 1 (80%): large-scale, unsupervised (2.9T text tokens, 1.5T text/image tokens, 400B text/image interleaved tokens)", '- Stage 1 (80%): large-scale, unsupervised (2.9T 文本 token, 1.5T 文本/图像 token, 400B 文本/图像 interleaved token)')
+ bilingual_text("- Stage 2 (20%): 50% of stage 1 data, 50% of high quality data", '- Stage 2 (20%): 50% of stage 1 数据, 50% of high 质量 数据')
- text("Training stability")
- text("- Text tokens have low entropy, image tokens have high entropy, leads to norm growth, logit drift problem")
- text("- Fixes: QK norm, z-loss regularization")
-
- text("Summary:")
- text("- Elegant (just autoregressive modeling of discrete tokens)")
- text("- Not as performant (discretization loses information - think OCR)")
- text("- Training with multiple modalities is tricky")
+ bilingual_text("Training stability", '训练稳定性')
+ bilingual_text("- Text tokens have low entropy, image tokens have high entropy, leads to norm growth, logit drift problem", '- 文本 token have low entropy, 图像 token have high entropy, leads to norm growth, logit drift problem')
+ bilingual_text("- Fixes: QK norm, z-loss regularization", '- Fixes: QK norm, z-损失 regularization')
+
+ bilingual_text("Summary:", '总结:')
+ bilingual_text("- Elegant (just autoregressive modeling of discrete tokens)", '- Elegant (just 自回归 模型ing of discrete token)')
+ bilingual_text("- Not as performant (discretization loses information - think OCR)", '- 说明:Not as performant (discretization loses information - think OCR)')
+ bilingual_text("- Training with multiple modalities is tricky", '- 训练 with multiple 模态 is tricky')
if __name__ == "__main__":
diff --git a/lecture_util.py b/lecture_util.py
index 6e5cdab..bd89387 100644
--- a/lecture_util.py
+++ b/lecture_util.py
@@ -1,4 +1,4 @@
-from edtrace import link
+from edtrace import link, text
def article_link(url: str) -> str:
@@ -13,5 +13,38 @@ def video_link(url: str) -> str:
return link(title="video", url=url)
+def bilingual_text(en: str, zh: str, **kwargs):
+ """Render English course text followed by its Chinese learning translation.
+
+ 渲染英文课程原文,并紧跟用于学习辅助的中文译文。
+ """
+ return text(f"{en}\n\n{zh}", **kwargs)
+
+
+def bilingual_note(en: str, zh: str, **kwargs):
+ """Render a bilingual explanatory note.
+
+ 渲染中英对照的解释性提示。
+ """
+ return bilingual_text(en, zh, **kwargs)
+
+
+def bilingual_caption(en: str, zh: str, **kwargs):
+ """Render a bilingual figure or table caption.
+
+ 渲染中英对照的图片或表格说明。
+ """
+ return bilingual_text(en, zh, **kwargs)
+
+
+def bilingual_verbatim(en: str, zh: str, **kwargs):
+ """Render an English verbatim block and a Chinese explanation below it.
+
+ 英文等宽块保持原样,中文说明单独放在下方,避免破坏 ASCII 对齐。
+ """
+ text(en, verbatim=True, **kwargs)
+ return text(zh)
+
+
def get_local_url(path: str) -> str:
- return "https://github.com/stanford-cs336/lectures/blob/main/" + path
\ No newline at end of file
+ return "https://github.com/stanford-cs336/lectures/blob/main/" + path
diff --git a/references.py b/references.py
index 679191f..d740df1 100644
--- a/references.py
+++ b/references.py
@@ -23,7 +23,8 @@ def join(*args):
organization="Google",
url="https://aclanthology.org/D07-1090.pdf",
notes=join(
- "Trained 5-gram model on 2T tokens"
+ "Trained 5-gram model on 2T tokens",
+ '在 2T token 上训练了 5-gram 模型。'
),
)
@@ -31,7 +32,7 @@ def join(*args):
title="A Neural Probabilistic Language Model", date="2003-02-01",
authors=["Yoshua Bengio", "Réjean Ducharme", "Pascal Vincent", "Christian Jauvin"],
url="https://www.jmlr.org/papers/volume3/bengio03a/bengio03a.pdf",
- notes="Used a feedforward neural network over last n words to predict the next word in a sequence"
+ notes=join('Used a feedforward neural network over last n words to predict the next word in a sequence', '使用前 n 个词上的前馈神经网络来预测序列中的下一个词。')
)
glorot_2010 = Reference(
@@ -46,37 +47,39 @@ def join(*args):
date="2011",
)
-seq2seq_2014 = url_reference("https://arxiv.org/pdf/1409.3215.pdf", organization="Google", notes="Introduced seq2seq (encode entire sentence into one vector, decode translation)")
+seq2seq_2014 = url_reference("https://arxiv.org/pdf/1409.3215.pdf", organization="Google", notes=join('Introduced seq2seq (encode entire sentence into one vector, decode translation)', '提出 seq2seq:把整个句子编码成一个向量,再解码为译文。'))
-adam_2014 = url_reference("https://arxiv.org/pdf/1412.6980.pdf", notes="Introduced Adam optimizer based on RMSProp and momentum")
+adam_2014 = url_reference("https://arxiv.org/pdf/1412.6980.pdf", notes=join('Introduced Adam optimizer based on RMSProp and momentum', '提出基于 RMSProp 和动量的 Adam 优化器。'))
-bahdanau_2015_attention = url_reference("https://arxiv.org/pdf/1409.0473.pdf", notes="Introduced attention mechanism (for machine translation)")
+bahdanau_2015_attention = url_reference("https://arxiv.org/pdf/1409.0473.pdf", notes=join('Introduced attention mechanism (for machine translation)', '提出注意力机制(用于机器翻译)。'))
sennrich_2016 = url_reference("https://arxiv.org/abs/1508.07909")
-layernorm_2016 = url_reference("https://arxiv.org/pdf/1607.06450.pdf", notes="Introduced LayerNorm")
+layernorm_2016 = url_reference("https://arxiv.org/pdf/1607.06450.pdf", notes=join('Introduced LayerNorm', '提出 LayerNorm。'))
cosine_learning_rate_2017 = url_reference("https://arxiv.org/pdf/1608.03983.pdf")
-transformer_2017 = url_reference("https://arxiv.org/pdf/1706.03762.pdf", organization="Google", notes="Introduced Transformer (for machine translation)")
+transformer_2017 = url_reference("https://arxiv.org/pdf/1706.03762.pdf", organization="Google", notes=join('Introduced Transformer (for machine translation)', '提出 Transformer(用于机器翻译)。'))
adamw_2017 = url_reference("https://arxiv.org/pdf/1711.05101.pdf", notes=join(
"Improves Adam by decoupling weight decay",
+ '通过解耦权重衰减改进 Adam。',
))
-ppo_2017 = url_reference("https://arxiv.org/pdf/1707.06347.pdf", notes="Introduced PPO (for RL)")
+ppo_2017 = url_reference("https://arxiv.org/pdf/1707.06347.pdf", notes=join('Introduced PPO (for RL)', '提出 PPO(用于强化学习)。'))
moe_2017 = url_reference("https://arxiv.org/pdf/1701.06538.pdf", organization="Google")
gpipe_2018 = url_reference("https://arxiv.org/pdf/1811.06965.pdf", organization="Google")
-large_batch_training_2018 = url_reference("https://arxiv.org/pdf/1812.06162.pdf", notes="Introduced critical batch size")
+large_batch_training_2018 = url_reference("https://arxiv.org/pdf/1812.06162.pdf", notes=join('Introduced critical batch size', '提出临界批大小。'))
elmo_2018 = url_reference("https://arxiv.org/abs/1802.05365")
bert_2018 = url_reference("https://arxiv.org/abs/1810.04805")
sparse_transformer_2019 = url_reference("https://arxiv.org/pdf/1904.10509.pdf", organization="OpenAI", notes=join(
- "Local attention"
+ "Local attention",
+ '局部注意力。'
))
gpt2_2019 = Reference(
@@ -86,7 +89,9 @@ def join(*args):
url="https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf",
notes=join(
"1.5B parameters",
+ '15 亿参数。',
"Pioneered stage release",
+ '开创了分阶段发布方式。',
),
)
@@ -94,14 +99,19 @@ def join(*args):
t5_2019 = url_reference("https://arxiv.org/pdf/1910.10683.pdf", organization="Google", notes=join(
"Encoder-decoder, frames tasks as text-to-text",
+ '编码器-解码器架构,把任务统一表述为文本到文本。',
"Introduced Colossal Cleaned Common Crawl (C4)",
+ '提出 Colossal Cleaned Common Crawl(C4)。',
"Filtering (Section 2.2)",
+ '过滤(第 2.2 节)。',
"11B parameters",
+ '110 亿参数。',
"Remove bias from feedforward layers",
+ '移除前馈层中的 bias。',
))
megatron_lm_2019 = url_reference("https://arxiv.org/pdf/1909.08053.pdf", organization="NVIDIA")
-zero_2019 = url_reference("https://arxiv.org/abs/1910.02054", organization="Microsoft", notes="Introduced ZeRO optimizer, can train 100B parameter model over 400 GPUs")
+zero_2019 = url_reference("https://arxiv.org/abs/1910.02054", organization="Microsoft", notes=join('Introduced ZeRO optimizer, can train 100B parameter model over 400 GPUs', '提出 ZeRO 优化器,可以在 400 块 GPU 上训练 100B 参数模型。'))
rms_norm_2019 = url_reference("https://arxiv.org/abs/1910.07467")
@@ -110,15 +120,22 @@ def join(*args):
kaplan_scaling_laws_2020 = url_reference("https://arxiv.org/pdf/2001.08361.pdf", organization="OpenAI", notes=join(
"Vary model size, dataset size, compute; get power laws",
+ '改变模型大小、数据集大小和计算量,得到幂律关系。',
"Larger models require fewer tokens",
+ '更大的模型需要更少 token。',
))
shazeer_2020 = url_reference("https://arxiv.org/pdf/2002.05202.pdf", organization="Google", notes=join(
"Experiments with different activation functions",
+ '实验比较不同激活函数。',
"Activation functions: ReLU, GeLU, Swish",
+ '激活函数:ReLU、GeLU、Swish。',
"Apply idea of gated units (GLU): ReGLU, GeGLU, SwiGLU",
+ '应用门控单元(GLU)的思想:ReGLU、GeGLU、SwiGLU。',
"FFN-SwiGLU = Swish(x W1) * xV W2",
+ '公式保持:FFN-SwiGLU = Swish(x W1) * xV W2。',
"Have 3 matrices now, so make hidden dimension 2/3 of the 2 matrix version",
+ '现在有 3 个矩阵,因此把隐藏维度设为双矩阵版本的 2/3。',
))
pre_post_norm_2020 = url_reference("https://arxiv.org/pdf/2002.04745.pdf")
@@ -127,18 +144,25 @@ def join(*args):
longformer_2020 = url_reference("https://arxiv.org/pdf/2004.05150.pdf", organization="AllenAI", notes=join(
"Sliding window (local) attention",
+ '滑动窗口(局部)注意力。',
"Global attention to capture task-specific information",
+ '使用全局注意力捕获任务特定信息。',
))
gpt_3_2020 = url_reference("https://arxiv.org/pdf/2005.14165.pdf", organization="OpenAI", notes=join(
"Introduces GPT-3",
+ '介绍 GPT-3。',
"Same as GPT-2, but alternating sparse and dense attention layers",
+ '与 GPT-2 类似,但交替使用稀疏和稠密注意力层。',
"175B parameters",
+ '175B 参数。',
"Data: 300B tokens",
+ '数据:300B token。',
))
the_pile_2020 = url_reference("https://arxiv.org/pdf/2101.00027.pdf", organization="EleutherAI", notes=join(
"825GB text, 22 diverse subsets (CommonCrawl, PubMed, ArXiv, GitHub, StackExchange, USPTO, OpenWebText2, Books3, etc.)",
+ '825GB 文本,包含 22 个多样子集(CommonCrawl、PubMed、ArXiv、GitHub、StackExchange、USPTO、OpenWebText2、Books3 等)。',
))
############################################################
@@ -146,17 +170,23 @@ def join(*args):
mmlu_2021 = url_reference("https://arxiv.org/pdf/2009.03300.pdf", organization="Berkeley", notes=join(
"57 subjects, multiple-choice",
+ '57 个科目,多项选择。',
))
rope_2021 = url_reference("https://arxiv.org/pdf/2104.09864.pdf", notes=join(
"Encodes absolute position with rotation matrix, incorporate relative position dependency in self-attention",
+ '用旋转矩阵编码绝对位置,并在自注意力中纳入相对位置依赖。',
"Key: R W x, where R is a block-diagonal sequence of d/2 rotation matrices (equation 13)",
+ '关键:R W x,其中 R 是由 d/2 个旋转矩阵组成的块对角序列(公式 13)。',
"Extrapolates to longer sequences",
+ '可外推到更长序列。',
))
megatron_parallelism_2021 = url_reference("https://arxiv.org/pdf/2104.04473.pdf", organization="NVIDIA", notes=join(
"Compose tensor, pipeline, data parallelism",
+ '组合张量并行、流水线并行和数据并行。',
"Achieve 52% MFU on 1T parameter model on 3072 GPUs",
+ '在 3072 块 GPU 上训练 1T 参数模型时达到 52% MFU。',
))
byt5_2021 = url_reference("https://arxiv.org/abs/2105.13626")
@@ -167,14 +197,19 @@ def join(*args):
url="https://arankomatsuzaki.wordpress.com/2021/06/04/gpt-j/",
notes=join(
"6.7B parameters",
+ '67 亿参数。',
"Attention and feedforward layers put in parallel",
+ '注意力层和前馈层并行放置。',
"v3 256 TPUs (5.4 PFLOPs) for 5 weeks",
+ '使用 v3 256 TPU(5.4 PFLOPs)训练 5 周。',
),
)
gopher_2021 = url_reference("https://arxiv.org/pdf/2112.11446.pdf", organization="DeepMind", notes=join(
"280B parameters",
+ '280B 参数。',
"Data: 300B tokens",
+ '数据:300B token。',
))
switch_transformers_2021 = url_reference("https://arxiv.org/abs/2101.03961", organization="Google")
@@ -184,48 +219,76 @@ def join(*args):
instruct_gpt_2022 = url_reference("https://arxiv.org/pdf/2203.02155.pdf", organization="OpenAI", notes=join(
"Training language models to follow instructions with human feedback",
+ '用人类反馈训练语言模型遵循指令。',
))
chinchilla_2022 = url_reference("https://arxiv.org/pdf/2203.15556.pdf", organization="DeepMind", notes=join(
"Introduced the rigorous analysis scaling laws for language models",
+ '提出了 the rigorous analysis 规模定律 for language 模型s',
"Key improvement over Kaplan: tune learning rate for the compute budget",
+ 'Key improvement over Kaplan: tune 学习率 for the 计算预算',
"Approach 1: for each model size, train with 4 learning rates, vary number of training tokens, fit lower envelope",
+ 'Approach 1: for each 模型 size, train with 4 学习率s, vary number of 训练 token, fit lower envelope',
"Approach 2 (IsoFLOP): for each model size, train with 9 training budgets, take last point",
+ 'Approach 2 (IsoFLOP): for each 模型 size, train with 9 训练 budgets, take last point',
"Approach 3: fit parametric function L(N, D) = E + A/N^alpha + B/D^beta to data collected from approaches 1 and 2",
+ 'Approach 3: fit parametric function L(N, D) = E + A/N^alpha + B/D^beta to 数据 collected from approaches 1 and 2',
"Conclusion: model and data should scale up at same rate",
+ '结论:模型和数据应以相同速率扩展。',
"Table 3: extrapolate to 10 trillion parameters",
+ 'Table 3: extrapolate to 10 trillion 参数',
"MassiveText, different data distribution (1.5 trillion tokens)",
+ 'MassiveText, different 数据 distribution (1.5 trillion token)',
"70B parameters",
+ '70B 参数。',
))
palm_2022 = url_reference("https://arxiv.org/pdf/2204.02311.pdf", organization="Google", notes=join(
"Data: Social media conversations, webpages, books, GitHub, Wikipedia, news",
+ '数据: Social media conversations, webpages, books, GitHub, Wikipedia, news',
"540B parameters",
+ '540B 参数',
"SwiGLU, parallelize attention and feedforward layers, multi-query attention, RoPE, remove biases",
+ 'SwiGLU, 并行ize 注意力 and 前馈 layers, multi-query 注意力, RoPE, remove biases',
"hardware: 6144 TPUv4, 46.2% MFU",
+ '硬件: 6144 TPUv4, 46.2% MFU',
"optimizer: Adafactor without factorization",
+ '优化器: Adafactor without factorization',
"Introduced the term model FLOPs utilization (MFU) metric (observed tokens/sec / theoretical max tokens/sec)",
+ '提出了 the term 模型 FLOPs utilization (MFU) metric (observed token/sec / theoretical max token/sec)',
))
gpt_neox_2022 = url_reference("https://arxiv.org/pdf/2204.06745.pdf", organization="EleutherAI", notes=join(
"Data: The Pile",
+ '数据:The Pile。',
"20B parameters",
+ '20B 参数。',
"Use RoPE, parallel attention and feedforward layers (15% throughput increase)",
+ '使用 RoPE,并行化注意力层和前馈层(吞吐量提升 15%)。',
"hardware: 12x8 A100s",
+ '硬件:12x8 A100。',
))
opt_175b_2022 = url_reference("https://arxiv.org/pdf/2205.01068.pdf", organization="Meta", notes=join(
"Data: The Pile, PushShift.io Reddit, deduplication",
+ '数据: The Pile, PushShift.io Reddit, 去重',
"175B parameters",
+ '175B 参数。',
"hardware: 992 A100 80GB for 2 months, lots of hardware failures",
+ '硬件: 992 A100 80GB for 2 months, lots of 硬件 failures',
"FSDP with Megatron-LM, fp16 with loss scaling",
+ '说明:FSDP with Megatron-LM, fp16 with loss scaling',
))
bloom_2022 = url_reference("https://arxiv.org/abs/2211.05100", organization="BigScience", notes=join(
"Model: BLOOM (176B parameters)",
+ '模型:BLOOM(176B 参数)。',
"Data: ROOTS",
+ '数据:ROOTS。',
"Hardware: 48x8 A100s on Jean Zay supercomputer for 3.5 months",
+ '硬件:Jean Zay 超算上的 48x8 A100,训练 3.5 个月。',
"ZeRO stage 1",
+ 'ZeRO stage 1。',
))
bahdanau_training_costs_2022 = Reference(
@@ -240,14 +303,20 @@ def join(*args):
llama_2023 = url_reference("https://arxiv.org/pdf/2302.13971.pdf", organization="Meta", notes=join(
"Train only on open data (detailed recipe that is replicated by RedPajama)",
+ 'Train only on open 数据 (detailed recipe that is replicated by RedPajama)',
"Optimize for fast inference at 7B",
+ 'Optimize for fast 推理 at 7B',
"Data: CommonCrawl, C4, GitHub, Wikipedia, Books, ArXiv, StackExchange",
+ '数据: CommonCrawl, C4, GitHub, Wikipedia, Books, ArXiv, StackExchange',
"Architecture: Pre-norm, SwiGLU, RoPE",
+ '说明:Architecture: Pre-norm, SwiGLU, RoPE',
"Training: 2048 A100 80GB for 21 days",
+ '说明:Training: 2048 A100 80GB for 21 days',
))
gpt_4_2023 = url_reference("https://arxiv.org/pdf/2303.08774.pdf", organization="OpenAI", notes=join(
"No details on the data or model architecture.",
+ '没有披露数据或模型架构细节。',
))
alpaca_2023 = Reference(title="Alpaca", authors=["Rohan Taori", "Ishaan Gulrajani", "Tianyi Zhang", "Yann Dubois", "Xuechen Li", "Carlos Guestrin", "Percy Liang", "Tatsunori B. Hashimoto"], date="2023-03-13", url="https://crfm.stanford.edu/2023/03/13/alpaca.html")
@@ -259,8 +328,11 @@ def join(*args):
gqa_2023 = url_reference("https://arxiv.org/pdf/2305.13245.pdf", organization="Google", notes=join(
"Multi-query attention (MQA) speeds up, but less expressive",
+ 'Multi-query 注意力 (MQA) speeds up, but less expressive',
"GQA: use an intermediate (more than one, less than number of heads) number of key-value heads",
+ '说明:GQA: use an intermediate (more than one, less than number of heads) number of key-value heads',
"Experiments on T5",
+ '说明:Experiments on T5',
))
lima_2023 = url_reference("https://arxiv.org/pdf/2305.11206.pdf")
@@ -271,11 +343,14 @@ def join(*args):
llama_2_2023 = url_reference("https://arxiv.org/pdf/2307.09288.pdf", organization="Meta", notes=join(
"2T tokens",
+ '说明:2T tokens',
"70B parameters",
+ '70B 参数。',
))
mistral_7b_2023 = url_reference("https://arxiv.org/pdf/2310.06825.pdf", organization="Mistral", notes=join(
"GQA, sliding window attention",
+ 'GQA, sliding window 注意力',
))
qk_norm_2023 = url_reference("https://arxiv.org/abs/2302.05442")
@@ -285,21 +360,29 @@ def join(*args):
llama_3_2024 = url_reference("https://arxiv.org/abs/2407.21783", organization="Meta", notes=join(
"15T tokens",
+ '说明:15T tokens',
"405B parameters",
+ '405B 参数',
))
deepseek_67b_2024 = url_reference("https://arxiv.org/pdf/2401.02954.pdf", organization="DeepSeek", notes=join(
"Data: DeepSeek, The Stack, Reddit, etc. (2T tokens)",
+ '数据: DeepSeek, The Stack, Reddit, etc. (2T token)',
"Architecture: LLaMA, but: for GQA increased depth, 67B parameters",
+ 'Architecture: LLaMA, but: for GQA increased depth, 67B 参数',
"Scaling laws: used non-embedding FLOPs with IsoFLOP",
+ '说明:Scaling laws: used non-embedding FLOPs with IsoFLOP',
))
mixtral_2024 = url_reference("https://arxiv.org/pdf/2401.04088.pdf", organization="Mistral")
olmo_7b_2024 = url_reference("https://arxiv.org/pdf/2402.00838.pdf", organization="AI2", notes=join(
"Data: subset of Dolma (2.46T tokens, CommonCrawl, The Stack, Reddit, etc.)",
+ '数据: subset of Dolma (2.46T token, CommonCrawl, The Stack, Reddit, etc.)',
"Architecture: no biases, non-parametric layer norm, SwiGLU (8/3 d increased to closest multiple of 128)",
+ '说明:Architecture: no biases, non-parametric layer norm, SwiGLU (8/3 d increased to closest multiple of 128)',
"Training: 256x4 AMD MI250X on LUMI supercomputer, 27x8 A100s, 800Gbps interconnect",
+ '说明:Training: 256x4 AMD MI250X on LUMI supercomputer, 27x8 A100s, 800Gbps interconnect',
))
dolma_2024 = url_reference("https://arxiv.org/abs/2402.00159")
@@ -308,29 +391,42 @@ def join(*args):
megascale_2024 = url_reference("https://arxiv.org/pdf/2402.15627.pdf", organization="Bytedance", notes=join(
"55.2% MFU for 175B parameter model over 12,288 GPUs",
+ '55.2% MFU for 175B 参数 模型 over 12,288 GPUs',
"Combine data, tensor, pipeline, sequence parallelism",
+ 'Combine 数据, tensor, pipeline, sequence 并行ism',
"Parallelize attention and feedforward layers, sliding window attention, LAMB optimizer",
+ 'Parallelize 注意力 and 前馈 layers, sliding window 注意力, LAMB 优化器',
))
nemotron_15b_2024 = url_reference("https://arxiv.org/pdf/2402.16819.pdf", organization="NVIDIA", notes=join(
"Data: 8T tokens, 70% English, 15% multilingual, 15% code",
+ '数据: 8T token, 70% English, 15% multilingual, 15% code',
"Architecture: RoPE, squared ReLU activations, no bias, no dropout, GQA (15B parameters)",
+ 'Architecture: RoPE, squared ReLU 激活s, no bias, no dropout, GQA (15B 参数)',
"Training: 384x8 H100s, After 8T tokens, train on higher quality sources + benchmark tasks",
+ '说明:Training: 384x8 H100s, After 8T tokens, train on higher quality sources + benchmark tasks',
))
yi_34b_2024 = url_reference("https://arxiv.org/pdf/2403.04652.pdf", organization="01.AI")
gemma_2024 = url_reference("https://arxiv.org/pdf/2403.08295.pdf", organization="Google DeepMind", notes=join(
"6T tokens",
+ '说明:6T tokens',
"MQA, RoPE, GeGLU, RMSNorm",
+ '说明:MQA, RoPE, GeGLU, RMSNorm',
"Training: 4096 v4 TPUs, Use ZeRO-3 like techniques",
+ '说明:Training: 4096 v4 TPUs, Use ZeRO-3 like techniques',
))
overtrained_scaling_laws_2024 = url_reference("https://arxiv.org/pdf/2403.08540.pdf", notes=join(
"Chinchilla scaling laws focus on loss of the trained model, ignoring inference costs.",
+ 'Chinchilla 规模定律 focus on loss of the trained 模型, ignoring 推理 costs.',
"Constant ratio of training tokens to parameters",
+ 'Constant ratio of 训练 token to 参数',
"Extrapolate over 300x training compute to 1.4B model on 900B tokens",
+ 'Extrapolate over 300x 训练 compute to 1.4B 模型 on 900B token',
"Look at task performance rather than validation loss",
+ '说明:Look at task performance rather than validation loss',
))
minicpm_2024 = wsd_2024 = url_reference("https://arxiv.org/pdf/2404.06395.pdf", organization="Tsinghua")
@@ -379,7 +475,9 @@ def join(*args):
smollm_2_2025 = url_reference("https://arxiv.org/pdf/2502.02737.pdf", notes=join(
"1.7B parameter model",
+ '1.7B 参数 模型',
"Introduces FineMath, StackEdu",
+ '说明:Introduces FineMath, StackEdu',
))
llama_4_2025 = Reference(title="Llama 4", organization="Meta", url="https://ai.meta.com/blog/llama-4-multimodal-intelligence/", date="2025-04-05")
]