59 / 90

第 59 天:无人值守远程训练、遥测流构建与指标深度分析

基于 Tmux 托管无头预训练、解析实时回传的 Loss 与梯度范数,拒绝将挂机等待混淆为学习时长。

模型与预训练 480 分钟有效研习(8 小时)
01 120 分钟

基于 Tmux 的无头后台持久化训练与实时遥测流构建

Agent 辅助工程检查

前置要求

第 58 天行前检查通过,Linux tmux/nohup 进程管理原理。

学习目标

在持久化 tmux 会话中解耦启动无头预训练任务,将运行指标重定向至结构化遥测日志,恪守不将挂机等待时间算作有效学习时长的工程原则。

操作指引

1. 以分离状态启动:tmux new-session -d -s pretrain '...' 创建会话但不连接。通过 tee -a 输出,使重启时 training.log 只追加、不被截断。
2. 确认会话(tmux ls)和进程(ps aux | grep '[t]rain.py')都存在。
3. 相隔一分钟取两次日志末尾,确认步数在增长。
4. 断连测试:不要在 tmux 里输入 exit,直接关闭 SSH 客户端,重新连接后重复第 2、3 步。解释进程为何存活:它的父进程是 tmux 服务器,而不是你的登录 shell。
5. 用 tmux attach -t pretrain 连接观察,再按 Ctrl-B 然后 d 分离。
6. 保留证据(两次末尾、断连前后的 ps 行、实际耗时),只把阅读遥测的分钟记为研习时间;等待 GPU 不算研习。

时间安排(120 分钟):阅读 20 + 练习 70 + 检查 30。 阅读:nanoGPT train.py 的日志循环和 W&B 实验追踪概述。练习:在模拟或真实运行上完成第 1-5 步。检查:凭记忆重做断连测试,并将证据与参考答案对照。

自主演练与记录

在脱机 tmux 会话中启动模拟训练。关闭终端重新登录,利用 ps aux | grep python 确认任务仍在运行且 training.log 持续更新。

独立掌握标准

成功构建不受本地网络终端中断影响的持久化后台训练管道与遥测日志流。

权威源资源与权利证明

linking Open Access / Terms of Service

Experiment Tracking and Real-Time ML Telemetry Guide

作者 / 机构: Lukas Biewald et al. / Weights & Biases | 原始出处链接 ↗

对应章节 / 时间戳: Experiments overview: How it works (wandb.init, run.log)

验证版本与日期: W&B Documentation (Verified: 2026-10-30)

入选理由:

Widely adopted concepts for decoupling active human study time from passive unattended GPU training runs.

来源前置条件:

Loss functions, learning rate schedules, and step loops.

安全回退方案(离线弹性):

Offline local CSV/JSONL telemetry logging is fully provided in lesson code.

对比候选资源与选型分析 (2)

Local self-hosted visualizer; compared alongside cloud telemetry dashboards.

MLflow Tracking Documentation ↗ compared_alternative [Apache 2.0]

Enterprise lifecycle manager; W&B is preferred for quick LLM run telemetry.

linking MIT

nanoGPT: train.py pretraining loop

作者 / 机构: Andrej Karpathy | 原始出处链接 ↗

对应章节 / 时间戳: train.py: logging (wandb_log, wandb.init) and the per-iteration print of loss, time and mfu

验证版本与日期: Git master commit 71c3905 (Verified: 2026-10-30)

入选理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

来源前置条件:

Python, PyTorch, and Day 46-48 Transformer model architecture.

安全回退方案(离线弹性):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

对比候选资源与选型分析 (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

02 120 分钟

实时遥测动态分析:损失轨迹、梯度范数与有效 MFU 核算

独立掌握检查(无 AI 辅助)

前置要求

第 59 天活动 1 与第 56 天算力数学模型。

学习目标

对回传的日志开展主动的数理核算:评估每秒处理 Token 吞吐率、跟踪梯度范数监控数值稳定性,并实时核算硬件有效利用率(MFU)。

操作指引

1. 把每行日志解析为 step、train_loss、val_loss、grad_norm、dt(每步秒数)和 tokens_per_sec。
2. 计算吞吐:tokens_per_sec = tokens_per_step / dt。
3. 计算 MFU = 6 * N * tokens_per_sec / 峰值 FLOPs,其中 6N 是权重矩阵乘法带来的每个词元前向加反向成本。明确写出 N、峰值及其 dtype。
4. 说明 6N 漏掉了什么:注意力得分的乘积,训练时每个词元约多 12 * n_layer * T * d_model FLOPs。为本次运行计算它,并说明你的 MFU 是否包含它。
5. 将 grad_norm 与截断阈值(例如 1.0)对比监控。若大多数步都被截断,说明学习率可能过高或批量过小。
6. 给损失异常分类:普通噪声、发散(损失快速上升或 NaN)、崩塌(损失停在 ln(V) 附近,即在词表上均匀猜测)。

时间安排(120 分钟):阅读 30 + 练习 55 + 检查 35。 阅读:EleutherAI Transformer Math 101 中关于计算量和 MFU 的部分,以及 W&B 指标概述。练习:题目计算及 (a)(b)。检查:独立为一行真实日志计算 MFU。

自主演练与记录

已知 N=124M, 单步处理 32,768 tokens, 单步耗时 0.25 秒,运行在 A100(BF16 峰值 312 TFLOPs)上。计算实时 tokens/秒吞吐与实测 MFU 百分比。(a) 6N 估计不包含注意力得分的 FLOPs。对 n_layer=12、T=1024、d_model=768 计算 12 * n_layer * T * d_model,并给出包含它的 MFU。(b) 对 GPT-2 词表 V=50257,什么损失值表示崩塌为均匀猜测?

独立掌握标准

能从真实训练日志行中准确计算出秒级吞吐量与 MFU 硬件利用率指标。

权威源资源与权利证明

linking Open Access / Terms of Service

Experiment Tracking and Real-Time ML Telemetry Guide

作者 / 机构: Lukas Biewald et al. / Weights & Biases | 原始出处链接 ↗

对应章节 / 时间戳: Experiments overview: How it works (wandb.init, run.log)

验证版本与日期: W&B Documentation (Verified: 2026-10-30)

入选理由:

Widely adopted concepts for decoupling active human study time from passive unattended GPU training runs.

来源前置条件:

Loss functions, learning rate schedules, and step loops.

安全回退方案(离线弹性):

Offline local CSV/JSONL telemetry logging is fully provided in lesson code.

对比候选资源与选型分析 (2)

Local self-hosted visualizer; compared alongside cloud telemetry dashboards.

MLflow Tracking Documentation ↗ compared_alternative [Apache 2.0]

Enterprise lifecycle manager; W&B is preferred for quick LLM run telemetry.

linking CC-BY 4.0

Transformer Math 101

作者 / 机构: Quentin Anthony, Stella Biderman, Hailey Schoelkopf | 原始出处链接 ↗

对应章节 / 时间戳: Compute Requirements (C = 6PD)

验证版本与日期: EleutherAI Research Blog (Verified: 2026-10-30)

入选理由:

Widely cited, practical industry guide deriving 6ND compute FLOPs, activation memory, KV cache size, and VRAM estimation.

来源前置条件:

Transformer matrix dimensions and matrix multiplication FLOP counting.

安全回退方案(离线弹性):

The 6ND derivation (2ND forward, 4ND backward) and peak VRAM estimation models are fully worked out in the curriculum.

对比候选资源与选型分析 (2)

Rigorous academic formulation; Transformer Math 101 provides accessible step-by-step arithmetic equations.

Kaplan et al. Section 2.1 Compute Budgeting ↗ compared_alternative [Open Access]

Original 6ND approximation; EleutherAI expands on activation memory and KV cache.

03 120 分钟

远程周期快照轮转审计与突发抢占断点自愈验证

Agent 辅助工程检查

前置要求

第 59 天活动 1 与 2,第 55 天原子化检查点规范。

学习目标

审计云端周期快照的生成频率与云盘配额消耗,模拟突发抢占杀进程,验证续训后损失轨迹无断层平滑延续。

操作指引

1. 检查每个检查点都以原子方式写入:先保存到同目录的 .tmp 文件,再用 os.replace 覆盖最终文件名(Day 55)。
2. 保存完整训练状态:模型、优化器(AdamW 动量)、步数和损失。让批数据只依赖步数,使恢复的运行看到相同的数据。
3. 采用只保留最近 3 个检查点的滚动策略,并且只在最新的 os.replace 成功后才删除最旧的一个。长时间运行前检查磁盘剩余空间。
4. 模拟抢占:在第 100 步保存后停止运行,相当于一个完成当前步、保存并退出的 SIGTERM 处理器。
5. 在全新的进程状态中从保存步 + 1 恢复,并将第 101-110 步的损失与不中断的基准运行对比。
6. 故意制造一次失败:不加载优化器状态就恢复,观察损失从哪里开始偏离。

时间安排(120 分钟):阅读 20 + 练习 70 + 检查 30。 阅读:nanoGPT train.py 中的检查点与恢复代码,以及 PyTorch 关于保存优化器状态的说明。练习:用参考脚本完成第 1-6 步。检查:不看笔记解释为什么不加载优化器的恢复会偏离。

自主演练与记录

运行断点恢复校验测试。确认第 100 步中断前记录的损失值与续训启动第 100 步评估值完全重合。

独立掌握标准

在云端进程遭受中断后,证明系统具备零损失断层恢复能力。

权威源资源与权利证明

linking MIT

nanoGPT: train.py pretraining loop

作者 / 机构: Andrej Karpathy | 原始出处链接 ↗

对应章节 / 时间戳: train.py: checkpoint dict {'model', 'optimizer', 'model_args', 'iter_num', 'best_val_loss', 'config'} written with torch.save to out_dir/ckpt.pt; the init_from == 'resume' branch reloads it

验证版本与日期: Git master commit 71c3905 (Verified: 2026-10-30)

入选理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

来源前置条件:

Python, PyTorch, and Day 46-48 Transformer model architecture.

安全回退方案(离线弹性):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

对比候选资源与选型分析 (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

linking BSD-3-Clause

PyTorch Documentation: Automatic Mixed Precision (torch.amp)

作者 / 机构: PyTorch Team | 原始出处链接 ↗

对应章节 / 时间戳: Gradient Scaling

验证版本与日期: PyTorch 2.x Documentation (Verified: 2026-10-30)

入选理由:

Official reference on autocast and GradScaler for numerical stability in FP16/BF16 training.

来源前置条件:

PyTorch tensors, autograd, and GPU compute basics.

安全回退方案(离线弹性):

All scaling and precision casting mechanics are demonstrated in self-contained Python examples.

对比候选资源与选型分析 (2)
NVIDIA Apex Documentation ↗ compared_alternative [BSD-3-Clause]

Legacy external AMP implementation; native torch.cuda.amp is the modern standard.

Framework wrapper around PyTorch AMP; we study the underlying PyTorch primitive.

04 120 分钟

考核评估:轻量级训练遥测监视器与损失发散看门狗开发

独立掌握检查(无 AI 辅助)

前置要求

第 59 天活动 1 至 3 全部内容。

学习目标

独立开发生产级 TelemetryMonitor 遥测监视脚本,持续跟踪日志流、计算瞬时与滑动平均 MFU,灵敏捕捉 Loss 停滞与 NaN 突变并发出警报。

操作指引

1. 创建 TelemetryMonitor(model_params, tokens_per_step, peak_flops),再加上滑动窗口大小和发散阈值(max_loss = 20.0)。
2. 用正则表达式解析形如 'step: %d, loss: %f, dt: %f' 的行,忽略所有不匹配的行。
3. 在 process_step 中先检查损失:非有限的损失或超过 max_loss 的损失返回 CRITICAL 告警,并建议立即终止。
4. 对正常的步,计算本步 MFU、全程平均 MFU、窗口内滑动的 tokens/秒以及损失移动平均。
5. 实现非阻塞的 tail 生成器:定位到 training.log 末尾,每读到新行就产出解析后的报告,没有新内容时产出 None,以便调用方检查时间限制。
6. 用 20 个正常步加一个注入 NaN 的模拟流进行测试:平均 MFU 必须等于活动 2 的值,NaN 必须在它自己那一行就被标记。

时间安排(120 分钟):阅读 15 + 练习 75 + 检查 30。 阅读:活动 2 的 MFU 计算和 W&B 告警概述。练习:构建并测试监视器。检查:运行掌握测试并通过全部断言,再自己添加一个测试。

自主演练与记录

向 TelemetryMonitor 注入模拟训练日志流。验证其在 20 步运行中精准追踪 MFU,并在注入 NaN 时毫秒级触发阻断告警。

独立掌握标准

独立构建能准确核算实时 MFU 并秒级捕获梯度发散的轻量级遥测监视看门狗。

权威源资源与权利证明

linking Open Access / Terms of Service

Experiment Tracking and Real-Time ML Telemetry Guide

作者 / 机构: Lukas Biewald et al. / Weights & Biases | 原始出处链接 ↗

对应章节 / 时间戳: Experiments overview: How it works (wandb.init, run.log)

验证版本与日期: W&B Documentation (Verified: 2026-10-30)

入选理由:

Widely adopted concepts for decoupling active human study time from passive unattended GPU training runs.

来源前置条件:

Loss functions, learning rate schedules, and step loops.

安全回退方案(离线弹性):

Offline local CSV/JSONL telemetry logging is fully provided in lesson code.

对比候选资源与选型分析 (2)

Local self-hosted visualizer; compared alongside cloud telemetry dashboards.

MLflow Tracking Documentation ↗ compared_alternative [Apache 2.0]

Enterprise lifecycle manager; W&B is preferred for quick LLM run telemetry.

linking CC-BY 4.0

Transformer Math 101

作者 / 机构: Quentin Anthony, Stella Biderman, Hailey Schoelkopf | 原始出处链接 ↗

对应章节 / 时间戳: N/A

验证版本与日期: EleutherAI Research Blog (Verified: 2026-10-30)

入选理由:

Widely cited, practical industry guide deriving 6ND compute FLOPs, activation memory, KV cache size, and VRAM estimation.

来源前置条件:

Transformer matrix dimensions and matrix multiplication FLOP counting.

安全回退方案(离线弹性):

The 6ND derivation (2ND forward, 4ND backward) and peak VRAM estimation models are fully worked out in the curriculum.

对比候选资源与选型分析 (2)

Rigorous academic formulation; Transformer Math 101 provides accessible step-by-step arithmetic equations.

Kaplan et al. Section 2.1 Compute Budgeting ↗ compared_alternative [Open Access]

Original 6ND approximation; EleutherAI expands on activation memory and KV cache.