NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

CodeBPE: Investigating Subtokenization Options for Large Language Model Pretraining on Source Code

Nadezhda Chirkova, Sergey Troshin

TL;DR

While researchers often obsess over model architectures and pretraining objectives, the way we "slice" source code into tokens matters just as much. By simply grouping punctuation marks into single tokens (a method called CodeBPE), researchers can reduce sequence lengths by 17% with zero loss in performance. Furthermore, by shrinking vocabulary sizes, they can actually boost model quality by up to 2%.

The Problem with Standard Tokenization

Most Large Language Models (LLMs) don't read raw text; they process "subtokens" generated by algorithms like Byte-Pair Encoding (BPE). While these algorithms were designed for natural language, they are frequently applied to source code with little modification.

Source code is fundamentally different from English: it is dense with punctuation (}]):), follows strict syntactic rules, and features complex identifiers like get_user_auth_token. Standard tokenizers often split these into nonsensical fragments, leading to unnecessarily long sequences that slow down training and inference, or "noisy" representations that hinder the model's ability to understand code logic. This paper investigates how to adapt subtokenization specifically for the unique structure of programming languages.

Methods: Rethinking Code Tokens

The researchers conducted a systematic study using PLBART (an encoder-decoder model) as their testing ground. They varied three main dimensions:

  1. Composite Token Complexity (Levels 0–4): They tested how "aggressive" the tokenizer should be in grouping characters.
  2. Level 0 (Baseline): Punctuation is always split (e.g., ( and ) are separate).
  3. Level 1 (CodeBPE): Allows grouping punctuation (e.g., }): becomes one token).
  4. Level 4: Allows arbitrary combinations (e.g., for i in range becomes one token).
  5. Algorithm Choice: They compared the industry-standard BPE against UnigramLM.
  6. Vocabulary Size: They tested sizes from 50K (standard) down to 2K.
Comparison of tokenization strategies and their tradeoffs in sequence length and quality.

These models were pretrained on a massive corpus of 230M Python functions, 470M Java functions, and 47M natural language descriptions, then evaluated on downstream tasks including code generation, summarization, clone detection, and translation.

Key Insights

  • The Punctuation "Free Lunch": In code, punctuation makes up nearly 13% of all characters. Grouping frequent punctuation clusters (Level 1) into single tokens reduces average sequence length by 17% without hurting accuracy. This is a pure efficiency win.
  • UnigramLM > BPE for Code: UnigramLM (a top-down filtering approach) produces subtokens that align much better with how humans write code (e.g., correctly splitting CamelCase or snake_case identifiers) compared to BPE's bottom-up merging.
  • The Small-Vocab Paradox: Counter-intuitively, reducing the vocabulary size from 50K to 10K often improves model quality by 0.5–2%. While smaller vocabularies make sequences longer (3–19%), they force the model to learn more fundamental, reusable building blocks of code, which aids generalization.
  • Generative vs. Discriminative Needs: Aggressive tokenization (Level 4, where whole phrases are one token) works adequately for "understanding" tasks like clone detection but fails miserably at "generative" tasks like translation, as the model loses the granular control needed to write precise syntax.

Key Results

Performance across downstream tasks and average sequence length for tokenization Levels 0-4.
  • Efficiency: Level 1 subtokenization (CodeBPE/CodeUnigramLM) consistently reduced sequence lengths by 17% across all tasks.
  • Performance Boost: Using UnigramLM with a 10K vocabulary significantly outperformed the standard 50K BPE baseline in 3 out of 8 tasks and matched it in the rest.
  • Speedup: Because sequences were shorter, the time required to generate predictions dropped by 20-23% for Level 1 and up to 46% for Level 4.
  • Transferability: A tokenizer trained only on Python still performs remarkably well on Java, suggesting that the "structure" of code subtokenization is somewhat universal across languages.

Limitations and Future Work

The study focused exclusively on the PLBART architecture. While the authors suspect these findings apply to decoder-only models (like GPT), they haven't yet proven it at the scale of billions of parameters. Additionally, the experiments were limited to Python and Java; languages with vastly different syntaxes (like Lisp or Haskell) might yield different results. Finally, while smaller vocabularies improve quality, the resulting longer sequences might hit the "context window" limits of certain models sooner. Future work could explore finding the optimal mathematical balance between these two constraints.

Summary updated

BibTeX
@inproceedings{chirkova2023codebpe,
  author = {Nadezhda Chirkova and Sergey Troshin},
  title = {{CodeBPE: Investigating Subtokenization Options for Large Language Model Pretraining on Source Code}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=htL4UZ344nF},
}
Figure