NaturalCC: A Toolkit to Naturalize the Source Code Corpus

Yao Wan, Yang He, Jianguo Zhang, Yulei Sui, Hai Jin, Guandong Xu, Caiming Xiong, Philip S. YuarXiv 2020 · arXiv

Abstract

NaturalCC is an open-source, modular toolkit built on PyTorch and Fairseq designed to streamline research in "Big Code" analysis. It provides a unified platform for tasks like code completion, comment generation, and retrieval, addressing the reproducibility crisis in software engineering research. By standardizing data pipelines and baseline implementations, NaturalCC allows researchers to focus on model innovation rather than infrastructure.

Introduction

In recent years, the "naturalness hypothesis"—the idea that programming languages share statistical properties with natural languages—has fueled a surge in deep learning models for software engineering. This has led to AI capable of completing code, writing documentation, and searching repositories using natural language. However, the field faces a major hurdle: reproducibility. Deep learning models are notoriously sensitive to specific datasets and hyperparameter tuning. Without a unified, open-source framework, researchers often spend more time wrestling with data pipelines than innovating. NaturalCC aims to be the "Fairseq of Code," providing a standardized environment where NLP and Software Engineering (SE) researchers can meet.

System Architecture

NaturalCC is built on top of Fairseq and PyTorch, inheriting their robust performance and flexibility. The toolkit’s architecture is divided into three main stages, as illustrated in the modular workflow below:

  1. Data Preprocessing: This stage handles tokenization (including Byte Pair Encoding), vocabulary building, and the extraction of structural features like Abstract Syntax Trees (ASTs) and control-flow graphs.
  2. Code Representation: The "brain" of the toolkit, including neural encoders ranging from standard RNNs and Transformers to specialized Graph Neural Networks (GNNs) that capture structural logic. It also supports pre-trained models like BERT and RoBERTa.
  3. Applications: The framework currently supports three major downstream tasks: code completion, comment generation, and semantic code retrieval.
The modular architecture of NaturalCC, illustrating the pipeline from raw code to downstream applications.

A standout feature is the Registry Mechanism. By using Python decorators (like @register_task), developers can add new models or tasks into the framework without modifying the core engine. This modularity allows researchers to swap components, such as an RNN encoder for a Transformer, with minimal code changes.

Datasets and Benchmarks

To ensure fair comparisons, NaturalCC integrates several pre-cleaned benchmark datasets:

  • CodeSearchNet: Used primarily for semantic code retrieval.
  • Py150: A massive collection of Python files for evaluating code completion.
  • Python & Java Datasets: Specifically curated for code comment generation (summarization) tasks.

User-Centric Design and Efficiency

Training on "Big Code" is computationally expensive. NaturalCC supports Mixed-Precision (FP16) calculation and multi-GPU training out of the box, significantly reducing memory usage and speeding up iteration cycles. Beyond the research-heavy Command Line Interface (CLI), the authors provide a Graphical User Interface (GUI) for quick demonstrations, making the models accessible to non-experts.

The NaturalCC GUI demonstrating a code completion task using language modeling.

Key Results

The toolkit serves as a system description, proving its utility by implementing several high-performance baselines:

  • Code Completion: Implemented SeqRNN and TravTrans, evaluated using Mean Reciprocal Rank (MRR) on the Py150 dataset.
  • Comment Generation: Implemented NeuralTransformer, achieving competitive results on BLEU and Rouge metrics for Python and Java.
  • Code Retrieval: Implemented four benchmark baselines (NBOW, 1D-CNN, biRNN, and SelfAttn) on the CodeSearchNet dataset.

Conclusion and Future Work

While NaturalCC is a powerful start, it is an ongoing project. The authors plan to expand the toolkit to include code clone detection, type inference, vulnerability detection, and masked language modeling. By centralizing these baselines, NaturalCC provides a fair playing field for future researchers to test new ideas against established methods.

Summary updated

Abstract

We present NaturalCC, an efficient and extensible toolkit to bridge the gap between natural language and programming language, and facilitate the research on big code analysis. Using NaturalCC, researchers both from natural language or programming language communities can quickly and easily reproduce the state-of-the-art baselines and implement their approach. NaturalCC is built upon Fairseq and PyTorch, providing (1) an efficient computation with multi-GPU and mixed-precision data processing for fast model training, (2) a modular and extensible framework that makes it easy to reproduce or implement an approach for big code analysis, and (3) a command line interface and a graphical user interface to demonstrate each model's performance. Currently, we have included several state-of-the-art baselines across different tasks (e.g., code completion, code comment generation, and code retrieval) for demonstration. The video of this demo is available at https://www.youtube.com/watch?v=q4W5VSI-u3E&t=25s.
BibTeX
@misc{wan2020naturalcc,
  author = {Yao Wan and Yang He and Jianguo Zhang and Yulei Sui and Hai Jin and Guandong Xu and Caiming Xiong and Philip S. Yu},
  title = {{NaturalCC: A Toolkit to Naturalize the Source Code Corpus}},
  year = {2020},
  eprint = {2012.03225},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2012.03225},
}
Figure