Skip to content

Commit

Permalink
Deal with multiple possible callables (#121)
Browse files Browse the repository at this point in the history
* Return `null` when there are multiple possible callables.

* Add test to exercise call string imprecision.

Based on the call string length. See wala/WALA#1417 (reply in thread).

* Expect the test to fail.

In the past, we could add 0's to the parameters, but since we are not
enforcing the existing of the node in the CG, we can no longer do that.
Still, this test should now fail if wala#207 is fixed.
  • Loading branch information
khatchad committed Jul 21, 2024
1 parent cbd558e commit 7777bf3
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,41 @@ public void testModelCall4()
test("tf2_test_model_call4.py", "SequentialModel.__call__", 1, 1, 3);
}

/**
* Test call string imprecision as described in
* https://github.com/wala/WALA/discussions/1417#discussioncomment-10085680. This should fail due
* to https://github.com/wala/ML/issues/207.
*/
@Test(expected = java.lang.AssertionError.class)
public void testModelCall5()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test(
new String[] {
"proj66/src/tf2_test_model_call5b.py",
"proj66/tf2_test_model_call5.py",
"proj66/tf2_test_model_call5a.py"
},
"tf2_test_model_call5.py",
"SequentialModel.__call__",
"proj66",
1,
1,
3);

test(
new String[] {
"proj66/src/tf2_test_model_call5b.py",
"proj66/tf2_test_model_call5.py",
"proj66/tf2_test_model_call5a.py"
},
"tf2_test_model_call5a.py",
"SequentialModel.__call__",
"proj66",
1,
1,
3);
}

@Test
public void testModelAttributes()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
Expand Down
1 change: 1 addition & 0 deletions com.ibm.wala.cast.python.test/.pydevproject
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<path>/${PROJECT_DIR_NAME}/data/proj35</path>
<path>/${PROJECT_DIR_NAME}/data/proj37</path>
<path>/${PROJECT_DIR_NAME}/data/proj45</path>
<path>/${PROJECT_DIR_NAME}/data/proj66</path>
</pydev_pathproperty>


Expand Down
1 change: 1 addition & 0 deletions com.ibm.wala.cast.python.test/data/proj66/src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Test https://github.com/wala/WALA/discussions/1417#discussioncomment-10085680.


def f(m, d):
return m.predict(d)


def g(m, d):
return f(m, d)
44 changes: 44 additions & 0 deletions com.ibm.wala.cast.python.test/data/proj66/tf2_test_model_call5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Test https://github.com/wala/WALA/discussions/1417#discussioncomment-10085680.

import tensorflow as tf
from src.tf2_test_model_call5b import g

# Create an override model to classify pictures


class SequentialModel(tf.keras.Model):

def __init__(self, **kwargs):
super(SequentialModel, self).__init__(**kwargs)

self.flatten = tf.keras.layers.Flatten(input_shape=(28, 28))

# Add a lot of small layers
num_layers = 100
self.my_layers = [
tf.keras.layers.Dense(64, activation="relu") for n in range(num_layers)
]

self.dropout = tf.keras.layers.Dropout(0.2)
self.dense_2 = tf.keras.layers.Dense(10)

def __call__(self, x):
print("Raffi 1")
x = self.flatten(x)

for layer in self.my_layers:
x = layer(x)

x = self.dropout(x)
x = self.dense_2(x)

return x

def predict(self, x):
return self(x)


input_data = tf.random.uniform([20, 28, 28])

model = SequentialModel()
result = g(model, input_data)
44 changes: 44 additions & 0 deletions com.ibm.wala.cast.python.test/data/proj66/tf2_test_model_call5a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Test https://github.com/wala/WALA/discussions/1417#discussioncomment-10085680.

import tensorflow as tf
from src.tf2_test_model_call5b import g

# Create an override model to classify pictures


class SequentialModel(tf.keras.Model):

def __init__(self, **kwargs):
super(SequentialModel, self).__init__(**kwargs)

self.flatten = tf.keras.layers.Flatten(input_shape=(28, 28))

# Add a lot of small layers
num_layers = 100
self.my_layers = [
tf.keras.layers.Dense(64, activation="relu") for n in range(num_layers)
]

self.dropout = tf.keras.layers.Dropout(0.2)
self.dense_2 = tf.keras.layers.Dense(10)

def __call__(self, x):
print("Raffi 2")
x = self.flatten(x)

for layer in self.my_layers:
x = layer(x)

x = self.dropout(x)
x = self.dense_2(x)

return x

def predict(self, x):
return self(x)


input_data = tf.random.uniform([20, 28, 28])

model = SequentialModel()
result = g(model, input_data)
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.collections.Pair;
import com.ibm.wala.util.intset.OrdinalSet;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

Expand Down Expand Up @@ -223,6 +224,8 @@ private IClass getCallable(CGNode caller, IClassHierarchy cha, PythonInvokeInstr
PointerKey receiver = pkf.getPointerKeyForLocal(caller, call.getUse(0));
OrdinalSet<InstanceKey> objs = builder.getPointerAnalysis().getPointsToSet(receiver);

Map<InstanceKey, IClass> instanceToCallable = new HashMap<>();

for (InstanceKey o : objs) {
AllocationSiteInNode instanceKey = getAllocationSiteInNode(o);
if (instanceKey != null) {
Expand Down Expand Up @@ -254,10 +257,29 @@ private IClass getCallable(CGNode caller, IClassHierarchy cha, PythonInvokeInstr
LOGGER.info("Applying callable workaround for https://github.com/wala/ML/issues/118.");
}

if (callable != null) return callable;
if (callable != null) {
if (instanceToCallable.containsKey(instanceKey))
throw new IllegalStateException("Exisitng mapping found for: " + instanceKey);

IClass previousValue = instanceToCallable.put(instanceKey, callable);
assert previousValue == null : "Not expecting a previous mapping.";
}
}
}

// if there's only one possible option.
if (instanceToCallable.values().size() == 1) {
IClass callable = instanceToCallable.values().iterator().next();
assert callable != null : "Callable should be non-null.";
return callable;
}

// if we have multiple candidates.
if (instanceToCallable.values().size() > 1)
// we cannot accurately select one.
LOGGER.warning(
"Multiple (" + instanceToCallable.values().size() + ") callable targets found.");

return null;
}

Expand Down

0 comments on commit 7777bf3

Please sign in to comment.