[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RaggedTensor slice gradient computation error #59727

Open
cbreak-black opened this issue Feb 17, 2023 · 3 comments
Open

RaggedTensor slice gradient computation error #59727

cbreak-black opened this issue Feb 17, 2023 · 3 comments
Assignees
Labels
comp:ops OPs related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.10 type:bug Bug

Comments

@cbreak-black
Copy link
cbreak-black commented Feb 17, 2023
Click to expand!

Issue Type

Bug

Have you reproduced the bug with TF nightly?

No

Source

source

Tensorflow Version

2.10

Custom Code

Yes

OS Platform and Distribution

Ubuntu 22.04

Mobile device

No response

Python version

3.10

Bazel version

No response

GCC/Compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current Behaviour?

Attempting to compute the gradient, both as part of an autographed keras layer, or in eager mode, fails. This seems to happen when concatenating different slices of a ragged tensor.

Standalone code to reproduce the issue

https://colab.research.google.com/drive/1kteIaQeDouRH-DEEYYeGE9jiMtgcXUZm#scrollTo=MOhPQNf4JDBB


import tensorflow as tf

values = tf.constant([0, 1,2,3,4,5,6,7,8,9], tf.float32)
values = tf.reshape(values, [-1, 1])

r = tf.RaggedTensor.from_row_lengths(values, [0, 2, 2, 1, 0, 2, 3, 0, 0])
r = tf.RaggedTensor.from_uniform_row_length(r, 3)
r = tf.RaggedTensor.from_uniform_row_length(r, 3)

def crop(raggedImage: tf.RaggedTensor, top, bottom, left, right) -> tf.RaggedTensor:
    '''
    Crops a ragged tensor, removing 'pixels' from the boundary.
    The input is interpreted as being b x h x w x s x c layout.
    Only the sample dimension may be ragged.
    '''
    if bottom > 0:
        bottom = -bottom
    else:
        bottom = None
    if right > 0:
        right = -right
    else:
        right = None
    cropped = raggedImage[:, top : bottom, left : right, :, :]
    return cropped

diameter = 3

with tf.GradientTape() as tape:
	tape.watch(r)
	l = []
	for u in range(diameter):
		for v in range(diameter):
			l.append(crop(r, u, diameter-1-u, v, diameter-1-v))
	s = tf.concat(l, axis=2)

tf.print(tape.gradient(s, [r]))

Relevant log output

Traceback (most recent call last):
  File "venv-2.10/lib/python3.10/site-packages/tensorflow/python/eager/backprop.py", line 663, in _num_elements
    shape_tuple = grad.values._shape_tuple()  # pylint: disable=protected-access
AttributeError: 'IndexedSlices' object has no attribute '_shape_tuple'
@google-ml-butler google-ml-butler bot added the type:bug Bug label Feb 17, 2023
@synandi synandi added TF 2.10 comp:ops OPs related issues labels Feb 20, 2023
@synandi
Copy link
Contributor
synandi commented Feb 23, 2023

@tilakrayal
I was able to replicate the issue in Ubuntu and colab. Please find the gist here for TF v2.10 and here for tf-nightly. Thank you!

@synandi synandi assigned tilakrayal and unassigned synandi Feb 23, 2023
@SuryanarayanaY
Copy link
Collaborator

@cbreak-black ,Thanks for reporting this.

The error is raised from below code.

elif isinstance(grad, indexed_slices.IndexedSlices):
  shape_tuple = grad.values._shape_tuple()  # pylint: disable=protected-access

from source:

def _num_elements(grad):
"""The number of elements in the `grad` tensor."""
if isinstance(grad, ops.Tensor):
shape_tuple = grad._shape_tuple() # pylint: disable=protected-access
elif isinstance(grad, indexed_slices.IndexedSlices):
shape_tuple = grad.values._shape_tuple() # pylint: disable=protected-access
else:
raise ValueError("`grad` not a Tensor or IndexedSlices.")
if shape_tuple is None or None in shape_tuple:
return 0
return functools.reduce(operator.mul, shape_tuple, 1)

The code indicates grad is of indexed_slices.IndexedSlices and grad.values should return a Tensor as per IndexedSlices API.

If the reported code snippet failing then the below code from above code block also may fail which needs to be checked.

if isinstance(grad, ops.Tensor):
  shape_tuple = grad._shape_tuple() 

It may need to be digged more for root cause. Thanks!

@SuryanarayanaY SuryanarayanaY added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Feb 28, 2023
@cbreak-black
Copy link
Author

Hi, as far as I was able to find with my limited knowledge of tensorflow's internals is that grad.values is itself an IndexedSlices typed object. I did not find out if this is expected (apparently not), and how this came to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.10 type:bug Bug
Projects
None yet
Development

No branches or pull requests

4 participants