Parameter count、FLOPs、6ND、MFU 怎么算?
训练要多久?
先弄清楚:每个概念是什么
- FLOPs 与 FLOP/s
- FLOPs 是总工作量,FLOP/s 是速度。一次乘加算 2 FLOPs。例:A100 的 bf16 峰值 312 TFLOP/s。
- 6ND
- N 个参数(本页记作 \(\Psi\))、D 个 token。每个参数对每个 token 在 forward 做一次乘加(2),backward 是两倍(4)。
- MFU
- model FLOPs utilization,实际 tokens/s × 6Ψ 除以峰值,分子只算模型必须的 FLOPs。
- HFU
- hardware FLOPs utilization,recompute 多算的也算进去,所以 ≥ MFU。例:PaLM 540B 的 MFU 46.2%,HFU 57.8%。
- Memory-bound
- 每搬 1 byte 只做很少运算,卡在 memory bandwidth 而不是算力上。
看图:6ND 从哪来,MFU 一般多少
30 秒回答
A Transformer layer has about 12h² parameters, 4h² in attention and 8h² in the MLP, so the model has about 12Lh² plus Vh. Each parameter does one multiply-add per token forward, 2 FLOPs, and backward costs twice that, so training takes about 6ND FLOPs; full recomputation adds a forward, about 8ND. MFU is tokens per second times 6N over peak FLOP/s; HFU also counts recomputation, so it is higher. Training time is 6ND over GPU count times peak times MFU: 7B on 1T tokens with 1024 A100s at 50% MFU takes about 3 days.
怎么算
Parameter
\[ \text{每层} = \underbrace{4h^2}_{\text{QKVO}} + \underbrace{8h^2}_{h\to4h\to h} = 12h^2,\qquad \Psi \approx 12Lh^2 + Vh \]GPT-3(\(L = 96\)、\(h = 12288\)、\(V = 51200\))→ 174.6B。
FLOPs
\[ C \approx 6\Psi D = \underbrace{2\Psi D}_{\text{forward}} + \underbrace{4\Psi D}_{\text{backward}} \]每层 forward \(24bsh^2 + 4bs^2h\) 的推导见 Transformer 复杂度。Megatron 的公式含 full recompute(4 次 forward),\(B\) = global batch 条数:
\[ F = 96BsLh^2\Big(1 + \frac{s}{6h} + \frac{V}{16Lh}\Big) \approx 8\Psi\cdot Bs \]MFU 与时长
\[ \text{MFU} = \frac{\text{tokens/s}\times 6\Psi}{\text{卡数}\times\text{峰值}} \] \[ \text{时长} \approx \frac{6\Psi D}{\text{卡数}\times\text{峰值}\times\text{MFU}} \]手算例子
- 时长:7B、1T token、1024 张 A100、MFU 50% → \(6\times7\text{B}\times10^{12} = 4.2\times10^{22}\) FLOPs,\(4.2\times10^{22} / (1024\times312\text{T}\times0.5) \approx 2.6\times10^5\) s ≈ 3 天。
- 反推 MFU:MT-NLG 530B 在 2240 张 A100 上跑 65.43K tokens/s → \(65.43\text{K}\times6\times530\text{B} / (2240\times312\text{T}) \approx 29.8\%\)。
- Megatron 版:时长 ≈ \(8\Psi D/(\text{卡数}\times X)\),\(X\) 是实测每卡 FLOP/s(含 recompute)。GPT-3 175B、300B token、1024 卡、\(X = 140\) TFLOP/s → 约 34 天。
- decode 为什么 memory-bound:batch = 1 时每个 token 读一遍全部 weight(\(2\Psi\) bytes),只做 \(2\Psi\) FLOPs,intensity ≈ 1。H100 SXM 的 ridge point ≈ 989 TFLOP/s(dense bf16)÷ 3.35 TB/s ≈ 295(见 roofline)。
必答点
- 每层 \(12h^2\),\(\Psi \approx 12Lh^2 + Vh\)
- \(C \approx 6\Psi D\):forward 2 + backward 4;full recompute → 8
- MFU 只算 \(6\Psi\);HFU 含 recompute,≥ MFU
- 时长 = \(6\Psi D\) ÷(卡数 × 峰值 × MFU)
- decode batch 1:intensity ≈ 1 ≪ ridge point → memory-bound
追问
每层输出多大?trainable 参数多少?
输入输出都是 \([b, s, h]\);Q、K、V reshape 成 \([b, a, s, h/a]\),score \([b, a, s, s]\),MLP 中间 \([b, s, 4h]\)。参数 \(12h^2\) 加 \(13h\)(QKV、output、两层 MLP 的 bias 共 \(9h\),两个 LayerNorm \(4h\))。
为什么用 MFU 而不是 HFU?MFU 一般多少?
HFU 取决于实现,recompute 越多它越高,不代表训得更快。MFU 只看模型和峰值,能跨系统比,数值见图 2。其中 GPT-3、MT-NLG、PaLM 来自 PaLM 论文,FLOPs 算了 attention,所以 MT-NLG 的 30.2% 比上面用 6N 手算的 29.8% 略高。
6ND 漏了什么?
漏了 attention 的 \(QK^\top\) 和乘 \(V\)。PaLM 每 token 加 \(12LHQT\)(\(HQ = h\)、\(T = s\)),和 \(6\Psi\) 之比约 \(s/(6h)\);GPT-3 只多 2.8%,long context 时不能忽略。
来源Kaplan et al. 2020 §2.1 eq. 2.1–2.2、Table 1(pp.6–7)· Narayanan et al. 2021 §5 eq. 2–4(pp.7–8)、Appendix(p.12)· Korthikanti et al. 2022 §6.3 Table 5(p.12)、Appendix A(p.15)· Chowdhery et al. 2022, PaLM §4.1 Table 3(p.9)、Appendix B(p.66)· Llama 3 §3.3.2(p.10)· NVIDIA H100 规格