Abstract
The utilization of programming language (PL) models, pre-trained on large-scale code corpora, as a means of automating software engineering processes has demonstrated considerable potential in streamlining various code generation tasks such as code completion, code translation, and program synthesis. However, current approaches mainly rely on supervised fine-tuning objectives borrowed from text generation, neglecting unique sequence-level characteristics of code, including but not limited to compilability as well as syntactic and functional correctness. To address this limitation, we propose PPOCoder, a new framework for code generation that synergistically combines pre-trained PL models with Proximal Policy Optimization (PPO) which is a widely used deep reinforcement learning technique. By utilizing non-differentiable feedback from code execution and structure alignment, PPOCoder seamlessly integrates external code-specific knowledge into the model optimization process. It's important to note that PPOCoder is a task-agnostic and model-agnostic framework that can be used across different code generation tasks and PLs. Extensive experiments on three code generation tasks demonstrate the effectiveness of our proposed approach compared to SOTA methods, achieving significant improvements in compilation success rates and functional correctness across different PLs.
Introduction
Programming language (PL) models pre-trained on large code corpora have revolutionized automated software engineering. However, most current Large Language Models (LLMs) are trained using supervised fine-tuning (SFT) objectives like cross-entropy loss, which treat code as plain text. This approach fails to capture the unique requirements of programming, such as compilability and functional correctness. Consequently, models often produce "hallucinated" code that looks plausible but fails to execute.
Existing Reinforcement Learning (RL) approaches for code are often restricted to a single language or task. To bridge the gap between text-based generation and logical execution, we introduce PPOCoder, a framework that optimizes for functional correctness through execution-based feedback.
The PPOCoder Framework
PPOCoder is a task-agnostic and model-agnostic framework that treats code generation as a Reinforcement Learning problem. It employs an Actor-Critic architecture where the "Actor" (the code generation model, e.g., CodeT5) generates code, and the "Critic" (a value model) estimates the expected reward for a given state.
Unlike previous RL methods for code that use the REINFORCE algorithm, PPOCoder utilizes Proximal Policy Optimization (PPO). PPO introduces a "trust region" for policy updates, ensuring training stability and preventing catastrophic updates that could degrade model performance. This allows the model to refine its policy without drifting too far from the original pre-trained knowledge.
Multi-Faceted Reward and Optimization
A significant challenge in RL for code is the "sparse reward" problem—the model often only receives feedback after generating a complete sequence. PPOCoder mitigates this by combining four distinct signals into a dense reward function:
- Compiler Feedback: Direct signals based on whether the code compiles or passes unit tests.
- Syntactic Matching (AST): Comparing Abstract Syntax Tree sub-trees to ensure structural alignment with ground truth.
- Semantic Matching (DFG): Using Data Flow Graphs to verify that the underlying logic (variable flow) is correct.
- KL-Divergence Penalty: A regularization term that prevents the model from deviating too far from its original pre-trained knowledge, aiding generalization and preventing memorization.
Experimental Results
We evaluated PPOCoder across tasks including code completion (CodeSearchNet), translation (XLCoST, TransCoder), and program synthesis (APPS, MBPP). The results demonstrate that execution-based feedback allows relatively small models to become highly competitive.
- Compilation Gains: PPOCoder improved the compilation rate of CodeT5 from 52.14% to 97.68% in completion tasks.
- Efficiency and Scale: A 770M parameter PPOCoder-enhanced model outperformed much larger models like GPT-Neo (2.7B) and even GPT-3 (175B) in functional correctness on the APPS benchmark.
- Zero-Shot Transfer: When tested on the MBPP dataset after training on APPS, PPOCoder achieved a pass@80 of 68.2%, surpassing the 137B parameter GPT (61.4%).
Conclusion
PPOCoder demonstrates that integrating execution-based feedback and structural analysis into the optimization process significantly enhances the functional correctness of generated code. By moving beyond text-matching to logic-matching, PPOCoder enables smaller, more efficient models to achieve state-of-the-art performance across diverse programming languages and tasks. Future work will explore more complex reward signals and broader applications in software engineering.
Summary updated