60 / 90

第 60 天:训后评测、账单审计、Safetensors 导出与资源彻底销毁

核算最终困惑度与采样连贯性、逐项对账实际开销与预算差额、导出安全权重并物理销毁云端资产。

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

最终检查点性能评测、困惑度核算与生成连贯性审计

Agent 辅助工程检查

前置要求

第 59 天云端预训练执行经验与第 53 天验证指标计算。

学习目标

在留出验证集(Held-out Validation Set)上系统评估最终收敛检查点,测算验证损失与困惑度(Perplexity),执行温度受控自回归采样审计生成连贯性。

操作指引

1. 用 map_location='cpu'(或你的 GPU)加载最终检查点,把其中的 'model' 状态加载到新构建的模型中,并调用 model.eval()。
2. 构建一个固定的留出验证批次列表,使两个检查点总在相同的词元上比较。
3. 在 torch.no_grad() 中把 L_val 计算为这些批次的平均交叉熵,再得 PPL = exp(L_val)。
4. 与均匀基线 ln(V) 比较:字符级 Shakespeare 的 V = 65,ln(65) = 4.1744,所以 L_val 低于 4.0 说明模型胜过瞎猜。
5. 用同一提示以 temperature=0.8、top_k=40 生成五个样本,并逐个检查退化循环(同一词元长串重复,或不同词元过少)。
6. 亲自阅读样本,并记录检查点步数、L_val、PPL、样本、解码设置和随机种子。

时间安排(120 分钟):阅读 20 + 练习 70 + 检查 30。 阅读:nanoGPT 中的 estimate_loss 与采样代码,以及 CS336 关于评估的笔记。练习:在最终检查点上完成第 1-6 步。检查:不看笔记,从 ln(V) 解释 L_val 和 PPL 的阈值。

自主演练与记录

运行最终模型评测脚本。断言验证损失低于随机初始化水平(Shakespeare 上 L_val < 4.0,或 PPL < 50),且生成文本中没有重复的单 Token 死循环。

独立掌握标准

能够以客观的困惑度指标与文本多样性度量,全面证实预训练模型的收敛质量。

权威源资源与权利证明

linking MIT

nanoGPT: train.py pretraining loop

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

对应章节 / 时间戳: train.py: estimate_loss() averages eval_iters batches per split; run every eval_interval; best_val_loss decides when a checkpoint is saved

验证版本与日期: 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 MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

作者 / 机构: Stanford University CS336 Staff | 原始出处链接 ↗

对应章节 / 时间戳: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

验证版本与日期: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

入选理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

来源前置条件:

Fluent Python, PyTorch, and linear algebra

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

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

对比候选资源与选型分析 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

02 120 分钟

全口径云端开销账单对账与每 Token 成本深度核算

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

前置要求

第 60 天活动 1 与第 58 天预算预警规则。

学习目标

执行全流程财务对账:逐项拆解 GPU 计费工时、持久化云盘租金及出网流量费用,核算十亿 Token 实际预训练单价并与行前预算对齐。

操作指引

1. 导出本次运行的云平台用量明细,而不只是汇总金额。
2. 逐项列出成本:算力(小时 * $/小时)、存储(GB * $/GB·月 * 天数 / 30.4)、网络出流量(GB * $/GB)。每行写明单位。
3. 各行相加,只对最终总额四舍五入到美分。
4. 与第 58 天预检预算对账,其上限必须在 (0, 20.0] 内:给出 PASS 或 FAIL;若 FAIL,给出超支金额以及能保持在上限内的最大算力时长。
5. 计算效率指标:每十亿 Token 成本 = 总额 / Token 数 * 1e9,以及隐含吞吐 Token 数 / (小时 * 3600)。
6. 用一句话写出超支原因,再用一句话写出下次防止它的改动。

时间安排(120 分钟):阅读 20 + 练习 65 + 检查 35。 阅读:EleutherAI Transformer Math 中的算力成本部分和你的第 58 天预检报告。练习:逐项账单和对账。检查:独立重做一个变式(算力 12 小时)的对账。

自主演练与记录

某次实验消耗 A100 工时 16.7 小时(单价 $1.50/hr),50GB 存储占用 1 天(约 $0.16),下载 2GB 模型产生流量费 $0.16,共训练 100 亿(10B)tokens。计算总账单金额与每十亿 Token 成本。然后与第 58 天的 $20 上限对账:这次运行是否通过?若未通过,给出超支金额以及能保持在上限内的最大算力小时数。

独立掌握标准

能精细化拆解并对账各项云端账单,准确测算模型工程的真实商业算力单价。

权威源资源与权利证明

linking CC-BY 4.0

Transformer Math 101

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

对应章节 / 时间戳: Engineering Takeaways for Compute Costs

验证版本与日期: 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.

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

作者 / 机构: Stanford University CS336 Staff | 原始出处链接 ↗

对应章节 / 时间戳: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

验证版本与日期: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

入选理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

来源前置条件:

Fluent Python, PyTorch, and linear algebra

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

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

对比候选资源与选型分析 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

03 120 分钟

Safetensors 安全格式导出与云端资源物理销毁下线流程

Agent 辅助工程检查

前置要求

第 60 天活动 1 与 2,Safetensors 规范及云资源下线流程。

学习目标

将 PyTorch 检查点无损提取为零拷贝的 Safetensors 安全格式,同步至本地资产库,并执行不可逆的云端实例与云盘彻底销毁流程,杜绝长尾扣费。

操作指引

1. 在 CPU 上加载 checkpoint.pt,只保留 'model' 权重;丢弃优化器状态和其他训练元数据。
2. 去掉 torch.compile 给每个键加上的 '_orig_mod.' 前缀。
3. 处理绑定权重:safetensors 拒绝共享存储的张量(GPT 中 lm_head.weight 就是 transformer.wte.weight)。为每个张量保存一份独立的连续副本,加载后再重新绑定。
4. 带元数据保存 model.safetensors,用 load_file 重新加载,并断言逐位一致的往返(相同的键,每个张量 torch.equal)。
5. 记录文件大小和 SHA-256,用 rsync 把文件拷回本地,在动云资源之前先比对本地摘要。
6. 按顺序清理:终止实例(是 terminated 而不是 stopped),删除本次运行的所有卷、快照和存储桶,稍后再列一次,确认活动实例 = 0、活动卷 = 0。

时间安排(120 分钟):阅读 20 + 练习 70 + 检查 30。 阅读:safetensors 文档(格式与 torch API)和 nanoGPT train.py 中的检查点结构。练习:导出脚本和清理清单。检查:不看笔记解释 safetensors 为何消除了 pickle 风险,以及为何绑定权重需要复制。

自主演练与记录

编写转换脚本 export_to_safetensors.py,读取 checkpoint.pt,剥离体积庞大的优化器状态,导出 model.safetensors 并验证可通过 load_file 完整还原。

独立掌握标准

成功导出无安全隐患的 Safetensors 权重文件,并建立零残留的云端资源彻底下线规范。

权威源资源与权利证明

linking Apache-2.0

Safetensors: Simple, Safe and Fast Tensor Storage

作者 / 机构: Nicolas Patry, Hugging Face Team | 原始出处链接 ↗

对应章节 / 时间戳: README 'Format' (8-byte header size, JSON header, tensor byte buffer)

验证版本与日期: Safetensors 0.4.x / GitHub (Verified: 2026-10-30)

入选理由:

Widely adopted format for secure model checkpoint export, preventing arbitrary code execution vulnerability inherent to Python pickle.

来源前置条件:

Binary serialization, memory mapping (mmap), and tensor state dictionaries.

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

Zero-copy tensor slicing and header dictionary parsing are demonstrated in pure Python.

对比候选资源与选型分析 (2)
Python pickle module documentation ↗ compared_alternative [PSF License]

Default PyTorch serialization; Safetensors replaces pickle to eliminate arbitrary code execution security hazards.

ONNX Model Format Specification ↗ compared_alternative [Apache 2.0]

Full computational graph interchange format; Safetensors focuses strictly on zero-overhead raw weight storage.

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.

04 120 分钟

考核评估:四语言规范化预训练复现实验技术报告撰写

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

前置要求

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

学习目标

全面汇整云端预训练复现实战证据,撰写涵盖模型配置、收敛曲线、困惑度评测、算力账单对账与彻底下线核验的标准化四语言技术报告。

操作指引

1. 从来源收集数字:最终步数和损失来自运行日志,MFU 来自第 59 天的监视器,总成本来自活动 2 的对账,清理证据来自云平台 CLI 输出。
2. 每个数字只存一处,并从这一份记录生成每个语言的章节,使各版本不可能不一致。
3. 检查内部一致性:PPL = exp(L_val),MFU 是 0 到 1 之间的比例。
4. 扫描整份报告中的机密和私有地址(IP 地址、访问密钥、令牌、私钥块)。
5. 如实报告预算结果:如果活动 2 未通过 $20 上限,每个版本都要写明并说明原因。
6. 只有在有证据时才签署清理:实例 = 0、卷 = 0,并注明日期。

时间安排(120 分钟):阅读 15 + 练习 75 + 检查 30。 阅读:CS336 关于报告的笔记和你在活动 1-3 中的记录。练习:填写报告并运行检查。检查:通过全部断言的掌握运行,再重读每个版本。

自主演练与记录

提交四语言预训练复现技术总结报告,断言关键评测数值(Loss、PPL、费用、MFU)在四种语言版本中完全一致且账单对账平齐。

独立掌握标准

产出符合工业界严谨规范的四语言预训练复现实验技术报告,确认资源彻底物理销毁。

权威源资源与权利证明

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

作者 / 机构: Stanford University CS336 Staff | 原始出处链接 ↗

对应章节 / 时间戳: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

验证版本与日期: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

入选理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

来源前置条件:

Fluent Python, PyTorch, and linear algebra

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

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

对比候选资源与选型分析 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

linking Apache-2.0

Safetensors: Simple, Safe and Fast Tensor Storage

作者 / 机构: Nicolas Patry, Hugging Face Team | 原始出处链接 ↗

对应章节 / 时间戳: README 'Format' (8-byte header size, JSON header, tensor byte buffer)

验证版本与日期: Safetensors 0.4.x / GitHub (Verified: 2026-10-30)

入选理由:

Widely adopted format for secure model checkpoint export, preventing arbitrary code execution vulnerability inherent to Python pickle.

来源前置条件:

Binary serialization, memory mapping (mmap), and tensor state dictionaries.

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

Zero-copy tensor slicing and header dictionary parsing are demonstrated in pure Python.

对比候选资源与选型分析 (2)
Python pickle module documentation ↗ compared_alternative [PSF License]

Default PyTorch serialization; Safetensors replaces pickle to eliminate arbitrary code execution security hazards.

ONNX Model Format Specification ↗ compared_alternative [Apache 2.0]

Full computational graph interchange format; Safetensors focuses strictly on zero-overhead raw weight storage.