Skip to content

Eager: Invalid placement of vars/consts depending on their types and not the tf.device #16106

Closed

Description

@kpot

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.

Activity

  1. added
    stat:awaiting response
    on Jan 14, 2018
  2. tensorflowbutler commented on Jan 14, 2018

    @tensorflowbutler
    Member

    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

  3. kpot commented on Jan 14, 2018

    @kpot
    Author

    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

  4. kpot commented on Jan 14, 2018

    @kpot
    Author

    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 like

    tensorflow.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/

  5. asimshankar commented on Jan 15, 2018

    @asimshankar
    Contributor

    @kpot : Thanks for the report.

    This is admittedly confusing, but is "working as intended".
    This is because int32 tensors 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 Reverse operation, 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. Setting tfe.DEVICE_PLACEMENT_SILENT will 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?

  6. kpot commented on Jan 15, 2018

    @kpot
    Author

    @asimshankar Thank you for the detailed explanation!

    I ran into this error while trying to use tf.nn.top_k together with tf.gather to 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.

  7. asimshankar commented on Jan 15, 2018

    @asimshankar
    Contributor

    @kpot - might I suggest alternatives for now?

    Two options:

    • Use int64 indices (which will be placed on GPU)
    import tensorflow as tf
    import tensorflow.contrib.eager as tfe
    tfe.enable_eager_execution()
    
    with tf.device('/gpu:0'):
      large_tensor = tf.constant([1, 2, 3, 4], dtype=tf.float32)
      i64 = tf.constant([1, 2], dtype=tf.int64)
    # All operations run on the GPU and out will be placed on the GPU
    # The next line could be placed inside the "with device" scope,
    # but it can be placed outside as well - since all inputs are on
    # GPU, the op will execute on GPU
    out = tf.gather(large_tensor, tf.nn.top_k(i64, k=2).values)
    • Pay the cost of copying the (hopefully small) indices tensor to GPU
    import tensorflow as tf
    import tensorflow.contrib.eager as tfe
    tfe.enable_eager_execution()
    
    with tf.device('/gpu:0'):
      large_tensor = tf.constant([1, 2, 3, 4], dtype=tf.float32)
      i32 = tf.constant([1, 2], dtype=tf.int32)
    # top_k will run on the CPU, but gather on the GPU,
    # so large_tensor will not move from GPU
    out = tf.gather(large_tensor, tf.nn.top_k(i32, k=2).values.gpu())

    Hope that helps.

  8. kpot commented on Jan 17, 2018

    @kpot
    Author

    @asimshankar Thanks, your example with int64 works great. However, when I modified my earlier example, changing int32 with int64, it failed again (on the same TF 1.5.0-rc1). My code exactly:

    import tensorflow as tf
    import tensorflow.contrib.eager as tfe
    
    tfe.enable_eager_execution()
    
    print('TensorFlow version:', tf.__version__)
    
    with tf.device('/gpu:0'):
        D = tfe.Variable([1, 2, 3], dtype=tf.int64)
        print('Variable D is placed on:', D.device)
    

    it fails with the message

    TensorFlow version: 1.5.0-rc1
    2018-01-17 23:42:46.460016: 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-17 23:42:46.460292: 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-17 23:42:46.460316: 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)
    Traceback (most recent call last):
      File "tf_bug.py", line 9, in <module>
        D = tfe.Variable([1, 2, 3], dtype=tf.int64)
      File "/home/kpot/pyves/ib/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-0/", dtype=DT_INT64, shape=[3], shared_name="2"]()
    	 (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'
     [Op:VarHandleOp] name: Variable/
    

    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 :)

  9. asimshankar commented on Jan 17, 2018

    @asimshankar
    Contributor

    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 the DT_INT64 type 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 int64 variables, 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.

  10. kpot commented on Jan 17, 2018

    @kpot
    Author

    @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.

  11. asimshankar commented on Jan 18, 2018

    @asimshankar
    Contributor

    @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 int64 type here. For other operations (like MatMul) things may be more complicated.

  12. removed
    stat:awaiting response
    on Jan 23, 2018
  13. tensorflowbutler commented on Jan 23, 2018

    @tensorflowbutler
    Member

    The original poster has replied to this issue after the stat:awaiting response label was applied.

  14. kpot commented on Jan 24, 2018

    @kpot
    Author

    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.

  15. asimshankar commented on Jan 24, 2018

    @asimshankar
    Contributor

    @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:

    import tensorflow as tf
    import tensorflow.contrib.eager as tfe
    
    tfe.enable_eager_execution(device_policy=tfe.DEVICE_PLACEMENT_SILENT)
    
    with tf.device('/cpu:0'):
      D = tfe.Variable([1, 2, 3], dtype=tf.int32)

    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)

  16. added a commit that references this issue on Jan 24, 2018
    f7b60fd
  17. xinmei9322 commented on Jan 31, 2018

    @xinmei9322

    I came across the same problem.

  18. rharish101 commented on Dec 9, 2018

    @rharish101

    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 using tf.layers.conv2d in eager with silent device placement and enforcing with tf.device('/gpu:0') fails with:

    NotFoundError: No registered 'VarHandleOp' OpKernel for GPU devices compatible with node {{node VarHandleOp}} = VarHandleOp[container="", dtype=DT_INT32, shape=[3,3,4,64], shared_name="b11/conv2d/kernel_42"]()
    	 (OpKernel was found, but attributes didn't match)
    	.  Registered:  device='GPU'; dtype in [DT_VARIANT]
      device='GPU'; dtype in [DT_INT64]
      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_CPU'
      device='XLA_GPU'
     [Op:VarHandleOp] name: b11/conv2d/kernel/
    
  19. alextp commented on Dec 10, 2018

    @alextp
    Contributor

    @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.

  20. rharish101 commented on Dec 11, 2018

    @rharish101

    @alextp I sort of found out the issue. It was occurring when I was passing an int to the conv2d layer. Looking at the docs, I should have been passing a float. When I explicitly converted the input using tf.to_float before passing to the conv2d layer, it worked.

    However, instead of warning me about int being an unsupported dtype, it gave me this exception. Is this a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions