Principled and practical static analysis for Python: Weakest precondition inference of hyperparameter constraints

Ingkarat Rak-amnouykit, Ana L. Milanova, Guillaume Baudart, Martin Hirzel, Julian DolbySoftw. Pract. Exp 2024 · DOI

TL;DR

This paper introduces a novel static analysis technique for Python that automatically extracts hyperparameter constraints from machine learning libraries. By inferring "weakest preconditions" for exception-raising code, the analysis can identify undocumented constraints, leading to more robust ML code and improved developer tools.

What Problem Does This Paper Address?

Machine learning (ML) operators, like those in scikit-learn, often have complex configurations controlled by hyperparameters. Incorrectly setting these hyperparameters can lead to runtime exceptions, performance degradation, or suboptimal model accuracy. While documentation often describes these constraints, it's frequently informal, incomplete, or outdated. Manually specifying these constraints for tools like hyperparameter optimizers is tedious and error-prone. This paper addresses the challenge of automatically and reliably extracting these critical hyperparameter constraints directly from the ML library code itself.

Methods

The core of the paper is an interprocedural weakest precondition (IWP) analysis specifically adapted for Python. Here's a breakdown of their key techniques:

  • PetPy: A simplified Python subset designed for formal analysis, allowing them to precisely define semantics for core Python constructs.
  • Intraprocedural Weakest Precondition (WP) Analysis: This standard technique works backward from an exception-raising statement to determine the conditions that must hold before that statement to prevent the exception.
  • Call Graph Construction: To handle exceptions that occur in called functions (interprocedural), they build a call graph that tracks function dependencies.
  • Reference Immutability Analysis: Python's dynamic nature and side effects can make static analysis unsound. They employ a reference immutability system to track mutable and read-only references, helping to ensure the soundness of inferred preconditions.
  • Concrete Evaluation: To manage the complexity of Python and simplify analysis results, they leverage the Python interpreter to evaluate parts of preconditions that can be determined statically. This is a key technique for making the analysis scalable.
  • 3-Address Code Translation: They translate Python code into a 3-address code representation, which is a common intermediate format for many static analysis techniques, including reference immutability.

Datasets

The authors evaluated their approach on several popular Python machine learning libraries. They did not introduce new datasets for training but rather used the source code of these libraries as their "dataset" for analysis.

  • Sklearn-compatible libraries: Sklearn itself, XGBoost, LightGBM, imblearn, category_encoders, MAPIE, metric_learn, sklearn_pandas.
  • Other libraries: TensorFlow, NumPy, Pandas.

Key Insights and Ideas

  • Code is the Ground Truth: The most reliable source for hyperparameter constraints is the actual code, not just the documentation. Exceptions are often the clearest indicators of these constraints.
  • Python's Dynamism is a Double-Edged Sword: Python's rich features (dynamic typing, first-class functions, reflection) make static analysis challenging (path explosion, unsoundness). However, leveraging the Python interpreter for "concrete evaluation" is a powerful way to tame this dynamism and make the analysis tractable.
  • Interprocedural Analysis is Crucial: Many critical constraints are not within a single function but arise from interactions between functions. Their IWP analysis effectively traces these interprocedural paths.
  • Soundness Matters: Simply finding a precondition isn't enough; it needs to be sound (i.e., if the precondition holds, the exception won't fire). Their reference immutability analysis helps ensure this, though it's not perfect due to Python's complexity.
  • JSON Schema for Practicality: Translating inferred constraints into JSON Schema makes them directly usable for validation tools, bridging the gap between static analysis and practical applications.

Key Results

  • Bug Discovery: The analysis identified real, undocumented constraints in libraries like scikit-learn, leading to 6 merged pull requests.
  • High Precision and Recall: For input validation experiments on 181 ML operators, their analysis achieved 95.6% precision and 75.7% recall, significantly outperforming previous approaches.
  • Soundness: 95.5% of inferred preconditions were judged sound by their analysis.
  • Scalability: The analysis runs in under 11 minutes per operator on average, demonstrating good scalability due to concrete evaluation and pruning heuristics.
  • Comparison: Their automatic approach significantly outperformed hand-written constraints and natural-language documentation extraction techniques in terms of F₁-score.

Limitations and Future Work

  • Inexpressible Clauses: Some complex preconditions are difficult to express in JSON Schema, leading to false negatives.
  • Uninterpreted Constructs: Python has many constructs (e.g., while loops, del, try) that are not precisely interpreted, potentially leading to unsoundness or imprecision.
  • Dynamic Class Loading: The analysis might struggle with code that dynamically loads classes or functions at runtime.
  • Future Work: The authors suggest extending the analysis to handle more Python features, improve precision, and explore applications beyond hyperparameter validation, such as more general program verification. They also highlight the need for better handling of loops and other complex control flow structures.

Summary updated

BibTeX
@article{rakamnouykit2024principled,
  author = {Ingkarat Rak-amnouykit and Ana L. Milanova and Guillaume Baudart and Martin Hirzel and Julian Dolby},
  title = {{Principled and practical static analysis for Python: Weakest precondition inference of hyperparameter constraints}},
  journal = {Softw. Pract. Exp.},
  year = {2024},
  doi = {10.1002/spe.3279},
}
Figure