[Paper Review] NatGen: Generative pre-training by "Naturalizing" source code
NatGen proposes a novel pre-training objective—'naturalizing' code—where models learn to convert semantically equivalent but unnatural code into human-like, natural code. By applying six semantic-preserving transformations to real code and fine-tuning on code generation, translation, and refinement tasks, NatGen achieves state-of-the-art performance, especially in zero-shot and few-shot settings, outperforming models like CodeT5.
Pre-trained Generative Language models (e.g. PLBART, CodeT5, SPT-Code) for source code yielded strong results on several tasks in the past few years, including code generation and translation. These models have adopted varying pre-training objectives to learn statistics of code construction from very large-scale corpora in a self-supervised fashion; the success of pre-trained models largely hinges on these pre-training objectives. This paper proposes a new pre-training objective, "Naturalizing" of source code, exploiting code's bimodal, dual-channel (formal & natural channels) nature. Unlike natural language, code's bimodal, dual-channel nature allows us to generate semantically equivalent code at scale. We introduce six classes of semantic preserving transformations to introduce un-natural forms of code, and then force our model to produce more natural original programs written by developers. Learning to generate equivalent, but more natural code, at scale, over large corpora of open-source code, without explicit manual supervision, helps the model learn to both ingest & generate code. We fine-tune our model in three generative Software Engineering tasks: code generation, code translation, and code refinement with limited human-curated labeled data and achieve state-of-the-art performance rivaling CodeT5. We show that our pre-trained model is especially competitive at zero-shot and few-shot learning, and better at learning code properties (e.g., syntax, data flow).
Motivation & Objective
- Address the scarcity of high-quality labeled data for code generation and other generative software engineering tasks.
- Leverage code's dual-channel nature (formal and natural language) to create a self-supervised pre-training objective that improves model understanding and generation.
- Develop a scalable, unsupervised method to pre-train models on large-scale open-source code by transforming natural code into unnatural forms and requiring restoration to the original.
- Improve model generalization and performance in low-resource settings (zero-shot and few-shot) through a pre-training task that emphasizes code naturalness and semantic fidelity.
- Demonstrate that learning to 'edit' code—i.e., naturalize it—leads to better performance on downstream generative tasks compared to standard denoising objectives.
Proposed method
- Apply six classes of semantic-preserving transformations to real, natural source code to generate 'unnatural' variants, preserving meaning while altering syntax and structure.
- Use the original, natural code as the target and the transformed code as input, training the model to reconstruct the original form in a sequence-to-sequence manner.
- Design the pre-training objective as a denoising autoencoder task, where the model learns to map unnatural code to its natural equivalent, requiring deep semantic and syntactic understanding.
- Pre-train a transformer-based model (NatGen) using this naturalization objective on large-scale open-source code corpora without human-annotated supervision.
- Fine-tune the pre-trained NatGen model on three downstream generative tasks: code generation, code translation, and code refinement, using limited human-curated labeled data.
- Adopt an encoder-decoder architecture similar to CodeT5, but replace its pre-training objectives with the naturalization task to improve generation quality and transferability.
Experimental results
Research questions
- RQ1Can a pre-training objective based on naturalizing code—transforming unnatural code into natural, developer-written code—improve model performance on downstream generative software engineering tasks?
- RQ2How does the naturalization pre-training objective compare to standard denoising objectives (e.g., masked token prediction) in terms of downstream performance, especially in low-resource settings?
- RQ3To what extent does the naturalization task enhance a model’s ability to learn syntactic and semantic properties of code, such as data flow and structure?
- RQ4Does the dual-channel nature of code (formal and natural language) enable a more effective self-supervised pre-training objective than traditional approaches?
- RQ5Can a model pre-trained on naturalization generalize effectively to zero-shot and few-shot settings without extensive fine-tuning?
Key findings
- NatGen achieves state-of-the-art performance on code generation, code translation, and code refinement tasks, rivaling or exceeding the performance of CodeT5.
- The model shows superior performance in zero-shot and few-shot learning settings, indicating strong generalization from the naturalization pre-training objective.
- NatGen generates code that is syntactically and semantically closer to human-written code than models pre-trained with standard denoising objectives.
- The naturalization pre-training task effectively teaches the model to understand and generate code with better structural and semantic fidelity, as evidenced by improved performance on code properties like data flow.
- The method is scalable and self-supervised, requiring no human-annotated data for pre-training, relying solely on automated transformations of existing code.
- Fine-tuning NatGen on limited labeled data yields strong results, demonstrating the effectiveness of the pre-training objective in low-data regimes.
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.