Why does setting an initialization value prevent placing a variable on a GPU in TensorFlow?

北城余情 提交于 2019-12-12 19:51:06

问题


I get an exception when I try to run the following very simple TensorFlow code, although I virtually copied it from the documentation:

import tensorflow as tf

with tf.device("/gpu:0"):
  x = tf.Variable(0, name="x")

sess = tf.Session()
sess.run(x.initializer) # Bombs!

The exception is:

tensorflow.python.framework.errors.InvalidArgumentError: Cannot assign a device to
node 'x': Could not satisfy explicit device specification '/device:GPU:0' because
no supported kernel for GPU devices is available.

If I change the variable's initial value to tf.zeros([1]) instead, everything works fine:

import tensorflow as tf

with tf.device("/gpu:0"):
  x = tf.Variable(tf.zeros([1]), name="x")

sess = tf.Session()     
sess.run(x.initializer)  # Works fine

Any idea what's going on?


回答1:


This error arises because tf.Variable(0, ...) defines a variable of element type tf.int32, and there is no kernel that implements int32 variables on GPU in the standard TensorFlow distribution. When you use tf.Variable(tf.zeros([1])), you're defining a variable of element type tf.float32, which is supported on GPU.

The story of tf.int32 on GPUs in TensorFlow is a long one. While it's technically easy to support integer operations running on a GPU, our experience has been that most integer operations actually take place on the metadata of tensors, and this metadata lives on the CPU, so it's more efficient to operate on it there. As a short-term workaround, several kernel registrations for int32 on GPUs were removed. However, if these would be useful for your models, it would be possible to add them as custom ops.


Source: In TensorFlow 0.10, the Variable-related kernels are registered using the TF_CALL_GPU_NUMBER_TYPES() macro. The current "GPU number types" are tf.float16, tf.float32, and tf.float64.



来源:https://stackoverflow.com/questions/39133312/why-does-setting-an-initialization-value-prevent-placing-a-variable-on-a-gpu-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!