NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Latent Programmer: Discrete Latent Codes for Program Synthesis

Joey Hong, David Dohan, Rishabh Singh, Charles Sutton, Manzil Zaheer

TL;DR

Program synthesis—automatically writing code from a specification—is notoriously difficult because the space of possible programs is astronomical. The Latent Programmer (LP) addresses this by mimicking how humans code: it first generates a high-level "plan" as a sequence of discrete latent tokens and then "fills in the blanks" to produce the final code. By searching through this learned plan space, LP achieves a significant boost in accuracy, particularly for long and complex programs where traditional search methods often fail.

The Search Problem in Program Synthesis

The core challenge in program synthesis is the "search problem." When trying to generate a program based on a few input-output examples (e.g., transforming "Alice Smith" to "Smith, A."), there are millions of ways to combine code tokens to achieve the desired result.

Standard approaches usually search for the program token-by-token (similar to a standard language model) or use human-defined "sketches" (templates with missing holes to be filled). However, token-by-token search easily gets bogged down in syntactic details, and manual sketches are difficult to scale across different programming languages. This paper asks a fundamental question: Can a model learn to generate high-level plans automatically, without a human having to define what a "plan" looks like?

Methods: A Two-Level Search Framework

To solve this, the authors introduce a two-level search framework powered by a discrete autoencoder, specifically leveraging a variation of the Vector Quantized Variational Autoencoder (VQ-VAE).

Latent Programmer Architecture

The system consists of three main Transformer-based components:

  1. Program Encoder: During training, this module "compresses" a real program into a short sequence of discrete latent tokens, effectively creating a "plan."
  2. Latent Predictor: This component learns to predict that plan sequence directly from the user's specification, such as input/output examples or a docstring.
  3. Latent Program Decoder: Finally, this module takes the predicted plan and the original specification to generate the final, executable code.

Instead of using continuous numbers—which are notoriously hard to search through—the model uses a "codebook" of discrete symbols. Each symbol represents a chunk of program logic. At inference time, the model first performs a beam search to find the most likely plans. Then, for each plan, it performs another search to find the best programs. This allows the model to explore broad structural strategies before committing to specific syntax.

Datasets and Key Insights

The researchers evaluated LP on two distinct domains: a String Transformation DSL (a synthetic dataset of 25 million tasks involving regex-like string manipulations) and Python Code Generation (111,000 pairs of Python docstrings and corresponding code snippets from GitHub).

The Latent Programmer revealed several key insights:

  • Learned Abstraction: Unlike previous "sketching" methods that require experts to define holes in code, LP discovers its own abstractions. The model decides which parts of the program are high-level structural components and which are minor details.
  • Temporal Compression: The latent codes provide a form of temporal abstraction. One latent token roughly corresponds to four program tokens, allowing the model to "think" in larger blocks.
  • Semantic Interpretability: Even though the latent tokens are learned in an unsupervised way, they often map to clear semantic concepts. Specific tokens consistently appeared when the model was tasked with operations like "extracting the last word" or "finding the first number."
  • Search Diversity: Searching in the "plan space" prevents the model from getting stuck in local minima. A small change in the plan can lead to a massive, structurally different change in the program.

Key Results

The two-level search approach yielded substantial improvements over existing baselines.

Accuracy comparison across different beam sizes
  • Accuracy Boost: On the string transformation task, LP achieved 68% accuracy (with a beam size of 100), outperforming the strong "RobustFill" baseline (61%) and other sketch-based methods like SketchAdapt.
  • Scaling with Complexity: The most impressive result was how LP handled program length. For the longest programs (10 expressions), LP was nearly twice as accurate as the baseline Transformer (25.6% vs 14.4%).
  • Python Performance: In the Python generation task, LP achieved a BLEU score of 21.3, a significant jump over the standard Transformer's 17.2.

Limitations and Future Work

While the latent codes are highly effective, they are statistical patterns rather than logical guarantees, meaning they aren't explicitly "grounded" in the strict semantics of the programming language. Furthermore, the current model doesn't "run" the code during the search process. The authors suggest that combining LP with execution-guided synthesis—where the model tests partial programs against the I/O examples—could lead to even higher performance. Finally, performance is sensitive to the size of the latent vocabulary and the compression factor, making the search for the "sweet spot" an ongoing engineering challenge.

Summary updated

BibTeX
@inproceedings{hong2021latent,
  author = {Joey Hong and David Dohan and Rishabh Singh and Charles Sutton and Manzil Zaheer},
  title = {{Latent Programmer: Discrete Latent Codes for Program Synthesis}},
  booktitle = {International Conference on Machine Learning},
  year = {2021},
  url = {https://openreview.net/forum?id=FfNoKgrnubm},
}
Figure