Abstract
Statistical language modeling techniques have successfully been applied to source code, yielding a variety of new software development tools, such as tools for code suggestion and improving readability. A major issue with these techniques is that code introduces new vocabulary at a far higher rate than natural language, as new identifier names proliferate. But traditional language models limit the vocabulary to a fixed set of common words. For code, this strong assumption has been shown to have a significant negative effect on predictive performance. But the open vocabulary version of the neural network language models for code have not been introduced in the literature. We present a new open-vocabulary neural language model for code that is not limited to a fixed vocabulary of identifier names. We employ a segmentation into subword units, subsequences of tokens chosen based on a compression criterion, following previous work in machine translation. Our network achieves best in class performance, outperforming even the state-of-the-art methods of Hellendoorn and Devanbu that are designed specifically to model code. Furthermore, we present a simple method for dynamically adapting the model to a new test project, resulting in increased performance. We showcase our methodology on code corpora in three different languages of over a billion tokens each, hundreds of times larger than in previous work. To our knowledge, this is the largest neural language model for code that has been reported.
1. Introduction
Source code exhibits statistical patterns similar to human language, a property often referred to as "naturalness." This has enabled the development of various software engineering tools based on language modeling, such as code completion and readability enhancers. However, code presents a unique challenge: the Out-of-Vocabulary (OOV) problem. Developers frequently create new identifiers, leading to a much higher rate of vocabulary growth compared to natural language.
In 2017, research by Hellendoorn and Devanbu suggested that traditional $n$-gram models with caching mechanisms outperformed deep learning models because the latter typically rely on fixed vocabularies. This led to skepticism regarding the suitability of deep neural networks for code. This paper re-evaluates that conclusion, introducing an open-vocabulary neural language model (NLM) that handles the OOV problem effectively and achieves state-of-the-art results.
2. Methodology
The proposed NLM addresses the limitations of fixed-vocabulary models through two primary innovations:
2.1 Subword Segmentation via Byte Pair Encoding (BPE)
Instead of treating each unique identifier as an atomic unit, the model employs Byte Pair Encoding (BPE) to segment tokens into subword units. This approach allows the model to represent any token—even those never seen during training—as a sequence of known sub-units. For instance, a rare identifier like setter might be decomposed into set and ter. This effectively eliminates the OOV bottleneck by allowing the model to "synthesize" new identifiers from common sub-components.
2.2 Dynamic Adaptation
To account for the "localness" of source code—where specific identifiers are highly frequent within a single project but rare elsewhere—the authors implement a dynamic adaptation strategy. As the model processes a new project, it performs a single gradient descent step on each new file. This lightweight fine-tuning allows the global model to adapt to the specific stylistic and lexical nuances of a local codebase on the fly.
The underlying architecture is a single-layer Gated Recurrent Unit (GRU), which was found to be efficient and slightly superior to LSTMs in preliminary testing.
3. Dataset and Scale
A core contribution of this work is the use of massive, "giga-token" corpora. The study utilizes three large-scale datasets for Java, C, and Python, significantly exceeding the scale of data used in prior research.
As shown in Table 1, the Java corpus alone contains over 1.4 billion tokens. The authors highlight that their training set is roughly 107 times larger than those used in previous state-of-the-art neural code modeling papers. This scale is crucial for demonstrating the superior scaling properties of neural models compared to $n$-gram models.
4. Key Results and Discussion
The open-vocabulary NLM achieves state-of-the-art performance across all three languages, outperforming the previously dominant nested cache $n$-gram models.
- Scaling and Performance: A key observation is that while $n$-gram models tend to reach a performance plateau as data increases, neural models continue to improve. In the Java within-project setting, the model achieved a cross-entropy of 1.04 bits per token and a Mean Reciprocal Rank (MRR) of 81.16%.
- Cross-Project Success: Even without seeing a project beforehand, the model achieved an MRR of 70.84% on Java, proving its strong generalization capabilities.
- Efficiency: The model proved to be remarkably efficient, with training completed in under 12 hours on a single GPU. Furthermore, performance remained stable across various subword vocabulary sizes (2,000 to 10,000 BPE operations).
5. Conclusion and Future Work
This research demonstrates that deep neural networks, when equipped with subword segmentation and dynamic adaptation, are the superior choice for modeling source code. The open-vocabulary approach successfully overcomes the OOV problem that previously hindered neural models in this domain.
Future work could explore hybrid architectures, such as combining subword units with pointer networks to explicitly model token copying. Additionally, integrating these high-performance models into production-ready IDE plugins remains a priority to provide developers with more accurate code suggestions and improved tooling.
Summary updated