步履不停:TensorFlow 2.4新功能一覽!
TensorFlow 2.4 正式發布!隨著對分布式訓練和混合精度提供更多支持,加入新的 Numpy 前端及用于監控和診斷性能瓶頸的工具,這個版本的亮點在于推出新功能,以及對性能和擴展方面的增強。
tf.distribute 的新增功能
參數服務器策略
在版本 2.4 中,實驗性引入了 tf.distribute 模塊的支持,可通過 ParameterServerStrategy 和自定義訓練循環對 Keras 模型進行異步訓練。與 MultiWorkerMirroredStrategy 一樣,ParameterServerStrategy 是一種多工作器數據并行策略;但其梯度更新方式為異步執行。
- ParameterServerStrategy
https://tensorflow.google.cn/api_docs/python/tf/distribute/experimental/ParameterServerStrategy
參數服務器訓練集群包含工作節點和參數服務器。系統會在參數服務器上創建變量,然后工作節點會在每個步驟中進行讀取和更新。變量的讀取和更新會在各工作節點上獨立進行,同時無需采取任何同步操作。由于工作節點互不依賴,因此該策略具有工作器容錯的優勢,并會在使用搶占式服務器時有所助益。
如要開始使用此策略,請查閱參數服務器訓練教程。此教程介紹了如何設置 ParameterServerStrategy,并說明了如何使用 ClusterCoordinator 類來創建資源、調度函數和處理任務失敗。
- 參數服務器訓練教程
https://tensorflow.google.cn/tutorials/distribute/parameter_server_training
- ClusterCoordinator
多工作節點鏡像策略
MultiWorkerMirroredStrategy 多工作節點鏡像策略 已順利度過實驗階段,現已成為穩定 API 的組成部分。與單個工作節點副本 MirroredStrategy 一樣,MultiWorkerMirroredStrategy 通過同步數據并行化實現分布式訓練。但利用 MultiWorkerMirroredStrategy,您可以在多臺機器上進行訓練,且每臺機器可以都搭載多個 GPU。
- MultiWorkerMirroredStrategy
https://tensorflow.google.cn/api_docs/python/tf/distribute/MultiWorkerMirroredStrategy
- MirroredStrategy
https://tensorflow.google.cn/api_docs/python/tf/distribute/MirroredStrategy
在同步訓練中,每個工作節點會在輸入數據的不同片段上計算正向和反向傳遞次數,并且在每個步驟結束時匯總梯度。對于這種稱為 All Reduce 的匯總, MultiWorkerMirroredStrategy 會使用集合運算保持變量同步。集合運算是 TensorFlow 圖表中的單個算子,可以根據硬件、網絡拓撲和張量大小在 TensorFlow 運行時中自動選擇 All Reduce 算法。集合運算還可實現其他集合運算,例如廣播和 All Gather。
- 集合運算
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/collective_ops.py
如要開始使用 MultiWorkerMirroredStrategy,請查閱使用 Keras 進行多工作器訓練教程,該教程已更新了有關數據集分片、保存/加載使用分布策略訓練的模型,以及使用 BackupAndRestore 回調進行故障恢復的詳細信息。
- 使用 Keras 進行多工作器訓練
https://tensorflow.google.cn/tutorials/distribute/multi_worker_with_keras
- BackupAndRestore
https://tensorflow.google.cn/api_docs/python/tf/keras/callbacks/experimental/BackupAndRestore
如果您不熟悉分布式訓練,并希望了解入門方法,或者有興趣在 Google 云端平臺 (GCP) 上進行分布式訓練,請參閱本博文,以獲取關于關鍵概念和步驟的介紹。
Keras 的相關更新
混合精度
在 TensorFlow 2.4 中,Keras 混合精度 API 已順利度過實驗階段,現已成為穩定的 API。大多數 TensorFlow 模型使用的是 float32 dtype;但也存在使用更少內存的低精度類型(如 float16)。混合精度指在同一模型中通過使用 16 位和 32 位浮點類型,以加快訓練速度。該 API 可使模型在 GPU 上性能提高 3 倍,在 TPU 上提高 60%。
- Keras 混合精度 API
https://tensorflow.google.cn/api_docs/python/tf/keras/mixed_precision
如要使用混合精度 API,您必須使用 Keras 層和優化工具,但無需使用其他 Keras 類,例如模型或損失。如果您對如何利用此 API 實現性能優化頗有興趣,請查閱混合精度教程。
- 混合精度教程
https://tensorflow.google.cn/guide/mixed_precision
優化工具
此版本支持重構 tf.keras.optimizers.Optimizer 類,使 model.fit 或自定義訓練循環的用戶能夠編寫任何適用于優化工具的訓練代碼。現所有內置的 tf.keras.optimizer.Optimizer 子類均可支持使用 gradient_transformers 和 gradient_aggregator 參數,您可借此輕松定義自定義梯度轉換。
- tf.keras.optimizers.Optimizer
https://tensorflow.google.cn/api_docs/python/tf/keras/optimizers/Optimizer
通過重構,您現在可以在編寫自定義訓練循環時將損失張量直接傳遞給 Optimizer.minimize:
- tape = tf.GradientTape()
- with tape:
- y_pred = model(x, training=True)
- loss = loss_fn(y_pred, y_true)
- # 如下所示,在使用損失“張量”時,您可以在“tf.GradientTape”中進行傳遞。
- optimizer.minimize(loss, model.trainable_variables, tapetape=tape)
此類更改旨在使 Model.fit 和自定義訓練循環都能擺脫優化工具細節的限制,從而使您無需修改,即可編寫任何適用于優化工具的訓練代碼。
函數式 API 模型構建的內部改進
最后,在 Keras 中,TensorFlow 2.4 可支持對 Keras Functional API 內部主要結構的重構,從而可降低函數式模型構建的內存消耗并簡化觸發邏輯。開展此類重構操作還能夠確保 TensorFlowOpLayers 行為可預測,并可與 CompositeTensor 類型的簽名一起使用。
隆重推出 tf.experimental.numpy
TensorFlow 2.4 以 tf.experimental.numpy 形式,實驗性引入了對 NumPy API 子集的支持。您可借此模塊,運行由 TensorFlow 加速的 NumPy 代碼。由于此 API 基于 TensorFlow 構建而成,因此可支持訪問所有 TensorFlow API,與 TensorFlow 實現無縫互操作,并會通過編譯和自動矢量化開展優化。例如,TensorFlow ND 數組可以與 NumPy 函數進行交互,同樣地,TensorFlow NumPy 函數也可以接受包括 tf.Tensor 和 np.ndarray 在內的不同類型的輸入。
- import tensorflow.experimental.numpy as tnp ```
- # 在輸入流水線中使用 NumPy 代碼
- dataset = tf.data.Dataset.from_tensor_slices(
- tnp.random.randn(1000, 1024)).map(
- lambda z: z.clip(-1,1)).batch(100)
- # 通過 NumPy 代碼計算梯度
- def grad(x, wt):
- with tf.GradientTape() as tape:
- tape.watch(wt)
- output = tnp.dot(x, wt)
- output = tf.sigmoid(output)
- return tape.gradient(tnp.sum(output), wt)
- tf.experimental.numpy
https://tensorflow.google.cn/api_docs/python/tf/experimental/numpy
- NumPy API 實驗性支持
https://github.com/tensorflow/community/blob/master/governance/api-reviews.md#experimental-apis
您可以查閱 TensorFlow 指南上的 NumPy API,了解更多關于使用此 API 的信息。
- TensorFlow 指南上的 NumPy API
https://tensorflow.google.cn/guide/tf_numpy
全新性能分析器工具
TensorFlow Profiler 中的多工作器支持
TensorFlow Profiler 是一套用于評估 TensorFlow 模型訓練性能和資源消耗情況的工具。TensorFlow Profiler 可幫助您了解模型中算子的硬件資源消耗、診斷瓶頸并最終加快訓練速度。
- TensorFlow Profiler
https://tensorflow.google.cn/guide/profiler
之前版本的TensorFlow Profiler 支持監控多 GPU、單主機訓練作業。在現在 2.4 版本中,您可以分析 MultiWorkerMirroredStrategy 訓練作業的性能。例如,您可以使用采樣模型 API 來執行按需分析,并連接到 MultiWorkerMirroredStrategy 工作節點上正在使用的同一服務器端口:
- # 在模型運行之前啟動性能分析器服務器。
- tf.profiler.experimental.server.start(6009)
- # 在此處插入模型代碼……
- # 例如,您的工作器 IP 地址是 10.0.0.2、10.0.0.3、10.0.0.4,然后您
- # 希望執行 2 秒鐘的性能分析。性能分析數據將
- # 保存至 Google Cloud Storage 路徑“your_tb_logdir”。
- tf.profiler.experimental.client.trace(
- 'grpc://10.0.0.2:6009,grpc://10.0.0.3:6009,grpc://10.0.0.4:6009',
- 'gs://your_tb_logdir',
- 2000)
- 采樣模型
https://tensorflow.google.cn/guide/profiler#sampling_mode
或者,您可以通過向 Capture Profile(捕獲分析結果)工具提供工作節點地址來使用 TensorBoard 配置文件插件。
分析完成后,您可以使用新的 Pod Viewer 工具選擇一個訓練步驟,并查閱所有工作節點的分步時間類別細分。
- Pod Viewer 工具
https://tensorflow.google.cn/guide/profiler#pod_viewer
有關如何使用 TensorFlow Profiler 的更多信息,請查閱新發布的 GPU 性能指南。此指南介紹了您在對模型訓練作業進行性能分析時可能遇到的常見情況,并提供了調試工作流程來幫助您優化性能,無論您是使用單個 GPU、多個 GPU 還是使用多臺機器進行訓練,均可從中受益。
- GPU 性能指南
https://tensorflow.google.cn/guide/gpu_performance_analysis
TFLite Profiler
在 2.4 版本中,您亦可在 Android 中啟用對 TFLite 內部結構的跟蹤。現在,您可以使用 Android 版 TFLite Profiler 來識別性能瓶頸。TFLite 性能評估指南介紹了如何使用 Android Studio CPU 性能分析器和系統跟蹤應用添加跟蹤事件,啟用 TFLite 跟蹤以及捕獲跟蹤。
使用 Android 系統跟蹤應用進行跟蹤的示例
- TFLite 性能評估指南
https://tensorflow.google.cn/lite/performance/measurement#trace_tensorflow_lite_internals_in_android
提供 GPU 支持的新功能
TensorFlow 2.4 可與 CUDA 11 和 cuDNN 8 一起運行,以支持最新上市的 NVIDIA Ampere GPU 架構。如需了解 CUDA 11 功能的更多信息,請查閱此 NVIDIA 開發者博客。
- NVIDIA 開發者博客
https://developer.nvidia.com/blog/cuda-11-features-revealed/
此外,我們亦會默認在搭載 Ampere 的 GPU 上啟用對 TensorFloat-32 的支持。TensorFloat-32(簡稱為“TF32”)是 NVIDIA Ampere GPU 的一種數學模式,可加快令某些 float32 算子(例如矩陣乘法和卷積)在 Ampere GPU 上的運行速度,但精度降低。如需了解更多信息,請查閱 tf.config.experimental.enable_tensor_float_32_execution 文檔。
- tf.config.experimental.enable_tensor_float_32_execution
https://tensorflow.google.cn/api_docs/python/tf/config/experimental/enable_tensor_float_32_execution