python - 如何检查 tensorflow 是否正在使用所有可用的 GPU

标签 python tensorflow gpu

我正在学习使用 Tensorflow 进行对象检测。为了加快训练过程,我采用了一个具有 4 个 GPU 的 AWS g3.16xlarge 实例。我正在使用以下代码来运行训练过程:

export CUDA_VISIBLE_DEVICES=0,1,2,3
 python object_detection/train.py --logtostderr --pipeline_config_path=/home/ubuntu/builder/rcnn.config --train_dir=/home/ubuntu/builder/experiments/training/

在 rcnn.config 中 - 我设置了 batch-size = 1。在运行时,我得到以下输出:

控制台输出

2018-11-09 07:25:50.104310: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Device peer to peer matrix
2018-11-09 07:25:50.104385: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1051] DMA: 0 1 2 3 
2018-11-09 07:25:50.104395: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] 0:   Y N N N 
2018-11-09 07:25:50.104402: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] 1:   N Y N N 
2018-11-09 07:25:50.104409: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] 2:   N N Y N 
2018-11-09 07:25:50.104416: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] 3:   N N N Y 
2018-11-09 07:25:50.104429: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla M60, pci bus id: 0000:00:1b.0, compute capability: 5.2)
2018-11-09 07:25:50.104439: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:1) -> (device: 1, name: Tesla M60, pci bus id: 0000:00:1c.0, compute capability: 5.2)
2018-11-09 07:25:50.104446: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:2) -> (device: 2, name: Tesla M60, pci bus id: 0000:00:1d.0, compute capability: 5.2)
2018-11-09 07:25:50.104455: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:3) -> (device: 3, name: Tesla M60, pci bus id: 0000:00:1e.0, compute capability: 5.2)

当我运行 nvidia-smi 时,我得到以下输出: nvidia-smi 输出

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.26                 Driver Version: 375.26                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla M60           Off  | 0000:00:1B.0     Off |                    0 |
| N/A   52C    P0   129W / 150W |   7382MiB /  7612MiB |     92%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla M60           Off  | 0000:00:1C.0     Off |                    0 |
| N/A   33C    P0    38W / 150W |   7237MiB /  7612MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla M60           Off  | 0000:00:1D.0     Off |                    0 |
| N/A   40C    P0    38W / 150W |   7237MiB /  7612MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla M60           Off  | 0000:00:1E.0     Off |                    0 |
| N/A   34C    P0    39W / 150W |   7237MiB /  7612MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0     97860    C   python                                        7378MiB |
|    1     97860    C   python                                        7233MiB |
|    2     97860    C   python                                        7233MiB |
|    3     97860    C   python                                        7233MiB |
+-----------------------------------------------------------------------------+

**nvidia-smi dmon**提供以下输出:

# gpu   pwr  temp    sm   mem   enc   dec  mclk  pclk
# Idx     W     C     %     %     %     %   MHz   MHz
    0   158    69    90    69     0     0  2505  1177
    1    38    36     0     0     0     0  2505   556
    2    38    45     0     0     0     0  2505   556
    3    39    37     0     0     0     0  2505   556

我对每个输出感到困惑。当我在程序识别 4 个不同 GPU 的可用性时读取控制台输出时,在 nvidia-smi 输出中,仅第一个 GPU 显示不稳定的 GPU-Util 百分比,其余为零。然而,同一张表在底部打印了所有 4 个 gpu 的内存使用情况。并且 nvidia-smi dmon 仅打印第一个 gpu 的 sm 值,而其他 gpu 为零。从这个blog我知道 dmon 中的零表示 GPU 空闲。

我想了解的是,train.py 是否利用了我实例中的所有 4 个 GPU。如果它没有利用所有 GPU,我如何确保 tensorflow 的 object_detection/train.py 针对所有 GPU 进行了优化。

最佳答案

检查它是否返回所有 GPU 的列表。

tf.test.gpu_device_name()

返回 GPU 设备的名称(如果可用)或空字符串。

然后您可以执行类似的操作以使用所有可用的 GPU。

# Creates a graph.
c = []
for d in ['/device:GPU:2', '/device:GPU:3']:
  with tf.device(d):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
    c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
  sum = tf.add_n(c)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(sum))

你看到下面的输出:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K20m, pci bus
id: 0000:02:00.0
/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: Tesla K20m, pci bus
id: 0000:03:00.0
/job:localhost/replica:0/task:0/device:GPU:2 -> device: 2, name: Tesla K20m, pci bus
id: 0000:83:00.0
/job:localhost/replica:0/task:0/device:GPU:3 -> device: 3, name: Tesla K20m, pci bus
id: 0000:84:00.0
Const_3: /job:localhost/replica:0/task:0/device:GPU:3
Const_2: /job:localhost/replica:0/task:0/device:GPU:3
MatMul_1: /job:localhost/replica:0/task:0/device:GPU:3
Const_1: /job:localhost/replica:0/task:0/device:GPU:2
Const: /job:localhost/replica:0/task:0/device:GPU:2
MatMul: /job:localhost/replica:0/task:0/device:GPU:2
AddN: /job:localhost/replica:0/task:0/cpu:0
[[  44.   56.]
 [  98.  128.]]

关于python - 如何检查 tensorflow 是否正在使用所有可用的 GPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53221523/

相关文章:

python - 使用 MatplotLib python 的交互式堆积条形图

当命令尚未完成时Python os.system()自动退出

python - 你如何接受 Python Bottle 服务器中的任何 URL?

python - 使用 Tensorflow/Keras 的神经网络中的负二项式损失

c++ - OpenCL 找不到 ATI 卡

python - 如何正确使用 PyDev 和两个不同的 Python 版本以及调用其他 python 脚本的脚本?

python - 使用 Tensorflow 对象检测的推理时间

python - 如何使用 Google Colab 安装 vizdoom?

c++ - OpenCV getMinFilter_GPU

ubuntu - 使用 caffe 运行神经网络时出错