NatGen: Generative pre-training by "Naturalizing" source code

Saikat Chakraborty, Toufique Ahmed, Yangruibo Ding, Premkumar T. Devanbu, Baishakhi Ray · DOI

Abstract

While large language models (LLMs) have demonstrated significant prowess in code-related tasks, they often struggle to capture the "naturalness" of human-written code—the stylistic and structural conventions that developers use to make logic readable. This paper introduces NatGen, a novel pre-training framework that employs a "Naturalization" objective. By training a model to transform semantically equivalent but "un-natural" code back into standard, developer-written code, NatGen learns to master both the formal logic of programming and the human-centric patterns of software engineering. Experimental results show that NatGen achieves state-of-the-art performance across various benchmarks, particularly excelling in low-data and zero-shot scenarios.

Introduction

The availability of massive repositories of raw source code on platforms like GitHub has fueled the development of code-based LLMs. However, high-quality labeled data for specific downstream tasks—such as code translation or bug fixing—remains scarce and expensive to produce. Current pre-training strategies typically rely on "denoising" objectives, where models learn to fill in missing tokens or correct syntax.

The authors of NatGen argue that these tasks fail to capture the "naturalness" of code. Code is inherently bimodal: it possesses a formal channel (strict logic interpreted by compilers) and a natural channel (identifiers, comments, and stylistic choices intended for humans). NatGen bridges this gap by forcing the model to act as a professional editor, reconstructing polished, natural code from logically sound but stylistically distorted versions.

The Naturalization Framework

The core innovation of NatGen is the Naturalization task. The researchers developed a pipeline to "de-naturalize" high-quality code using six semantic-preserving transformations. These transformations alter the natural channel while keeping the formal logic intact:

  1. Loop Transformation: Interchanging while loops and for loops.
  2. Dead Code Injection: Inserting non-executable code blocks that do not affect the output.
  3. Block and Operand Swap: Reversing if-else blocks by negating conditions or swapping operands (e.g., changing a < b to b > a).
  4. Confusing Code Insertion: Replacing straightforward assignments with ternary operators or shorthand increments (e.g., i++).
  5. Variable Renaming: Replacing descriptive, human-readable variable names with generic identifiers like var_1.

NatGen, based on a 12-layer Transformer architecture, is trained to take this distorted input and reconstruct the original "natural" code. This process requires the model to simultaneously understand the underlying semantics (to preserve logic) and the stylistic preferences of human developers (to predict the most likely natural form).

Experimental Setup

NatGen was pre-trained on a diverse corpus including CodeSearchNet (covering Java, Python, Go, JavaScript, Ruby, and PHP) and an additional 2 million functions for C and C# scraped from GitHub.

To evaluate its performance, the model was fine-tuned and tested on several key benchmarks:

  • Concode: For text-to-code generation.
  • CodeXGLUE: Specifically for Java-to-C# translation.
  • Refinement/BugFix: Utilizing small and medium datasets focused on code repair.

Key Insights and Results

The "Editor" Analogy and Semantic Mastery

NatGen functions similarly to a professional book editor. To fix a poorly written sentence, an editor must first understand the author's intent. By "editing" code, NatGen develops a deeper representation of intent than models that simply perform token-level completion. This is reflected in NatGen's high "Syntax Match" and "Dataflow Match" scores, indicating it generates complete, functional programs rather than fragmented code.

Superiority in Low-Data Scenarios

One of NatGen's most significant advantages is its efficiency in few-shot learning. In scenarios with only 100 to 500 training examples, NatGen significantly outperformed CodeT5. In Java-to-C# translation tasks with minimal data, the performance gap was particularly pronounced, demonstrating that the Naturalization task prepares the model for real-world generation tasks even before extensive fine-tuning.

The Challenge of Variable Renaming

Ablation studies revealed that "naturalizing" variable names is the most difficult sub-task, with the model achieving only approximately 40% exact match. This underscores the high degree of human context and domain knowledge embedded in naming conventions, which remains a frontier for code understanding.

Benchmarking Performance

NatGen achieved state-of-the-art results across code generation, translation, and bug fixing. Notably, in reconstruction tasks, NatGen achieved a 97.31 CodeBLEU score, compared to CodeT5’s 9.74. This massive disparity proves that NatGen has a much stronger grasp of how developers actually structure and write their work.

Limitations and Future Work

Despite its success, NatGen faces challenges when multiple de-naturalization transformations are applied simultaneously; the model often prioritizes fixing only one distortion. Additionally, the current method for injecting dead code is somewhat formulaic, leading to a high "solve" rate (99%) that may not reflect the complexity of real-world obfuscated code. Future research will focus on incorporating more complex refactoring patterns and advanced program analysis to further harden the naturalization task.

Conclusion

NatGen demonstrates that teaching models to recognize and produce "natural" code is a powerful alternative to standard denoising objectives. By focusing on the bimodal nature of source code, NatGen provides a more robust foundation for software engineering automation, particularly in environments where labeled data is at a premium.

Summary updated

BibTeX
@inproceedings{chakraborty2022natgen,
  author = {Saikat Chakraborty and Toufique Ahmed and Yangruibo Ding and Premkumar T. Devanbu and Baishakhi Ray},
  title = {{NatGen: Generative pre-training by "Naturalizing" source code}},
  booktitle = {ESEC/SIGSOFT FSE},
  year = {2022},
  doi = {10.1145/3540250.3549162},
}
Figure