概述
It lets you train deeper, larger networks by trading extra compute for much lower memory use.
深入探討
Training normally retains intermediate activations needed for backpropagation. Activation checkpointing saves selected boundary tensors and recomputes internal activations during backward. This trades additional computation for a lower activation-memory footprint; the actual savings and runtime overhead depend on checkpoint placement, operations, model graph, and hardware. In a simplified chain of N similarly sized layers, a textbook schedule with checkpoint boundaries spaced about sqrt(N) layers apart can reduce activation storage to order sqrt(N) checkpoints, with recomputation cost governed by that schedule. This asymptotic example is not a universal prediction for framework implementations. PyTorch exposes activation-checkpointing utilities, and its current guidance also describes selective rematerialization and compiler-assisted choices. It can combine with FSDP and mixed precision, but benchmark the full training workload.
現實世界的實施
透過丟棄和重新計算層激活,在單一 GPU 上訓練具有更大批量大小的深度轉換器。
在高解析度影像上微調視覺模型,否則啟動圖會溢出 GPU 記憶體。
擁抱 Face Transformers 啟用gradient_checkpointing=True,以在微調期間適應十億參數模型。
將檢查點與 FSDP 結合,使參數和活化都保持較小,從而能夠訓練非常大的語言模型。
梯度檢查點的未來
梯度檢查點現在是大型模型訓練的標準,並且越來越自動化,函式庫會為您選擇最佳檢查點位置。它與 FSDP、混合精度和卸載自然配合,以提高模型尺寸。期望「選擇性」檢查點僅重新計算廉價的操作,同時快取昂貴的操作(如注意力矩陣),加上 PyTorch 的 torch.compile 等工具中的編譯器驅動方法,自動決定保存哪些內容與重新計算以獲得最佳速度記憶體平衡。
付諸實踐
為什麼這很重要
- 成本與預算. 多年來,架構決策決定著效能和營運成本。
- 更明確的決策. 技術教育幫助團隊選擇正確的堆疊,而不僅僅是最新的堆疊。
- 品質管控. 更好的工程選擇可以減少生產中的可靠性事故。
留意
- 優化一項基準測試可以隱藏更廣泛的系統弱點。
- 基礎設施和維護成本常常被低估。
- 隨著系統變得更加複雜,安全性和可觀察性差距可能會擴大。
後續步驟
- 在實施之前定義延遲、品質和成本目標。
- 在實際負載和資料條件下進行基準測試。
- 儀器監控錯誤、漂移和使用者影響。
- 在擴展之前準備回滾和事件回應路徑。
不斷探索
常見問題
What is Gradient Checkpointing?
梯度檢查點(也稱為激活檢查點)是一種節省記憶體的技巧,它在前向傳播期間丟棄大多數中間激活,並在反向傳播期間動態重新計算它們。它可以讓您透過用額外的計算換取更低的記憶體使用來訓練更深、更大的網路。
為了節省內存,梯度檢查點主要交換什麼?
梯度檢查點在向後傳遞過程中重新計算丟棄的激活,花費額外的計算來換取顯著減少的記憶體。
為什麼激活通常在前向傳遞過程中儲存?
反向傳播使用前向傳遞的中間活化來計算梯度,因此它們必須可用,除非重新計算。
In a simplified chain of N similarly sized layers, what can checkpoint boundaries spaced about sqrt(N) layers apart achieve in the textbook schedule?
The square-root schedule is a theoretical result for a simplified sequential computation graph, not a universal implementation guarantee.
What most determines gradient-checkpointing runtime overhead in a practical model?
Recomputation overhead varies with checkpoint policy and the cost of operations selected for recomputation.
在 PyTorch 中,哪個實用程式通常用於將梯度檢查點應用於模組?
torch.utils.checkpoint 包裝一個模組,以便在向後期間重新計算其內部啟動而不是儲存。