RLTF: Reinforcement Learning from Unit Test Feedback

Jiate Liu, Yiqin Zhu, Kaiwen Xiao, Qiang Fu, Xiao Han, Yang Wei, Deheng Ye

Abstract

The goal of program synthesis, or code generation, is to generate executable code based on given descriptions. Recently, there has been an increasing number of studies employing reinforcement learning (RL) to improve the performance of large language models (LLMs) for code. However, some of the current representative RL methods have only used offline frameworks, limiting the exploration of new sample spaces. Additionally, the utilization of unit test signals is limited, not accounting for specific error locations within the code. To address these issues, we proposed RLTF, i.e., Reinforcement Learning from Unit Test Feedback, a novel online RL framework with unit test feedback of multi-granularity for refining code LLMs. Our approach generates data in real-time during training and simultaneously utilizes fine-grained feedback signals to guide the model towards producing higher-quality code. Extensive experiments show that RLTF achieves state-of-the-art performance on the APPS and the MBPP benchmarks.

Introduction

Generating functional code is significantly more challenging than generating natural language. While a sentence with a minor typo remains readable, a single character error in code can lead to a compilation failure or a runtime crash. Current approaches to improving code LLMs via Reinforcement Learning (RL) face two major hurdles: offline bottlenecks and coarse-grained feedback.

Many existing models, such as CodeRL, rely on offline frameworks that train on static datasets. This prevents the model from exploring new sample spaces or interacting dynamically with a compiler during training, often leading to distribution shifts. Furthermore, most RL methods use a binary "all-or-nothing" reward—either the program passes all tests or it doesn't. This coarse-grained approach fails to inform the model where it went wrong. If a 50-line program has a single index error on line 27, penalizing the entire script equally is an inefficient way to learn.

Feature comparison between RLTF and existing code generation RL frameworks.

The RLTF Framework

RLTF addresses these limitations through two primary innovations: an online RL framework and multi-granularity feedback.

Online RL Framework

Unlike offline methods, RLTF utilizes two LLMs with shared weights. One model generates code in real-time and interacts with a compiler and unit test suite. These results are stored in a dynamic online buffer. The second model then samples from this buffer to update its weights. This closed-loop system allows the model to learn from its most recent mistakes and explore a wider variety of code solutions dynamically.

Multi-granularity Feedback

The core of RLTF is its nuanced reward system, which moves beyond binary pass/fail signals:

  • Coarse-grained: Standard rewards based on whether the code passed, failed, or had a syntax error.
  • Fine-grained: If a runtime error occurs (e.g., TypeError or IndexError), the system identifies the specific line responsible. It applies a targeted penalty only to that segment of code, helping the model pinpoint the bug.
  • Adaptive Feedback: For programs that pass some but not all tests, the model receives a reward proportional to the ratio of passed test cases, providing a smoother gradient for improvement.
Overview of the RLTF framework showing the online RL loop and multi-granularity feedback.

Experimental Results

The researchers evaluated RLTF on two major Python benchmarks: the APPS (Automated Programming Progress Standard) dataset, containing 10,000 problems ranging from introductory to competition-level, and the MBPP (Mostly Basic Programming Problems) dataset.

RLTF achieved state-of-the-art (SOTA) results on the APPS benchmark using a CodeT5 (770M) base model. On "Introductory" level problems, RLTF achieved a pass@1 score of 8.40% (with Critic Sampling), a significant jump over CodeRL’s 7.08%. On the MBPP benchmark, RLTF reached a pass@1 of 30.4% in zero-shot evaluations, surpassing the previous best of 26.1%. The method also proved robust when scaled to larger architectures like CodeGen-2.7B.

Discussion and Future Work

Ablation studies confirmed that fine-grained feedback provides the most significant performance boost; telling a model exactly which line is broken is far more effective than simply labeling the code as incorrect. Additionally, the online buffer helps the model avoid distribution shifts by generating fresh data during training.

Currently, sub-error types are categorized manually for Python, which may limit immediate transferability to other programming languages. Future work will investigate the use of static code analyzers (like linters) to provide even more detailed feedback signals and automate error categorization for multi-language support.

Summary updated

BibTeX
@article{liu2023rltf,
  author = {Jiate Liu and Yiqin Zhu and Kaiwen Xiao and QIANG FU and Xiao Han and Yang Wei and Deheng Ye},
  title = {{RLTF: Reinforcement Learning from Unit Test Feedback}},
  journal = {Accepted by TMLR},
  year = {2023},
  url = {https://openreview.net/forum?id=hjYmsV6nXZ},
}
Figure