Language Models Can Teach Themselves to Program Better

Patrick Haluptzok, Matthew Bowers, Adam Tauman Kalai

Introduction

Training state-of-the-art code generation models typically requires massive amounts of human-authored code. However, this approach has a "ceiling": human code is often buggy, inefficient, and accompanied by ambiguous natural language descriptions.

In games like Go or Chess, models like AlphaZero surpassed humans by playing against themselves. This paper asks: Can we create a similar self-improvement loop for programming? The challenge is that, unlike a game with a clear win condition, "correctness" in programming is hard to evaluate when the problem is described in messy human language. To solve this, the researchers shift the focus from English descriptions to Programming Puzzles—formal, code-based specifications that a machine can verify instantly. By generating their own programming puzzles, solving them, and using a Python interpreter to verify the results, models can create high-quality synthetic training data that more than doubles their performance on human-authored benchmarks.

The Unsupervised Self-Improvement Pipeline

The authors propose a novel pipeline for unsupervised self-improvement, leveraging a Python interpreter as the ultimate judge of correctness.

Self-improvement pipeline
  1. Puzzle Generation: The Language Model (LM) is prompted with a few examples of existing puzzles to "dream up" new ones. These puzzles are formatted as Python functions where a solution must satisfy an assert statement.
  2. Filtering: To ensure quality, the system discards trivial puzzles (e.g., those solved by simple constants) and those that fail to parse.
  3. Puzzle Solving: The LM then attempts to solve these newly generated synthetic puzzles.
  4. Verification (The "Teacher"): A Python interpreter executes the code. Only puzzle-solution pairs that execute correctly and efficiently within a time limit are kept.
  5. Fine-tuning: The LM is fine-tuned on this "verified-correct" synthetic dataset.
  6. Iteration: This process can be repeated, with the newly improved model generating even better puzzles for the next round.

Key Insights and Ideas

  • The Interpreter as a Ground Truth: The core "aha moment" is that while an LLM might hallucinate, a Python interpreter does not. By using the interpreter to filter synthetic data, the model effectively "distills" the logic of the programming language itself.
  • Diversity Over Quantity: Simply generating trillions of basic addition problems isn't helpful. Using UMAP embedding visualizations, the authors show that larger models (like Codex) generate a much more diverse "cloud" of problems, which is why they are more effective for training.
UMAP visualizations of puzzle diversity
  • "Watching a Sibling": Interestingly, a larger model (2.7B parameters) can still improve by training on data generated by a much smaller model (125M), provided that data has been verified by the interpreter. The model learns by "watching its smaller sibling interact with the teacher."
  • Code as Specification: By moving away from English prompts to code-based puzzles, the researchers removed the "semantic gap" that usually leads to errors in code generation.

Datasets and Results

The primary benchmark used is P3 (Python Programming Puzzles), containing 155 training and 228 test puzzles ranging from simple string manipulations to complex algorithmic challenges. The authors also released four massive datasets, each containing 1 million verified puzzle-solution pairs generated by different models (GPT-Neo 125M, 1.3B, 2.7B, and OpenAI’s Codex).

The results show a dramatic performance boost across the board:

  • Doubled Accuracy: GPT-Neo models more than doubled their accuracy on held-out test puzzles after fine-tuning on their own synthetic data.
  • Codex-Powered Gains: When the GPT-Neo 2.7B model was fine-tuned on data generated by the more powerful Codex model, its Pass@100 score jumped from 7.5% to 38.2%.
  • Verification is Critical: In an ablation study, training on unverified synthetic data was significantly less effective than training on verified data, proving that the interpreter's "self-correction" step is the secret sauce.

Limitations and Future Work

While promising, the method has limitations. The authors noted that performance gains began to plateau after two iterations, highlighting the challenge of keeping the "curriculum" progressively harder. Additionally, because the models were fine-tuned exclusively on code puzzles, they risk "catastrophic forgetting" of their ability to understand natural language instructions.

Ultimately, the authors suggest this method could be a stepping stone toward solving longstanding open problems in mathematics or computer science, provided the model can be incentivized to generate and solve increasingly complex algorithmic puzzles.

Summary updated

BibTeX
@inproceedings{haluptzok2023language,
  author = {Patrick Haluptzok and Matthew Bowers and Adam Tauman Kalai},
  title = {{Language Models Can Teach Themselves to Program Better}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=SaRj2ka1XZ3},
}
Figure