首页 > 解决方案 > 没有 NCCL 的 MirroredStrategy

问题描述

我想使用 MirroredStrategy 在同一台机器上使用多个 GPU。我尝试了其中一个示例: https ://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/distribute/python/examples/simple_tfkeras_example.py

结果是:ValueError:操作类型未在 RAID 上运行的二进制文件中注册“NcclAllReduce”。确保在此进程中运行的二进制文件中注册了 Op 和 Kernel。在构建 NodeDef 'NcclAllReduce' 时

我使用的是 Windows,因此 Nccl 不可用。是否可以强制 TensorFlow 不使用该库?

标签: tensorflow

解决方案


Windows 上有一些用于 NCCL 的二进制文件,但处理起来很烦人。

作为替代方案,Tensorflow 在 MirroredStrategy 中为您提供了与 Windows 原生兼容的其他三个选项。它们是分层复制、归约到第一个 GPU 和归约到 CPU。您最有可能寻找的是分层副本,但您可以测试它们中的每一个,看看什么能给您带来最好的结果。

如果您使用 tensorflow 2.0 之前的版本,您将使用 tf.contrib.distribute:

# Hierarchical Copy
cross_tower_ops = tf.contrib.distribute.AllReduceCrossTowerOps(
        'hierarchical_copy', num_packs=number_of_gpus))
    strategy = tf.contrib.distribute.MirroredStrategy(cross_tower_ops=cross_tower_ops)

# Reduce to First GPU
cross_tower_ops = tf.contrib.distribute. ReductionToOneDeviceCrossTowerOps()
strategy = tf.contrib.distribute.MirroredStrategy(cross_tower_ops=cross_tower_ops)

# Reduce to CPU
cross_tower_ops = tf.contrib.distribute. ReductionToOneDeviceCrossTowerOps(
    reduce_to_device="/device:CPU:0")
strategy = tf.contrib.distribute.MirroredStrategy(cross_tower_ops=cross_tower_ops)

2.0以后只需要使用tf.distribute即可!下面是一个使用 2 个 GPU 设置 Xception 模型的示例:

strategy = tf.distribute.MirroredStrategy(devices=["/gpu:0", "/gpu:1"], 
                                          cross_device_ops=tf.distribute.HierarchicalCopyAllReduce())
with strategy.scope():
    parallel_model = Xception(weights=None,
                              input_shape=(299, 299, 3),
                              classes=number_of_classes)
    parallel_model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

推荐阅读