具有多个 GPU 的 Tensorflow 多个会话

Tensorflow multiple sessions with multiple GPUs

我有一个带 2 个 GPU 的工作站,我正在尝试同时 运行 多个 tensorflow 作业,这样我就可以一次训练多个模型,等等。

例如,我尝试通过 python API 使用 script1.py:

将会话分成不同的资源
with tf.device("/gpu:0"):
    # do stuff

在script2.py中:

with tf.device("/gpu:1"):
    # do stuff

在script3.py

with tf.device("/cpu:0"):
    # do stuff

如果我 运行 每个脚本本身,我可以看到它正在使用指定的设备。 (此外,这些模型非常适合单个 GPU,即使两者都可用也不会使用另一个。)

但是,如果一个脚本是 运行ning 而我尝试 运行 另一个,我总是得到这个错误:

I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 8
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:909] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_init.cc:103] Found device 0 with properties: 
name: GeForce GTX 980
major: 5 minor: 2 memoryClockRate (GHz) 1.2155
pciBusID 0000:01:00.0
Total memory: 4.00GiB
Free memory: 187.65MiB
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:909] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_init.cc:103] Found device 1 with properties: 
name: GeForce GTX 980
major: 5 minor: 2 memoryClockRate (GHz) 1.2155
pciBusID 0000:04:00.0
Total memory: 4.00GiB
Free memory: 221.64MiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:127] DMA: 0 1 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 0:   Y Y 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 1:   Y Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:702] Creating    TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 980, pci bus id: 0000:01:00.0)
I tensorflow/core/common_runtime/gpu/gpu_device.cc:702] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX 980, pci bus id: 0000:04:00.0)
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:42] Allocating 187.40MiB bytes.
E tensorflow/stream_executor/cuda/cuda_driver.cc:932] failed to allocate 187.40M (196505600 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY
F tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:47] Check failed: gpu_mem != nullptr  Could not allocate GPU device memory for device 0. Tried to allocate 187.40MiB
Aborted (core dumped)

似乎每个 tensorflow 进程在加载时都试图获取机器上的所有 GPU,即使并非所有设备都将用于 运行 模型。

我看到有一个选项可以限制每个进程使用的 GPU 数量

tf.GPUOptions(per_process_gpu_memory_fraction=0.5)

...我还没有尝试过,但这似乎会让两个进程尝试共享每个 GPU 的 50%,而不是 运行 在单独的 GPU 上将每个进程...

有谁知道如何配置 tensorflow 以仅使用一个 GPU 而让另一个 GPU 可用于另一个 tensorflow 进程?

TensorFlow 将尝试使用它可见的所有 GPU 设备(等量内存)。如果您想 运行 在不同的 GPU 上进行不同的会话,您应该执行以下操作。

  1. 运行 每个会话在不同的 Python 进程中。
  2. 使用 CUDA_VISIBLE_DEVICES environment variable 的不同值启动每个进程。例如,如果您的脚本名为 my_script.py 并且您有 4 个 GPU,则可以 运行 以下内容:

    $ CUDA_VISIBLE_DEVICES=0 python my_script.py  # Uses GPU 0.
    $ CUDA_VISIBLE_DEVICES=1 python my_script.py  # Uses GPU 1.
    $ CUDA_VISIBLE_DEVICES=2,3 python my_script.py  # Uses GPUs 2 and 3.
    

    请注意,TensorFlow 中的 GPU 设备仍将从零开始编号(即 "/gpu:0" 等),但它们将对应于您使用 CUDA_VISIBLE_DEVICES.[=16 设置为可见的设备=]