From Where Words Come: Efficient Regularization of Code Tokenizers Through Source Attribution
Organizations: CAIRO, Technical University of Applied Sciences Würzburg-Schweinfurt · JetBrains Research · TU Delft
Abstract
Efficiency and safety of Large Language Models (LLMs), among other factors, rely on the quality of tokenization. A good tokenizer not only improves inference speed and language understanding but also provides extra defense against jailbreak attacks and lowers the risk of hallucinations. In this work, we investigate the efficiency of code tokenization, in particular from the perspective of data source diversity. We demonstrate that code tokenizers are prone to producing unused, and thus under-trained, tokens due to the imbalance in repository and language diversity in the training data, as well as the dominance of source-specific, repetitive tokens that are often unusable in future inference. By modifying the BPE objective and introducing merge skipping, we implement different techniques under the name Source-Attributed BPE (SA-BPE) to regularize BPE training and minimize overfitting, thereby substantially reducing the number of under-trained tokens while maintaining the same inference procedure as with regular BPE. This provides an effective tool suitable for production use.
Figures & tables
| Qwen2.5 Coder 3B | CodeGemma 7B | StarCoder2 3B | |
| Category | 2% of 151k tokens | 2% of 262k tokens | 2% of 49k tokens |
| Special tokens | 00 18 0 (0.6%) | 0 101 0 (2.0%) | 00 0 0 (0.0%) |
| Digits and numbers | 000 2 0 (0.1%) | 00 77 0 (1.5%) | 00 0 0 (0.0%) |
| Punctuation | 0 124 0 (4.1%) | 0 521 (10.2%) | 125 (12.2%) |
| Full or partial variable or function names | 000 4 0 (0.1%) | 00 64 0 (1.2%) | 451 (44.0%) |
| ALL–CAPS Latin words | 000 0 0 (0.0%) | 00 60 0 (1.2%) | 0 88 0 (8.6%) |
| Tokenizer | CR | Used | MTL | Steps |
|---|---|---|---|---|
| BPE | 3.90 | 19585 | 6.75 | 32508 |
| Skip | 3.90 | 19862 | 6.57 | 36263 |
| Skip | 3.91 | 20117 | 6.51 | 40096 |
| Skip | 3.91 | 20343 | 6.50 | 43575 |
| Skip | 3.92 | 20741 | 6.50 | 51093 |
| 3.91 | 20491 | 6.54 | 32508 |
| Priority Criterion | Merge Skip | Compression | Coverage | MTL | # 3-digit numbers | # Under-trained |
| (BPE) | — (BPE) | 3.623 | 29240 | 6.23 | 517 | 640 |
| Skip | 3.623 | 31416 | 5.89 | 655 | 52 | |
| — | 3.637 | 31267 | 5.85 | 772 | 151 | |
| Skip | 3.635 | 31357 | 5.84 | 779 | 75 | |
| — | 3.641 | 30876 | 6.02 | 595 | 202 | |
| — | 3.643 | 31066 | 5.99 | 607 | 72 |
| Tokenizer | CR | Used | MTL | Time |
|---|---|---|---|---|
| BPE | 3.62 | 29240 | 6.23 | 0:12 |
| PickyBPE | 3.61 | 28673 | 6.22 | 27:04 ∗ |
| BoundlessBPE | 3.56 | 29375 | 6.70 | 28:55 ∗ |
| SA-BPE (ours) | 3.64 | 31567 | 5.86 | 0:12 |
Appendix figures & tables31 assets
Supplementary material from the paper’s appendix.
Appendix
| Model Parameter | Value |
|---|---|
| Hidden size | 768 |
| Intermediate size | 2048 |
| Number of hidden layers | 12 |
| Number of attention heads | 12 |
| Number of key-value heads | 4 |
| Max position embeddings | 2048 |
| Training Parameter | Value |
|---|---|
| Optimizer | adamW |
| Weight decay | 0.01 |
| Gradient clipping | 1.0 |
| Adam beta1 | 0.9 |
| Adam beta2 | 0.95 |
| Adam epsilon |
| Language | # Documents | Size (in GB) |
|---|---|---|
| Java | 2,741,833 | 11.15 |
| JavaScript | 2,757,358 | 8.40 |
| Python | 1,770,216 | 7.90 |
| PHP | 2,006,486 | 7.02 |
| C++ | 990,971 | 6.67 |
| C# | 1,468,542 | 5.70 |
| Language | Extensions |
|---|---|
| Java | java |
| C# | cs |
| C++ | cpp , hpp |
| Python | py |
| Haskell | hs |
| Dart | dart |
| Min | Gini | Compression |
|---|---|---|
| 1 (BPE) | 0.0576 | 3.5693 |
| 2 | 0.0576 | 3.5743 |
| 3 | 0.0570 | 3.5719 |
| 4 | 0.0568 | 3.5710 |
| 5 | 0.0567 | 3.5651 |
| 6 | 0.0566 | 3.5588 |
| Priority Criterion | Merge Skip | Java | JavaScript | Python | PHP | C++ | C# | Go | Rust | Ruby |
|---|---|---|---|---|---|---|---|---|---|---|
| (BPE) | — (BPE) | 3.875 | 3.481 | 3.634 | 3.842 | 3.225 | 4.265 | 3.030 | 3.499 | 3.683 |
| Skip | 3.867 | 3.499 | 3.629 | 3.822 | 3.224 | 4.244 | 3.026 | 3.492 | 3.686 | |
| — | 3.887 | 3.507 | 3.644 | 3.848 | 3.238 | 4.267 | 3.037 | 3.507 | 3.692 | |
| Skip | 3.880 | 3.508 | 3.643 | 3.837 | 3.235 | 4.263 | 3.030 | 3.506 | 3.693 | |
| — | 3.901 | 3.505 | 3.650 | 3.859 | 3.239 | 4.284 | 3.040 | 3.513 | 3.702 | |
| — | 3.903 | 3.507 | 3.652 | 3.860 | 3.240 | 4.286 | 3.041 | 3.514 | 3.703 |
| Priority Criterion | Merge Skip | Kotlin | Scala | Swift | Vue | Dart | Lua | Haskell | Julia | OCaml |
|---|---|---|---|---|---|---|---|---|---|---|
| (BPE) | — (BPE) | 3.968 | 3.562 | 4.140 | 3.165 | 3.894 | 3.264 | 3.340 | 3.041 | 3.305 |
| Skip | 3.941 | 3.559 | 4.162 | 3.178 | 3.911 | 3.294 | 3.340 | 3.049 | 3.319 | |
| — | 3.980 | 3.567 | 4.167 | 3.186 | 3.912 | 3.296 | 3.350 | 3.055 | 3.334 | |
| Skip | 3.980 | 3.564 | 4.166 | 3.187 | 3.912 | 3.296 | 3.342 | 3.056 | 3.335 | |
| — | 3.985 | 3.574 | 4.162 | 3.181 | 3.920 | 3.284 | 3.356 | 3.052 | 3.318 | |
| — | 3.987 | 3.576 | 4.166 | 3.183 | 3.921 | 3.285 | 3.357 | 3.055 | 3.316 |
| Priority Criterion | Merge Skip | Java | JavaScript | Python | PHP | C++ | C# | Go | Rust | Ruby |
|---|---|---|---|---|---|---|---|---|---|---|
| (BPE) | — (BPE) | 17935 | 13643 | 15693 | 15541 | 15755 | 15370 | 16019 | 15701 | 12618 |
| Skip | 18763 | 14211 | 16315 | 16106 | 16437 | 15993 | 16767 | 16376 | 13041 | |
| — | 18816 | 14359 | 16541 | 16266 | 16549 | 16163 | 16993 | 16594 | 13251 | |
| Skip | 18850 | 14377 | 16561 | 16278 | 16566 | 16188 | 17007 | 16625 | 13267 | |
| — | 18688 | 14130 | 16247 | 16113 | 16279 | 15890 | 16574 | 16180 | 12965 | |
| — | 18756 | 14184 | 16289 | 16160 | 16314 | 15926 | 16627 | 16231 | 13000 |
| Priority Criterion | Merge Skip | Kotlin | Scala | Swift | Vue | Dart | Lua | Haskell | Julia | OCaml |
|---|---|---|---|---|---|---|---|---|---|---|
| (BPE) | — (BPE) | 16898 | 7375 | 14528 | 11475 | 15626 | 13189 | 5565 | 6214 | 5325 |
| Skip | 17693 | 7475 | 15191 | 12006 | 16443 | 13726 | 5653 | 6296 | 5405 | |
| — | 17834 | 7484 | 15353 | 12262 | 16649 | 13875 | 5650 | 6354 | 5431 | |
| Skip | 17871 | 7483 | 15380 | 12295 | 16681 | 13908 | 5654 | 6360 | 5434 | |
| — | 17561 | 7460 | 15005 | 11924 | 16229 | 13568 | 5610 | 6293 | 5381 | |
| — | 17627 | 7473 | 15053 | 11975 | 16291 | 13608 | 5617 | 6298 | 5387 |
| Priority Criterion | Merge Skip | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | (BPE) | — (BPE) | 0 | 8304 | 9168 | 9386 | 5848 | 6362 | 9580 | 10004 | 10088 | 10228 |
| 2 | 8304 | 0 | 5572 | 5392 | 6132 | 5878 | 3292 | 6434 | 6472 | 6392 | ||
| 3 | — | 9168 | 5572 | 0 | 372 | 6366 | 6316 | 5006 | 2666 | 2826 | 2894 | |
| 4 | 9386 | 5392 | 372 | 0 | 6562 | 6346 | 4842 | 2614 | 2764 | 2678 | ||
| 5 | — | 5848 | 6132 | 6366 | 6562 | 0 | 576 | 4834 | 5892 | 5956 | 6086 | |
| 6 | — | 6362 | 5878 | 6316 | 6346 | 576 | 0 | 4454 | 5590 | 5618 | 5728 |
| Priority Criterion | Merge Skip | Under-trained token examples |
| (BPE) | — (BPE) | 0123351271903 , _AtaSmartAttributeDisplayType , minCuSize , DISKinematics |
| Skip | IMARY , ientation , vanced , pendencies , ventory | |
| — | OfSurgeServices , AceHighFlush , PeriodoDeclara , OXWSMTGS | |
| Skip | arency , ICENSE , ployment , ereum , avascript | |
| — | gridBagConstraintsPeriodoDeclara , HighFlush , ablytyped | |
| — | IntoConstraints , scalably , ilibj , VisualStyleBackColor , arency |