-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects48/in/A.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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) | ||
|
||
|
||
# @tf.function | ||
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)) |
2 changes: 2 additions & 0 deletions
2
...r.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects48/in/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tensorflow==2.9.3 | ||
nose==1.3.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters