Tensorflow-gpu错误:InvalidArgumentError:无法为操作分配设备' MatMul'

时间:2017-11-18 04:44:02

标签: python tensorflow anaconda tensorflow-gpu

我使用tensorflow网站上提供的测试代码测试了anaconda中GPU的张量流:

import tensorflow as tf
with tf.device('/device:GPU:0'):
  a = tf.constant([1,2,3,4,5,6],shape=[2,3],name='a')
  b = tf.constant([1,2,3,4,5,6],shape=[3,2],name='b')
  c = tf.matmul(a,b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

我创建了anaconda环境并使用pip install tensorflow-gpu安装了tensorflow + gpu。 Ipython笔记本用于执行上面的代码并继续得到错误

InvalidArgumentError: Cannot assign a device for operation 'MatMul': Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
     [[Node: MatMul = MatMul[T=DT_INT32, transpose_a=false, transpose_b=false, _device="/device:GPU:0"](a, b)]]

似乎MatMul运算符无法加载到GPU上。我不知道为什么GPU设备没有支持的内核,因为cuda和cudNN已正确安装。否则,tensorflow消息显示gpu被识别:

name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.683
pciBusID 0000:02:00.0
Total memory: 10.91GiB
Free memory: 10.75GiB
2017-11-17 19:12:50.212054: W tensorflow/stream_executor/cuda/cuda_driver.cc:523] A non-primary context 0x55a56f0c2420 exists before initializing the StreamExecutor. We haven't verified StreamExecutor works with that.
2017-11-17 19:12:50.213035: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 1 with properties:
name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.683
pciBusID 0000:82:00.0
Total memory: 10.91GiB
Free memory: 10.75GiB
2017-11-17 19:12:50.213089: I tensorflow/core/common_runtime/gpu/gpu_device.cc:847] Peer access not supported between device ordinals 0 and 1
2017-11-17 19:12:50.213108: I tensorflow/core/common_runtime/gpu/gpu_device.cc:847] Peer access not supported between device ordinals 1 and 0
2017-11-17 19:12:50.213132: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0 1
2017-11-17 19:12:50.213148: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y N
2017-11-17 19:12:50.213156: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 1:   N Y
2017-11-17 19:12:50.213169: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0)
2017-11-17 19:12:50.213179: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0
/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0
2017-11-17 19:12:50.471348: I tensorflow/core/common_runtime/direct_session.cc:300] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0
/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0

有两个gpus,他们俩都遇到了同样的问题。正确安装cuda和cudnn库,并在anaconda中设置环境变量。 cuda示例(deviceQuery)代码能够无错误地编译和运行,并显示result = pass。否则,Matmul可以加载到CPU上并完成计算。程序中的变量ab能够加载到GPU设备上。给出了张量流消息:

2017-11-17 20:27:25.965655: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0)
2017-11-17 20:27:25.965665: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0
/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0
2017-11-17 20:27:26.228395: I tensorflow/core/common_runtime/direct_session.cc:300] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0
/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0

MatMul: (MatMul): /job:localhost/replica:0/task:0/cpu:0
2017-11-17 20:27:26.229489: I tensorflow/core/common_runtime/simple_placer.cc:872] MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const): /job:localhost/replica:0/task:0/gpu:0
2017-11-17 20:27:26.229512: I tensorflow/core/common_runtime/simple_placer.cc:872] b: (Const)/job:localhost/replica:0/task:0/gpu:0
a: (Const): /job:localhost/replica:0/task:0/gpu:0
2017-11-17 20:27:26.229526: I tensorflow/core/common_runtime/simple_placer.cc:872] a: (Const)/job:localhost/replica:0/task:0/gpu:0

我重新安装了nvidia驱动程序,cuda和anaconda几次,但从未解决过这个问题。如果有任何建议会很好。

  • 操作系统平台和发行版:Linux Ubuntu 16.04
  • :二进制
  • 安装的TensorFlow
  • TensorFlow版本:1.3
  • Python版:2.7.14
  • GCC /编译器版本(如果从源代码编译):5.4.0
  • NVIDIA驱动程序:384.98
  • CUDA / cuDNN版本:cuda 8.0 / cuDNN 6.0
  • GPU型号和内存:Geforce 1080Ti

1 个答案:

答案 0 :(得分:0)

您正尝试在GPU上使用tf.int32DT_INT32)数据类型的多个张量。错误消息表示不支持在GPU上乘以DT_INT32个张量。

请注意,网站上的代码使用了浮动张量(tf.float32)(假设您正在讨论https://www.tensorflow.org/tutorials/using_gpu中的代码)

更改:

a = tf.constant([1,2,3,4,5,6],shape=[2,3],name='a')

为:

a = tf.constant([1.,2.,3.,4.,5.,6.],shape=[2,3],name='a')

或:

a = tf.constant([1,2,3,4,5,6],shape=[2,3],name='a',dtype=tf.float32)

类似地,b应该使错误消失,因为确实有内核支持GPU上的float32张量的矩阵乘法。

希望有所帮助。