Skip to content

Commit 2e9b1da

Browse files
authored
New API and tests for missing operator overloading (#137)
- New API and new operator overloading issue (#81) - New gradient test. - Reduce epochs in test. - Easier to run.
1 parent 25d2f80 commit 2e9b1da

File tree

8 files changed

+92
-5
lines changed

8 files changed

+92
-5
lines changed

com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflowModel.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ public void testTf2()
217217
testTf2("tf2_test_dataset10.py", "add", 2, 2, 2, 3);
218218
testTf2("tf2_test_tensor_list.py", "add", 2, 2, 2, 3);
219219
testTf2("tf2_test_tensor_list2.py", "add", 0, 0);
220-
testTf2("tf2_test_tensor_list3.py", "add", 0, 0);
220+
testTf2(
221+
"tf2_test_tensor_list3.py",
222+
"add",
223+
0,
224+
0); // NOTE: Change to 2, 2, 2, 3 once https://github.com/wala/ML/issues/136 is fixed.
221225
testTf2("tf2_test_tensor_list4.py", "add", 0, 0);
222226
testTf2("tf2_test_tensor_list5.py", "add", 0, 0);
223227
testTf2("tf2_test_model_call.py", "SequentialModel.__call__", 1, 1, 3);
@@ -234,10 +238,10 @@ public void testTf2()
234238
"neural_network.py",
235239
"cross_entropy_loss",
236240
1,
237-
4,
241+
8,
238242
3); // NOTE: Change to 2 tensor parameters once https://github.com/wala/ML/issues/127 is
239243
// fixed. Values 2 and 3 will correspond to the tensor parameters.
240-
testTf2("neural_network.py", "run_optimization", 2, 2, 2, 3);
244+
testTf2("neural_network.py", "run_optimization", 2, 3, 2, 3);
241245
testTf2(
242246
"neural_network.py",
243247
"accuracy",
@@ -259,10 +263,19 @@ public void testTf2()
259263
testTf2("tf2_test_add5.py", "f", 1, 1, 2);
260264
testTf2("tf2_test_add6.py", "f", 1, 1, 2);
261265
testTf2("multigpu_training.py", "run_optimization", 2, 4, 2, 3);
266+
testTf2(
267+
"multigpu_training.py",
268+
"average_gradients",
269+
0,
270+
0); // NOTE: Change to 1, 1, 2 once https://github.com/wala/ML/issues/136 is fixed.
262271
testTf2("tf2_test_reduce_mean.py", "f", 1, 1, 2);
263272
testTf2("tf2_test_reduce_mean.py", "g", 1, 1, 2);
264273
testTf2("tf2_test_reduce_mean.py", "h", 1, 1, 2);
265274
testTf2("tf2_test_gradient.py", "f", 1, 1, 2);
275+
testTf2("tf2_test_gradient2.py", "f", 1, 1, 2);
276+
testTf2("tf2_test_multiply.py", "f", 1, 1, 2);
277+
testTf2("tf2_test_multiply2.py", "f", 1, 1, 2);
278+
testTf2("tf2_test_sparse_softmax_cross_entropy_with_logits.py", "f", 1, 1, 2);
266279
}
267280

268281
private void testTf2(

com.ibm.wala.cast.python.ml/data/tensorflow.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@
7575
<putfield class="LRoot" field="conv3d" fieldType="LRoot" ref="nn" value="conv3d" />
7676
<new def="softmax" class="Ltensorflow/functions/softmax" />
7777
<putfield class="LRoot" field="softmax" fieldType="LRoot" ref="nn" value="softmax" />
78+
<new def="sparse_softmax_cross_entropy_with_logits" class="Ltensorflow/functions/sparse_softmax_cross_entropy_with_logits" />
79+
<putfield class="LRoot" field="sparse_softmax_cross_entropy_with_logits" fieldType="LRoot" ref="nn" value="sparse_softmax_cross_entropy_with_logits" />
7880
<new def="sigmoid" class="Ltensorflow/math/sigmoid" />
7981
<putfield class="LRoot" field="sigmoid" fieldType="LRoot" ref="nn" value="sigmoid" />
8082
<putfield class="LRoot" field="sigmoid" fieldType="LRoot" ref="math" value="sigmoid" />
8183
<new def="add" class="Ltensorflow/math/add" />
8284
<putfield class="LRoot" field="add" fieldType="LRoot" ref="x" value="add" />
8385
<putfield class="LRoot" field="add" fieldType="LRoot" ref="math" value="add" />
86+
<new def="multiply" class="Ltensorflow/math/multiply" />
87+
<putfield class="LRoot" field="multiply" fieldType="LRoot" ref="x" value="multiply" />
88+
<putfield class="LRoot" field="multiply" fieldType="LRoot" ref="math" value="multiply" />
8489
<new def="reduce_mean" class="Ltensorflow/math/reduce_mean" />
8590
<putfield class="LRoot" field="reduce_mean" fieldType="LRoot" ref="x" value="reduce_mean" />
8691
<putfield class="LRoot" field="reduce_mean" fieldType="LRoot" ref="math" value="reduce_mean" />
@@ -285,6 +290,18 @@
285290
<return value="xx" />
286291
</method>
287292
</class>
293+
<class name="multiply" allocatable="true">
294+
<!-- https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/multiply -->
295+
<method name="read_data" descriptor="()LRoot;">
296+
<new def="x" class="Ltensorflow/math/multiply" />
297+
<return value="x" />
298+
</method>
299+
<method name="do" descriptor="()LRoot;" numArgs="4" paramNames="self x y name">
300+
<!-- Even though tf.multiply() isn't a tensor "generator," it can convert its non-tensor arguments to tensors. -->
301+
<call class="LRoot" name="read_data" descriptor="()LRoot;" type="virtual" arg0="arg0" def="xx" />
302+
<return value="xx" />
303+
</method>
304+
</class>
288305
<class name="reduce_mean" allocatable="true">
289306
<!-- https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/reduce_mean -->
290307
<method name="do" descriptor="()LRoot;" numArgs="5" paramNames="self input_tensor axis keepdims name">
@@ -682,6 +699,12 @@
682699
<return value="logits" />
683700
</method>
684701
</class>
702+
<class name="sparse_softmax_cross_entropy_with_logits" allocatable="true">
703+
<!-- https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits -->
704+
<method name="do" descriptor="()LRoot;" numArgs="4" paramNames="self labels logits name">
705+
<return value="labels" />
706+
</method>
707+
</class>
685708
</package>
686709
<package name="tensorflow/estimator">
687710
<class name="Estimator" allocatable="true">

com.ibm.wala.cast.python.test/data/multigpu_training.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
# Training parameters.
4444
learning_rate = 0.001
45-
training_steps = 1000
45+
training_steps = 1
4646
# Split batch size equally between GPUs.
4747
# Note: Reduce batch size if you encounter OOM Errors.
4848
batch_size = 1024 * num_gpus
49-
display_step = 20
49+
display_step = 1
5050

5151
# Network parameters.
5252
conv1_filters = 64 # number of filters for 1st conv layer.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# From https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/GradientTape#gradient.
2+
3+
import tensorflow as tf
4+
5+
6+
def f(a):
7+
pass
8+
9+
10+
x = tf.ragged.constant([[1.0, 2.0], [3.0]])
11+
with tf.GradientTape() as g:
12+
g.watch(x)
13+
y = tf.multiply(x, x)
14+
f(g.gradient(y, x))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# From https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/math/multiply#for_example/
2+
3+
import tensorflow as tf
4+
5+
6+
def f(a):
7+
pass
8+
9+
10+
x = tf.constant(([1, 2, 3, 4]))
11+
f(tf.math.multiply(x, x))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# From https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/math/multiply#for_example/
2+
3+
import tensorflow as tf
4+
5+
6+
def f(a):
7+
pass
8+
9+
10+
f(tf.math.multiply(7, 6))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# from https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits
2+
3+
import tensorflow as tf
4+
5+
6+
def f(a):
7+
pass
8+
9+
10+
logits = tf.constant(
11+
[[2.0, -5.0, 0.5, -0.1], [0.0, 0.0, 1.9, 1.4], [-100.0, 100.0, -100.0, -100.0]]
12+
)
13+
labels = tf.constant([0, 3, 1])
14+
f(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits.numpy()))

com.ibm.wala.cast.python.test/data/tf2_test_tensor_list3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Test https://github.com/wala/ML/issues/136.
2+
13
import tensorflow as tf
24

35

0 commit comments

Comments
 (0)