1. 程式人生 > >TensorFlow在指定CPU/GPU上執行的方法

TensorFlow在指定CPU/GPU上執行的方法

轉自:http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/using_gpu.html

使用 GPUs

支援的裝置

在一套標準的系統上通常有多個計算裝置. TensorFlow 支援 CPU 和 GPU 這兩種裝置. 我們用指定字串 strings 來標識這些裝置. 比如:

  • "/cpu:0": 機器中的 CPU
  • "/gpu:0": 機器中的 GPU, 如果你有一個的話.
  • "/gpu:1": 機器中的第二個 GPU, 以此類推…

如果一個 TensorFlow 的 operation 中兼有 CPU 和 GPU 的實現, 當這個運算元被指派裝置時, GPU 有優先權. 比如matmul

中 CPU 和 GPU kernel 函式都存在. 那麼在 cpu:0 和 gpu:0 中, matmul operation 會被指派給 gpu:0 .

記錄裝置指派情況

為了獲取你的 operations 和 Tensor 被指派到哪個裝置上執行, 用 log_device_placement 新建一個 session, 並設定為 True.

  1. # 新建一個 graph.
  2. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name=‘a’)
  3. b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3
    , 2], name=‘b’)
  4. c = tf.matmul(a, b)
  5. # 新建session with log_device_placement並設定為True.
  6. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
  7. # 執行這個 op.
  8. print sess.run(c)

你應該能看見以下輸出:

  1. Device mapping:
  2. /job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
  3. id: 0000:05:00.0
  4. b: /job:localhost/replica:0
    /task:0/gpu:0
  5. a: /job:localhost/replica:0/task:0/gpu:0
  6. MatMul: /job:localhost/replica:0/task:0/gpu:0
  7. [[ 22. 28.]
  8. [ 49. 64.]]

手工指派裝置

如果你不想使用系統來為 operation 指派裝置, 而是手工指派裝置, 你可以用 with tf.device 建立一個裝置環境, 這個環境下的 operation 都統一執行在環境指定的裝置上.

  1. # 新建一個graph.
  2. with tf.device('/cpu:0'):
  3. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  4. b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  5. c = tf.matmul(a, b)
  6. # 新建session with log_device_placement並設定為True.
  7. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
  8. # 執行這個op.
  9. print sess.run(c)

你會發現現在 a 和 b 操作都被指派給了 cpu:0.

  1. Device mapping:
  2. /job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
  3. id: 0000:05:00.0
  4. b: /job:localhost/replica:0/task:0/cpu:0
  5. a: /job:localhost/replica:0/task:0/cpu:0
  6. MatMul: /job:localhost/replica:0/task:0/gpu:0
  7. [[ 22. 28.]
  8. [ 49. 64.]]

在多GPU系統裡使用單一GPU

如果你的系統裡有多個 GPU, 那麼 ID 最小的 GPU 會預設使用. 如果你想用別的 GPU, 可以用下面的方法顯式的宣告你的偏好:

  1. # 新建一個 graph.
  2. with tf.device('/gpu:2'):
  3. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  4. b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  5. c = tf.matmul(a, b)
  6. # 新建 session with log_device_placement 並設定為 True.
  7. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
  8. # 執行這個 op.
  9. print sess.run(c)

如果你指定的裝置不存在, 你會收到 InvalidArgumentError 錯誤提示:

  1. InvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':
  2. Could not satisfy explicit device specification '/gpu:2'
  3. [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]
  4. values: 1 2 3...>, _device="/gpu:2"]()]]

為了避免出現你指定的裝置不存在這種情況, 你可以在建立的 session 裡把引數 allow_soft_placement 設定為 True, 這樣 tensorFlow 會自動選擇一個存在並且支援的裝置來執行 operation.

  1. # 新建一個 graph.
  2. with tf.device('/gpu:2'):
  3. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  4. b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  5. c = tf.matmul(a, b)
  6. # 新建 session with log_device_placement 並設定為 True.
  7. sess = tf.Session(config=tf.ConfigProto(
  8. allow_soft_placement=True, log_device_placement=True))
  9. # 執行這個 op.
  10. print sess.run(c)

使用多個 GPU

如果你想讓 TensorFlow 在多個 GPU 上執行, 你可以建立 multi-tower 結構, 在這個結構 裡每個 tower 分別被指配給不同的 GPU 執行. 比如:

  1. # 新建一個 graph.
  2. c = []
  3. for d in ['/gpu:2', '/gpu:3']:
  4. with tf.device(d):
  5. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
  6. b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
  7. c.append(tf.matmul(a, b))
  8. with tf.device('/cpu:0'):
  9. sum = tf.add_n(c)
  10. # 新建session with log_device_placement並設定為True.
  11. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
  12. # 執行這個op.
  13. print sess.run(sum)

你會看到如下輸出:

  1. Device mapping:
  2. /job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K20m, pci bus
  3. id: 0000:02:00.0
  4. /job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: Tesla K20m, pci bus
  5. id: 0000:03:00.0
  6. /job:localhost/replica:0/task:0/gpu:2 -> device: 2, name: Tesla K20m, pci bus
  7. id: 0000:83:00.0
  8. /job:localhost/replica:0/task:0/gpu:3 -> device: 3, name: Tesla K20m, pci bus
  9. id: 0000:84:00.0
  10. Const_3: /job:localhost/replica:0/task:0/gpu:3
  11. Const_2: /job:localhost/replica:0/task:0/gpu:3
  12. MatMul_1: /job:localhost/replica:0/task:0/gpu:3
  13. Const_1: /job:localhost/replica:0/task:0/gpu:2
  14. Const: /job:localhost/replica:0/task:0/gpu:2
  15. MatMul: /job:localhost/replica:0/task:0/gpu:2
  16. AddN: /job:localhost/replica:0/task:0/cpu:0
  17. [[ 44. 56.]
  18. [ 98. 128.]]