步履不停:TensorFlow 2.4新功能一覽!
TensorFlow 2.4 正式發(fā)布!隨著對分布式訓練和混合精度提供更多支持,加入新的 Numpy 前端及用于監(jiān)控和診斷性能瓶頸的工具,這個版本的亮點在于推出新功能,以及對性能和擴展方面的增強。
tf.distribute 的新增功能
參數(shù)服務器策略
在版本 2.4 中,實驗性引入了 tf.distribute 模塊的支持,可通過 ParameterServerStrategy 和自定義訓練循環(huán)對 Keras 模型進行異步訓練。與 MultiWorkerMirroredStrategy 一樣,ParameterServerStrategy 是一種多工作器數(shù)據(jù)并行策略;但其梯度更新方式為異步執(zhí)行。
- ParameterServerStrategy
https://tensorflow.google.cn/api_docs/python/tf/distribute/experimental/ParameterServerStrategy
參數(shù)服務器訓練集群包含工作節(jié)點和參數(shù)服務器。系統(tǒng)會在參數(shù)服務器上創(chuàng)建變量,然后工作節(jié)點會在每個步驟中進行讀取和更新。變量的讀取和更新會在各工作節(jié)點上獨立進行,同時無需采取任何同步操作。由于工作節(jié)點互不依賴,因此該策略具有工作器容錯的優(yōu)勢,并會在使用搶占式服務器時有所助益。
如要開始使用此策略,請查閱參數(shù)服務器訓練教程。此教程介紹了如何設置 ParameterServerStrategy,并說明了如何使用 ClusterCoordinator 類來創(chuàng)建資源、調度函數(shù)和處理任務失敗。
- 參數(shù)服務器訓練教程
https://tensorflow.google.cn/tutorials/distribute/parameter_server_training
- ClusterCoordinator
多工作節(jié)點鏡像策略
MultiWorkerMirroredStrategy 多工作節(jié)點鏡像策略 已順利度過實驗階段,現(xiàn)已成為穩(wěn)定 API 的組成部分。與單個工作節(jié)點副本 MirroredStrategy 一樣,MultiWorkerMirroredStrategy 通過同步數(shù)據(jù)并行化實現(xiàn)分布式訓練。但利用 MultiWorkerMirroredStrategy,您可以在多臺機器上進行訓練,且每臺機器可以都搭載多個 GPU。
- MultiWorkerMirroredStrategy
https://tensorflow.google.cn/api_docs/python/tf/distribute/MultiWorkerMirroredStrategy
- MirroredStrategy
https://tensorflow.google.cn/api_docs/python/tf/distribute/MirroredStrategy
在同步訓練中,每個工作節(jié)點會在輸入數(shù)據(jù)的不同片段上計算正向和反向傳遞次數(shù),并且在每個步驟結束時匯總梯度。對于這種稱為 All Reduce 的匯總, MultiWorkerMirroredStrategy 會使用集合運算保持變量同步。集合運算是 TensorFlow 圖表中的單個算子,可以根據(jù)硬件、網(wǎng)絡拓撲和張量大小在 TensorFlow 運行時中自動選擇 All Reduce 算法。集合運算還可實現(xiàn)其他集合運算,例如廣播和 All Gather。
- 集合運算
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/collective_ops.py
如要開始使用 MultiWorkerMirroredStrategy,請查閱使用 Keras 進行多工作器訓練教程,該教程已更新了有關數(shù)據(jù)集分片、保存/加載使用分布策略訓練的模型,以及使用 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 已順利度過實驗階段,現(xiàn)已成為穩(wěn)定的 API。大多數(shù) TensorFlow 模型使用的是 float32 dtype;但也存在使用更少內(nèi)存的低精度類型(如 float16)?;旌暇戎冈谕荒P椭型ㄟ^使用 16 位和 32 位浮點類型,以加快訓練速度。該 API 可使模型在 GPU 上性能提高 3 倍,在 TPU 上提高 60%。
- Keras 混合精度 API
https://tensorflow.google.cn/api_docs/python/tf/keras/mixed_precision
如要使用混合精度 API,您必須使用 Keras 層和優(yōu)化工具,但無需使用其他 Keras 類,例如模型或損失。如果您對如何利用此 API 實現(xiàn)性能優(yōu)化頗有興趣,請查閱混合精度教程。
- 混合精度教程
https://tensorflow.google.cn/guide/mixed_precision
優(yōu)化工具
此版本支持重構 tf.keras.optimizers.Optimizer 類,使 model.fit 或自定義訓練循環(huán)的用戶能夠編寫任何適用于優(yōu)化工具的訓練代碼?,F(xiàn)所有內(nèi)置的 tf.keras.optimizer.Optimizer 子類均可支持使用 gradient_transformers 和 gradient_aggregator 參數(shù),您可借此輕松定義自定義梯度轉換。
- tf.keras.optimizers.Optimizer
https://tensorflow.google.cn/api_docs/python/tf/keras/optimizers/Optimizer
通過重構,您現(xiàn)在可以在編寫自定義訓練循環(huán)時將損失張量直接傳遞給 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 和自定義訓練循環(huán)都能擺脫優(yōu)化工具細節(jié)的限制,從而使您無需修改,即可編寫任何適用于優(yōu)化工具的訓練代碼。
函數(shù)式 API 模型構建的內(nèi)部改進
最后,在 Keras 中,TensorFlow 2.4 可支持對 Keras Functional API 內(nèi)部主要結構的重構,從而可降低函數(shù)式模型構建的內(nèi)存消耗并簡化觸發(fā)邏輯。開展此類重構操作還能夠確保 TensorFlowOpLayers 行為可預測,并可與 CompositeTensor 類型的簽名一起使用。
隆重推出 tf.experimental.numpy
TensorFlow 2.4 以 tf.experimental.numpy 形式,實驗性引入了對 NumPy API 子集的支持。您可借此模塊,運行由 TensorFlow 加速的 NumPy 代碼。由于此 API 基于 TensorFlow 構建而成,因此可支持訪問所有 TensorFlow API,與 TensorFlow 實現(xiàn)無縫互操作,并會通過編譯和自動矢量化開展優(yōu)化。例如,TensorFlow ND 數(shù)組可以與 NumPy 函數(shù)進行交互,同樣地,TensorFlow NumPy 函數(shù)也可以接受包括 tf.Tensor 和 np.ndarray 在內(nèi)的不同類型的輸入。
- 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 支持監(jiān)控多 GPU、單主機訓練作業(yè)。在現(xiàn)在 2.4 版本中,您可以分析 MultiWorkerMirroredStrategy 訓練作業(yè)的性能。例如,您可以使用采樣模型 API 來執(zhí)行按需分析,并連接到 MultiWorkerMirroredStrategy 工作節(jié)點上正在使用的同一服務器端口:
- # 在模型運行之前啟動性能分析器服務器。
- tf.profiler.experimental.server.start(6009)
- # 在此處插入模型代碼……
- # 例如,您的工作器 IP 地址是 10.0.0.2、10.0.0.3、10.0.0.4,然后您
- # 希望執(zhí)行 2 秒鐘的性能分析。性能分析數(shù)據(jù)將
- # 保存至 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(捕獲分析結果)工具提供工作節(jié)點地址來使用 TensorBoard 配置文件插件。
分析完成后,您可以使用新的 Pod Viewer 工具選擇一個訓練步驟,并查閱所有工作節(jié)點的分步時間類別細分。
- Pod Viewer 工具
https://tensorflow.google.cn/guide/profiler#pod_viewer
有關如何使用 TensorFlow Profiler 的更多信息,請查閱新發(fā)布的 GPU 性能指南。此指南介紹了您在對模型訓練作業(yè)進行性能分析時可能遇到的常見情況,并提供了調試工作流程來幫助您優(yōu)化性能,無論您是使用單個 GPU、多個 GPU 還是使用多臺機器進行訓練,均可從中受益。
- GPU 性能指南
https://tensorflow.google.cn/guide/gpu_performance_analysis
TFLite Profiler
在 2.4 版本中,您亦可在 Android 中啟用對 TFLite 內(nèi)部結構的跟蹤?,F(xiàn)在,您可以使用 Android 版 TFLite Profiler 來識別性能瓶頸。TFLite 性能評估指南介紹了如何使用 Android Studio CPU 性能分析器和系統(tǒng)跟蹤應用添加跟蹤事件,啟用 TFLite 跟蹤以及捕獲跟蹤。
使用 Android 系統(tǒng)跟蹤應用進行跟蹤的示例
- 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 開發(fā)者博客。
- NVIDIA 開發(fā)者博客
https://developer.nvidia.com/blog/cuda-11-features-revealed/
此外,我們亦會默認在搭載 Ampere 的 GPU 上啟用對 TensorFloat-32 的支持。TensorFloat-32(簡稱為“TF32”)是 NVIDIA Ampere GPU 的一種數(shù)學模式,可加快令某些 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