Coder Reviewer Reranking for Code Generation

Tianyi Zhang, Tao Yu, Tatsunori Hashimoto, Mike Lewis, Wen-Tau Yih, Daniel Fried, Sida Wang

TL;DR

Standard code generation models often fall into the trap of preferring "degenerate" solutions—like empty functions or repetitive loops—simply because they have high model likelihood. This paper introduces Coder-Reviewer Reranking, a method that uses the same language model to both generate code and "review" it by calculating how likely the original instruction was given the generated code, leading to absolute accuracy gains of up to 17%.

What Problem Does This Paper Address?

Large Language Models (LLMs) are surprisingly good at writing code, but they have a "short-sighted" streak. The standard way to use them is to sample multiple candidate programs and pick the one the model finds most probable (ranking by $p(y|x)$, where $x$ is the instruction and $y$ is the code).

However, raw model likelihood is biased. It tends to favor very short, trivial solutions (like a function that just says pass) or highly repetitive ones. Paradoxically, as you sample more candidates, the performance of these "Coder-only" models often decreases because the probability of a high-scoring but "degenerate" solution appearing increases. The authors aim to fix this by introducing a "Reviewer" that provides a second opinion.

Methods

The authors propose a dual-model approach called Coder-Reviewer Reranking:

  1. The Coder ($p(y|x)$): This is the standard generative direction. Given a natural language instruction, the model samples diverse code candidates.
  2. The Reviewer ($p(x|y)$): This is the "inverse" direction. The model is prompted to evaluate the likelihood of the original instruction given the generated code. If the code is a generic "pass" statement, it won't explain the specific, detailed instruction well, and the Reviewer will give it a low score.
  3. The Consensus: Candidates are reranked based on the product of both scores: $p(y|x)p(x|y)$.

Mathematically, this is a specific instantiation of the Maximum Mutual Information (MMI) objective. It favors solutions that have high mutual information with the instruction, effectively down-weighting "generic" code that could apply to any prompt.

Crucially, this is implemented via prompting. No extra training is required. They simply flip the prompt order (Code $\rightarrow$ Instruction) and use the same underlying LLM. They also include a "Degenerate Solution Rejection" step—simple heuristics to filter out empty or extremely repetitive code before reranking.

Datasets

The researchers evaluated their method across six datasets spanning different programming languages and settings:

  • Zero-shot Python: HumanEval (hand-written problems), MBPP-Sanitized (crowdsourced), and Plotting (a subset of DS-1000 involving complex matplotlib tasks).
  • Few-shot (3-shot): MBPP (original version), Spider (Natural Language to SQL), and NL2Bash (Natural Language to Bash commands).

They tested eight different models, including the Codex family (davinci-002), InCoder, and CodeGen, ranging from 1B to 175B parameters.

Key Insights and Ideas

  • The "More is More" Stability: Unlike Coder-only ranking, where accuracy often drops as you increase the number of samples, Coder-Reviewer is remarkably stable. It actually benefits from more samples because the Reviewer is robust enough to filter out the "lucky" degenerate solutions that fool the Coder.
  • Consensus Filters Noise: A Coder might like a short program; a Reviewer might like a program that just repeats the prompt back as a comment. Only a correct program satisfies both directions of the "conversation."
  • Execution-Free Reranking: Many state-of-the-art rerankers rely on executing the code and checking outputs (like MBR-EXEC). Coder-Reviewer doesn't require execution, making it applicable to languages or tasks where setting up a runtime environment is difficult (like Bash or complex data visualization).
  • MMI via Prompting: The paper demonstrates that sophisticated objectives like MMI can be achieved through clever zero-shot and few-shot prompting of off-the-shelf models, rather than complex architectural changes.

Key Results

  • Significant Accuracy Boosts: Coder-Reviewer consistently outperformed Coder-only reranking, with absolute accuracy improvements of up to 17%.
  • Beating Execution-Based Methods: On datasets like HumanEval, Coder-Reviewer actually outperformed execution-based methods (MBR-EXEC). This is likely because MBR-EXEC is only as good as its test cases; if the test cases in the prompt are poor, execution-based ranking fails, whereas the Reviewer can still "understand" the intent.
  • Broad Applicability: The method worked across all three model families and all three programming languages (Python, SQL, Bash), proving it isn't just a quirk of one specific model.

Limitations and Future Work

  • Infilling Models: The current study focused on left-to-right generation. The authors noted that applying this to "infilling" models (which generate code in the middle of a file) is a logical next step but wasn't covered here.
  • Computational Overhead: To rerank, you have to run a forward pass of the model for every candidate to get the Reviewer score. For 100 candidates, this doubles the inference cost compared to Coder-only ranking.
  • Prompt Sensitivity: While the method is "easy to implement," the quality of the Reviewer depends on the prompt. Finding the "perfect" Reviewer prompt for every possible task remains an open area for optimization.

Summary updated

BibTeX
@inproceedings{zhang2023coder,
  author = {Tianyi Zhang and Tao Yu and Tatsunori Hashimoto and Mike Lewis and Wen-Tau Yih and Daniel Fried and Sida Wang},
  title = {{Coder Reviewer Reranking for Code Generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=jLbQTKnP1s_},
}
Figure