新内容:参数、显存与算力估算
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.