Dive into Deep Learning · §13.4
显存层次、精度与检查点
参数与激活预算 · 混合精度(bf16/tf32) · 激活检查点 · 梯度累积
动机
fp32 权重(4N)、梯度(4N)与 Adam 矩(8N)在 24\text{ GiB} RTX 4090 上仅余 8.9\text{ GiB}。参数显存
模型参数、梯度与优化器状态所占显存仅取决于参数量 N、优化器选择与精度——与批量大小无关:
| 优化器与精度配置 | 每参数存储数组 | 字节 / 参数 | 1.01B GPT 状态 |
|---|---|---|---|
普通 SGD(float32) |
权重(4\text{ B})+ 梯度(4\text{ B}) | 8\text{ B} | 7.5\text{ GiB} |
SGD + 动量(float32) |
权重 + 梯度 + 速度(4\text{ B}) | 12\text{ B} | 11.3\text{ GiB} |
Adam / AdamW(float32) |
权重 + 梯度 + 一阶与二阶矩(8\text{ B}) | 16\text{ B} | 15.1\text{ GiB} |
Adam,混合精度(bf16) |
以上四项均 fp32(16\text{ B})+ bf16 权重副本(2\text{ B}) |
18\text{ B} | 17.0\text{ GiB} |
1.01 billion parameters, 16N = 15.1 GiB
激活显存
反向传播存储
每词元核算
fp32 下 149\text{--}160\text{ KiB/tok})。fp32 对数概率(212\text{ KiB/tok}),外加实测峰值超出计数的余量 \Delta。自动微分存储钩子
挂载 torch.autograd.graph.saved_tensors_hooks,并对 1 与 2 条序列、1 与 2 个模块做差分,即可精确分离单个模块(a)与输出头(a_{\textrm{out}})的每词元保存字节数:
one block: 149 KiB per token (18.7 fp32 values of width d)
embedding, output layer, and loss: 212 KiB per token
scaled_dot_product_attention、RMSNorm 与 F.silu 作为单融合内核执行,每模块恰好只存理论最小值 18.7d = 149\text{ KiB/token}。显存预算
将固定参数状态(梯度跨微批量持久保留时为 16N 字节)与 L 个每词元 a 字节的模块以及输出头 a_{\textrm{out}} 相加,即得 n 个词元的总显存预算:
M(n) \;\approx\; 16N \;+\; n\,(L a + a_{\textrm{out}}) \;\le\; M_{\textrm{GPU}}
峰值显存对比清单
在 2 块 GPT 上跨 1{,}024 至 8{,}192 词元测量实际峰值显存(torch.cuda.max_memory_allocated),发现超出已存激活清单 \Delta \approx 393\text{ KiB/token}:
1024 tokens: peak 1.53 GiB above the weights
2048 tokens: peak 1.77 GiB above the weights
4096 tokens: peak 3.53 GiB above the weights
8192 tokens: peak 7.06 GiB above the weights
2048 to 4096 tokens: measured 905 KiB per token; counted 511 KiB
4096 to 8192 tokens: measured 903 KiB per token; counted 511 KiB
backward() 刚启动、所有前向激活与 V 个已存对数概率均仍存活时,交叉熵反向传播物化了两个宽度为 V = 50{,}257 的 float32 梯度张量(分别对应对数概率与 logit:2 \times 4V = 8V \approx 393\text{ KiB/token})。分配器轨迹
记录 4{,}096 词元上一步 Adam 的全部 CUDA 缓存分配器事件,直观呈现激活与梯度的锯齿状生命周期:
混合精度
bfloat16(2\text{ B}):将张量核心吞吐翻倍至 165\text{ TFLOP/s},并把每模块激活存储压缩 \sim 40\%。float32(4\text{ B}):防止微小更新(\eta g \sim 10^{-5}w)舍入归零,并保住 RMSNorm、残差与交叉熵的数值精度。bfloat16 与 float32 共享 8 位指数(\sim 10^{\pm 38} 动态范围)。精度与容量
在 bfloat16 混合精度下,每个模块的两条残差流输入保持 float32(4\text{ B}),其余 \sim 20d 中另外 18 项降至 bfloat16(2\text{ B}),预测模块激活缩至 float32 的 (2 \cdot 4 + 18 \cdot 2) / (20 \cdot 4) = 0.55:
one block: 87 KiB per token in bf16, 0.58 of fp32
embedding, output layer, and loss: 306 KiB per token
bfloat16 权重副本将固定参数显存从 16N = 15.1\text{ GiB} 推高至 18N = 17.0\text{ GiB}(激活余量从 8.9 \to 7.0\text{ GiB}),且庞大的 float32 损失对数概率(a_{\textrm{out}})并未缩小。精度与吞吐
混合精度首先是速度优化。在计算受限的宽度 8192 三层 MLP 单步 SGD(每步 1.1 \times 10^{12}\text{ FLOPs/step};82.6\text{ TFLOP/s} 下算力下界 13.3\text{ ms},165.2\text{ TFLOP/s} 下 6.7\text{ ms},外加 fp32 SGD 更新 1.3\text{ ms})上做基准测试,可将张量核心执行与 16 位操作数分离开来:
fp32: 23.15 ms, 48 TFLOP/s
tf32: 15.01 ms, 73 TFLOP/s
bf16: 9.68 ms, 114 TFLOP/s
fp32 \to 张量核心 tf32(\sim 1.5\times):理论上限同为 82.6\text{ TFLOP/s} 且存储同为 32 位,但张量核心分块指令利用率达 \sim 90\%,高于标量 ALU 的 \sim 60\%。tf32 \to bf16(1.5\text{--}1.75\times):矩阵乘耗时减半至 \sim 6.7\text{ ms},而访存受限的 fp32 SGD 更新(1.3\text{ ms})与权重转换保持不变。激活检查点
\textrm{Peak/tok} \;=\; L \cdot 4d \;+\; a \;+\; a_{\textrm{out}} \;+\; \Delta
fp32 输入(18 \times 8\text{ KiB} = \mathbf{144\text{ KiB/tok}},远低于 2.7\text{ MiB/tok})。容量对比
对 18 块 1B 参数 GPT 求值 M(n) \le 24\text{ GiB},可见检查点如何彻底改变激活容量:
fp32, count : 2.83 MiB per token, room for 3,229 tokens
fp32, measured : 3.22 MiB per token, room for 2,844 tokens
bf16, measured : 2.21 MiB per token, room for 3,272 tokens
fp32, checkpointing: 0.88 MiB per token, room for 10,435 tokens
bf16, checkpointing: 0.91 MiB per token, room for 7,956 tokens
bf16 装下的词元反而少于 fp32,因其额外 2N = 1.9\text{ GiB} 的 bf16 权重副本(在 PyTorch 中还伴随更大的输出层与损失)抵消了单个重计算模块省下的显存。检查点基准
在 4 块堆叠(float32 下 d = 2048)上,检查点仅保存 4 个模块输入加最终输出(5 \times 4d = 40\text{ KiB/token}):
checkpointing=False: stores 606 KiB per token, peak 1.25 GiB, 47 ms per step
checkpointing=True : stores 40 KiB per token, peak 0.42 GiB, 59 ms per step
定向重计算
一旦逐块检查点将全部 18 个模块压至 144\text{ KiB/token},两项改进直指剩余瓶颈:
mm 输出而只重算廉价逐元素算子,几乎不增 FLOPs 即可大幅省显存。梯度累积
\frac{t_{\textrm{accumulator traffic}}}{t_{\textrm{micro-batch compute}}} \;=\; \frac{12N / \beta}{6Nn / P} \;=\; \frac{2 I^*}{n}
累积基准
将 2 块 GPT 上的 4{,}096 词元拆分为 m = 4 个 1{,}024 词元微批量,既精确复现整批梯度,又将激活显存缩减四倍:
1 x 4096 tokens: peak 3.53 GiB above the weights, 83 ms per step (83 to 83), device busy 83 ms
matrix products 69 ms, other kernels 13 ms
4 x 1024 tokens: peak 1.92 GiB above the weights, 99 ms per step (99 to 99), device busy 99 ms
matrix products 74 ms, other kernels 25 ms
检验理解
将本节的显存与耗时公式应用于 18 块 1B 参数 GPT(d = 2048,bf16 屋脊拐点 I^* = 164\text{ FLOP/byte})的两道设计题:
bfloat16 训练 524{,}288 词元全局批量。2 条序列微批量(n = 2{,}048 词元,m = 256)无需检查点即可装下,而 4 条序列(n = 4{,}096 词元,m = 128)须开启逐块检查点(\sim 25\% 重计算开销)。哪种方案完成一次全局步更快?解答:
核心要点
| 技术 / 组件 | 对单步显存 M(n) 的影响 | 时间 / 算力代价 | 适用场景 |
|---|---|---|---|
| 固定 Adam 状态 | 16N\text{ B}(fp32),18N\text{ B}(bf16) |
每步固定 | 基线参数 + 梯度 + 两份矩缓冲 |
| 模块激活(a) | 每词元 \approx 20d 值(fp32 160\text{ KiB}) |
基线前后向 | 线性输入(4d)+ 注意力(4d)+ SwiGLU(11d) |
bf16 混合精度 |
模块激活 \times 0.6,状态 +2N | 提速 1.5\text{--}1.75\times | 张量核心 GPU 上始终启用(无需损失缩放) |
| 逐块检查点 | 模块存储 La \to L \cdot 4d + a | 步时 +25\text{--}33\% | 合理微批量 n 装不进 M_{\textrm{GPU}} 时 |
| 分块交叉熵 | 消除 V 宽的 a_{\textrm{out}} + 8V 尖峰 | +3\% FLOPs(2dV) | 词表 V 主导检查点显存时 |
| 梯度累积 | 在 M(B_{\textrm{micro}}) 内跑 B_{\textrm{global}} = m \, B_{\textrm{micro}} | +\frac{2I^*}{n} 累加器流量 | 有限 GPU 上达成大全局批量 |