[Paper Review] Energy-Based Models for Code Generation under Compilability Constraints
This paper proposes an Energy-Based Model (EBM) to enforce compilability in neural code generation by constraining autoregressive models to produce only syntactically valid code. Using KL-Adaptive Distributional Policy Gradient, the method fine-tunes a generative model to approximate the EBM, significantly improving compilability rates without sacrificing diversity or complexity—outperforming standard reinforcement learning fine-tuning that causes catastrophic forgetting.
Neural language models can be successfully trained on source code, leading to applications such as code completion. However, their versatile autoregressive self-supervision objective overlooks important global sequence-level features that are present in the data such as syntactic correctness or compilability. In this work, we pose the problem of learning to generate compilable code as constraint satisfaction. We define an Energy-Based Model (EBM) representing a pre-trained generative model with an imposed constraint of generating only compilable sequences. We then use the KL-Adaptive Distributional Policy Gradient algorithm (Khalifa et al., 2021) to train a generative model approximating the EBM. We conduct experiments showing that our proposed approach is able to improve compilability rates without sacrificing diversity and complexity of the generated samples.
Motivation & Objective
- To address the limitation of autoregressive neural code models in generating syntactically correct, compilable code due to myopia and lack of global sequence constraints.
- To formulate code generation under compilability constraints as a constraint satisfaction problem, enabling a principled distributional approach.
- To train a generative model that approximates the constrained EBM distribution while preserving the original model's diversity and complexity.
- To overcome the catastrophic forgetting problem common in reinforcement learning fine-tuning for sequence-level rewards.
- To provide a method that improves compilability without degrading fluency or structural complexity of generated code.
Proposed method
- Define an Energy-Based Model (EBM) that assigns low energy (high probability) only to compilable code sequences, encoding the compilability constraint as a global feature.
- Use the KL-Adaptive Distributional Policy Gradient (KL-DPG) algorithm to train an autoregressive generative model to approximate the EBM distribution.
- Leverage the original pre-trained language model as a prior, minimizing KL divergence between the fine-tuned policy and the original model.
- Apply the EBM constraint during training via a differentiable proxy, enabling end-to-end optimization with policy gradient methods.
- Use compilation feedback as a reward signal to guide the policy toward generating only valid, compilable code sequences.
- Integrate the EBM into a reinforcement learning framework that balances constraint satisfaction and distributional fidelity.
Experimental results
Research questions
- RQ1Can an Energy-Based Model effectively encode compilability as a global constraint in code generation?
- RQ2Can the KL-Adaptive Distributional Policy Gradient algorithm successfully train a generative model to approximate the EBM while preserving diversity?
- RQ3Does enforcing compilability via EBM reduce the risk of catastrophic forgetting compared to standard RL fine-tuning?
- RQ4To what extent does the proposed method improve compilability rates without degrading the complexity or fluency of generated code?
- RQ5How do different fine-tuning strategies (e.g., RL with $R(x)=b(x)$, $R(x)=P(x)$, or KL-DPG) affect the distribution of compilation errors?
Key findings
- The proposed EBM-based method significantly improves compilability rates compared to standard autoregressive models and RL fine-tuning baselines.
- The method maintains high diversity and complexity in generated code, avoiding the severe degradation seen in standard reinforcement learning fine-tuning.
- KL-DPG fine-tuning achieves better distributional alignment with the original model than standard RL methods, reducing catastrophic forgetting.
- The approach effectively reduces compilation errors, particularly in long-range syntactic dependencies, by enforcing global constraints.
- Qualitative analysis shows that the EBM-based method produces fewer and more localized compilation errors compared to baselines.
- The method outperforms both standard RL with heuristic rewards and policy gradient methods that maximize compilation accuracy alone.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.