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

Fix missing callbacks #57

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public void testTf2()
// once https://github.com/wala/ML/issues/106 is fixed.
testTf2("tf2_test_model_call3.py", "SequentialModel.call", 1, 4, 2);
testTf2("tf2_test_model_call4.py", "SequentialModel.__call__", 1, 4, 2);
testTf2("tf2_test_callbacks.py", "get_dataset", 0, 0);
khatchad marked this conversation as resolved.
Show resolved Hide resolved
testTf2("tf2_test_callbacks2.py", "replica_fn", 1, 2, 2);
testTf2("tf2_test_callbacks3.py", "get_dataset", 0, 0);
testTf2("tf2_test_callbacks4.py", "replica_fn", 1, 2, 2);
khatchad marked this conversation as resolved.
Show resolved Hide resolved
}

private void testTf2(
Expand Down
58 changes: 58 additions & 0 deletions com.ibm.wala.cast.python.ml/data/tensorflow.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<new def="estimator" class="Lobject" />
<putfield class="LRoot" field="estimator" fieldType="LRoot" ref="x" value="estimator" />

<new def="distribute" class="Lobject" />
<putfield class="LRoot" field="distribute" fieldType="LRoot" ref="x" value="distribute" />

<new def="nn" class="Lobject" />
<putfield class="LRoot" field="nn" fieldType="LRoot" ref="x" value="nn" />
<new def="random" class="Lobject" />
Expand All @@ -58,6 +61,9 @@
<new def="Estimator" class="Ltensorflow/estimator/Estimator" />
<putfield class="LRoot" field="Estimator" fieldType="LRoot" ref="estimator" value="Estimator" />

<new def="MirroredStrategy" class="Ltensorflow/distribute/MirroredStrategy" />
<putfield class="LRoot" field="MirroredStrategy" fieldType="LRoot" ref="distribute" value="MirroredStrategy" />

<new def="inputs" class="Lobject" />
<putfield class="LRoot" field="inputs" fieldType="LRoot" ref="estimator" value="inputs" />

Expand Down Expand Up @@ -786,6 +792,58 @@
</class>
</package>

<package name="tensorflow/distribute">
<class name="MirroredStrategy" allocatable="true">
khatchad marked this conversation as resolved.
Show resolved Hide resolved
<method name="do" descriptor="()LRoot;" numArgs="3" paramNames="self function">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there three arguments but only two parameter names?

<new def="x" class="Ltensorflow/distribute/distribute_datasets_from_function/distribute_datasets_from_function" />
<putfield class="LRoot" field="distribute_datasets_from_function" fieldType="LRoot" ref="self" value="x" />
<return value="arg0" />
<new def="y" class="Ltensorflow/distribute/experimental_distribute_datasets_from_function/experimental_distribute_datasets_from_function" />
<putfield class="LRoot" field="experimental_distribute_datasets_from_function" fieldType="LRoot" ref="self" value="y" />
<return value="arg1" />
<new def="z" class="Ltensorflow/distribute/run/run" />
<putfield class="LRoot" field="run" fieldType="LRoot" ref="self" value="z" />
<return value="arg2" />
khatchad marked this conversation as resolved.
Show resolved Hide resolved
</method>
</class>
</package>

<package name="tensorflow/distribute/distribute_datasets_from_function">
<class name="distribute_datasets_from_function" allocatable="true">
<method name="do" descriptor="()LRoot;" numArgs="2" paramNames="self dataset_fn">
<new def="x" class="Lobject" />
<putfield class="LRoot" field="MirroredStrategy" fieldType="LRoot" ref="self" value="x" />
<putfield class="LRoot" field="$callback" fieldType="LRoot" ref="x" value="dataset_fn" />
<call class="LRoot" name="do" descriptor="()LRoot;" type="virtual" arg0="dataset_fn" arg1="1" numArgs="2" def="xx" />
<return value="xx" />
</method>
</class>
</package>

<package name="tensorflow/distribute/experimental_distribute_datasets_from_function">
<class name="experimental_distribute_datasets_from_function" allocatable="true">
<method name="do" descriptor="()LRoot;" numArgs="2" paramNames="self dataset_fn">
<new def="x" class="Lobject" />
<putfield class="LRoot" field="MirroredStrategy" fieldType="LRoot" ref="self" value="x" />
<putfield class="LRoot" field="$callback" fieldType="LRoot" ref="x" value="dataset_fn" />
<call class="LRoot" name="do" descriptor="()LRoot;" type="virtual" arg0="dataset_fn" arg1="1" numArgs="2" def="xx" />
<return value="xx" />
</method>
</class>
</package>

<package name="tensorflow/distribute/run">
<class name="run" allocatable="true">
<method name="do" descriptor="()LRoot;" numArgs="2" paramNames="self fn">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://www.tensorflow.org/api_docs/python/tf/distribute/Strategy#run, run() takes also args=(). Looks like that needs to be passed to the callback.

<new def="x" class="Lobject" />
<putfield class="LRoot" field="MirroredStrategy" fieldType="LRoot" ref="self" value="x" />
<putfield class="LRoot" field="$callback" fieldType="LRoot" ref="x" value="fn" />
khatchad marked this conversation as resolved.
Show resolved Hide resolved
<call class="LRoot" name="do" descriptor="()LRoot;" type="virtual" arg0="fn" arg1="1" numArgs="2" def="xx" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does arg1 take 1 here?

<return value="xx" />
</method>
</class>
</package>

<package name="tensorflow/app">
<class name="run" allocatable="true">
<method name="do" descriptor="()LRoot;" numArgs="3" paramNames="self main argv">
Expand Down
15 changes: 15 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import tensorflow as tf
khatchad marked this conversation as resolved.
Show resolved Hide resolved

# Testing API https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/MirroredStrategy#distribute_datasets_from_function
# Making sure that function `get_dataset` is in the CG
khatchad marked this conversation as resolved.
Show resolved Hide resolved

def get_dataset(input_context):
khatchad marked this conversation as resolved.
Show resolved Hide resolved
batch_size = input_context.get_per_replica_batch_size(2)
return tf.data.Dataset.range(4).batch(batch_size)

global_batch_size = 2

strategy = tf.distribute.MirroredStrategy(devices=["GPU:0", "GPU:1"])

input_context = tf.distribute.InputContext()
dist_dataset = strategy.distribute_datasets_from_function(get_dataset)
14 changes: 14 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_callbacks2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tensorflow as tf

# Code from https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/Strategy#example_usage_2
# Testing API https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/MirroredStrategy#run
# Making sure that function `replica_fn` is in the CG

strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
tensor_input = tf.constant(3.0)

@tf.function
def replica_fn(input):
khatchad marked this conversation as resolved.
Show resolved Hide resolved
return input*2.0

result = strategy.run(replica_fn, args=(tensor_input,))
khatchad marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_callbacks3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import tensorflow as tf

# Testing API https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/MirroredStrategy#distribute_datasets_from_function
# Making sure that function `get_dataset` is in the CG

def get_dataset(input_context):
batch_size = input_context.get_per_replica_batch_size(2)
return tf.data.Dataset.range(4).batch(batch_size)

global_batch_size = 2

strategy = tf.distribute.MirroredStrategy(devices=["GPU:0", "GPU:1"])

input_context = tf.distribute.InputContext()
dist_dataset = strategy.experimental_distribute_datasets_from_function(get_dataset)
14 changes: 14 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_callbacks4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tensorflow as tf

# Code from https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/Strategy#example_usage_2
# Testing API https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/MirroredStrategy#run
# Making sure that function `replica_fn` is in the CG

strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
tensor_input = tf.constant(3.0)

@tf.function
def replica_fn(input):
return input*2.0

result = strategy.run(replica_fn, (tensor_input,))
Loading