分布在多个GPU的硬件模型

问题描述 投票:0回答:3

我试图创建一个非常大的Keras模型和跨多个GPU分发。需要明确的是我并不想穿上多GPU型号相同的多个副本;我试图把在多个GPU的一个大模型。我已经使用Keras的multi_gpu_model功能已经关闭,但基于很多的内存不足错误的我,而这样做就好像它只是复制模型,而不是分发它像我希望得到。

我看着Horovod而是因为我有很多窗户特定测井仪器运行的我不敢随便使用它。

这似乎离开我只使用tf.estimators。它不是从文件明确,虽然我将如何使用这些估计做我想要做的事。例如其分销策略在tf.contrib.distribute,让我能够有效批出我期待的方式做模型?

就是我寻求与估计可能的,如果是哪种策略,我应该用怎么办?

python tensorflow keras
3个回答
1
投票

您可以使用API​​估计。使用tf.keras.estimator.model_to_estimator转换模型

session_config = tf.ConfigProto(allow_soft_placement=True)
distribute = tf.contrib.distribute.MirroredStrategy(num_gpus=4)
run_config = tf.estimator.RunConfig(train_distribute=distribute)
your_network = tf.keras.estimator.model_to_estimator(model_fn=your_keras_model, config=run_config)
your_network.train(input_fn)

不要忘了编译模型


0
投票

您可以手动指定您Keras模型的不同部分使用TensorFlow后端不同的GPU。 This guide提供了详细的实施例和this article解释了使用Keras与TensorFlow。

import tensorflow as tf

with tf.device("/device:GPU:0"):
    #Create first part of your neural network

with tf.device("/device:GPU:1"):
    #Create second part of your neural network

#...

with tf.device("/device:GPU:n"):
    #Create nth part of your neural network

当心:CPU和多GPU之间的通信延迟可能会增加巨大开销的训练。


0
投票

您需要设备的并行性。在Keras常见问题的This section提供了一个例子,如何用Keras做到这一点:

# Model where a shared LSTM is used to encode two different sequences in parallel
input_a = keras.Input(shape=(140, 256))
input_b = keras.Input(shape=(140, 256))

shared_lstm = keras.layers.LSTM(64)

# Process the first sequence on one GPU
with tf.device_scope('/gpu:0'):
    encoded_a = shared_lstm(tweet_a)
# Process the next sequence on another GPU
with tf.device_scope('/gpu:1'):
    encoded_b = shared_lstm(tweet_b)

# Concatenate results on CPU
with tf.device_scope('/cpu:0'):
    merged_vector = keras.layers.concatenate([encoded_a, encoded_b],
                                             axis=-1)
© www.soinside.com 2019 - 2024. All rights reserved.