SMAT-Lab / Scalpel

Scalpel: The Python Static Analysis Framework
Apache License 2.0
286 stars 43 forks source link

Type inferencing and library code #36

Closed khatchad closed 2 years ago

khatchad commented 2 years ago

Consider the following example:

import tensorflow as tf

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)

  @tf.function
  def call(self, x):
    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 main():
    input_data = tf.random.uniform([20, 28, 28])

    eager_model = SequentialModel()
    eager_model(input_data)

if __name__ == "__main__":
    main()

Running the type inferencing on this example, I get something similar to the following on be392671a5f55f5da8b2ca0a079207c55261ab03:

$ python type_infer_tutorial.py
{'file': 'graph_execution_time_comparison.py', 'line_number': 28, 'variable': 'input_data', 'function': 'main', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 30, 'variable': 'eager_model', 'function': 'main', 'type': {'callable'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 10, 'variable': 'num_layers', 'function': '__init__', 'type': {'int'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 16, 'parameter': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 17, 'variable': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 22, 'variable': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 23, 'variable': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 20, 'variable': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 10, 'variable': 'num_layers', 'function': '__init__', 'type': {'int'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 16, 'parameter': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 17, 'variable': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 22, 'variable': 'x', 'function': 'call', 'type': {'any'}}
{'file': 'graph_execution_time_comparison.py', 'line_number': 23, 'variable': 'x', 'function': 'call', 'type': {'any'}}

Question: is it possible to know that input_data is of type Tensor?

Jarvx commented 2 years ago

Hi, inferring input types for a pure static analysis tool is challenging. We are still working on some algorithms to improve the performance of this part.

khatchad commented 2 years ago

Yes, of course. I completely understand! Thank you your hard work and prompt responses!

Jarvx commented 2 years ago

Yes, of course. I completely understand! Thank you your hard work and prompt responses!

Thanks for your interest. We are working on better inference algorithms at the moment. I will now close this issue. Please feel free to reopen if you still want to discuss.