INTERACTIVE LLM ENGINEERING LAB
LoopLab · Interactive LLM Engineering Lab
七天,从张量到 Loop-MoE:推导、实现并训练一个 Decoder-only MoE + Loop Transformer
Seven days from tensors to Loop-MoE: derive, implement and train a decoder-only MoE + Loop Transformer
课程 Course · 运行 Run · 结构 Structure · 实验 Experiments · 来源 Sources
| 课程 Day | 主题 Topic | 学生交付 Deliverable |
|---|---|---|
| 预备 Prep | 线性代数、微积分与概率速查 Linear algebra, calculus and probability |
通过预备诊断与 day0_basics.pyPass the diagnostic and day0_basics.py |
| Day 1 | 张量与神经网络 Tensors and neural networks |
可训练 MLP,手算梯度与 Autograd 一致 Trainable MLP, manual gradients match Autograd |
| Day 2 | 自回归语言建模 Autoregressive language modeling |
Bigram LM:数据、训练与生成 Bigram LM: data, training, sampling |
| Day 3 | Transformer | Decoder-only Mini GPT,通过因果性测试 Mini GPT that passes the causality test |
| Day 4 | MoE | 稀疏 MoE Block,专家调用与负载正确 Sparse MoE block with correct dispatch and load |
| Day 5 | Loop Transformer | Loop Core,共享梯度与门控边界正确 Loop core with correct shared gradients and gate bounds |
| Day 6 | Loop-MoE | 可训练的微型 Loop-MoE LM Trainable tiny Loop-MoE LM |
| Day 7 | 科研实验 Research |
2×2 消融报告与闭卷重建 2×2 ablation report and closed-book rebuild |
每课按“直觉 → 推导 → 形状 → 代码 → 实验 → 独立实现 → 验收”推进。Every lesson goes intuition → derivation → shapes → code → lab → implementation → tests.
LoopLab 是交互式教材、数学与模型可视化实验室和独立科研工作台。课程页的 PyTorch 代码直接取自 python/ 中被测试的源文件;实验室的数字由 TypeScript 引擎在浏览器本地实时计算,与 PyTorch 生成的数值样例交叉核对。它受 GLM-5.4 相关公开研究启发,目标是架构原理级复现,不声称复刻任何私有代码、权重或性能。
LoopLab is an interactive textbook, a visual math and model laboratory, and a research workbench. The PyTorch code shown in lessons is read straight from the tested sources in python/; lab numbers are computed locally in the browser by a TypeScript engine and cross-checked against PyTorch-generated fixtures. It is inspired by public research around GLM-5.4 and reproduces architectural principles only; it does not claim to replicate any private code, weights or performance.
| 8 | ≈ 28 h | 8 | 8,396,034 |
|---|---|---|---|
| 节课程(7 天 + 预备) lessons (7 days + prep) |
有效学习时间(设计目标) study time (design target) |
个实时计算实验 live labs |
参考模型参数(PyTorch 计数) reference model parameters |
| 页面 Page | 路由 Route | 内容 What it holds |
|---|---|---|
| 学习总览 Overview |
/ |
课程路径、进度、预备诊断、掌握度热力格、实验入口 Path, progress, diagnostic, mastery heatmap, labs |
| 课程 Lessons |
/learn/$lessonId |
推导、代码、题卡、实验、关卡测验、练习与验收 Derivations, code, question cards, labs, checkpoint quiz, exercises, tests |
| 复习 Review |
/review |
三格间隔复习:今天到期的题,每题换新变式 Spaced review of due items with fresh variants |
| 实验室 Labs |
/lab、/lab/$experimentId |
八个实验:Tensor、Gradient、Softmax、Attention、MoE、Loop、Architecture、Ablation Eight labs |
| 模型工作台 Workbench |
/workbench |
结构、参数与预算、实验结果导入 Structure, budget, result import |
| 独立复现 Assessment |
/assessment |
闭卷练习、模块重建、期末测验、评分自评、答辩题 Closed-book tasks, rebuild, final exam, rubric, defense |
| 参考资料 References |
/references |
来源核验状态与术语表 Source status and glossary |
Web(Node 22.12+、pnpm 10;corepack enable 后自动使用 package.json 锁定的 pnpm 版本):
Web (Node 22.12+, pnpm 10; after corepack enable the pnpm version pinned in package.json is used):
corepack enable
pnpm install # 首次安装耗时较长,请等到出现 Done
pnpm dev # http://localhost:3000
pnpm check # oxlint + oxfmt + tsc + vitest + build
pnpm test:e2e # Playwright(需要已安装的 Chromium)若 pnpm dev 报 vite: command not found 或 node_modules missing,说明首次安装没有完成:再运行一次 pnpm install 即可,已下载的包不会重复下载。pnpm run 也会在依赖不完整时自动补装。
If pnpm dev reports vite: command not found or node_modules missing, the first install did not finish: run pnpm install again; packages already downloaded are reused. pnpm run also completes a partial install before running.
Python(uv,CPU 即可,检测到 CUDA / MPS 时自动使用):
Python (uv; CPU is enough, CUDA / MPS are used when present):
cd python
uv sync
uv run pytest # 参考实现测试 · reference tests
uv run pytest exercises -k day3 # 你的练习 · your exercises
uv run looplab train --preset tiny --task corpus --steps 300 --out runs/tiny
uv run looplab generate --checkpoint runs/tiny/model.pt --prompt "注意力"
uv run looplab ablate --task vartrack --match params --seeds 0 1 --out runs/ablation.json把 runs/*.json 拖进 /workbench 或 /lab/ablation 即可对照分析。
Drop runs/*.json into /workbench or /lab/ablation to compare runs.
apps/web/ TanStack Start 教学产品 · teaching web app
packages/core/ 确定性数学引擎与数据契约 · math engine and data contract
python/src/looplab/ PyTorch 参考实现 · reference implementation
python/exercises/ 练习模板与测试 · exercise templates and tests
python/solutions/ 参考答案 · reference solutions
fixtures/ 跨语言数值样例与 JSON Schema · cross-language fixtures
docs/ 决策、教学设计、实验方法、来源 · decisions, design, methods, sources
.github/workflows/ CI
参数量以 PyTorch 实际计数为准,并与公式在 TypeScript、Python 两侧一致;FLOPs 是只计矩阵乘法的估算。仓库内置的消融结果是沙箱 CPU 上的真实 smoke 运行(d = 64,300 步,2 个种子),不是性能跑分。来源核验日期 2026-11-29,详见来源清单与实现状态。
Parameter counts are PyTorch counts that match the formula in both TypeScript and Python; FLOPs are matmul-only estimates. The bundled ablation results are a real smoke run on a sandbox CPU (d = 64, 300 steps, 2 seeds), not a benchmark. Sources were checked on 2026-11-29; see Sources and Status.
代码使用 MIT 许可证,课程文本使用 CC BY 4.0。
Code is MIT-licensed; course text is CC BY 4.0.