ponder-lab / Hybridize-Functions-Refactoring

Refactorings for optimizing imperative TensorFlow clients for greater efficiency.
Eclipse Public License 2.0
0 stars 0 forks source link

Calling next on an iterator over native Python containers should have Python side-effects #278

Open khatchad opened 1 year ago

khatchad commented 1 year ago

Consider the following code:

# From  https://www.tensorflow.org/guide/function#using_python_iterators_and_generators

import tensorflow as tf

@tf.function
def buggy_consume_next(iterator):
  tf.print("Value:", next(iterator))

iterator = iter([1, 2, 3])

buggy_consume_next(iterator)
# This reuses the first value from the iterator, rather than consuming the next value.
buggy_consume_next(iterator)
buggy_consume_next(iterator)

Above, iterator is over a Python container (a list here). Calling next() on the iterator moves its cursor over the container. Thus, that could be considered a Python side-effect, but only because the underlying container is a native Python container.

Regression

I believe is a type inferencing problem. The function call to iter() above will return a certain kind of iterator depending on its argument's type:

>>> type(iter([1,2,3]))
<class 'list_iterator'>