Repository navigation
Closed
Description
Hi,
I'm currently testing eager execution on TF 1.5.0-rc1 (built it with XLA and CUDA enabled) and observe strange behavior: variables and constants get created either on GPU or CPU depending on their types, and not with tf.device(...): block. Moreover, on creation of int32 variable it fails completely. For example, when I run the following code
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
print('TensorFlow version:', tf.__version__)
with tf.device('/gpu:0'):
A = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
print('Const A is placed on:', A.device)
B = tf.constant([1, 2, 3], dtype=tf.int32)
print('Const B is placed on:', B.device)
C = tfe.Variable([1.0, 2.0, 3.0], dtype=tf.float32)
print('Variable C is placed on:', C.device)
D = tfe.Variable([1, 2, 3], dtype=tf.int32)
print('Variable D is placed on:', D.device)
I get the following output:
TensorFlow version: 1.5.0-rc1
2018-01-14 01:16:06.385929: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:895] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-01-14 01:16:06.386198: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1105] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:01:00.0
totalMemory: 10.91GiB freeMemory: 363.06MiB
2018-01-14 01:16:06.386223: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1195] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
Const A is placed on: /job:localhost/replica:0/task:0/device:GPU:0
Const B is placed on: CPU:0
Variable C is placed on: /job:localhost/replica:0/task:0/device:GPU:0
Traceback (most recent call last):
File "tf_bug.py", line 18, in <module>
D = tfe.Variable([1, 2, 3], dtype=tf.int32)
File "/home/kpot/pyves/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 277, in __init__
constraint=constraint)
File "/home/kpot/pyves/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 422, in _init_from_args
graph_mode=self._in_graph_mode)
File "/home/kpot/pyves/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 53, in _eager_safe_variable_handle
container=container)
File "/home/kpot/pyves/lib/python3.6/site-packages/tensorflow/python/ops/gen_resource_variable_ops.py", line 396, in var_handle_op
attrs=_attrs, ctx=_ctx, name=name)
File "/home/kpot/pyves/lib/python3.6/site-packages/tensorflow/python/eager/execute.py", line 66, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.NotFoundError: No registered 'VarHandleOp' OpKernel for GPU devices compatible with node VarHandleOp = VarHandleOp[container="eager-execution-2/", dtype=DT_INT32, shape=[3], shared_name="11"]()
(OpKernel was found, but attributes didn't match)
. Registered: device='GPU'; dtype in [DT_VARIANT]
device='GPU'; dtype in [DT_COMPLEX128]
device='GPU'; dtype in [DT_COMPLEX64]
device='GPU'; dtype in [DT_BOOL]
device='GPU'; dtype in [DT_DOUBLE]
device='GPU'; dtype in [DT_FLOAT]
device='GPU'; dtype in [DT_HALF]
device='CPU'
device='XLA_GPU'
device='XLA_CPU'
[Op:VarHandleOp] name: Variable/
As you can see, the constants and variables get placed either on GPU:0 or CPU:0 despite all of them gathered inside the same tf.device('/gpu:0') block.
Metadata
Metadata
Assignees
Labels
No labels
Activity
tensorflowbutler commentedon Jan 14, 2018
Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks.
Have I written custom code
OS Platform and Distribution
TensorFlow installed from
Bazel version
CUDA/cuDNN version
GPU model and memory
Exact command to reproduce
kpot commentedon Jan 14, 2018
Have I written custom code: No
OS Platform and Distribution: Ubuntu 16.04.1
TensorFlow installed from: the source code, tag r1.5.0-r1 without custom modifications
Bazel version: 0.7.0
CUDA/cuDNN version: CUDA 9.0/ cuDNN 7.0.5
GPU model and memory: GeForce GTX 1080 Ti, 11Gb RAM
kpot commentedon Jan 14, 2018
Just in case, I've rebuilt TF without enabling XLA (but still with CUDA). No effect. The bug still shows up identically with the same stack trace.
It seems that the bug also has some side effects: some of the functions like tf.reverse and others sometimes won't work with GPU-placed tensors, ignoring
tf.device('/gpu:0')section and throwing exception liketensorflow.python.framework.errors_impl.InvalidArgumentError: Tensors on conflicting devices: cannot compute StridedSlice as input #0 was expected to be on /job:localhost/replica:0/task:0/device:CPU:0 but is actually on /job:localhost/replica:0/task:0/device:GPU:0 (operation running on /job:localhost/replica:0/task:0/device:GPU:0) Tensors can be copied explicitly using .gpu() or .cpu(), or transparently copied by using tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT). Copying tensors between devices may slow down your model [Op:StridedSlice] name: strided_slice/asimshankar commentedon Jan 15, 2018
@kpot : Thanks for the report.
This is admittedly confusing, but is "working as intended".
This is because
int32tensors are placed in host memory. This hack comes from here. You should not observe this behavior with other types.The general idea is that many (though not all) GPU kernels require the tensor to be in host memory when operating on int32 inputs (you noticed this in the
Reverseoperation, and the same holds for many other operations). When working with TensorFlow graphs (as opposed to eager execution), this is still the case, but the runtime copies tensors between devices if needed. Settingtfe.DEVICE_PLACEMENT_SILENTwill achieve the same effect with eager execution. We didn't make automatic copying between devices default for eager execution as that can often lead to hard to diagnose performance problems (and tracing the precise copies that become bottlenecks can be a little involved).This is something we need to clean up (CC @alextp @josh11b @agarwal-ashish @akshayka @allenlavoie ). In the interim, the mental model to work with is that the int32 type is almost always in host memory.
Mind if I ask how you ran into this error? Did you have a need to operate on int32 tensors on GPUs?
kpot commentedon Jan 15, 2018
@asimshankar Thank you for the detailed explanation!
I ran into this error while trying to use
tf.nn.top_ktogether withtf.gatherto collect prioritized slices from a large tensor. Hence the int32 (indices). This is just a thing that my model requires before each training step. And I hoped it would be faster to do everything on the GPU side instead of dragging the data back and forth from CPU.asimshankar commentedon Jan 15, 2018
@kpot - might I suggest alternatives for now?
Two options:
int64indices (which will be placed on GPU)Hope that helps.
kpot commentedon Jan 17, 2018
@asimshankar Thanks, your example with int64 works great. However, when I modified my earlier example, changing
int32withint64, it failed again (on the same TF 1.5.0-rc1). My code exactly:it fails with the message
out of curiosity I've tried various
[u]intN, none of them work as a dtype for the variable. It's not like I currently need such variables, just wanted to be thorough :)asimshankar commentedon Jan 17, 2018
Taking a step back: In general, for any given TensorFlow operation there are multiple implementations (kernels) based on the device and set of types that the kernel implements support for.
For some operations, like
Gather, there are GPU kernels that support int64 types.For other operations, like
MatMul, the int64 GPU kernel is not implemented yet.Variables are created using the
VarHandleOp, and as you noticed, it doesn't implement a GPU kernel for theDT_INT64type yet. However, for this particular op, it seems like registering the int64 type for the kernels here will suffice.Long story short: If you need
int64variables, it should be trivial to add support for them. However, be warned that in general, GPU operations are not integer friendly so you will run into coverage issues there.kpot commentedon Jan 17, 2018
@asimshankar While working with reinforcement learning I discovered that for some problems running on the same GPU not only the neural network, but the whole environment simulation, sometimes gives a big boost in performance (about 10x in my case). So now I often use TF more like "a general computational framework for GPUs" of which deep learning is only a part. And it doesn't disappoint. Hence the need for integer variables and perhaps some other peculiarities. A lot of data to track - a lot of indices. Unfortunately, writing and debugging such simulations can be painful sometimes, and eager execution looks very promising in that respect.
I understand that writing a specialized kernel is a much better way to do such things. But for a rapid prototyping tools like TF are priceless. It doesn't matter if the performance suffers a little here and there if I still can get a decent speedup. Most importantly the things should be doable.
asimshankar commentedon Jan 18, 2018
@kpot - there is no disagreement about TensorFlow being useful as a general purpose library, and we're happy to receive PRs and/or work on addressing any gaps.
For the variable in particular, turns out we don't need to write a new kernel - simply register the existing kernel for the
int64type here. For other operations (likeMatMul) things may be more complicated.tensorflowbutler commentedon Jan 23, 2018
The original poster has replied to this issue after the stat:awaiting response label was applied.
kpot commentedon Jan 24, 2018
Unfortunately there is nothing I can do here myself. I'm not a CUDA/TF developer and don't have the expertise necessary for the task. So for now I simply returned back to classical use of TF via building and executing computation graphs.
Feel free to close the issue if there is nothing that can be done about it at this stage.
asimshankar commentedon Jan 24, 2018
@kpot : As a workaround, can you keep your integer variables on CPU? And furthermore, you can enable silent copying of tensors between CPU and GPU in a way that may cost performance (when the copies happen), but will work. Something like this:
This will be more similar to graph where the variable is actually placed in CPU memory and the contents are transparently transferred to GPU when needed. (See https://www.tensorflow.org/versions/r1.5/api_docs/python/tf/contrib/eager/enable_eager_execution)
Does that work?
(We sincerely appreciate your feedback and your help in exposing these issues so we can iron them out)
Silent copies for int32 tensors in TFE.
xinmei9322 commentedon Jan 31, 2018
I came across the same problem.
rharish101 commentedon Dec 9, 2018
Silent copying does not work for me when using
tfe.enable_eager_execution(device_policy=tfe.DEVICE_PLACEMENT_SILENT)for me. I'm running tensorflow 1.12.0 in Arch Linux.Also, using
with tf.device('/gpu:0')explicitly works for some examples, but not all. For eg. when usingtf.layers.conv2din eager with silent device placement and enforcingwith tf.device('/gpu:0')fails with:alextp commentedon Dec 10, 2018
@rharish101 can you open a new issue with instructions to reproduce?
I don't know why your conv kernel is trying to create a variable with a variant dtype; that is not supported.
rharish101 commentedon Dec 11, 2018
@alextp I sort of found out the issue. It was occurring when I was passing an
intto the conv2d layer. Looking at the docs, I should have been passing afloat. When I explicitly converted the input usingtf.to_floatbefore passing to the conv2d layer, it worked.However, instead of warning me about
intbeing an unsupported dtype, it gave me this exception. Is this a bug?