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.