diff --git a/edu.cuny.hunter.hybridize.core/src/edu/cuny/hunter/hybridize/core/analysis/Function.java b/edu.cuny.hunter.hybridize.core/src/edu/cuny/hunter/hybridize/core/analysis/Function.java index e3d68cce5..1fb15b5e3 100644 --- a/edu.cuny.hunter.hybridize.core/src/edu/cuny/hunter/hybridize/core/analysis/Function.java +++ b/edu.cuny.hunter.hybridize.core/src/edu/cuny/hunter/hybridize/core/analysis/Function.java @@ -342,6 +342,11 @@ public boolean getReduceRetracingParamExists() { */ private Boolean likelyHasTensorParameter; + /** + * True iff this {@link Function} has at least one parameter that is not likely a tensor. + */ + private Boolean likelyHasNonTensorParameters; + /** * True iff this {@link Function} has Python side-effects. */ @@ -638,6 +643,7 @@ public void inferTensorTensorParameters(TensorTypeAnalysis analysis, CallGraph c if (params != null) { exprType[] actualParams = params.args; // FIXME: Looks like we are only considering position parameters here. + if (actualParams != null) { for (int paramInx = 0; paramInx < actualParams.length; paramInx++) { exprType paramExpr = actualParams[paramInx]; @@ -699,6 +705,13 @@ && tensorAnalysisIncludesParameterContainer(analysis, paramInx, callGraph, continue; // next parameter. } + // if there is at least one parameter and we haven't found a tensor parameter. + if (actualParams.length > 0) { + // then we must have encountered a "non-tensor" parameter. + this.likelyHasNonTensorParameters = TRUE; + LOG.info(this + " likely has a non-tensor parameter: " + paramName); + } + monitor.worked(1); } } @@ -709,6 +722,11 @@ && tensorAnalysisIncludesParameterContainer(analysis, paramInx, callGraph, LOG.info(this + " does not likely have a tensor parameter."); } + if (this.likelyHasNonTensorParameters == null) { + this.likelyHasNonTensorParameters = FALSE; + LOG.info(this + " does not likely have a non-tensor parameter."); + } + monitor.done(); } @@ -1352,4 +1370,13 @@ public Set getWarnings() { public Set getErrors() { return this.getRefactoringStatusEntries(RefactoringStatusEntry::isError); } + + /** + * Returns true iff this {@link Function} has at least one parameter that is likely not a tensor. + * + * @return True iff this {@link Function} has at least one parameter that is likely not a tensor. + */ + public Boolean getLikelyHasNonTensorParameters() { + return likelyHasNonTensorParameters; + } } diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter/in/A.py new file mode 100644 index 000000000..3c65b9955 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter/in/A.py @@ -0,0 +1,5 @@ +def f(): + pass + + +f() diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter/in/requirements.txt new file mode 100644 index 000000000..e69de29bb diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter2/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter2/in/A.py new file mode 100644 index 000000000..2a0674720 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter2/in/A.py @@ -0,0 +1,5 @@ +def f(a): + pass + + +f(5) diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter2/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter2/in/requirements.txt new file mode 100644 index 000000000..e69de29bb diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter3/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter3/in/A.py new file mode 100644 index 000000000..6bc88aa7e --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter3/in/A.py @@ -0,0 +1,8 @@ +import tensorflow as tf + + +def f(a): + pass + + +f(tf.constant(5)) diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter3/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter3/in/requirements.txt new file mode 100644 index 000000000..b154f958f --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter3/in/requirements.txt @@ -0,0 +1 @@ +tensorflow==2.9.3 diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter4/in/A.py b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter4/in/A.py new file mode 100644 index 000000000..ef91650f8 --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter4/in/A.py @@ -0,0 +1,8 @@ +import tensorflow as tf + + +def f(a, b): + pass + + +f(tf.constant(5), 5) diff --git a/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter4/in/requirements.txt b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter4/in/requirements.txt new file mode 100644 index 000000000..b154f958f --- /dev/null +++ b/edu.cuny.hunter.hybridize.tests/resources/HybridizeFunction/testLikelyHasNonTensorParameter4/in/requirements.txt @@ -0,0 +1 @@ +tensorflow==2.9.3 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 cb80d9dc2..dd6a184b7 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 @@ -5833,4 +5833,32 @@ public void testCallback() throws Exception { Function f = getFunction("replica_fn"); assertTrue(f.getLikelyHasTensorParameter()); } + + @Test + public void testLikelyHasNonTensorParameter() throws Exception { + Function f = getFunction("f"); + assertFalse("This function has no parameters.", f.getLikelyHasTensorParameter()); + assertFalse("This function has no parameters.", f.getLikelyHasNonTensorParameters()); + } + + @Test + public void testLikelyHasNonTensorParameter2() throws Exception { + Function f = getFunction("f"); + assertFalse("This function has one parameter.", f.getLikelyHasTensorParameter()); + assertTrue("This function has one parameter.", f.getLikelyHasNonTensorParameters()); + } + + @Test + public void testLikelyHasNonTensorParameter3() throws Exception { + Function f = getFunction("f"); + assertTrue("This function has one (tensor) parameter.", f.getLikelyHasTensorParameter()); + assertFalse("This function has one (tensor) parameter.", f.getLikelyHasNonTensorParameters()); + } + + @Test + public void testLikelyHasNonTensorParameter4() throws Exception { + Function f = getFunction("f"); + assertTrue("This function has one tensor parameter and one non-tensor parameter.", f.getLikelyHasTensorParameter()); + assertTrue("This function has one tensor parameter and one non-tensor parameter.", f.getLikelyHasNonTensorParameters()); + } }