Authors: Wenting Zhao, Ibrahim Abdelaziz, Julian Dolby, Kavitha Srinivas, Mossad Helali, and Essam Mansour
Introduction
Python has become the primary language for Artificial Intelligence (AI) and Data Science. However, its dynamic nature—characterized by dynamic typing and heavy reliance on native C/C++ extensions (e.g., NumPy, PyTorch)—presents significant challenges for static analysis. Traditional tools often struggle to disambiguate code constructs without execution, leading to a "blind spot" that hinders the development of advanced IDE features and automated machine learning (AutoML) systems.
We present Serenity, a novel static analysis framework designed to handle Python’s notoriously dynamic nature by treating complex libraries as opaque abstractions and using dataflow analysis to provide superior context to AI models.
The Serenity Framework
Serenity relies on three primary innovations to make the analysis of complex Python code feasible and performant:
1. Dynamic Dispatch Modeling
Rather than attempting to resolve every ambiguous call to a single type, Serenity treats uncertainty as a first-class citizen. It models ambiguous constructs as "dynamic dispatches" over a set of possible types, allowing the analysis to proceed without making brittle assumptions that often lead to failure in traditional static tools.
2. The "Turtles" Model for Library Abstraction
Analyzing the internal implementation of massive libraries like NumPy is often computationally prohibitive or impossible when the source code is in C. Borrowing the phrase "turtles all the way down," Serenity treats library-returned objects as opaque types called "Turtles." Instead of analyzing how a library function works, Serenity tracks the resulting "Turtle" object and monitors subsequent method calls to infer its behavior and context.
3. Dataflow Slicing
Serenity constructs a comprehensive dataflow graph of the program. For tasks like code completion, the framework performs "dataflow slicing" to isolate the specific code paths relevant to a target variable. This allows the system to capture dependencies that may be separated by hundreds of lines of code, effectively "teleporting" relevant context into the model's prompt.
Experimental Setup and Datasets
The framework was evaluated using two primary benchmarks:
- ETH150K: A standard dataset of 150,000 Python files used for training and testing code completion models.
- AutoML Benchmarks: 77 datasets from sources like Kaggle, OpenML, and PMLB were utilized to assess Serenity's ability to synthesize machine learning pipelines.
Key Results and Insights
Boosting Code Completion Accuracy
By providing Large Language Models (LLMs) with precise context via dataflow slices, Serenity significantly improves prediction accuracy. For instance, fine-tuning the CuBERT model with Serenity’s slices increased Top-1 accuracy from a 21% baseline to 69%. Top-5 accuracy reached 78%.
Static AutoML with KGpip
Serenity powers KGpip, an AutoML system that statically mines thousands of existing scripts to predict optimal pipelines for new datasets. KGpip achieved an average performance score of 0.77, outperforming established dynamic tools like AutoSklearn (0.71) and FLAML (0.71) without requiring expensive trial-and-error execution.
Handling the Long Tail
The combination of raw text and dataflow analysis proved particularly effective at predicting "rare" function calls—those appearing infrequently in the training data—which typically pose the greatest challenge for generative models.
Conclusion and Future Work
Serenity demonstrates that a "soundy" analysis—one that prioritizes practical utility and smart trade-offs over theoretical perfection—can unlock significant performance gains in AI-driven developer tools. While current limitations include challenges with certain Python primitives and parser-related mapping errors, future work will focus on modeling built-in types and integrating dataflow slices into larger generative models like GitHub Copilot.
Summary updated