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

Calling next with a default value on an exhausted Dataset iterator raises an OutOfRangeError in graph mode #37983

Closed
hannesdm opened this issue Mar 27, 2020 · 3 comments
Assignees
Labels
comp:autograph Autograph related issues TF 2.1 for tracking issues in 2.1 release type:bug Bug WIP

Comments

@hannesdm
Copy link

System information

  • Have I written custom code: Yes
  • OS Platform and Distribution: Windows 10
  • TensorFlow installed from binary: 2.1.0

Describe the current behavior
next(iterator, default) is supposed to give the next element in the iterator or the value given as default if the iterator is at the end.
However, when using the above construction in a function with @tf.function, the default value is not returned and an error (tensorflow.python.framework.errors_impl.OutOfRangeError) is produced when trying to call next on an iterator that is at the end.
When running this code in eager mode, the default value is returned as expected.

Describe the expected behavior
In graph mode the default value should be returned when at the end of an iterator.

Standalone code to reproduce the issue

import tensorflow as tf

x = tf.convert_to_tensor([[1], [2], [3]])
ds = tf.data.Dataset.from_tensor_slices(x)
dsi = iter(ds)


@tf.function # remove this to get the expected behaviour
def func():
    for _ in range(4):
        tf.print(next(dsi, -1))


func()

Output (see below for a full stacktrace):

[1]
[2]
[3]
2020-03-27 18:56:09.523946: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Out of range: End of sequence
	 [[{{node IteratorGetNext_3}}]]

Expected output:

[1]
[2]
[3]
-1

Other info / logs
Colab link: https://colab.research.google.com/drive/1PBxoXiE48aC-bo-aY-Bau1Igt4Aj6OFy

stacktrace.txt

@hannesdm hannesdm added the type:bug Bug label Mar 27, 2020
@hannesdm hannesdm changed the title Calling next with a default value on an exhausted Dataset iterator raises an OutOfRangeError Calling next with a default value on an exhausted Dataset iterator raises an OutOfRangeError in graph mode Mar 27, 2020
@khimraj
Copy link

khimraj commented Mar 28, 2020

@hannesdm, I have tried after removing @tf.function decorator and got expected output.

@hannesdm
Copy link
Author

@hannesdm, I have tried after removing @tf.function decorator and got expected output.

Yes, everything works as it should without @tf.function, the bug only occurs in graph mode i.e. with @tf.function.

@ravikyram ravikyram added TF 2.1 for tracking issues in 2.1 release TF 2.2 Issues related to TF 2.2 comp:autograph Autograph related issues labels Mar 30, 2020
@ravikyram ravikyram assigned ymodak and unassigned ravikyram Mar 30, 2020
@ymodak ymodak added TF 2.1 for tracking issues in 2.1 release and removed TF 2.2 Issues related to TF 2.2 TF 2.1 for tracking issues in 2.1 release labels Mar 31, 2020
@mdanatg mdanatg self-assigned this Apr 10, 2020
@mdanatg mdanatg added the WIP label Apr 10, 2020
@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:autograph Autograph related issues TF 2.1 for tracking issues in 2.1 release type:bug Bug WIP
Projects
Development

No branches or pull requests

5 participants