在Tensorflow代码中指定gpu:/ gpu:0始终有效吗?

我的工作站里有3个显卡,其中一个是Quadro K620,另外两个是Titan X.现在我想在其中一个显卡上运行我的张量流代码,这样我就可以让其他显卡闲置到另一个任务.

但是,无论设置tf.device(‘/ gpu:0’)还是设置tf.device(‘/ gpu:1’),我发现第一个Titan X显卡总是在工作,我不知道为什么.

import argparse
import os
import time
import tensorflow as tf
import numpy as np
import cv2

from Dataset import Dataset
from Net import Net

FLAGS = None

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--foldername', type=str, default='./data-large/')
    parser.add_argument('--batch_size', type=int, default=100)
    parser.add_argument('--num_epoches', type=int, default=100)
    parser.add_argument('--learning_rate', type=float, default=0.5)

    FLAGS = parser.parse_args()
    net = Net(FLAGS.batch_size, FLAGS.learning_rate)

    with tf.Graph().as_default():
        # Dataset is a class for encapsulate the input pipeline
        dataset = Dataset(foldername=FLAGS.foldername,
                              batch_size=FLAGS.batch_size,
                              num_epoches=FLAGS.num_epoches)

        images, labels = dataset.samples_train

        ## The following code defines the network and train
        with tf.device('/gpu:0'): # <==== THIS LINE
            logits = net.inference(images)
            loss = net.loss(logits, labels)
            train_op = net.training(loss)

            init_op = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables())
            sess = tf.Session()
            sess.run(init_op)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start_time = time.time()
            try:
                step = 0
                while not coord.should_stop():
                    _, loss_value = sess.run([train_op, loss])
                    step = step + 1
                    if step % 100 == 0:
                        format_str = ('step %d, loss = %.2f, time: %.2f seconds')
                        print(format_str % (step, loss_value, (time.time() - start_time)))
                        start_time = time.time()
            except tf.errors.OutOfRangeError:
                print('done')
            finally:
                coord.request_stop()

            coord.join(threads)
            sess.close()

关于“< === THIS LINE:”这一行 如果我设置tf.device(‘/ gpu:0’),监视器会说:

|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Quadro K620         Off  | 0000:03:00.0      On |                  N/A |
| 34%   45C    P0     2W /  30W |    404MiB /  1993MiB |      5%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX TIT...  Off  | 0000:04:00.0     Off |                  N/A |
| 22%   39C    P2   100W / 250W |  11691MiB / 12206MiB |      8%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX TIT...  Off  | 0000:81:00.0     Off |                  N/A |
| 22%   43C    P2    71W / 250W |    111MiB / 12206MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

显示第一张Titan X卡正在工作.

如果我设置tf.device(‘/ gpu:1’),监视器会说:

|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Quadro K620         Off  | 0000:03:00.0      On |                  N/A |
| 34%   45C    P0     2W /  30W |    411MiB /  1993MiB |      3%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX TIT...  Off  | 0000:04:00.0     Off |                  N/A |
| 22%   52C    P2    73W / 250W |  11628MiB / 12206MiB |     12%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX TIT...  Off  | 0000:81:00.0     Off |                  N/A |
| 22%   42C    P2    71W / 250W |  11628MiB / 12206MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

显示两张Titan X卡正在工作,而不是单独的第二张Titan X.

那么这背后的任何原因以及如何指定我希望我的程序运行的gpu?

最佳答案 只是一个猜测,但是当你调用
minimize()时,
tf.train.Optimizer对象的默认行为(我期望在net.training(loss)中创建)是colocate_gradients_with_ops = False.这可能导致反向传播操作放在默认设备上,这将是/ gpu:0.

要弄清楚是否发生这种情况,你可以迭代sess.graph_def并查找在NodeDef.device字段中有/ gpu:0的节点,或者有一个空的设备字段(在这种情况下,它们将放在/ gpu上) :默认为0).

检查正在使用的设备的另一个选项是在运行步骤时使用output_partition_graphs=True option.这显示了TensorFlow实际使用的设备(而不是在sess.graph_def中,您的程序正在请求的设备),并且应该准确显示/ gpu:0上正在运行的节点.

点赞