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.