TensorFlow 2.4 的革新包括对于分布式训练和混合精度的新功能反对,对 NumPy API 子集的试验性反对以及一些用于监测本能瓶颈的新工具。
根据 TensorFlow 官方博客,本次革新的主要内容整理如下:
tf.distribute 中的新功能
参数服务器策略
在 TensorFlow 2.4 中,tf.distribute 模块引入了对利用 ParameterServerStrategy 和自定义训练循环进行模型异步训练的试验性反对。和 MultiWorkerMirroredStrategy 类似,ParameterServerStrategy 是一种多职责器(multi-worker)数据并行策略,但梯度革新是异步的。
参数服务器训练集群由职责服务器和参数服务器组成。变量在参数服务器上创建,然后在每个步骤中由职责器读取和革新。变量的读取和革新在职责器之间是独立进行的,不存在任何同步。由于职责器彼此不依赖,因此该策略具有职责器容错的优势,如果利用可抢占 VM,该策略将很有用。
多职责器镜像策略
MultiWorkerMirroredStrategy 已经脱离试验阶段,成为稳定的 API。像单职责器的 MirroredStrategy 一样,MultiWorkerMirroredStrategy 通过同步数据并行实现分布式训练,顾名思义,借助 MultiWorkerMirroredStrategy 可以在多台机器上进行训练,每台机器都可能具有多个 GPU。
Keras 革新
混合精度
在 TensorFlow 2.4 中,Keras 混合精度 API 已经脱离试验阶段,成为稳定的 API。大多数 TensorFlow 模型利用 float32 dtype,但现在有些低精度数据类型占用的内存更少,比如 float16。混合精度指的是在同一模型中利用 16 位和 32 位浮点数以进行更快的训练。这一 API 可将模型本能在 GPU 上提高到 3 倍,在 TPU 上提高 60%。要利用混合精度 API,必须利用 Keras 层和优化器,但不一定需要利用其他 Keras 类。
优化器
本次革新包括重构 tf.keras.optimizers.Optimizer 类,让 model.fit 的用户和自定义训练循环的用户能够编写可与任何优化器一起利用的训练代码。所有内置 tf.keras.optimizer.Optimizer 子类都可接受 gradient_transformers 和 gradient_aggregator 参数,轻松定义自定义梯度变换。
重构之后,利用者可以在编写自定义训练循环时直接将损失张量传递给 Optimizer.minimize:
tape = tf.GradientTape()with tape: y_pred = model(x, training=True) loss = loss_fn(y_pred, y_true)# You can pass in the `tf.GradientTape` when using a loss `Tensor` as shown below.optimizer.minimize(loss, model.trainable_variables, tape=tape)
这些革新的目标是让 Model.fit 和自定义训练循环与优化器细节更加不相关,从而让利用者无需修改即可编写出与任何优化器共同利用的训练代码。
最后,TensorFlow 2.4 的革新还包括 Keras Functional API 内部的重构,改善了函数式模型构造所产生的内存消耗并简化了触发逻辑。这种重构可以保证 TensorFlowOpLayers 的行为可预测,并且可以利用 CompositeTensor 类型签名(type signature)。
tf.experimental.numpy
TensorFlow 2.4 引入了对 NumPy API 子集的试验性反对。该模块可以运行由 TensorFlow 加速的 NumPy 代码,由于这一 API 是基于 TensorFlow 构建的,因此可与 TensorFlow 无缝衔接,允许访问所有 TensorFlow API 并通过编译和自动矢量化提供优化后的运行。
例如,TensorFlow ND 数组可以与 NumPy 函数互通,类似地,TensorFlow NumPy 函数可以接受包括 tf.Tensor 和 np.ndarray 在内的不同类型输入。
import tensorflow.experimental.numpy as tnp# Use NumPy code in input pipelinesdataset = tf.data.Dataset.from_tensor_slices( tnp.random.randn(1000, 1024)).map(lambda z: z.clip(-1,1)).batch(100)# Compute gradients through NumPy codedef 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)
新的本能分析工具
TensorFlow Profiler 是度量 TensorFlow 模型的训练本能和资源消耗情况的工具,用来诊断本能瓶颈,最终加快训练速度。
此前,TensorFlow Profiler 反对多 GPU 单主机训练。到了 2.4 版本,利用者可以测试 MultiWorkerMirroredStrategy 的训练职责了,比如利用采样模式 API 按需配置,并连接到 MultiWorkerMirroredStrategy 职责器正在利用的同一服务器。
# Start a profiler server before your model runs.tf.profiler.experimental.server.start(6009)# Model code goes here….# E.g. your worker IP addresses are 10.0.0.2, 10.0.0.3, 10.0.0.4, and you# would like to profile for a duration of 2 seconds. The profiling data will# be saved to the Google Cloud Storage path “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)
另外,你可以通过向捕获配置文件工具提供职责器地址来利用 TensorBoard 配置文件插件。配置之后,你可以利用新的 Pod Viewer tool 来选择训练步骤,并查看所有职责器上该步骤的 step-time 的细分。
TFLite Profiler
TFLite Profiler 则反对在 Android 中跟踪 TFLite 内部信息,以识别本能瓶颈。
GPU 反对
TensorFlow 2.4 与 CUDA 11 和 cuDNN 8 配合运行,反对最新发布的英伟达安培架构 GPU,对于 CUDA 11 的新特性,可以参考英伟达开发者博客:
在新版本中,默认情况下会启用安培 GPU 的新特性——对 TensorFloat-32 的反对。TensorFloat-32 又简称为 TF32,是英伟达 Ampere GPU 的一种数学精度模式,可导致某些 float32 运算(如矩阵乘法和卷积)在安培架构 GPU 上运行得更快,但精度略有降低。
若想更深入了解,可参阅文档:
https://www.tensorflow.org/api_docs/python/tf/config/experimental/enable_tensor_float_32_execution
原创文章,作者:机器之心,如若转载,请注明出处:https://www.iaiol.com/news/tensorflow24-lai-le-shang-xian-dui-fen-bu-shi-xun-lian-he/