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

Top Pass: improve code generation by pass@k-maximized code ranking

Zhi-Cun Lyu, Xinye Li, Zheng Xie, Ming Li

Abstract

While Large Language Models (LLMs) have demonstrated remarkable capabilities in code generation, their practical utility is often hampered by the need for extensive sampling to find a correct solution. We introduce Top Pass, a novel ranking framework that directly optimizes the pass@k metric. Unlike traditional classification-based rankers, Top Pass ensures that correct, high-quality code candidates are prioritized at the top of the list, significantly reducing human verification effort. Our evaluation across multiple benchmarks shows that Top Pass consistently outperforms existing state-of-the-art methods, achieving up to a 32.9% relative improvement in pass@1 on challenging competitive programming tasks.

Introduction

The current paradigm for solving complex programming tasks with LLMs involves generating a large pool of candidates (e.g., 100–1000) and relying on test cases to identify a functional solution. However, this "sample and hope" strategy creates a significant bottleneck: human developers typically only have the patience to inspect the top few results. This efficiency is measured by pass@k, the probability that at least one of the top $k$ ranked candidates is correct.

Existing code rankers often treat this as a binary classification problem, attempting to predict whether a snippet is correct. This approach fails to account for the relative ranking of candidates and is susceptible to "false positives"—buggy code that passes weak test cases.

Top Pass improves the usability of code generation systems by ensuring correct candidates appear early in the ranked list.

Methodology: Direct Pass@k Optimization

Top Pass shifts the focus from absolute classification to relative ranking through several key innovations:

1. Ranking vs. Classification

Traditional rankers use cross-entropy loss to classify code as correct or incorrect. Top Pass instead employs a surrogate "hinge square" loss function designed to be differentiable while directly maximizing the pass@k metric. This approach penalizes the model specifically when a correct program is ranked lower than an incorrect one within the critical top $k$ slots.

Comparison between traditional classification-based rankers and the Top Pass ranking approach.

2. Strategic Candidate Selection

To improve generalization and robustness, Top Pass utilizes a selection strategy for training:

  • Top Positives: The model focuses on a proportion ($p$) of high-scoring correct solutions, which typically represent elegant and efficient implementations.
  • Top Negatives: The model identifies "tricky" incorrect solutions—those that might pass some tests or look superficially correct—and ensures they are ranked below the positives.

By ignoring "Bottom Positives" (messy or lucky code) and "Bottom Negatives" (obviously broken code), the model learns to distinguish subtle logical correctness from common pitfalls. The ranker is built upon CodeBERT, a pre-trained model optimized for understanding the relationship between natural language and source code.

Evaluation and Results

We evaluated Top Pass across four major benchmarks: CodeContests (competitive programming), APPS (introductory to competition level), MBPP (basic Python), and HumanEval (functional correctness).

Performance Gains

Top Pass consistently outperformed standalone LLMs and previous state-of-the-art rankers like CodeRanker. On the CodeContests benchmark, Top Pass achieved a 32.9% relative improvement in pass@1 when ranking candidates from DeepSeek-Coder. Furthermore, it more than doubled the effectiveness of ChatGPT, raising its pass@1 score from 2.9% to 7.3%. On the APPS dataset, improvements were observed across all difficulty levels, demonstrating that the model understands complex logic rather than just simple patterns.

Robustness to Weak Supervision

A critical finding is the model's resilience to noisy training data. Even when a high percentage of "correct" labels are actually false positives (buggy code passing weak tests), Top Pass maintains superior ranking quality compared to standard classification methods.

Discussion and Future Work

While Top Pass significantly improves the "human-in-the-loop" experience, it remains a post-processing step. Future research could explore integrating the pass@k loss directly into the initial training or Reinforcement Learning from Human Feedback (RLHF) loops of LLMs to eliminate the inference overhead of generating hundreds of candidates. Additionally, while the current study focused on Python, extending this ranking logic to multi-lingual environments and broader software engineering tasks presents a promising avenue for future development.

Summary updated

BibTeX
@article{lyu2024top,
  author = {Zhi-Cun Lyu and Xinye Li and Zheng Xie and Ming Li},
  title = {{Top Pass: improve code generation by pass@k-maximized code ranking}},
  journal = {Frontiers of Computer Science},
  year = {2024},
}
Figure