Parameter count、FLOPs、6ND、MFU 怎么算?
训练要多久?

英文问法How many trainable parameters does this Transformer have? · Where does 6ND come from? · How do you compute MFU? · Given the token budget, how long will training take? · Show that LLM decoding is memory-bound.

考过NVIDIA Senior MLE 电面 2025-10("某个transformer给定输入dimension,每层输出多大,能train的参数有多少",1p3a 1152213)· NVIDIA Senior System Software Engineer 2024(证明 LLM 是 memory-intensive 而不只是 compute-intensive,Medium)· 牛客("mfu计算公式,6Nd公式是什么"、"给定训练所需的Tokens,怎么估计模型训练所需的完整时间?")· Anthropic("Performance-Model a Sharded Matrix Multiply: FLOPs, Memory, Network and Roofline",PracHub)

先弄清楚:每个概念是什么

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 一般多少

forwardY = X × WX[D, in]W[in, out]Y[D, out]×=2 · D · in · out FLOPsin · out 正好是 W 的参数个数backwarddX = dY × WᵀdY[D, out]Wᵀ[out, in]dX[D, in]×=2 · D · in · out FLOPs和 forward 一样大backwarddW = Xᵀ × dYXᵀ[in, D]dY[D, out]dW[in, out]×=2 · D · in · out FLOPs和 forward 一样大每参数每 tokenforward 2dX 2dW 2重算 forward 26ΨD:MFU 只算这些8ΨD:full recompute 再跑一遍 forward,HFU 也算它
图 1:每个 weight 矩阵做一次 Y = XW。backward 要算 dX(传给前一层)和 dW(更新 W)两个同样大的矩阵乘,所以是 forward 的 2 倍。
025%50%75%100% = 峰值GPT-321.3%MT-NLG30.2%Llama 3 405B38%到 43%PaLM 540B46.2%HFU 57.8%
图 2:灰色满格是 GPU 的峰值 FLOP/s,橙色是 MFU,蓝色是 PaLM 的 HFU 多出来的部分。这几个模型的 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}} \]

手算例子

必答点

追问

每层输出多大?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 规格