新內容:參數、記憶體與算力估算
Agent 輔助工程檢查
前置要求
第 47 天的區塊參數統計、第 48 天的權重共享、第 50 天的 AdamW、第 55 天的檢查點。
學習目標
精確統計 GPT 的參數量,換算為推論和訓練記憶體,並用 6ND 法則估算訓練 FLOPs、時長和費用。
操作指引
這是今天唯一的新內容,其餘時間複習第 8 週。第 58、59、64、65 天會重複使用這些公式。
1. 參數(d = n_embd,V = 詞表大小,L = 上下文長度):嵌入 V*d + L*d;每區塊 12*d^2(注意力 4*d^2,MLP 8*d^2)+ 4*d(兩個 LayerNorm);最終 LayerNorm 2*d;共享權重的輸出頭不額外計數。該公式不含線性層偏置:GPT-2 每區塊有 9*d 個(QKV 3d、注意力輸出 d、MLP 4d + d)。用 PyTorch 的 sum(p.numel()) 核對結果;同一公式算出 GPT-2 Medium(350M:d = 1,024,24 區塊)為 354,601,984,含偏置為 354,823,168。
2. 僅權重的記憶體:每個參數 4 位元組(FP32)、2(FP16/BF16)、1(INT8)、0.5(INT4)。
3. 混合精度 AdamW 訓練每個參數佔 16 位元組(ZeRO 論文):FP16 權重 2 + FP16 梯度 2 + FP32 主權重、一階矩、二階矩 4 + 4 + 4。激活值另計,隨批次大小和上下文成長;CUDA 上下文約再加 1 GB。
4. 算力:C = 6 * N * D FLOPs(前向 2ND,反向 4ND)。時長 = C / (GPU 數 * 峰值 FLOP/s * MFU * 3600),MFU 通常為 30-50%。
自主實作與記錄
1. GPT-2 Small:V = 50,257,L = 1,024,n_layer = 12,n_embd = 768,共享輸出頭。用上面的公式統計參數量,再算出包含 GPT-2 線性層偏置時的參數量。FP16 權重佔多少位元組?
2. 一個 10 億參數的模型用混合精度 AdamW 訓練,在目前批次大小下激活值需要 8 GB。能放進 24 GB 的 GPU 嗎?
3. 在一張 A100(BF16 峰值 312 TFLOP/s)上以 40% MFU、每小時 $1.50 用 10B token 訓練 125M 模型。給出 FLOPs、時長和費用。
每區塊:12 * 768^2 + 4 * 768。共享的嵌入只計一次。靜態訓練記憶體為 N * 16 位元組。有效吞吐量為 312e12 * 0.40 FLOP/s。
1. 每區塊 = 12 * 768^2 + 4 * 768 = 7,080,960;12 區塊 = 84,971,520。嵌入 = 50,257 * 768 + 1,024 * 768 = 39,383,808。最終 LayerNorm = 2 * 768 = 1536。總計 N = 124,356,864。加上線性層偏置(12 * 9 * 768 = 82,944)得 124,439,808,與 bias=True 的 GPT-2 在 PyTorch 中統計的數量一致。N = 124,356,864 時的 FP16 權重:248,713,728 位元組 = 237.2 MiB(248.7 MB)。
2. 靜態 = 1e9 * 16 位元組 = 16 GB。合計 = 16 + 8(激活)+ 1(CUDA 上下文)= 25 GB > 24 GB,除非使用梯度檢查點、更小的微批次加梯度累積或 ZeRO 卸載,否則會記憶體不足。
3. C = 6 * N * D = 6 * 125e6 * 10e9 = 7.5e18 FLOPs。有效吞吐量 = 1.248e14 FLOP/s。時長 = 60,096 秒 = 16.69 小時。費用 = 16.69 * $1.50 = $25.04。
能分別在含和不含線性層偏置時得到精確參數量,用每參數 16 位元組法則預測記憶體不足,並由 6ND 和 MFU 算出時長與費用。
權威來源資源與權利證明
Scaling Laws for Neural Language Models
對應章節 / 時間戳: Section 2.1 Parameter and Compute Scaling of Transformers (C ≈ 6N FLOPs per token)
驗證版本與日期: arXiv:2001.08361v1 (Verified: 2026-10-30)
Foundational empirical study formulating power laws governing cross-entropy loss with respect to model parameters N, dataset size D, and compute C.
Basic calculus, logarithms, and parameter counting fundamentals.
Scaling formulas, power-law exponents, and parameter compute trade-offs are derived in the lesson text.
對比候選資源與選型分析 (2)
Early formulation of power-law scaling; Kaplan focuses explicitly on autoregressive transformer language models.
General vision/translation scaling; Kaplan specifically establishes modern decoder-only LLM compute budgeting.
Transformer Math 101
對應章節 / 時間戳: Compute Requirements (C = 6PD) and Memory Requirements: Training
驗證版本與日期: 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.
Original 6ND approximation; EleutherAI expands on activation memory and KV cache.
ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
對應章節 / 時間戳: Section 3.1 Model States: Optimizer States, Gradients and Parameters (2Ψ + 2Ψ + KΨ = 16Ψ bytes, K = 12)
驗證版本與日期: SC20 / arXiv:1910.02054v3 (Verified: 2026-10-30)
Source of the 16 bytes per parameter accounting for mixed-precision Adam that the lesson's memory arithmetic uses.
Adam optimizer mechanics from Day 34 and parameter memory arithmetic.
Complete analytical formulas for bytes-per-parameter across AdamW vs SGD and mixed precision are fully provided.
對比候選資源與選型分析 (2)
Focuses on tensor slicing and inter-GPU communication; ZeRO provides the standard breakdown of optimizer and parameter memory footprints.
Foundational mixed-precision paper; ZeRO formalizes the exact 16-byte Adam state footprint.