58 / 90

第 58 天:雲端預訓練行前預檢、支出上限與零成本乾跑

審計 GPU 運行環境、確立硬性支出熔斷限額、校驗依賴完整性並執行本地單步乾跑。

模型與預訓練 480 分鐘有效研習(8 小時)
01 120 分鐘

雲端 GPU 訓練環境合規審計與硬體連通性校驗

Agent 輔助工程檢查

前置要求

第 56 天資源規劃數學計算與 SSH 公鑰基礎。

學習目標

在嚴禁將雲廠商憑據或密鑰硬編碼進代碼庫的原則下,全面校驗遠程主機 GPU 算力規格、CUDA 運行時及 PyTorch 設備就緒狀態。

操作指引

1. 遵循安全連接規範:使用標準 SSH 密鑰鑒權,嚴禁將私鑰、Access Key 或密碼寫入任何腳本或配置文件。
2. 執行 nvidia-smi 審查物理顯卡型號與顯存總量,防範雲服務商實際下發型號與計費規格不符。
3. 在隔離的 Python 虛擬環境中核驗 torch.cuda.is_available()、卡號與物理顯存量。
4. 檢驗底層算力卡對原生 bfloat16 算子的硬體支持度(torch.cuda.is_bf16_supported())。

自主實作與記錄

編寫遠程環境校驗腳本 verify_env.py,自動核查 Python 版本、CUDA 可用性、顯存容量(>=15GB)及 BF16 支持,通過返回 0,不達標則返回 1 並輸出詳細診斷。

獨立掌握標準

獨立編寫環境審計腳本,在不洩露憑據的前提下對遠程 GPU 規格達成 100% 自動核驗。

權威來源資源與權利證明

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 MIT

nanoGPT: train.py pretraining loop

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: train.py: system settings (device, dtype bfloat16/float16 selection, compile) and DDP settings

驗證版本與日期: 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 分鐘

嚴苛支出上限配置、自動熔斷停機與賬單陷阱防禦

獨立掌握檢查(無 AI 輔助)

前置要求

第 58 天活動 1 與第 56 天雲端費用核算公式。

學習目標

設定絕對嚴格的單次實驗預算上限(最高 $10.00 - $20.00),部署空閒自動關機看門狗,徹底根絕磁盤持續扣費與實例失控常開的賬單陷阱。

操作指引

1. 測算絕對安全運行窗口:最大時長 = 預算上限 / 單價。如設定 $15 上限、費率 $1.5/hr,則硬性運行上限為 10 小時。
2. 洞悉雲端賬單暗坑:關機(Stop)僅停用 GPU 計費,掛載的雲硬碟(Volume)仍會 7x24 小時扣費,直至執行物理註銷(Destroy)。
3. 構建自動巡檢看門狗:即時輪詢 nvidia-smi,當檢測到 GPU 連續 15 分鐘空閒(利用率 < 5%)時,立即觸發實例關機。
4. 確立明確的訓練終止三要素:達到既定 step、驗證集 Loss 達標、或觸碰預算警戒線即刻退出。

自主實作與記錄

編寫預算守門腳本 budget_watchdog.py,監控訓練主進程,當 (運行小時 * 費率) 達到最大額度時,自動向訓練進程發送 SIGTERM 觸發優雅持久化並停機。

獨立掌握標準

能獨立部署在達到預算天花板時自動切斷算力開銷的熔斷程序。

權威來源資源與權利證明

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 分鐘

訓練配方合理性核查與單 Batch 零成本乾跑預檢

Agent 輔助工程檢查

前置要求

第 58 天活動 1 與 2,PyTorch AMP 自動混合精度機制。

學習目標

在雲端啟動前,利用單 Batch(batch_size=1)執行本地零成本乾跑,驗證張量維度對齊、交叉熵損失收斂、AMP GradScaler 縮放穩定性及顯存峰值。

操作指引

1. 構造尺寸為 (1, 64) 的合成輸入張量 x 與目標張量 y。
2. 在 torch.cuda.amp.autocast(dtype=torch.bfloat16 if supported else torch.float16) 上下文中執行單步前向傳播。
3. 計算交叉熵損失並調用 scaler.scale(loss).backward() 執行反向求導。
4. 核驗 optimizer.step() 已更新權重,且 scaler.update() 調整縮放因子的過程中沒有產生 NaN。

自主實作與記錄

編寫並執行 dry_run.py 單步乾跑測試。確認 loss 為有效實數,模型各層梯度均成功回傳,列印準確的峰值顯存佔用。

獨立掌握標準

在零費用前提下完成單步閉環驗證,杜絕語法、張量不匹配或溢出導致的雲端秒崩。

權威來源資源與權利證明

linking MIT

nanoGPT: train.py pretraining loop

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: train.py: eval_only flag and default config values (batch_size, block_size, max_iters)

驗證版本與日期: 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 | 原始出處連結 ↗

對應章節 / 時間戳: Autocasting

驗證版本與日期: 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 輔助)

前置要求

第 58 天活動 1 至 3 全部內容。

學習目標

獨立開發工程級 PreflightValidator 預檢門禁類,綜合執行硬體自檢、依賴復核、預算上限校驗與單步試跑,出具受檢的 GO/NO-GO 決策報告。

操作指引

1. 封裝 PreflightValidator(config, budget_ceiling_usd) 類。
2. 依次編排四個校驗階段:硬體規格審計、環境依賴鎖定、預算熔斷上限(嚴格 <= $20.00)、單步前向反向乾跑。
3. 實施強安全熔斷策略:任一子項不通過,或預算上限 budget_ceiling_usd 不在 (0, 20.0] 區間內時,強行判定為 'NO-GO' 並終止下游任務。
4. 導出標準化 JSON 格式預檢憑證,供後續審計備查。

自主實作與記錄

運行 PreflightValidator 單元測試,驗證合規參數下輸出 'GO',而在預算超標或環境缺失時精準攔截並輸出 'NO-GO'。

獨立掌握標準

構建出能夠全自動阻斷超支或缺陷環境啟動的工程級行前安全檢查門禁。

權威來源資源與權利證明

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 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.