Matra

A custom tokenizer algorithm that outperforms GPT-5, Gemini 3.5 Flash, Gemma-4-31B, Qwen-3.6-MoE, Sarvam-105B, Sutra-v2, and MUTANT (Krutrim-2) across 23 languages, slashing sequence lengths by 70.3% with lowest fertility of 2.06 and highest BPT of 8.90.

UPDATES 2026-06-15 | 3:45 PM Benchmarked against GPT-5, Gemini 3.5 Flash, and Gemma-4-31B on IN22-Gen. Matra scores lowest fertility (2.06) and highest BPT (8.90) across all 23 languages.
+4 more
Table of Contents

Technical report: matra_paper_200k.pdf

LLM with Matra as tokenizer is still pending. If anyone wants to help me with compute, hmu @ e-mail

Live Demo

note: visit huggingface space link if the embed fails to load.

Matra: A Tokenizer Built for India’s Languages

200k, 128k vocab size. 23 languages. The lowest token counts and highest compression across the board.

In active development, more optimzation, tests, language specific tokenizers coming soon.

Most tokenizers treat Indian languages as an afterthought, fragmented syllables and half-formed characters scattered across a vocabulary designed for English. Matra was built from the ground up to fix that. The result, benchmarked on June 15 2026 against the IN22-Gen test split at 200k tokens per language, is unambiguous: Matra produces fewer tokens, denser representations, and dramatically lower fragmentation than GPT-5, Gemini 3.5 Flash, Gemma-4-31B, Qwen-3.6-MoE, Sarvam-105B, and Sutra-v2 across all 23 scheduled languages of India.

That is not a claim. It is a number: 1,103,089 total tokens to encode the entire benchmark corpus, versus 1,794,545 for the next-best competitor (Gemma-4-31B) and 3,225,298 for the worst (Qwen-3.6-MoE).


Architectural Innovations

Tokenization for Indic scripts is hard for a structural reason. A single syllable in Hindi, Tamil, or Kannada may be encoded as 2–4 UTF-8 bytes. A tokenizer trained predominantly on English text will decompose that syllable into individual bytes or Unicode code-points, producing a cascade of meaningless fragments. Every extra token is wasted context, wasted compute, and a weaker representation for the model.

Matra addresses this with seven architectural innovations.

Unicode-Aware Pre-Tokenization

The pre-tokenizer uses a hand-crafted regex pattern that respects Unicode letter, mark, and number categories (\p{L}, \p{M}, \p{N}). Diacritics are kept attached to their base letters. CJK characters are split individually (they are morphologically atomic). Right-to-left scripts and Arabic extensions are handled natively. Whitespace, four-space indents, tabs, newlines are preserved as explicit tokens for code-aware applications.

Language-Weighted Training (XLM-R Scaling)

A two-stage merge strategy with script-aware frequency scaling. During Stage 1, word frequencies are multiplied by a lang_weight factor when the word contains target-script characters - detected via is_target_script() across Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada, Malayalam, Sinhala, Arabic/Perso-Arabic (Urdu, Kashmiri, Sindhi), Ol Chiki (Santali), and Meetei Mayek (Manipuri).

The scaling follows the XLM-R alpha-smoothing formula:

$$w_w = w^{\frac{1-\alpha}{\alpha}}$$

With $\alpha = 0.75$, target-script words receive a ~1.33× frequency boost while English remains unscaled. Stage 2 uses a blended sentence-level multiplier: a sentence with 80% Hindi and 20% English gets 80% of the full weight, preventing code-switched “Hinglish” from distorting the merge budget.

Two-Stage Merge Strategy

Stage 1 learns intra-word subword merges (morphological prefixes, suffixes, character n-grams) constrained within word boundaries. Stage 2 extends merges across word boundaries within a sentence, learning multi-word “superword” compounds. The merge budget is split: transition_vocab_size tokens allocated to Stage 1, the remainder to Stage 2. This decouples morphological learning from syntactic collocation, and no single-character fragment is promoted until both stages complete.

Sentence Boundary Detection

Pre-tokenized word tokens are grouped into sentences using a zero-width lookbehind split on [.!?\n।] followed by a lookahead for whitespace. During Stage 2, any merge whose resulting token contains a sentence delimiter (. ! ? \n ) is permanently banned from the vocabulary. This prevents semantically invalid tokens like “delhi. the” from ever being learned, preserving clean sentence boundaries for downstream transformer models.

R2L Digit Grouping

Following Singh & Strouse (2024), digit sequences are chunked right-to-left in groups of three using a positive-lookahead regex:

(?:\p{N}{1,3}(?=(?:\p{N}{3})*(?!\p{N})))

This preserves base-10 alignment in right-to-left scripts and prevents the arithmetic hallucination that arises when digit strings are fragmented across arbitrary byte boundaries.

Hapax Legomena Pruning

The drop_singletons flag removes word entries with frequency ≤ 1.0 between Pass 1 and Stage 1. Hapax legomena - words appearing exactly once in the corpus - contribute noise to merge statistics without providing generalizable patterns. Pruning them frees the merge budget for higher-frequency morphological units. Sentences containing pruned words are also filtered before Stage 2.

This is mathematically defensible as a data-efficiency choice; for maximally pure results, drop_singletons defaults to False.

Resource Efficiency

2-Pass Streaming Architecture

A naive in-memory BPE trainer accumulates four large dicts simultaneously: word sequences, word frequencies, word-to-string maps, and sentence counters. On a 300 MB corpus (2.8 GB effective after alpha-smoothing), this peaks at 75+ GB RAM - well beyond consumer hardware.

Matra splits training into two streaming passes:

  1. Pass 1 - Stream the corpus, populate word-level dicts, run Stage 1 BPE, then delete all three word dicts and call gc.collect().
  2. Pass 2 - Re-stream the corpus (the iterator_factory is called a second time), populate sentence-level statistics, and run Stage 2 BPE.

This decouples word and sentence accumulation:

Phase1-Pass RAM2-Pass RAM
Word accumulation~60 GB~2-3 GB
Stage 1 BPE~70 GB~3-4 GB
Sentence accumulation~75 GB~1-2 GB
Stage 2 BPE~75 GB~3-4 GB
Peak~75 GB~5-6 GB

Training comfortably fits within a 16 GB machine budget.

RAM Shield

Stage 2 operates on a single concatenated 1D array of all corpus sentences - ~39M tokens for a 300 MB corpus. Each merge creates new pairs at the two affected boundaries, and the majority of those new pairs are singletons (freq=1). BPE requires freq >= 2 to execute a merge, but the dict still holds them - and with millions of merges, the dict grows unboundedly until the OOM killer fires.

The RAM Shield hard-caps memory: when len(pair_counts) > max_pairs (default 3,000,000), all singleton pairs are purged, the heap is rebuilt from live entries, and gc.collect() releases the freed memory. Purging is mathematically lossless - no pair with freq <= 1 can ever be selected for merging.

Early stopping terminates the merge loop when the top heap entry’s frequency drops below 2, preventing the trainer from grinding through dead heap pops. On typical corpora, Stage 2 halts at ~5K–15K merges (well before the budget) once no more repeat pairs exist.

Combined effect: Stage 2 peak RSS is hard-capped at ~2.5 GB regardless of corpus size, and OOM is impossible.

Checkpointing & Crash Recovery

The trainer auto-saves a .ckpt file after each phase boundary, keyed by a 16-char SHA-256 hash of the training config {vocab_size, alpha, drop_singletons, lang_weight, passes}. This gives two superpowers:

  1. Crash recovery - kill mid-Stage 1 with Ctrl-C, restart with --resume-from pass1, the 20-minute Pass 1 is loaded from cache.
  2. Hyperparameter sweep - train once with --vocab 200000 --alpha 0.5, then re-run with --vocab 256000 --alpha 0.5 --reuse-pass1 to skip Pass 1 entirely.

Checkpoints are written atomically (.tmp + os.replace()), surviving mid-write SIGKILL. On load, __version__, __stage__, and config_hash are verified against the current run - all mismatches raise ValueError with actionable messages.

Cython-Accelerated Hot Paths

The inner merge loop (_bpe_merge.pyx) and pair-position builder (_bpe_passb.pyx) are optionally compiled with Cython via pyximport. When available, they replace the equivalent Python loops at the two points that dominate training time. On a 200k-token corpus the difference is roughly 4–5× wall-clock time. The pure-Python fallback remains correct and usable without a build step, making Matra deployable in environments where compiling C extensions is impractical.

O(1) Encode Path

At inference time, the byte-to-token mapping is pre-computed as a 256-entry tuple indexed by raw byte value, eliminating per-byte dictionary lookups. The merge pass uses a lazy min-heap with integer-keyed _merge_ranks_int maps - pairs are resolved via (id0, id1) → rank lookups instead of string hashing. A bounded LRU cache (default 2,000,000 entries) means repeated subwords pay zero merge cost after the first occurrence. The end-to-end complexity is $O(n \log n)$ per sentence in the number of pre-tokenization tokens.


Benchmark Results: IN22-Gen, 23 Languages

Benchmark date: 15 June 2026. Test split: IN22-Gen. Corpus size: approximately 200,000 tokens per language. Tokenizers evaluated: Matra, GPT-5, Gemini 3.5 Flash, Gemma-4-31B, MUTANT (Krutrim-2), Qwen-3.6-MoE, Sarvam-105B, Sutra-v2.

Metrics:

  • SeqRed (Sequence Reduction): percentage reduction in token count versus the raw byte sequence. Higher is better.
  • NSL (Normalised Sequence Length): token count divided by the character count of the source text. Lower is better.
  • BPT (Bytes Per Token): average bytes encoded per token. Higher is better.
  • Fert (Fertility): average tokens produced per word. Lower is better.
  • 1ch%: percentage of tokens that encode a single character. Lower is better.

Aggregate across all 23 languages

TokenizerSeqRedNSLBPTFert1ch%Total Tokens
Matra70.3%0.12258.902.067.51,103,089
Sutra-v264.6%0.14537.292.4725.11,311,098
Sarvam-105B64.9%0.14547.352.4529.21,301,947
Gemini 3.5 Flash51.5%0.19596.283.340.01,794,568
Gemma-4-31B51.5%0.19596.283.3439.31,794,545
GPT-537.4%0.24945.584.2744.72,327,945
MUTANT (Krutrim-2)23.4%0.30254.565.2955.22,864,195
Qwen-3.6-MoE13.2%0.34183.356.0777.63,225,298

Matra’s total token count is 42% lower than Gemma-4-31B and 66% lower than Qwen-3.6-MoE on the same corpus. The fertility score of 2.06 means the average Indic word is encoded in just over two tokens; Qwen requires three times as many.

Selected per-language highlights

Hindi: Matra achieves 79.9% sequence reduction, a fertility of 1.09, and 32,218 total tokens. GPT-5 requires 51,905 and a fertility of 1.76.

Tamil: Matra achieves 77.3% sequence reduction and a BPT of 12.03, the highest encoding density of any tokenizer on this language. The next best is Gemma-4-31B at 9.67.

Bengali: Matra achieves 77.4% sequence reduction and 34,179 total tokens. Gemma-4-31B and Sarvam-105B are tied at 40,950.

Manipuri (Meitei script): Where GPT-5, Qwen, and MUTANT collapse entirely (negative sequence reduction, fertility above 16), Matra encodes Manipuri at 53.6% sequence reduction with a fertility of 3.05. Sarvam-105B and Sutra-v2 are the only other tokenizers with positive sequence reduction on this script.

Odia: GPT-5 achieves near-zero sequence reduction (−0.1%) on Odia. MUTANT and Qwen are worse. Matra achieves 71.5% with 49,755 tokens, the second-best after Sarvam-105B (54,656).

Full per-language breakdown (23 languages × 7 tokenizers) - click to expand
LanguageTokenizerSeqRed↑NSL↓BPT↑Fert↓1ch%↓Tokens
as (Assamese)GPT-557.4%0.15976.262.9738.068,816
Gemini 3.5 Flash58.9%0.15416.492.870.066,401
Gemma-4-31B58.9%0.15416.492.8740.666,400
MUTANT (Krutrim-2)32.4%0.25363.944.7270.2109,244
Matra71.9%0.10539.501.966.745,342
Qwen-3.6-MoE23.9%0.28533.505.3180.7122,906
Sarvam-105B58.9%0.15416.492.8740.666,400
Sutra-v267.3%0.12258.162.2826.252,789
bn (Bengali)GPT-563.0%0.13867.212.5631.155,995
Gemini 3.5 Flash72.9%0.10149.871.870.040,951
Gemma-4-31B72.9%0.10149.871.8719.740,950
MUTANT (Krutrim-2)52.8%0.17685.663.2748.671,433
Matra77.4%0.084611.821.567.534,179
Qwen-3.6-MoE31.1%0.25763.884.7668.6104,090
Sarvam-105B72.9%0.10149.871.8719.740,950
Sutra-v268.1%0.11938.382.2124.348,208
brx (Bodo)GPT-548.5%0.19315.183.9642.684,466
Gemini 3.5 Flash53.9%0.17295.783.540.075,637
Gemma-4-31B53.9%0.17295.783.5433.675,636
MUTANT (Krutrim-2)42.5%0.21584.634.4252.194,396
Matra65.1%0.13127.622.695.757,376
Qwen-3.6-MoE28.1%0.26973.715.5276.4117,971
Sarvam-105B53.9%0.17295.783.5433.675,636
Sutra-v249.6%0.18935.283.8839.282,800
doi (Dogri)GPT-557.5%0.16566.042.3141.067,844
Gemini 3.5 Flash62.7%0.14536.882.030.059,505
Gemma-4-31B62.7%0.14536.882.0332.459,504
MUTANT (Krutrim-2)50.4%0.19325.182.7050.679,152
Matra67.4%0.12687.881.773.851,960
Qwen-3.6-MoE30.5%0.27063.703.7879.0110,857
Sarvam-105B62.7%0.14536.882.0332.459,504
Sutra-v259.1%0.15926.282.2235.165,232
en (English)GPT-579.1%0.20894.791.3215.333,561
Gemini 3.5 Flash78.3%0.21734.601.380.034,920
Gemma-4-31B78.3%0.21734.601.3820.234,919
MUTANT (Krutrim-2)78.1%0.21894.571.3919.335,178
Matra81.0%0.19045.251.2111.430,595
Qwen-3.6-MoE77.7%0.22324.481.4218.835,869
Sarvam-105B78.3%0.21734.601.3820.234,919
Sutra-v280.9%0.19095.241.215.330,675
gu (Gujarati)GPT-560.2%0.15146.612.5634.659,667
Gemini 3.5 Flash57.7%0.16106.212.730.063,455
Gemma-4-31B57.7%0.16106.212.7336.163,454
MUTANT (Krutrim-2)38.4%0.23454.263.9760.292,431
Matra73.4%0.10149.861.729.139,973
Qwen-3.6-MoE18.4%0.31053.225.2691.4122,402
Sarvam-105B65.9%0.12987.702.2026.951,161
Sutra-v264.5%0.13517.402.2926.453,260
hi (Hindi)GPT-567.7%0.12587.951.7622.451,905
Gemini 3.5 Flash72.1%0.10889.191.520.044,886
Gemma-4-31B72.1%0.10889.191.5218.144,885
MUTANT (Krutrim-2)61.5%0.15016.662.1031.761,902
Matra79.9%0.078112.801.096.032,218
Qwen-3.6-MoE34.3%0.25603.913.5773.1105,590
Sarvam-105B72.1%0.10889.191.5218.144,885
Sutra-v269.4%0.11928.391.6617.349,182
kn (Kannada)GPT-558.9%0.15076.643.7840.171,024
Gemini 3.5 Flash59.4%0.14876.723.730.070,092
Gemma-4-31B59.4%0.14876.723.7337.670,091
MUTANT (Krutrim-2)53.5%0.17045.874.2746.880,282
Matra72.7%0.100010.002.509.947,119
Qwen-3.6-MoE19.6%0.29473.397.3883.9138,860
Sarvam-105B68.0%0.11748.522.9428.255,326
Sutra-v267.2%0.12008.333.0126.056,554
kok (Konkani)GPT-556.5%0.16476.073.1338.266,568
Gemini 3.5 Flash59.9%0.15206.582.880.061,431
Gemma-4-31B59.9%0.15206.582.8832.561,430
MUTANT (Krutrim-2)50.4%0.18815.323.5746.776,022
Matra70.7%0.11129.002.117.744,936
Qwen-3.6-MoE27.8%0.27353.665.1978.0110,566
Sarvam-105B59.9%0.15206.582.8832.561,430
Sutra-v258.9%0.15596.412.9632.963,018
ks (Kashmiri)GPT-535.9%0.35452.823.6867.3104,745
Gemini 3.5 Flash43.1%0.31443.183.260.092,891
Gemma-4-31B43.1%0.31443.183.2661.092,890
MUTANT (Krutrim-2)34.1%0.36432.753.7869.6107,639
Matra55.2%0.24754.042.577.373,121
Qwen-3.6-MoE30.6%0.38392.603.9872.7113,435
Sarvam-105B43.1%0.31443.183.2661.092,890
Sutra-v245.1%0.30323.303.1546.889,598
mai (Maithili)GPT-561.7%0.14546.882.3135.257,157
Gemini 3.5 Flash64.9%0.13327.512.110.052,360
Gemma-4-31B64.9%0.13327.512.1134.552,359
MUTANT (Krutrim-2)50.8%0.18695.352.9752.073,470
Matra72.2%0.10549.491.675.041,417
Qwen-3.6-MoE29.1%0.26943.714.2877.5105,896
Sarvam-105B64.9%0.13327.512.1134.552,359
Sutra-v260.6%0.14976.682.3838.658,848
ml (Malayalam)GPT-562.9%0.13477.434.0134.368,334
Gemini 3.5 Flash64.4%0.12947.733.850.065,635
Gemma-4-31B64.4%0.12947.733.8532.165,634
MUTANT (Krutrim-2)49.0%0.18505.415.5154.593,866
Matra72.3%0.10069.942.996.951,027
Qwen-3.6-MoE16.7%0.30233.319.0086.5153,397
Sarvam-105B68.1%0.11598.633.4527.658,822
Sutra-v268.5%0.11438.753.4025.058,019
mni (Manipuri)GPT-5-151.4%0.94611.0616.5099.6386,349
Gemini 3.5 Flash-85.6%0.69841.4312.180.0285,205
Gemma-4-31B-85.6%0.69841.4312.1891.9285,204
MUTANT (Krutrim-2)-151.3%0.94591.0616.4999.5386,258
Matra53.6%0.17475.723.0512.371,346
Qwen-3.6-MoE-151.4%0.94611.0616.5099.6386,359
Sarvam-105B66.7%0.12547.972.1923.351,212
Sutra-v267.3%0.12308.132.155.650,247
mr (Marathi)GPT-562.5%0.14047.122.7331.060,860
Gemini 3.5 Flash70.3%0.11148.982.170.048,287
Gemma-4-31B70.3%0.11148.982.1721.048,286
MUTANT (Krutrim-2)54.5%0.17035.873.3241.773,828
Matra76.5%0.088011.371.719.438,130
Qwen-3.6-MoE27.7%0.27073.695.2776.3117,368
Sarvam-105B70.3%0.11148.982.1721.048,286
Sutra-v268.9%0.11658.592.2722.450,484
ne (Nepali)GPT-564.8%0.13087.652.5427.354,170
Gemini 3.5 Flash67.6%0.12068.292.340.049,957
Gemma-4-31B67.6%0.12068.292.3422.849,956
MUTANT (Krutrim-2)53.1%0.17425.743.3841.972,147
Matra76.4%0.087811.381.705.936,387
Qwen-3.6-MoE29.5%0.26193.825.0873.0108,505
Sarvam-105B67.6%0.12068.292.3422.849,956
Sutra-v269.4%0.11398.782.2121.447,170
or (Odia)GPT-5-0.1%0.37462.677.3198.6175,015
Gemini 3.5 Flash28.1%0.26903.725.250.0125,669
Gemma-4-31B28.1%0.26903.725.2574.4125,668
MUTANT (Krutrim-2)-162.7%0.98291.0219.1995.2459,205
Matra71.5%0.10659.392.087.849,755
Qwen-3.6-MoE11.4%0.33143.026.4792.7154,810
Sarvam-105B68.7%0.11708.552.2828.354,656
Sutra-v267.3%0.12228.182.3930.057,095
pa (Punjabi)GPT-545.8%0.21464.662.7656.879,889
Gemini 3.5 Flash43.2%0.22504.442.900.083,750
Gemma-4-31B43.2%0.22504.442.9061.283,749
MUTANT (Krutrim-2)36.9%0.24994.003.2268.093,022
Matra72.4%0.10949.141.414.940,728
Qwen-3.6-MoE16.6%0.33013.034.2594.9122,894
Sarvam-105B64.5%0.14047.121.8127.852,261
Sutra-v269.5%0.12088.281.5518.644,952
sa (Sanskrit)GPT-552.5%0.17515.714.5644.075,377
Gemini 3.5 Flash58.4%0.15356.523.990.066,066
Gemma-4-31B58.4%0.15356.523.9935.966,065
MUTANT (Krutrim-2)47.4%0.19395.165.0448.483,451
Matra70.9%0.10719.342.796.446,087
Qwen-3.6-MoE23.9%0.28053.577.3075.8120,747
Sarvam-105B58.4%0.15356.523.9935.966,065
Sutra-v255.8%0.16306.144.2437.670,150
sat (Santali)GPT-5-164.5%0.99611.0016.5993.9442,477
Gemini 3.5 Flash-2.0%0.38432.606.400.0170,688
Gemma-4-31B-2.0%0.38432.606.4084.5170,687
MUTANT (Krutrim-2)-150.2%0.94231.0615.6999.6418,593
Matra47.4%0.19805.053.3012.687,934
Qwen-3.6-MoE-150.2%0.94251.0615.6999.6418,656
Sarvam-105B65.1%0.13137.612.1926.958,338
Sutra-v267.1%0.12408.072.063.755,070
sd (Sindhi)GPT-553.4%0.18165.512.5746.274,948
Gemini 3.5 Flash56.8%0.16845.942.380.069,496
Gemma-4-31B56.8%0.16845.942.3839.869,495
MUTANT (Krutrim-2)46.0%0.21084.742.9857.886,969
Matra62.8%0.14496.902.056.059,806
Qwen-3.6-MoE27.6%0.28233.543.9981.0116,498
Sarvam-105B56.8%0.16845.942.3839.869,495
Sutra-v255.7%0.17295.792.4436.071,326
ta (Tamil)GPT-563.7%0.13307.523.3930.268,509
Gemini 3.5 Flash71.8%0.10359.672.630.053,282
Gemma-4-31B71.8%0.10359.672.6321.353,281
MUTANT (Krutrim-2)58.8%0.15116.623.8538.077,811
Matra77.3%0.083112.032.118.442,797
Qwen-3.6-MoE26.0%0.27153.686.9176.6139,809
Sarvam-105B71.8%0.10359.672.6321.353,281
Sutra-v271.3%0.10519.512.6819.654,142
te (Telugu)GPT-558.7%0.15436.483.3037.265,917
Gemini 3.5 Flash61.2%0.14486.903.100.061,879
Gemma-4-31B61.2%0.14486.903.1032.361,878
MUTANT (Krutrim-2)51.2%0.18235.483.9047.377,905
Matra72.8%0.10179.842.188.643,437
Qwen-3.6-MoE18.0%0.30633.266.5590.1130,885
Sarvam-105B67.4%0.12178.222.6027.351,991
Sutra-v267.1%0.12318.132.6323.752,576
ur (Urdu)GPT-565.1%0.19625.101.6922.954,352
Gemini 3.5 Flash66.5%0.18825.311.620.052,125
Gemma-4-31B66.5%0.18825.311.6220.852,124
MUTANT (Krutrim-2)61.5%0.21664.621.8630.059,991
Matra76.0%0.13517.401.162.337,419
Qwen-3.6-MoE57.0%0.24164.142.0839.166,928
Sarvam-105B66.5%0.18825.311.6220.852,124
Sutra-v268.1%0.17945.571.5416.549,703

Matra 200K vs. 128K: Vocab Size Scaling

Matra was evaluated at two vocabulary scales (128K and 200K) to measure the impact of vocabulary size on compression quality. Both benchmarks use the same IN22-Gen test corpus, though the set of competing tokenizers differs - the 128K run compares against GPT-5, Gemini 2.5 Flash, Qwen-3.6-MoE, Sarvam-105B, and Sutra-v2, while the 200K run adds Gemma-4-31B and MUTANT (Krutrim-2).

MetricMatra 128KMatra 200KImprovement
Total Tokens1,390,7971,103,089−20.7%
Fertility2.592.06−20.5%
BPT7.188.90+24.0%
1ch% ↓8.2%7.5%−8.5%

Scaling vocabulary size from 128K to 200K delivers consistent across-the-board compression gains. Total token count drops by over a fifth, fertility approaches the ideal 2.0 threshold, and bytes-per-token rises to 8.90 - the highest encoding density among all tokenizers at either scale. The 128K variant remains a compact alternative for deployment environments where embedding table size is at a premium.


Architecture Notes

The core class is Matra, initialised with a target vocabulary size and a set of special tokens. The public interface is intentionally narrow.

from tokenizer import Matra

tok = Matra(target_vocab_size=300)
tok.train_from_iterator(texts)          # texts: Iterable[str]

ids = tok.encode("नमस्ते दुनिया")
text = tok.decode(ids)

tok.save("matra.json")
tok.load("matra.json")

The saved format is a JSON file containing the merge list (as space-separated string pairs) and the vocabulary (as a list of [id, token_str] pairs). The format is intentionally compatible with the HuggingFace tokenizer interface for downstream use.

Key parameters:

  • target_vocab_size: final vocabulary size including the 256 byte-level base tokens and special tokens. Default 300.
  • transition_vocab_size: intermediate vocabulary size used in Stage 1 training. Default 275.
  • lang_weight: upsampling multiplier for target-script text during pair counting. Default 1.0; increase for corpus mixes that are predominantly English.
  • gc_threshold: ratio of merge count to token count at which an intermediate garbage-collection pass is triggered during training. Default 3.0.
  • max_cache_size: maximum number of entries in the encode cache. Default 2,000,000.

Limitations and Known Behaviour

The benchmark results on this page use a vocabulary of 200,000 tokens, trained on a ~300 MB multilingual corpus via the 2-pass streaming pipeline. A compact 300-token variant is also available for transfer-learning and low-resource scenarios - smaller vocabulary sizes increase cross-lingual transferability at the cost of raw compression.

Cython compilation is optional. The pure-Python pipeline is identical in output; only training speed differs (4-5× slower without the compiled extensions).


Reproducing the Benchmark

The benchmark was run on the IN22-Gen test split using the script included in the repository. Tokenizer weights for GPT-5, Gemini 3.5 Flash, Gemma-4-31B, Qwen-3.6-MoE, Sarvam-105B, Sutra-v2, and MUTANT (Krutrim-2) were loaded from their respective public releases. Matra was trained on a held-out training split; the benchmark corpus was not seen during training.

All metrics are computed at the token level against the original UTF-8 byte stream.