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

Handle "leaky" tensors #281

Open
khatchad opened this issue Nov 1, 2023 · 0 comments
Open

Handle "leaky" tensors #281

khatchad opened this issue Nov 1, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@khatchad
Copy link
Member

khatchad commented Nov 1, 2023

Consider the following code:

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

import tensorflow as tf
from nose.tools import assert_raises


@tf.function
def leaky_function(a):
  global x
  x = a + 1  # Bad - leaks local tensor
  return x  # Good - uses local tensor


correct_a = leaky_function(tf.constant(1))

print(correct_a.numpy())  # Good - value obtained from function's returns
try:
  x.numpy()  # Bad - tensor leaked from inside the function, cannot be used here
except AttributeError as expected:
  print(expected)


def captures_leaked_tensor(b):
  b += x  # Bad - `x` is leaked from `leaky_function`
  return b


with assert_raises(TypeError):
  captures_leaked_tensor(tf.constant(2))

Here, capture_leaked_tensor() doesn't explicitly have Python side-effects, but it reads from a global variable that was written to by a function that does have Python side-effects. Thus, captures_leaked_tensor should not be converted to hybrid.

Expected Behavior

  1. The function capture_leaked_tensor() should not be hybridized.
  2. If capture_leaked_tensor() is already hybrid, we should warn.
@khatchad khatchad added the bug Something isn't working label Nov 1, 2023
khatchad added a commit that referenced this issue Nov 2, 2023
khatchad added a commit that referenced this issue Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant