From 89f55a82b12d58a6eb8fa1fcb21a3a3fd60f189f Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Mon, 16 Oct 2023 10:40:08 -0400 Subject: [PATCH] More tests for side-effects. --- .../testPythonSideEffects2/in/A.py | 13 +++++++++ .../in/requirements.txt | 1 + .../testPythonSideEffects3/in/A.py | 15 ++++++++++ .../in/requirements.txt | 1 + .../testPythonSideEffects4/in/A.py | 10 +++++++ .../in/requirements.txt | 0 .../HybridizeFunctionRefactoringTest.java | 29 ++++++++++++++++++- 7 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/A.py create mode 100644 edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/requirements.txt create mode 100644 edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/A.py create mode 100644 edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/requirements.txt create mode 100644 edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/A.py create mode 100644 edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/requirements.txt diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/A.py new file mode 100644 index 00000000..5a6b0564 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/A.py @@ -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) diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/requirements.txt new file mode 100644 index 00000000..b154f958 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects2/in/requirements.txt @@ -0,0 +1 @@ +tensorflow==2.9.3 diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/A.py new file mode 100644 index 00000000..e743bea7 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/A.py @@ -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) diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/requirements.txt new file mode 100644 index 00000000..b154f958 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects3/in/requirements.txt @@ -0,0 +1 @@ +tensorflow==2.9.3 diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/A.py new file mode 100644 index 00000000..2bb9174e --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/A.py @@ -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) diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testPythonSideEffects4/in/requirements.txt new file mode 100644 index 00000000..e69de29b diff --git a/edu.cuny.hunter.hybridize.tests/test cases/edu/cuny/hunter/hybridize/tests/HybridizeFunctionRefactoringTest.java b/edu.cuny.hunter.hybridize.tests/test cases/edu/cuny/hunter/hybridize/tests/HybridizeFunctionRefactoringTest.java index e4fa2fc1..f3532b22 100644 --- a/edu.cuny.hunter.hybridize.tests/test cases/edu/cuny/hunter/hybridize/tests/HybridizeFunctionRefactoringTest.java +++ b/edu.cuny.hunter.hybridize.tests/test cases/edu/cuny/hunter/hybridize/tests/HybridizeFunctionRefactoringTest.java @@ -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 {