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

Adding initial support for decorator tf.function #86

Merged

Conversation

tatianacv
Copy link
Contributor

Currently, we are using jython2 in com.ibm.wala.cast.python.ml.test, where we don't encounter decorator problems when running the tests. But when we use jython3, we do encounter problems with decorator tf.function. Therefore we are adding initial support for the decorator tf.function in tensorflow.xml. Initial support for tf.function because after this change, in jython3, can now process the decorator that has parenthesis e.g. tf.function() or tf.function(...).

Refer to #33 for the issue with inconsistent jython versions.

After this change we see:
Test code with parenthesis:

import tensorflow as tf

@tf.function()
def returned(a):
  return a

a = tf.range(5)
b = returned(a)

The analysis of the test with parenthesis:

answer:
[Node: <Code body of function Lscript tf2_testing_decorator.py> Context: CallStringContext: [ com.ibm.wala.FakeRootClass.fakeRootMethod()V@2 ], v251][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Ret-V:Node: synthetic < PythonLoader, Ltensorflow/functions/range, do()LRoot; > Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@96 ]][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Node: synthetic < PythonLoader, Ltensorflow/functions/range, do()LRoot; > Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@96 ], v6][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Node: <Code body of function Lscript tf2_testing_decorator.py/returned> Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@98 ], v2][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Node: <Code body of function Lscript tf2_testing_decorator.py> Context: CallStringContext: [ com.ibm.wala.FakeRootClass.fakeRootMethod()V@2 ], v257][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Ret-V:Node: <Code body of function Lscript tf2_testing_decorator.py/returned> Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@98 ]][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]

Test code with parenthesis:

import tensorflow as tf

@tf.function
def returned(a):
  return a

a = tf.range(5)
b = returned(a)

The analysis of the test without parenthesis:

answer:
[Node: <Code body of function Lscript tf2_testing_decorator.py> Context: CallStringContext: [ com.ibm.wala.FakeRootClass.fakeRootMethod()V@2 ], v249][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Ret-V:Node: synthetic < PythonLoader, Ltensorflow/functions/range, do()LRoot; > Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@95 ]][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Node: synthetic < PythonLoader, Ltensorflow/functions/range, do()LRoot; > Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@95 ], v6][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Node: synthetic < PythonLoader, Ltensorflow/class/Function, do()LRoot; > Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@97 ], v2][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Node: <Code body of function Lscript tf2_testing_decorator.py> Context: CallStringContext: [ com.ibm.wala.FakeRootClass.fakeRootMethod()V@2 ], v255][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]
[Ret-V:Node: synthetic < PythonLoader, Ltensorflow/class/Function, do()LRoot; > Context: CallStringContext: [ script tf2_testing_decorator.py.do()LRoot;@97 ]][{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]

We see that with the decorator, expr is classified as a CALL, and without it is classified as an Attribute.

* Adding initial support for decorators

* Applying spotless

* Adding tests

* Making example simpler, renaming files

* Applying spotless

* Adding more tests

* Reverting formatting
@khatchad khatchad merged commit 7aaa4dc into wala:master Aug 14, 2023
1 check passed
@khatchad khatchad deleted the contrib_initial_support_tffunction_decorator branch August 14, 2023 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants