Skip to content

Commit

Permalink
More tests for side-effects.
Browse files Browse the repository at this point in the history
  • Loading branch information
khatchad committed Oct 16, 2023
1 parent 0672e7b commit 89f55a8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# From https://www.tensorflow.org/guide/function#executing_python_side_effects.

import tensorflow as tf


def f(x):
# print("Traced with", x) # This is a Python side-effect.
tf.print("Executed with", x) # THis isn't.


f(1)
f(1)
f(2)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tensorflow==2.9.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# From https://www.tensorflow.org/guide/function#executing_python_side_effects.

import tensorflow as tf


def f(x):
# Assigning x to a."
a = x
print("Traced with", a) # This is a transitive Python side-effect.
tf.print("Executed with", x) # This isn't.


f(1)
f(1)
f(2)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tensorflow==2.9.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# From https://www.tensorflow.org/guide/function#executing_python_side_effects.


def f(x):
x = 5 # no side-effect as `x` is a local variable..


f(1)
f(1)
f(2)
Original file line number Diff line number Diff line change
Expand Up @@ -4562,7 +4562,34 @@ public void testPythonSideEffects() throws Exception {
Function function = getSingleFunction();
assertFalse(function.isHybrid());
assertFalse(function.getLikelyHasTensorParameter()); // the example uses a primitive type.
assertTrue(function.getHasPythonSideEffects());
assertTrue("Expecting a Python side-effect.", function.getHasPythonSideEffects());
}

@Test
public void testPythonSideEffects2() throws Exception {
Function function = getSingleFunction();
assertFalse(function.isHybrid());
assertFalse(function.getLikelyHasTensorParameter()); // the example uses a primitive type.
// there's a call to a TF operation. So, no "Python" side-effects.
assertFalse("TF operations shouldn't be considered Python side-effects.", function.getHasPythonSideEffects());
}

@Test
public void testPythonSideEffects3() throws Exception {
Function function = getSingleFunction();
assertFalse(function.isHybrid());
assertFalse(function.getLikelyHasTensorParameter()); // the example uses a primitive type.
// there's a transitive Python side-effect.
assertTrue("Expecting a Python side-effect from a transitive local variable.", function.getHasPythonSideEffects());
}

@Test
public void testPythonSideEffects4() throws Exception {
Function function = getSingleFunction();
assertFalse(function.isHybrid());
assertFalse(function.getLikelyHasTensorParameter()); // the example uses a primitive type.
// there's a Python statement but no side-effect.
assertFalse("This Python statement only modifies a local variable, so no side-effects.", function.getHasPythonSideEffects());
}

private Function getSingleFunction() throws Exception {
Expand Down

0 comments on commit 89f55a8

Please sign in to comment.