# 配套代码：把课程里的算法亲手跑一遍

> DMXSF 的配套代码仓库提供 61 个可在笔记本 CPU 上运行的 Python 实验，覆盖全部 66 篇含可运行内容的 Wiki，并核对核心公式与实验机制。

- URL: https://dmxsf.com/code/
- 更新于: 2026-11-18

[dmxsf/dmxsf](https://github.com/dmxsf/dmxsf) 提供 61 个 CPU 实验，从手算张量、核对 GPU 屋顶线，到跑通本地推理接口和 Agent，再到训练、评测与安全。每个实验都附中英文说明、运行命令和实测输出。默认运行无需 GPU、模型 API 或额外下载权重。

## 快速开始

```bash
git clone https://github.com/dmxsf/dmxsf.git
cd dmxsf
uv sync                      # 需要 Python 3.10 以上；也可以 pip install -e .
uv run dmxsf list            # 列出全部实验
uv run dmxsf run foundations/tensors-and-shapes
uv run dmxsf roadmap weekend # 周末速通的实验安排
```

依赖用 [uv](https://docs.astral.sh/uv/) 管理；不想装 uv，`pip install -e .` 之后把命令里的 `uv run` 去掉即可。

## 代码与课程怎么对应

- **66 篇含实操内容的 Wiki 都有对应实验。** 目录 `labs/<章节>/<页面>/` 与主页面路径一致，信息论和下一个 token 预测等紧密相关的两页可以共用一个实验。页尾的「配套代码」卡片给出完整命令、源码位置和对应语言的 README。
- **实验核对核心公式与机制。** 参数量、显存账本和概率用可复算的数值检查，GPU 性能或规模效应有时采用小型 CPU 演示。输出里 `[ok]` 表示检查通过，`[FAIL]` 会让命令以非零状态退出。`--seed N` 可以更换随机种子。
- **原规模结果的复现条件写在说明里。** README 会注明哪些结果来自演示，哪些可用 `--hf` 或 `--bge` 复现，哪些需运行 Wiki 正文的实验。
- **库、实验、测试分开。** `src/dmxsf/` 是可以导入的库，`labs/` 是读起来像教程的脚本，`tests/` 是对库的测试；CI 在每次提交时跑完全部测试，并把每个实验以 `--quick` 跑一遍。

## 怎么用

1. 读一篇 Wiki，遇到公式或表格时记下想验证的地方。
2. 运行它的实验，对着输出读源码；每个实验开头的说明写明了它核对页面的哪些内容。
3. 改一个参数再跑一遍，做实验说明里的练习。

## 环境与耗时

- 只需要 CPU。第一次运行用到预训练模型的实验，会先在本机训练一个很小的字节级模型（几分钟），之后缓存在 `~/.cache/dmxsf`。
- 每个实验完整运行的耗时见下面的列表；`--quick` 是冒烟版本，用来确认环境没问题。
- 毫秒、吞吐量这类计时数字因机器而异，读实验里的比值，不读绝对值。

## 全部实验

每个实验对应一篇或两篇 Wiki，目录与 Wiki 路径一致；输出会逐项核对页面上的数字。 (61 个实验)

### 入门指南

#### [大模型是什么：一个小到能读完的完整循环](https://github.com/dmxsf/dmxsf/tree/main/labs/start/what-is-an-llm)

在一个 842,496 参数、能整个打开的 GPT 上跑通完整循环：预测一个字节、接上去、再预测；从分词、嵌入到 softmax、训练目标和三个关键数字（参数、token、上下文）。末尾是全部课程页面与实验的对照表。

- 对应页面: [大模型是什么：从“预测下一个词”到通用助手](https://dmxsf.com/wiki/start/what-is-an-llm/)、[大模型学习路线图：知识库地图与四条学习路线](https://dmxsf.com/wiki/start/learning-roadmap/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run start/what-is-an-llm`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/start/what-is-an-llm/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/start/what-is-an-llm/README.md)

#### [公式速查：参数量、显存与算力](https://github.com/dmxsf/dmxsf/tree/main/labs/start/cheat-sheet)

交叉核对公开模型参数量、KV 显存、训练 FLOPs 和 pass@k。

- 对应页面: [大模型速查表：参数量、算力、显存与解码速度怎么估](https://dmxsf.com/wiki/start/cheat-sheet/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run start/cheat-sheet`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/start/cheat-sheet/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/start/cheat-sheet/README.md)

### 基础概念

#### [张量形状：手算线性层](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/tensors-and-shapes)

用线性投影、偏置和 ReLU 练习位置轴与特征轴。

- 对应页面: [Python 与张量形状：从一张表到线性层](https://dmxsf.com/wiki/foundations/tensors-and-shapes/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run foundations/tensors-and-shapes`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/tensors-and-shapes/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/tensors-and-shapes/README.md)

#### [下一个词预测与信息论](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/next-token-prediction)

在课程页的玩具语料上数二元组，核对熵、交叉熵与 KL 散度的关系；在莎士比亚文本上看字符 n 元组在数据耗尽前的收益；再用梯度下降训练一个神经二元组，让它学出同一张计数表。

- 对应页面: [从语言建模到下一个 token 预测](https://dmxsf.com/wiki/foundations/next-token-prediction/)、[信息论回顾：熵、交叉熵与 KL 散度](https://dmxsf.com/wiki/foundations/information-theory/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run foundations/next-token-prediction`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/next-token-prediction/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/next-token-prediction/README.md)

#### [神经网络基础：梯度与 XOR](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/neural-network-basics)

用数值差分独立核对反传，再比较线性层和带隐藏层的网络怎样学习 XOR。

- 对应页面: [神经网络基础：从线性层到反向传播](https://dmxsf.com/wiki/foundations/neural-network-basics/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run foundations/neural-network-basics`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/neural-network-basics/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/neural-network-basics/README.md)

#### [分词器与词嵌入](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/tokenizer-and-embeddings)

手写 BPE（课程页的玩具语料），实现永不出现 <unk> 的字节级 BPE，看训练文本如何决定一种语言的 token 成本；嵌入表等价于一次矩阵乘法；以及嵌入表在整个模型里占多大比例。

- 对应页面: [词嵌入与 Tokenizer 原理](https://dmxsf.com/wiki/foundations/tokenizer-and-embeddings/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run foundations/tokenizer-and-embeddings`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/tokenizer-and-embeddings/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/tokenizer-and-embeddings/README.md)

#### [优化器与训练稳定性](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/optimizers-and-training-stability)

手写 SGD、Adam、AdamW 并在 float64 下与 PyTorch 对拍；Adam 为何在病态二次函数上更快；解耦的权重衰减；学习率调度；混合精度与 FP32 主权重；一个被污染的批次里梯度裁剪和 beta2 各起什么作用。

- 对应页面: [优化器与训练稳定性：AdamW、学习率调度、混合精度与损失尖峰](https://dmxsf.com/wiki/foundations/optimizers-and-training-stability/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run foundations/optimizers-and-training-stability`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/optimizers-and-training-stability/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/foundations/optimizers-and-training-stability/README.md)

### 模型架构

#### [从 RNN 到注意力：梯度与检索路径](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/from-rnn-to-attention)

对比循环链路和门控记忆链路的梯度，再用注意力找回此前的位置。

- 对应页面: [从 RNN 到注意力：Transformer 为什么出现](https://dmxsf.com/wiki/architecture/from-rnn-to-attention/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/from-rnn-to-attention`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/from-rnn-to-attention/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/from-rnn-to-attention/README.md)

#### [注意力机制](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/attention-basics)

手算课程页的三位置例子；为什么要除以 sqrt(d)（点积的方差是 d）；掩码、多头与位置；再训练一个必须靠注意力才能完成“复制上次出现之后的内容”的小模型。

- 对应页面: [Transformer 注意力机制入门](https://dmxsf.com/wiki/architecture/attention-basics/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/attention-basics`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/attention-basics/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/attention-basics/README.md)

#### [位置编码与长上下文](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/positional-encoding)

正弦编码是一组周期不同的时钟；RoPE 的分数只依赖相对距离；位置插值、NTK-aware、YaRN 与 Llama 3.1 的拉伸配方并排计算；五种位置方案在短文本上训练、在长文本上测试。

- 对应页面: [位置编码详解（绝对 → 相对 → RoPE）](https://dmxsf.com/wiki/architecture/positional-encoding/)、[长上下文：位置外推、训练配方与成本](https://dmxsf.com/wiki/architecture/long-context/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/positional-encoding`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/positional-encoding/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/positional-encoding/README.md)

#### [Transformer Block 与现代架构](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/transformer-block)

数清一个 GPT-2 small 的 Block（12d²+13d 个参数）；看真实模型的参数分布；比较 Pre-Norm 与 Post-Norm 在不同深度的信号与梯度；再从 GPT-2 到 Llama，一次只改一处。

- 对应页面: [Transformer Block 完整拆解](https://dmxsf.com/wiki/architecture/transformer-block/)、[现代大模型架构：从 GPT-2 到 Llama、Qwen 与 DeepSeek](https://dmxsf.com/wiki/architecture/modern-llm-architecture/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/transformer-block`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/transformer-block/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/transformer-block/README.md)

#### [高效注意力：GQA、MLA 与 FlashAttention](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/efficient-attention)

KV Cache 公式与课程页的表格、真实张量对拍；MLA 缓存潜向量；FlashAttention 的分块与在线 softmax；滑动窗口注意力；把 MHA 模型通过头的均值池化转成 GQA 并微调。

- 对应页面: [高效注意力：MQA、GQA、MLA 与 FlashAttention](https://dmxsf.com/wiki/architecture/efficient-attention/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/efficient-attention`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/efficient-attention/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/efficient-attention/README.md)

#### [混合专家（MoE）](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/mixture-of-experts)

只计算被路由到的 token 的 MoE 层；在真实模块上数总参数与激活参数；能跑起来的 DeepSeek-V3 风格玩具模型；专家容量；负载均衡损失；解码时一个批次会激活多少专家；以及在字节数据上训练的 MoE 与同算力、同参数量稠密模型的对比。

- 对应页面: [混合专家（MoE）：路由、负载均衡与稀疏激活](https://dmxsf.com/wiki/architecture/mixture-of-experts/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/mixture-of-experts`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/mixture-of-experts/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/mixture-of-experts/README.md)

#### [状态空间与混合架构：等价扫描与显存](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/state-space-and-hybrid)

对比同一选择性更新的递推与显式因果形式，并分开核算混合架构的状态和缓存。

- 对应页面: [状态空间模型与混合架构：用固定大小的状态换掉不断增长的 KV Cache](https://dmxsf.com/wiki/architecture/state-space-and-hybrid/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run architecture/state-space-and-hybrid`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/state-space-and-hybrid/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/architecture/state-space-and-hybrid/README.md)

### 算力与系统

#### [硬件入门：容量与复制时间](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/hardware-basics)

用一致的单位算张量容量和理想复制时间。

- 对应页面: [大模型硬件入门：CPU、GPU、内存与数据搬运](https://dmxsf.com/wiki/systems/hardware-basics/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run systems/hardware-basics`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/hardware-basics/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/hardware-basics/README.md)

#### [GPU 屋顶线：字节、运算与分块复用](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/gpu-and-roofline)

从 A100 和 H100 的硬件规格算转折点，再核对线性层和分块矩阵乘法的数据搬运量。

- 对应页面: [GPU 与屋顶线模型：为什么大模型常常卡在显存带宽上](https://dmxsf.com/wiki/systems/gpu-and-roofline/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run systems/gpu-and-roofline`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/gpu-and-roofline/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/gpu-and-roofline/README.md)

#### [GPU 内核：分块矩阵乘与访存](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/gpu-kernels)

在不规则形状上实现分块矩阵乘，逐项核对输出与逻辑访存量。

- 对应页面: [GPU 内核入门：从分块矩阵乘到算子融合](https://dmxsf.com/wiki/systems/gpu-kernels/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run systems/gpu-kernels`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/gpu-kernels/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/gpu-kernels/README.md)

#### [数值精度：舍入、溢出与损失缩放](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/numerical-precision)

在 CPU 上复现低精度累加停滞、稳定 softmax，以及损失缩放如何保留小梯度。

- 对应页面: [数值精度：FP16、BF16 与 FP8 训练里的三类故障](https://dmxsf.com/wiki/systems/numerical-precision/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run systems/numerical-precision`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/numerical-precision/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/numerical-precision/README.md)

#### [集合通信：模拟环形 All-Reduce](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/collective-communication)

逐轮追踪 Reduce-Scatter 与 All-Gather，再用 α-β 模型核对每个 rank 的通信量。

- 对应页面: [集合通信：all-reduce 为什么有这么多种算法](https://dmxsf.com/wiki/systems/collective-communication/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run systems/collective-communication`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/collective-communication/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/systems/collective-communication/README.md)

### 预训练

#### [预训练流水线](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/training-pipeline)

花算力之前的两个免费检查；基线训练与监控日志的读法；然后是三个实验：数据配比、从检查点恢复的四种做法（只有一种严格精确）、验证集泄漏进训练数据。

- 对应页面: [大模型训练流水线总览](https://dmxsf.com/wiki/pretraining/training-pipeline/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run pretraining/training-pipeline`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/training-pipeline/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/training-pipeline/README.md)

#### [数据工程：构建并审计去重过滤器](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/data-engineering)

比较精确重复与词片相似度，用分桶概率曲线审视去重阈值。

- 对应页面: [预训练数据工程：过滤、去重与配比](https://dmxsf.com/wiki/pretraining/data-engineering/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run pretraining/data-engineering`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/data-engineering/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/data-engineering/README.md)

#### [扩展定律](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/scaling-laws)

重算课程页的幂律与计算最优配比；推理成本如何改变最优模型；一次真实的小型扩展实验（按规模调学习率、训练网格、拟合 L(N, D)、预测没跑过的配置）；以及 12 个点的拟合能识别什么、不能识别什么。

- 对应页面: [扩展定律：算力、参数与数据如何分配](https://dmxsf.com/wiki/pretraining/scaling-laws/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run pretraining/scaling-laws`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/scaling-laws/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/scaling-laws/README.md)

#### [训练的算力与显存](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/compute-and-memory)

页面上的每个公式都用实测核对：PyTorch 的 FLOP 计数器、自动求导为反向保存的张量、优化器状态；再到 MFU、GPU 小时，以及 8 × 80 GiB 上训练 Llama 2 7B 的完整账本。

- 对应页面: [训练的算力与显存账：FLOPs、MFU 与显存预算](https://dmxsf.com/wiki/pretraining/compute-and-memory/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run pretraining/compute-and-memory`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/compute-and-memory/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/compute-and-memory/README.md)

#### [分布式训练](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/distributed-training)

在一台机器的多个进程里搭起来并实测：数据并行的梯度平均、环形 all-reduce、数据并行与 ZeRO-1 / ZeRO-3 并排训练（权重一致，实测显存与通信量）、Megatron 式张量并行、流水线调度。

- 对应页面: [分布式训练入门：数据并行、张量并行与流水线并行](https://dmxsf.com/wiki/pretraining/distributed-training/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run pretraining/distributed-training`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/distributed-training/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/pretraining/distributed-training/README.md)

### 后训练与对齐

#### [SFT 数据：损失看到什么](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/sft-data)

SFT 损失到底在平均什么（-100 掩码、打包与填充）；用埋好答案的数据做健康检查；四个对照实验：同配置重复运行的波动、每类样本数、错误标签、提示部分算不算损失与回放通用文本。

- 对应页面: [SFT 数据构造与质量控制](https://dmxsf.com/wiki/post-training/sft-data/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/sft-data`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/sft-data/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/sft-data/README.md)

#### [LoRA 微调](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/lora-finetuning)

从零实现 LoRA 并与全量微调对比：精确的参数账本、两者各自的学习率窗口、秩与 alpha、该适配哪些矩阵、学到的更新有多低秩、合并进权重和 4-bit（NF4）权重、显存与速度。

- 对应页面: [LoRA 微调实战](https://dmxsf.com/wiki/post-training/lora-finetuning/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/lora-finetuning`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/lora-finetuning/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/lora-finetuning/README.md)

#### [奖励模型：成对打分与标签噪声](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/reward-models)

在连通比较图上拟合 Bradley–Terry 分数，再核对独立标注噪声下的一致率和表现上限。

- 对应页面: [奖励模型：把人类偏好变成可以优化的分数](https://dmxsf.com/wiki/post-training/reward-models/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/reward-models`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/reward-models/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/reward-models/README.md)

#### [强化学习基础：策略梯度、基线与 GAE](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/rl-fundamentals)

枚举小策略，核对数值梯度，观察 GAE 的 λ 如何改变有限轨迹中的优势。

- 对应页面: [强化学习基础：策略梯度、优势估计与 PPO](https://dmxsf.com/wiki/post-training/rl-fundamentals/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/rl-fundamentals`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/rl-fundamentals/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/rl-fundamentals/README.md)

#### [偏好优化（DPO）](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/preference-optimization)

手算 DPO 损失；闭式最优解对照随机竞争者与拟合结果；在小型字节模型上做真实 DPO（学习率、beta、被选回答的对数概率下降、以关掉 LoRA 作参考模型）；以及 KL 预算增大时的奖励黑客。

- 对应页面: [偏好优化：从 RLHF 到 DPO](https://dmxsf.com/wiki/post-training/preference-optimization/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/preference-optimization`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/preference-optimization/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/preference-optimization/README.md)

#### [推理强化学习（GRPO 与可验证奖励）](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/reasoning-rl)

验证器、组内优势、GRPO 损失、裁剪与 k3 KL 估计量的精确核对；然后是真实的 RLVR 训练：小模型仅凭精确匹配给出的 0/1 奖励完成策略更新。

- 对应页面: [推理强化学习：RLVR 与 GRPO](https://dmxsf.com/wiki/post-training/reasoning-rl/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/reasoning-rl`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/reasoning-rl/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/reasoning-rl/README.md)

#### [推理强化学习系统：采样与训练调度](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/rollout-and-training)

回放串行与重叠调度，核对版本滞后和丢弃批次。

- 对应页面: [推理强化学习系统：异步采样、训练与策略滞后](https://dmxsf.com/wiki/post-training/rollout-and-training/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run post-training/rollout-and-training`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/rollout-and-training/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/post-training/rollout-and-training/README.md)

### 推理与部署

#### [第一个推理服务：本机 HTTP 接口](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/first-inference-service)

向演示推理器发送真实的本机 HTTP 请求并检查状态码。

- 对应页面: [第一个推理服务：让模型函数接收 HTTP 请求](https://dmxsf.com/wiki/inference/first-inference-service/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/first-inference-service`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/first-inference-service/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/first-inference-service/README.md)

#### [解码与采样](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/decoding-and-sampling)

精确复现课程页的四 token 例子（温度、top-k、top-p、min-p 以及顺序为何重要）；采样是否服从处理后的分布；各个旋钮对真实文本的影响；束搜索、重复惩罚；批次形状是否会改变贪心输出。

- 对应页面: [解码与采样：温度、Top-k、Top-p 与束搜索](https://dmxsf.com/wiki/inference/decoding-and-sampling/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/decoding-and-sampling`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/decoding-and-sampling/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/decoding-and-sampling/README.md)

#### [KV Cache](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/kv-cache)

显存公式对照真实缓存张量；有缓存与无缓存的解码结果完全一致；节省的量用两种方式衡量：处理的 token 位置数（精确）与 CPU 计时（看比值）；再看预填充与解码的差别。

- 对应页面: [KV Cache 与推理性能优化](https://dmxsf.com/wiki/inference/kv-cache/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/kv-cache`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/kv-cache/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/kv-cache/README.md)

#### [前缀缓存：命中与排队](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/prefix-caching)

用两种缓存路由回放突发与等间隔请求。

- 对应页面: [前缀缓存与 PD 分离：让重复计算只发生一次](https://dmxsf.com/wiki/inference/prefix-caching/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/prefix-caching`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/prefix-caching/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/prefix-caching/README.md)

#### [量化](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/quantization)

Llama 3 8B 在 80 GB 显卡上的账本；取整网格；以及在小模型上的实验：就近取整、SmoothQuant、AWQ 式缩放搜索、GPTQ、KV Cache 量化，其中的离群值是人为构造并明确标注的。

- 对应页面: [模型量化：INT8、INT4、GPTQ 与 AWQ 的原理与实测](https://dmxsf.com/wiki/inference/quantization/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/quantization`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/quantization/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/quantization/README.md)

#### [推理服务：连续批处理、分页 KV 与前缀缓存](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/continuous-batching)

Roofline 与精确的批量解码；静态与连续批处理的纸面推演和真实模型对比；带写时复制的分页 KV 池；基于哈希的前缀缓存；一个请求的成本（TTFT、TPOT、容量）。看比值，不看毫秒。

- 对应页面: [连续批处理与 PagedAttention：推理服务如何同时服务很多请求](https://dmxsf.com/wiki/inference/continuous-batching/)、[LLM 推理服务：延迟指标、实测方法与引擎选型](https://dmxsf.com/wiki/inference/serving/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/continuous-batching`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/continuous-batching/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/continuous-batching/README.md)

#### [投机解码](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/speculative-decoding)

保持目标模型精确分布的接受/拒绝规则：在课程页的玩具分布上、用精确代数、在真实模型上逐一验证；Leviathan 等人的公式；带 KV Cache 的草稿/目标字节模型对；以及什么时候不划算。

- 对应页面: [投机解码：用小模型打草稿，让大模型一次验证多个 token](https://dmxsf.com/wiki/inference/speculative-decoding/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run inference/speculative-decoding`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/speculative-decoding/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/inference/speculative-decoding/README.md)

### 应用工程

#### [第一个 Agent：有界工具循环](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/first-agent)

回放只读查找循环，验证成功、缺失与步数上限。

- 对应页面: [第一个 Agent：从只读工具到有界循环](https://dmxsf.com/wiki/applications/first-agent/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/first-agent`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/first-agent/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/first-agent/README.md)

#### [Prompt Engineering：可测量的提示词](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/prompt-engineering)

手工渲染对话模板；前缀缓存能复用什么；把格式错误与判断错误分开的评分器；用代码重新解十二道应用题；12 张工单的评测集有多可信。加 --hf 可在 Qwen2.5-1.5B-Instruct 上重跑页面里的实验。

- 对应页面: [Prompt Engineering 系统指南：任务规格、消息分层、示例与评测回归](https://dmxsf.com/wiki/applications/prompt-engineering/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/prompt-engineering`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/prompt-engineering/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/prompt-engineering/README.md)
- 可选：加 --hf 或 --bge 运行真实模型，需要 uv sync --extra hf

#### [结构化输出与函数调用](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/structured-output-and-function-calling)

从自动机开始的约束解码：逐 token 复现课程页的演示；逐步掩码为什么不等于模型在合法输出上的分布；在见过与没见过该格式的字节模型上加掩码；Schema、校验与重试。

- 对应页面: [结构化输出与函数调用：从 JSON 模式到受约束解码](https://dmxsf.com/wiki/applications/structured-output-and-function-calling/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/structured-output-and-function-calling`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/structured-output-and-function-calling/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/structured-output-and-function-calling/README.md)

#### [RAG：逐个零件](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/rag)

切块、相似度与索引大小、按公式实现的 BM25、排名融合（RRF）、检索指标与上下文拼装，离线复现课程页的数字；加 --bge 还会运行稠密检索器与重排序模型。

- 对应页面: [RAG 系统搭建实战](https://dmxsf.com/wiki/applications/rag/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/rag`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/rag/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/rag/README.md)
- 可选：加 --hf 或 --bge 运行真实模型，需要 uv sync --extra hf

#### [Agent 与工具调用](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/agents-and-tool-calling)

带护栏（格式、业务规则、重复调用、确认、步数上限）的工具调用循环与评分器；模型换成回放页面决策的脚本替身；最后比较 pass^k 与 pass@k。这里不运行任何语言模型。

- 对应页面: [Agent 与工具调用实战：从 Function Calling 到 ReAct 工作流](https://dmxsf.com/wiki/applications/agents-and-tool-calling/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/agents-and-tool-calling`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/agents-and-tool-calling/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/agents-and-tool-calling/README.md)

#### [Agent 上下文与记忆：预算与优先级](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/context-and-memory)

整组保留工具轮，验证版本记录覆盖与删除。

- 对应页面: [Agent 上下文工程与长期记忆：窗口、版本和状态](https://dmxsf.com/wiki/applications/context-and-memory/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/context-and-memory`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/context-and-memory/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/context-and-memory/README.md)

#### [Agent 评测：重复成功与终态判分](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/agent-evaluation)

对比相同单次成功率下的重复成功，并判分四种日历终态。

- 对应页面: [Agent 评测：评的是系统，不是模型](https://dmxsf.com/wiki/applications/agent-evaluation/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/agent-evaluation`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/agent-evaluation/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/agent-evaluation/README.md)

#### [MCP 工具：检查发现、调用与模式大小](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/mcp-and-tool-ecosystem)

离线检查 JSON-RPC 的 tools/list 与 tools/call 报文，测量缩小候选工具目录后返回给客户端的数据。

- 对应页面: [MCP 与工具生态：协议怎么工作，工具多了怎么办](https://dmxsf.com/wiki/applications/mcp-and-tool-ecosystem/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run applications/mcp-and-tool-ecosystem`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/mcp-and-tool-ecosystem/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/applications/mcp-and-tool-ecosystem/README.md)

### 多模态

#### [多模态大模型](https://github.com/dmxsf/dmxsf/tree/main/labs/multimodal/multimodal-llms)

从图像块到视觉 token；这些 token 在 KV Cache 与 FLOPs 上的成本；LLaVA 的投影层与两阶段训练；CLIP 的对比损失，以及在渲染图形上从零训练的小型 CLIP。加 --hf 可在 CIFAR-10 上运行 CLIP ViT-B/32。

- 对应页面: [多模态 LLM 原理与应用：图像如何变成 token](https://dmxsf.com/wiki/multimodal/multimodal-llms/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run multimodal/multimodal-llms`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/multimodal/multimodal-llms/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/multimodal/multimodal-llms/README.md)
- 可选：加 --hf 或 --bge 运行真实模型，需要 uv sync --extra hf

#### [扩散与流匹配：核对高斯路径](https://github.com/dmxsf/dmxsf/tree/main/labs/multimodal/diffusion-and-flow-matching)

实测扩散前向方差，积分一条可解析的流，再比较欧拉误差怎样随步数变化。

- 对应页面: [扩散模型与流匹配：从噪声一步步生成图像、动作与文本](https://dmxsf.com/wiki/multimodal/diffusion-and-flow-matching/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run multimodal/diffusion-and-flow-matching`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/multimodal/diffusion-and-flow-matching/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/multimodal/diffusion-and-flow-matching/README.md)

#### [世界模型：观察与重规划](https://github.com/dmxsf/dmxsf/tree/main/labs/multimodal/world-models)

在有隐藏障碍的网格上对比固定计划和反馈重规划。

- 对应页面: [世界模型与规划：从预测状态到闭环控制](https://dmxsf.com/wiki/multimodal/world-models/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run multimodal/world-models`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/multimodal/world-models/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/multimodal/world-models/README.md)

### 评测

#### [基准污染](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-evaluation)

课程页的 n-gram 污染检测器；一个答案只能靠记忆得到的基准；模型的训练流里三分之一原样泄漏、三分之一改写、三分之一没有；检测器能抓到什么，以及“只在干净子集上重测”会得出什么结论。

- 对应页面: [LLM 评测方法论：基准、数据污染与评测流程](https://dmxsf.com/wiki/evaluation/llm-evaluation/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run evaluation/llm-evaluation`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/llm-evaluation/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/llm-evaluation/README.md)

#### [评测统计](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/eval-statistics)

pass@k 对照暴力枚举；95% 区间对照真实覆盖率；配对比较与样本量表对照真实检验的功效；最后在留出文本上比较两个真实模型。

- 对应页面: [评测的统计学：pass@k、置信区间与所需题数](https://dmxsf.com/wiki/evaluation/eval-statistics/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run evaluation/eval-statistics`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/eval-statistics/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/eval-statistics/README.md)

#### [LLM 评委](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-as-judge)

一个偏差可调的评委替身，所以每个数字都有精确答案：交换顺序能抵消位置偏差却抵消不了长度偏差；Cohen's kappa 能识破永远猜多数类的评委；按长度校正的 Bradley-Terry 排行榜。

- 对应页面: [LLM-as-a-Judge：用模型当评委，以及它的偏差与校准](https://dmxsf.com/wiki/evaluation/llm-as-judge/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run evaluation/llm-as-judge`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/llm-as-judge/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/llm-as-judge/README.md)

#### [构建评测集：标签噪声、一致率与题目信息](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/building-eval-sets)

模拟四选一标签噪声来校对公式，计算 Cohen κ，并找到 2PL 题目最能区分的能力点。

- 对应页面: [构建评测集：题目来源、标注质量与题目的信息量](https://dmxsf.com/wiki/evaluation/building-eval-sets/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run evaluation/building-eval-sets`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/building-eval-sets/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/building-eval-sets/README.md)

#### [基准污染：检测措辞重合并分切片复核](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/benchmark-contamination)

用合成题计算真实重合标记，再分切片检验总体平均怎样掩盖污染。

- 对应页面: [基准污染：怎么造出来、怎么检测、怎么避免](https://dmxsf.com/wiki/evaluation/benchmark-contamination/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run evaluation/benchmark-contamination`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/benchmark-contamination/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/benchmark-contamination/README.md)

#### [生产评测：配对回放和发布门槛](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/production-evaluation)

回放配对结果，检查关键切片的退化和覆盖缺口。

- 对应页面: [LLM 生产评测：离线回放、灰度发布与回滚](https://dmxsf.com/wiki/evaluation/production-evaluation/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run evaluation/production-evaluation`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/production-evaluation/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/evaluation/production-evaluation/README.md)

### 安全与对齐风险

#### [工具授权：分开数据、动作建议与权限](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/prompt-injection-and-jailbreaks)

在模拟运行时回放正常读取与权限边界案例，验证数据内容无法授予新权限。

- 对应页面: [提示注入与越狱：LLM 应用的安全边界](https://dmxsf.com/wiki/safety/prompt-injection-and-jailbreaks/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run safety/prompt-injection-and-jailbreaks`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/safety/prompt-injection-and-jailbreaks/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/safety/prompt-injection-and-jailbreaks/README.md)

#### [幻觉与校准：弃答、置信度与准确率](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/hallucination-and-calibration)

用留出的校准样本拟合温度，在合成分数上观察回答率与错误率的折衷。

- 对应页面: [幻觉与校准：模型为什么会一本正经地胡说](https://dmxsf.com/wiki/safety/hallucination-and-calibration/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run safety/hallucination-and-calibration`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/safety/hallucination-and-calibration/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/safety/hallucination-and-calibration/README.md)

#### [隐私审计：合成金丝雀的曝光度](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/memorization-and-privacy)

用完全合成的候选分布比较排名曝光度与首选输出，再计算均匀排名的参照值。

- 对应页面: [记忆与隐私：模型记住了什么，怎样量出来，怎样降下来](https://dmxsf.com/wiki/safety/memorization-and-privacy/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run safety/memorization-and-privacy`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/safety/memorization-and-privacy/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/safety/memorization-and-privacy/README.md)

### 前沿

#### [测试时计算：独立采样、重排与投票](https://github.com/dmxsf/dmxsf/tree/main/labs/frontier/test-time-compute)

比较独立与完全相关的重复采样，并估算有噪选择器对 Best-of-N 的影响。

- 对应页面: [测试时计算：把算力花在推理上](https://dmxsf.com/wiki/frontier/test-time-compute/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run frontier/test-time-compute`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/frontier/test-time-compute/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/frontier/test-time-compute/README.md)

#### [合成数据：长尾消失与验证筛选](https://github.com/dmxsf/dmxsf/tree/main/labs/frontier/synthetic-data-and-self-improvement)

测量逐代替换数据时消失的长尾类别，再用独立验证器筛选合成标签。

- 对应页面: [合成数据与自我改进：模型给模型造数据，何时有效，何时塌缩](https://dmxsf.com/wiki/frontier/synthetic-data-and-self-improvement/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run frontier/synthetic-data-and-self-improvement`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/frontier/synthetic-data-and-self-improvement/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/frontier/synthetic-data-and-self-improvement/README.md)

#### [机制可解释性：叠加特征与因果替换](https://github.com/dmxsf/dmxsf/tree/main/labs/frontier/interpretability)

测量非正交特征的干扰，再替换一个受控激活，解释玩具预测为何变化。

- 对应页面: [机制可解释性：叠加、稀疏自编码器与回路](https://dmxsf.com/wiki/frontier/interpretability/)
- 在配套仓库里运行（加 --quick 先快速跑一遍）： `uv run dmxsf run frontier/interpretability`
- [查看实验脚本](https://github.com/dmxsf/dmxsf/blob/main/labs/frontier/interpretability/run.py)
- [实验说明](https://github.com/dmxsf/dmxsf/blob/main/labs/frontier/interpretability/README.md)

## 跟着学习路线做实验

这些路线直接取自学习路线图：每一周（或每一步）要读的页面背后的实验，就是这一周要运行的实验。

### 路线零：周末速通（约 10 小时）

- 第 1 步: [大模型是什么：一个小到能读完的完整循环](https://github.com/dmxsf/dmxsf/tree/main/labs/start/what-is-an-llm)
- 第 2 步: [下一个词预测与信息论](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/next-token-prediction)
- 第 3 步: [注意力机制](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/attention-basics)
- 第 4 步: [Transformer Block 与现代架构](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/transformer-block)
- 第 5 步: [预训练流水线](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/training-pipeline)
- 第 6 步: [KV Cache](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/kv-cache)
- 第 7 步: [基准污染](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-evaluation)

### 路线一：AI 应用开发者（8 周）

- 第 1 周: [大模型是什么：一个小到能读完的完整循环](https://github.com/dmxsf/dmxsf/tree/main/labs/start/what-is-an-llm)、[下一个词预测与信息论](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/next-token-prediction)、[解码与采样](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/decoding-and-sampling)
- 第 2 周: [Prompt Engineering：可测量的提示词](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/prompt-engineering)、[结构化输出与函数调用](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/structured-output-and-function-calling)
- 第 3 周: [基准污染](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-evaluation)、[评测统计](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/eval-statistics)
- 第 4 周: [第一个推理服务：本机 HTTP 接口](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/first-inference-service)、[RAG：逐个零件](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/rag)
- 第 5 周: [第一个 Agent：有界工具循环](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/first-agent)、[Agent 与工具调用](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/agents-and-tool-calling)、[Agent 评测：重复成功与终态判分](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/agent-evaluation)、[MCP 工具：检查发现、调用与模式大小](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/mcp-and-tool-ecosystem)、[Agent 上下文与记忆：预算与优先级](https://github.com/dmxsf/dmxsf/tree/main/labs/applications/context-and-memory)
- 第 6 周: [工具授权：分开数据、动作建议与权限](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/prompt-injection-and-jailbreaks)、[幻觉与校准：弃答、置信度与准确率](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/hallucination-and-calibration)、[隐私审计：合成金丝雀的曝光度](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/memorization-and-privacy)
- 第 7 周: [推理服务：连续批处理、分页 KV 与前缀缓存](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/continuous-batching)、[KV Cache](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/kv-cache)、[量化](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/quantization)
- 第 8 周: [构建评测集：标签噪声、一致率与题目信息](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/building-eval-sets)、[LLM 评委](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-as-judge)、[生产评测：配对回放和发布门槛](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/production-evaluation)

### 路线二：LLM 工程师（14 周）

- 第 1 周: [张量形状：手算线性层](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/tensors-and-shapes)、[神经网络基础：梯度与 XOR](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/neural-network-basics)、[下一个词预测与信息论](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/next-token-prediction)
- 第 2 周: [分词器与词嵌入](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/tokenizer-and-embeddings)、[从 RNN 到注意力：梯度与检索路径](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/from-rnn-to-attention)、[注意力机制](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/attention-basics)
- 第 3 周: [位置编码与长上下文](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/positional-encoding)、[Transformer Block 与现代架构](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/transformer-block)
- 第 4 周: [Transformer Block 与现代架构](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/transformer-block)、[高效注意力：GQA、MLA 与 FlashAttention](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/efficient-attention)
- 第 5 周: [预训练流水线](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/training-pipeline)、[优化器与训练稳定性](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations/optimizers-and-training-stability)、[数据工程：构建并审计去重过滤器](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/data-engineering)
- 第 6 周: [扩展定律](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/scaling-laws)、[训练的算力与显存](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/compute-and-memory)
- 第 7 周: [硬件入门：容量与复制时间](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/hardware-basics)、[GPU 屋顶线：字节、运算与分块复用](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/gpu-and-roofline)、[数值精度：舍入、溢出与损失缩放](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/numerical-precision)、[GPU 内核：分块矩阵乘与访存](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/gpu-kernels)
- 第 8 周: [分布式训练](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/distributed-training)、[集合通信：模拟环形 All-Reduce](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/collective-communication)
- 第 9 周: [SFT 数据：损失看到什么](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/sft-data)、[LoRA 微调](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/lora-finetuning)、[隐私审计：合成金丝雀的曝光度](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/memorization-and-privacy)
- 第 10 周: [奖励模型：成对打分与标签噪声](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/reward-models)、[强化学习基础：策略梯度、基线与 GAE](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/rl-fundamentals)、[偏好优化（DPO）](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/preference-optimization)
- 第 11 周: [KV Cache](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/kv-cache)、[量化](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/quantization)
- 第 12 周: [推理服务：连续批处理、分页 KV 与前缀缓存](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/continuous-batching)、[投机解码](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/speculative-decoding)、[前缀缓存：命中与排队](https://github.com/dmxsf/dmxsf/tree/main/labs/inference/prefix-caching)
- 第 13 周: [基准污染](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-evaluation)、[评测统计](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/eval-statistics)、[构建评测集：标签噪声、一致率与题目信息](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/building-eval-sets)
- 第 14 周: [多模态大模型](https://github.com/dmxsf/dmxsf/tree/main/labs/multimodal/multimodal-llms)、[测试时计算：独立采样、重排与投票](https://github.com/dmxsf/dmxsf/tree/main/labs/frontier/test-time-compute)

### 路线三：LLM 研究者（16 周）

- 第 1 周: [从 RNN 到注意力：梯度与检索路径](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/from-rnn-to-attention)
- 第 2 周: 本步骤以阅读或写作为主
- 第 3 周: [Transformer Block 与现代架构](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/transformer-block)
- 第 4 周: [扩展定律](https://github.com/dmxsf/dmxsf/tree/main/labs/pretraining/scaling-laws)
- 第 5 周: [混合专家（MoE）](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/mixture-of-experts)
- 第 6 周: [高效注意力：GQA、MLA 与 FlashAttention](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/efficient-attention)
- 第 7 周: [GPU 屋顶线：字节、运算与分块复用](https://github.com/dmxsf/dmxsf/tree/main/labs/systems/gpu-and-roofline)
- 第 8 周: [位置编码与长上下文](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture/positional-encoding)
- 第 9 周: [奖励模型：成对打分与标签噪声](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/reward-models)
- 第 10 周: [强化学习基础：策略梯度、基线与 GAE](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/rl-fundamentals)、[偏好优化（DPO）](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/preference-optimization)
- 第 11 周: [推理强化学习（GRPO 与可验证奖励）](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/reasoning-rl)、[推理强化学习系统：采样与训练调度](https://github.com/dmxsf/dmxsf/tree/main/labs/post-training/rollout-and-training)
- 第 12 周: [测试时计算：独立采样、重排与投票](https://github.com/dmxsf/dmxsf/tree/main/labs/frontier/test-time-compute)、[世界模型：观察与重规划](https://github.com/dmxsf/dmxsf/tree/main/labs/multimodal/world-models)
- 第 13 周: [基准污染](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/llm-evaluation)、[评测统计](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/eval-statistics)、[构建评测集：标签噪声、一致率与题目信息](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/building-eval-sets)、[基准污染：检测措辞重合并分切片复核](https://github.com/dmxsf/dmxsf/tree/main/labs/evaluation/benchmark-contamination)
- 第 14 周: [幻觉与校准：弃答、置信度与准确率](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/hallucination-and-calibration)、[工具授权：分开数据、动作建议与权限](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/prompt-injection-and-jailbreaks)、[隐私审计：合成金丝雀的曝光度](https://github.com/dmxsf/dmxsf/tree/main/labs/safety/memorization-and-privacy)
- 第 15 周: 本步骤以阅读或写作为主
- 第 16 周: 本步骤以阅读或写作为主

