Mixed Precision Training:精度該保留在哪裡
選讀・ML 01C・數值計算與梯度檢查 的論文導讀(第 1/1 篇)。引用編號對應本頁底部的參考文獻。
導覽:本頁與本系列: 導覽:本頁與本系列
問題與論點
訓練能否使用較便宜的算術,又不丟失讓學習成立的微小變化?Micikevicius 等人提出的是協調運作的配方,而不是把所有張量轉成半精度 [12]。先讀浮點數範圍、捨入與梯度下降。儲存活化值、累加總和、更新參數是不同操作,不必使用同一格式。
方法與圖解

沿圖 1 的更新迴圈閱讀:FP32 主權重產生 FP16 副本供層運算使用;乘積可以採較低精度,累加與敏感操作則保留 FP32。最佳化器更新主權重,因此小到無法立刻改變 FP16 權重的更新,仍可跨步累積。
損失縮放處理另一個問題:梯度可能在到達最佳化器以前,就低於 FP16 可表示的範圍。寫成
其中 為原損失, 是本次微分期間固定的正縮放值, 為縮放後損失, 為參數, 是交給更新步驟的還原梯度。精確算術下梯度不變,中間量的大小卻改變了。凡是結果取決於梯度大小的操作,都必須先還原尺度。
證據
論文涵蓋影像分類、偵測與語音系統,並非只測一個網路。圖 2 的語音實驗說明保留主權重的重要性;圖 3 則分析偵測器的小梯度分布。在所報告的 SSD 實驗中,縮放值 8 已足夠,但這不是所有模型通用的安全常數。各任務的準確度比較,應與硬體吞吐量主張分開閱讀。
設計分析
主權重保護微小參數更新;縮放保護微小梯度,兩者不能互相替代。縮放過大反而會溢位。公平比較應固定模型、資料與訓練排程,同時檢查準確度、時間及記憶體。由於還保留 FP32 主副本,即使許多活化值採 FP16,整體訓練記憶體也不會自動減半。
閱讀檢查點
某權重更新一次後,捨入回 FP16 的數值完全相同。解釋為何多次 FP32 主權重更新,最後仍可改變 FP16 副本。再指出:這套機制為何救不回在反向傳播途中已被捨入為零的梯度?圖中的兩種失敗位置不同。
限制與下一步
論文研究特定 FP16/FP32 配方與支援硬體。不可把它的範圍分析直接套用到另一種浮點格式,也不能保證受記憶體頻寬限制的工作得到同樣加速。接著讀 ML 02 最佳化;診斷訓練時,除了學習率,也記錄數值格式、縮放策略及最佳化器狀態精度。
接著閱讀: Adam:自適應更新與收斂論證的限制
參考文獻
- I. Goodfellow, Y. Bengio and A. Courville, Deep Learning, MIT Press, 2016, ch. 4 (Numerical Computation) and ch. 8. Book
- D. Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic,” ACM Computing Surveys, vol. 23, no. 1, pp. 5–48, 1991. DOI
- IEEE, “IEEE Standard for Floating-Point Arithmetic,” IEEE Std 754-2019, 2019. DOI
- N. J. Higham, Accuracy and Stability of Numerical Algorithms, 2nd ed., SIAM, 2002. DOI
- D. Kalamkar et al., “A Study of BFLOAT16 for Deep Learning Training,” arXiv:1905.12322, 2019. arXiv
- J. Nocedal and S. J. Wright, Numerical Optimization, 2nd ed., Springer, 2006. DOI
- S. Boyd and L. Vandenberghe, Convex Optimization, Cambridge University Press, 2004. Book
- Y. N. Dauphin, R. Pascanu, C. Gulcehre, K. Cho, S. Ganguli and Y. Bengio, “Identifying and Attacking the Saddle Point Problem in High-Dimensional Non-Convex Optimization,” NeurIPS, 2014. Paper
- B. A. Pearlmutter, “Fast Exact Multiplication by the Hessian,” Neural Computation, vol. 6, no. 1, pp. 147–160, 1994. DOI
- D. C. Liu and J. Nocedal, “On the Limited Memory BFGS Method for Large Scale Optimization,” Mathematical Programming, vol. 45, pp. 503–528, 1989. DOI
- A. G. Baydin, B. A. Pearlmutter, A. A. Radul and J. M. Siskind, “Automatic Differentiation in Machine Learning: a Survey,” Journal of Machine Learning Research, vol. 18, no. 153, pp. 1–43, 2018. arXiv
- P. Micikevicius et al., “Mixed Precision Training,” ICLR, 2018. arXiv
- PyTorch documentation, “Numerical Accuracy.” Documentation
- L. Bottou, F. E. Curtis and J. Nocedal, “Optimization Methods for Large-Scale Machine Learning,” SIAM Review, vol. 60, no. 2, pp. 223–311, 2018. DOI
- J. M. Cohen, S. Kaur, Y. Li, J. Z. Kolter and A. Talwalkar, “Gradient Descent on Neural Networks Typically Occurs at the Edge of Stability,” ICLR, 2021. arXiv
- J. Martens and R. Grosse, “Optimizing Neural Networks with Kronecker-factored Approximate Curvature,” ICML, 2015. Paper
- V. Gupta, T. Koren and Y. Singer, “Shampoo: Preconditioned Stochastic Tensor Optimization,” ICML, 2018. Paper
- J. Liu, J. Su, X. Yao et al., “Muon is Scalable for LLM Training,” arXiv:2502.16982, 2025. arXiv
- P. Micikevicius et al., “FP8 Formats for Deep Learning,” arXiv:2209.05433, 2022. arXiv
- M. Milakov and N. Gimelshein, “Online Normalizer Calculation for Softmax,” arXiv:1805.02867, 2018. arXiv
- T. Dao, D. Y. Fu, S. Ermon, A. Rudra and C. Ré, “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness,” NeurIPS, 2022. Paper
- J. Shah, G. Bikshandi, Y. Zhang, V. Thakkar, P. Ramani and T. Dao, “FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision,” NeurIPS, 2024. Paper
- NumPy developers, “numpy.linalg.cond,” NumPy documentation, accessed 2026-11-05. Documentation
- PyTorch contributors, “torch.autograd.gradcheck.gradcheck,” PyTorch documentation, accessed 2026-11-05. Documentation