Training memory 由什么构成?怎么估?

英文问法What takes GPU memory during training? · How much memory does it take to train a 14B model with Adam? · Why does AdamW need 4× the parameter memory? · How many trainable parameters does LoRA add?

考过NVIDIA Senior MLE 电面 2025-10(全是计算题:SGD/Adam 各要多少 parameter、每层输出多大、LoRA/QLoRA 的 parameter count 和 initialization,Fail,1p3a 1152213)· NVIDIA DevTech NG 2026(MLP 的维度变化,1p3a 1161658)· 拼多多("训练时占用显存的数据有哪些")· 字节("14B:FP16 权重大概多大;训练还要加哪些")· 滴滴("AdamW 优化器为什么会占用4倍模型参数量显存?")· Anthropic("Estimate VRAM and Compare Model Parallelism",PracHub)

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

Parameter 与 gradient
weight 和 gradient 各一份,mixed precision 下各 2 bytes。例:14B 模型的 bf16 weight 是 28 GB。
Optimizer state
Adam 的 m、v 加一份 fp32 master weight,共 12 bytes,是大头(推导见 optimizer)。
Activation
forward 时留给 backward 用的中间结果。和参数量无关,随 \(b\)、\(s\) 线性涨,attention 的 score 随 \(s^2\) 涨。
Buffer 与 fragmentation
gradient all-reduce 前常拼成一个大 buffer,1.5B 模型的 fp32 buffer 就 6 GB。fragmentation 严重时,ZeRO 观察到还剩 30% 以上空闲却 OOM。
LoRA / QLoRA
冻结原 weight,只训练低秩的 \(BA\)。QLoRA 再把冻结的 weight 压成 4-bit。

看图:16 bytes 怎么分,activation 有多大

param2 bytes28 GBgrad2 bytes28 GBfp32 master4 bytes56 GBAdam m4 bytes56 GBAdam v4 bytes56 GBbf16:2 + 2 = 4 bytesfp32 optimizer state:4 + 4 + 4 = 12 bytesmodel states 合计:16 bytes × 14B 参数 = 224 GBweightinference 只要 bf16 weight:28 GB(再加 KV cache)
图 1:14B 模型每个参数的 16 bytes,按 GB 画。橙色是 bf16 的 parameter 和 gradient,蓝色的 fp32 optimizer state 占 3/4。inference 只要 28 GB 的 weight 加 KV cache。
每层 activation,单位 sbh bytes(s = 4096、h = 8192、a = 64、b = 1,80 层)不切分、不重算:sbh(34 + 5as/h) = 194 sbh345as/h = 160:s×s 的 softmax、dropout34 = attention 11 + MLP 19 + LN 4每层 ≈ 6.5 GB,80 层 521 GB,放不下TP = 8 + SP + selective:34sbh/8每层 ≈ 143 MB,80 层 11.4 GBfull recompute:只存每层输入 2sbh每层 ≈ 67 MB,80 层 5.4 GB,多一次 forward
图 2:手算例子里一层的 activation,按 sbh 的倍数画。橙色的 s×s 部分(160)远大于其余部分(34),sequence 越长越占 GPU memory。虚线框是第一行的长度。

30 秒回答

Four parts. Parameters and gradients in bf16 take 2 bytes each. Adam's state, an fp32 master copy plus two moments, takes 12, so model states total 16 bytes per parameter: a 14B model needs 28 GB for weights but 224 GB to train. Activations saved for backward take about s·b·h·(34 + 5as/h) bytes per layer; TP plus SP divides that by t, and recomputation cuts it further. Then add buffers and fragmentation. LoRA trains only r·(d_in + d_out) parameters per matrix, and QLoRA stores frozen weights in 4-bit.

怎么估:四部分相加

部分bytes14B 例
parameter(bf16)\(2\Psi\)28 GB
gradient(bf16)\(2\Psi\)28 GB
optimizer state(fp32 master、m、v)\(12\Psi\)168 GB
model states 合计\(16\Psi\)224 GB
activation每层见下式,\(\times L\)看 \(b\)、\(s\)

Activation(fp16,不做 parallelism / recompute)

\[ \text{每层} = sbh\Big(34 + \frac{5as}{h}\Big)\ \text{bytes} \]

34 = attention 11 + MLP 19 + 两个 LayerNorm 4(单位 \(sbh\));\(5as/h\) 是 \(s\times s\) 的 softmax 输出、dropout mask 和 dropout 输出。

\[ \text{TP + SP:} \div t;\qquad \text{再加 selective recompute:} \frac{34\,sbh}{t} \] \[ \text{full recompute:} 2sbh\ \text{(只存每层输入)} \]

PP 用 1F1B 时第一个 stage 存 \(p\) 个 micro-batch × \(L/p\) 层,仍是 \(L\) 层。

手算例子

\(s = 4096\)、\(h = 8192\)、\(a = 64\)、\(L = 80\)、\(b = 1\):

LoRA:每个矩阵加 \(r(d_\text{in} + d_\text{out})\) 个 trainable 参数,\(A\) 随机、\(B = 0\) → 初始 \(\Delta W = 0\)。冻结的 weight 只占 2 bytes/param,16 bytes 只算在 LoRA 参数上。例:\(h = 4096\)、32 层、只加在 \(W_q\)、\(W_v\)、\(r = 8\) → \(2\times32\times8\times8192 \approx 4.2\)M。QLoRA:冻结 weight 存成 NF4,65B finetune 从 >780 GB 降到 <48 GB,细节见 LoRA。

必答点

追问

SGD、momentum、Adam 各要多少?"4 倍参数量"怎么来?

全 fp32 时 weight + gradient 8 bytes,加上 state 是 SGD 8、momentum 12、Adam 16 bytes/param。Adam 的 w、g、m、v 四份就是"4 倍",表格见 optimizer。

ZeRO 能省多少?

ZeRO-1/2/3 依次切 optimizer state、gradient、parameter。7.5B、DP \(N = 64\) 时每卡从 120 GB 降到 31.4、16.6、1.9 GB,公式见 DP、ZeRO、FSDP。

LoRA 为什么只把 B 初始化成 0?

\(B = 0\) 让训练从原模型出发。\(A\)、\(B\) 都是 0 时两边的 gradient 全是 0,学不动,式子见 LoRA。

OOM 了怎么办?

先减 micro-batch 加 gradient accumulation,再上 selective recompute,列表见 activation recompute。

来源Korthikanti et al. 2022 §4.1 eq. 1(p.4)、§4.2 eq. 2–5(pp.5–8)、§5 Table 2(p.9)· Rajbhandari et al. 2020, ZeRO Fig. 1(p.3)、§3.1–3.2(pp.7–8)· Hu et al. 2021, LoRA §4.1–4.2(pp.4–5)· Dettmers et al. 2023, QLoRA §1(p.2)、§3(pp.4–5)