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
Tensor size-related conditional logic ignored in tf.function
#57492
Comments
Here's an even simpler way to reproduce the issue, which doesn't involve import tensorflow as tf
@tf.function
def f(x):
x = tf.convert_to_tensor(x)
if tf.size(x) > 0:
tf.assert_greater(tf.size(x), 0)
return x
f(tf.zeros((1,))) # Works fine
f(tf.zeros((0,))) # Bombs out Here's the traceback: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-8-6c94edddf473>", line 1, in <module>
f(tf.zeros((0,)))
File "/home/hosford42/.local/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/home/hosford42/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 1127, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.InvalidArgumentError: in user code:
File "<ipython-input-6-df56e54e761f>", line 5, in f *
tf.assert_greater(tf.size(x), 0)
InvalidArgumentError: Condition x > y did not hold element-wise:
x (cond/Size:0) =
0
y (cond/assert_greater/y:0) =
0 |
tf.function
I even tried building a import tensorflow as tf
@tf.function
def f(x):
x = tf.convert_to_tensor(x)
return tf.cond(tf.size(x) == 0, lambda: 0, lambda: (tf.assert_greater(tf.size(x), 0), tf.size(x))[-1])
f(tf.zeros((1,)))
f(tf.zeros((0,))) Traceback: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-24fccfd78995>", line 11, in <module>
f(tf.zeros((0,)))
File "/home/hosford42/.local/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/home/hosford42/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 1127, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.InvalidArgumentError: in user code:
File "<ipython-input-3-24fccfd78995>", line 7, in f *
return tf.cond(tf.size(x) == 0, lambda: 0, lambda: (tf.assert_greater(tf.size(x), 0), tf.size(x))[-1])
InvalidArgumentError: Condition x > y did not hold element-wise:
x (cond/Size:0) =
0
y (cond/assert_greater/y:0) =
0 |
At this point, I see no other options for working around this besides modifying the tensor to be non-empty, and then cutting it back down to size again afterwards. |
@gadagashwini, |
Click to expand!
Issue Type
Bug
Source
binary
Tensorflow Version
v2.9.0-18-gd8ce9f9c301
Custom Code
Yes
OS Platform and Distribution
Ubuntu 20.04.4 LTS
Mobile device
n/a
Python version
Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] on linux
Bazel version
n/a
GCC/Compiler version
n/a
CUDA/cuDNN version
No response
GPU model and memory
n/a
Current Behaviour?
Standalone code to reproduce the issue
Relevant log output
The text was updated successfully, but these errors were encountered: