Learning to superoptimize programs

Rudy Bunel, Alban Desmaison, M. Pawan Kumar, Philip H. S. Torr, Pushmeet Kohli

TL;DR

This paper introduces a novel machine learning approach to "superoptimize" computer programs, making them significantly more efficient without changing their functionality. By learning a smarter way to explore possible program transformations, their method outperforms existing state-of-the-art techniques on challenging benchmarks.

What Problem Does This Paper Address?

The core problem is code super-optimization: transforming a given program into a more efficient version while guaranteeing that its input-output behavior remains identical. This is akin to finding the most concise and performant way to express a computation, similar to paraphrasing in natural language but with a focus on execution speed. While compilers perform optimization, they don't guarantee finding the absolute best version. Existing methods like stochastic search (e.g., Stoke) explore possible program modifications but do so using a fixed, "blind" strategy for proposing changes. This means they might waste time exploring irrelevant or inefficient transformations, especially when the program's nature (e.g., bitwise operations) suggests more promising avenues. The paper aims to overcome this by making the search process "smarter" and more informed.

Methods

The paper's key innovation is learning the proposal distribution for a stochastic search algorithm. Instead of using a generic, uniform distribution to suggest program modifications (like Stoke), they train a model to predict which modifications are more likely to lead to better programs.

Here's a breakdown:

  • Stochastic Search (MCMC): The foundation is a Markov Chain Monte Carlo (MCMC) sampling approach, specifically using the Metropolis algorithm. This involves starting with an initial program and iteratively proposing changes (rewrites). Each proposed change is accepted or rejected based on whether it preserves correctness and improves efficiency (e.g., reduces runtime).
  • Proposal Distribution: This is the crucial component. It dictates how new program modifications are generated. The authors propose to parameterize this distribution and learn its parameters.
  • Reinforcement Learning (REINFORCE): To learn the proposal distribution's parameters, they employ a reinforcement learning technique called the REINFORCE algorithm (also known as the score function estimator). This allows them to estimate gradients of the expected improvement, enabling them to adjust the proposal distribution to favor more promising transformations.
  • Two Model Types:
    • Bias Model: A simpler model that learns a general bias for proposing moves, not conditioned on the specific input program. This acts as a learned prior.
  • Multi-Layer Perceptron (MLP) Model: A more sophisticated model that takes features of the input program (e.g., its opcodes) and uses an MLP to generate a conditional proposal distribution. This allows the search to be tailored to the specific program being optimized.

Datasets

The authors evaluate their approach on two types of datasets:

  • Hacker's Delight Corpus: A collection of 25 well-known bit-manipulation tasks. They augment this by running Stoke to generate various optimized versions of these programs, creating a larger dataset for training and evaluation. This dataset is relatively homogeneous in terms of task types.
  • Automatically Generated Programs: To address the potential simplicity of the Hacker's Delight tasks, they synthesize a larger, more diverse dataset of programs. This is done by running Stoke in a "random walk" mode (where all moves have equal cost) for many iterations. This creates a more challenging benchmark.

Key Insights and Ideas

  • Informed Search is Crucial: The core insight is that a "blind" proposal distribution in stochastic search is a significant bottleneck. By learning to propose more relevant transformations, the search becomes much more efficient.
  • Learning a "Good Guess": The learned proposal distribution acts like a "good guess" for which program modifications are likely to be beneficial. This dramatically speeds up the search process compared to random exploration.
  • Conditional Proposals Offer More Power: The MLP-based model, which conditions its proposals on the input program's features, generally outperforms the simpler "bias" model, especially on more diverse datasets. This highlights the benefit of tailoring the search strategy to the specific problem.
  • Beyond Super-optimization: The authors suggest their approach is not limited to code super-optimization and could be applied to other stochastic search problems where learning an informed proposal distribution is beneficial.

Key Results

  • Significant Performance Improvement: Both the "bias" and "MLP" learned proposal distributions significantly outperform the baseline uniform proposal distribution (used by Stoke) on the Hacker's Delight benchmark. The MLP model achieved a final average relative score of 31.51% compared to the uniform baseline's 53.71% (lower is better, indicating greater improvement).
  • Better Generalization on Diverse Data: On the automatically generated programs, the MLP model also showed superior performance, achieving a score of 62.27% compared to the uniform baseline's 78.15%.
  • Faster Convergence: Figure 3 visually demonstrates that the learned proposal distribution achieves better scores in fewer iterations than the uniform distribution. In one example, the learned model achieved better results with 100 iterations than the uniform model did with 400 iterations.
  • Minimal Throughput Impact: Despite the added complexity of learning, the overhead for sampling from the learned proposal distribution was minimal, allowing for efficient MCMC iterations (Table 3).

Limitations and Future Work

  • Program Representation: The authors acknowledge that obtaining informative representations of programs remains a challenge. Their current approach might ignore crucial structural information (like temporal aspects) due to limited data.
  • Data Diversity: For mature domains like computer vision, rich representations are available. For programs, especially synthetic ones, gathering enough diverse and structured data to learn truly powerful representations is difficult.
  • Future Work:
    • Gathering larger datasets of frequently used programs to better measure practical performance.
  • Exploring more sophisticated program representations to capture richer structural information.
  • Applying the learned proposal distribution concept to other stochastic search problems.

Summary updated

BibTeX
@inproceedings{bunel2016learning,
  author = {Rudy Bunel and Alban Desmaison and M. Pawan Kumar and Philip H. S. Torr and Pushmeet Kohli},
  title = {{Learning to superoptimize programs}},
  booktitle = {Oxford},
  year = {2016},
  url = {https://openreview.net/forum?id=yO6u1m9P-zv},
}
Figure