← Back to Deep Learning Mind Map
🧠 Deep LearningID: mixed-precision-fp8

Mixed Precision FP16/BF16/FP8

混合精度 FP16/BF16/FP8
🎯Core Definition
Mixed precision uses low-precision formats (FP16/BF16/FP8) for the forward and backward passes while keeping FP32 master weights and optimizer states, trading precision for speed and memory. Bit layouts and ranges: ① FP16 = 1 sign + 5 exponent + 10 mantissa; max value 65504=215×(2−2−10)65504 = 2^{15} \times (2 - 2^{-10}); min normal 2−14≈6.1×10−52^{-14} \approx 6.1\times10^{-5}; min subnormal 2−24≈6×10−82^{-24} \approx 6\times10^{-8}. Problem: in deep networks / large-batch training gradients are often below 10−810^{-8} and underflow to zero in FP16 → parameters stop updating. Solution: loss scaling — multiply the loss by a factor SS (commonly 2242^{24} or 1024) before backprop so gradients are amplified SS×, then divide back on the FP32 master weights before updating; dynamic loss scaling: on Inf/NaN detection set S←S/2S \leftarrow S/2 and skip the step; after a period without overflow, double SS. ② BF16 = 1 sign + 8 exponent + 7 mantissa: the exponent width equals FP32's, so the dynamic range matches FP32 (about ±3.4×1038\pm3.4\times10^{38}), eliminating underflow entirely with no loss scaling needed; the cost is only 7 mantissa bits (~3 significant decimal digits). ③ FP8: E4M3 (4 exponent + 3 mantissa, max ~448; better precision, smaller range) for the forward pass (weights/activations); E5M2 (5 exponent + 2 mantissa, max ~57344; larger range, lower precision) for backward gradients (preventing overflow/underflow). Standard pipeline: FP32 master weights → copy a low-precision view each step for forward → multiply loss by SS and backprop → unscale the low-precision gradients and update FP32 master weights (EMA, weight decay, gradient clipping can all be composed in FP32).
💡Use Cases
standard for large-model training/inference (natively supported on A100/H100/TPU, tensor-core throughput doubling as precision halves); interview favorites: why FP16 needs loss scaling, BF16 vs FP16, and why FP8 splits into E4M3/E5M2.
⚡Key Problems Solved
trading precision for speed and memory — memory halves and Tensor Core throughput doubles (FP8 doubles it again on H100) without losing training quality: FP32 master weights preserve numerical precision; BF16 trades range for precision, fixing FP16's underflow (no loss scaling needed, more stable training); FP8 assigns range vs precision by role (forward activations need precision to avoid information loss, gradients need range to avoid overflow), the next-generation doubling for both inference and training throughput.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Mixed Precision FP16/BF16/FP8.
Use Cases
In production engineering, what are the primary use cases for Mixed Precision FP16/BF16/FP8 and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Write FP16's bit layout (1 sign + 5 exponent + 10 mantissa) and max value 65504=215×(2−2−10)65504 = 2^{15} \times (2 - 2^{-10}); explain why training gradients often underflow (below 2−24≈6×10−82^{-24} \approx 6\times10^{-8}).
2
Loss scaling: why multiply the loss by SS before backprop and divide back on FP32 master weights before updating? Why does the dynamic policy (halve SS on overflow and skip the step, double after a clean streak) work?
3
Why doesn't BF16 (1+8+7) need loss scaling? Its exponent width equals FP32's, giving a range of about ±3.4×1038\pm3.4\times10^{38}; what is the cost (7 mantissa bits) and when is the precision insufficient?
4
Why are FP8 E4M3 and E5M2 used for the forward pass and gradients respectively? Compare their range/precision trade-offs (max ~448 vs ~57344, 3 vs 2 mantissa bits).
5
Standard mixed-precision pipeline: FP32 master weights, low-precision forward/backward, loss scaling and unscaling, FP32 updates; how does it compose with gradient clipping, EMA and DDP?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Mixed Precision FP16/BF16/FP8"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardGradient ClippingNext Card →DDP & Ring-AllReduce

🔗 More Deep Learning Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/DL/card/mixed-precision-fp8
中文速览与面试要点

混合精度 FP16/BF16/FP8 (Mixed Precision FP16/BF16/FP8)

定义: 混合精度 = 用低精度格式(FP16/BF16/FP8)做前向与反向计算,FP32 保存主权重(master weights)与优化器状态,兼顾速度与精度。位布局与值域: ① FP16 = 1 符号 + 5 指数 + 10 尾数,最大可表示数 65504 = 2^15 (2 - 2^-10),最小正规数 2^-14 6.1 10^-5,最小次正规数 2^-24 6 10^-8。问题: 深层网络/大批量训练中梯度经常小于 10^-8,FP16 直接下溢为 0 → 参数不再更新(下溢问题)。解法 Loss Scaling: 反向前把 loss 乘缩放因子 S(常用 2^24 或 1024),梯度整体放大 S 倍再反传,更新前在 FP32 主权重上除回 S;动态 Loss Scaling: 检测到 Inf/NaN 时 S S/2 并跳过该步,一段时间无溢出则 S 翻倍。② BF16 = 1 符号 + 8 指数 + 7 尾数: 指数位数与 FP32 相同 → 动态范围与 FP32 一致(约 3.4 10^38),彻底免于下溢、不需要 loss scaling;代价是尾数仅 7 位(约 3 位有效十进制数字),低精度。③ FP8: E4M3(4 指数 + 3 尾数,最大约 448,精度更高、范围小)→ 用于前向(权重/激活);E5M2(5 指数 + 2 尾数,最大约 57344,范围大、精度低)→ 用于反向梯度(防溢出/下溢)。标准训练流程: FP32 主权重副本 → 每步拷贝低精度副本做前向 → loss 乘 S 后反向 → 低精度梯度 unscale 后更新 FP32 主权重(可与 EMA、权重衰减、梯度裁剪在 FP32 上叠加)。 使用场景: 大模型训练/推理标配(A100/H100/TPU 原生支持,张量核算力随精度减半而翻倍);面试高频“FP16 为什么要 loss scaling”“BF16 与 FP16 的区别”“FP8 为什么分 E4M3/E5M2”。 解决的问题: 精度换速度与显存——显存减半、Tensor Core 吞吐翻倍(FP8 在 H100 上再翻倍),同时不丢训练精度: FP32 主权重保证数值精度;BF16 用“范围换精度”解决 FP16 的下溢缺陷(无需 loss scaling、训练更稳);FP8 把范围与精度按角色分配(前向激活要精度防信息丢失、梯度要范围防溢出),是推理与训练吞吐再翻倍的下一代方案。

核心要点

  • 写出 FP16 位分布(1 符号 + 5 指数 + 10 尾数)与最大可表示数 65504 = 2^15 (2 - 2^-10);解释训练中梯度为什么容易下溢(小于 2^-24 6 10^-8)。
  • Loss Scaling 原理: 为什么反向前把 loss 乘 S、更新前在 FP32 主权重上除回?动态策略(溢出时 S 减半并跳过该步、连续无溢出则翻倍)为什么有效?
  • BF16(1+8+7)为什么不需要 loss scaling?指数位与 FP32 相同 → 范围约 3.4 10^38;代价(尾数 7 位)是什么,什么场景精度不够?
  • FP8 E4M3 与 E5M2 为什么分别用于前向与梯度?比较两者的范围/精度取舍(最大约 448 vs 57344,尾数 3 vs 2 位)。
  • 混合精度标准流程: FP32 master weights、低精度前向/反向、loss scaling 与 unscale、FP32 更新;与梯度裁剪、EMA、DDP 如何配合?