Tokenizer Bottleneck Faced by Domain-Specific SLMs
When fine-tuning a general language model for a specific domain, the tokenizer inherits that of the base model. Tokenizers for models like Qwen and Gemma are trained on multilingual and multi-domain texts such as source code, contracts, and Chinese. However, this general vocabulary creates two types of waste for models that perform only single language and single task in production. First, Fragmentation. The most frequently repeated sequences in the domain—JSON envelopes of the grader, evaluation phrases, standard clauses of contracts, identifiers in code—are split into multiple tokens. Second, Excess Vocabulary. Thousands of entries (like Chinese tokens) that are never used in the domain remain in the vocabulary, and the model calculates probabilities for all these entries at every generation step.
The automatic grading system for Portuguese essays operated by the Dharma AI team is based on Qwen3-4B, taking essays, prompts, and reference texts as input and outputting JSON containing grades and comments. The model is already small with 4 billion parameters and trained for the task, but it was still using the original Qwen3-4B tokenizer (151,669 entries).
Reading is Cheap, Writing is Expensive: Asymmetry of Inference Costs
It is easy to think that simply reducing the number of tokens is enough, but it is essential to understand the asymmetric structure where one generated token costs 7.4 times that of an input token. Inference is divided into two stages.
- Prefill: Processes input all at once. Weight reading is reused for all input tokens, leading to compute-bound operations.
- Decode: Every time a token is generated, a full pass is performed. Weight reading is reused only a few times, leading to memory-bound operations.
Even if weight reading is distributed through batch processing, the decode bottleneck is not resolved. Moreover, the KV cache (attention memory of already processed contexts) must also be read at each decode stage, increasing the burden as the context lengthens.
Think of a shipping company that charges per box, but charges seven times more for outgoing boxes than for incoming ones. Reducing the total number of boxes helps. Reducing the outgoing boxes helps seven times more.
The third cost factor is the size of the vocabulary itself. The LM head (last layer) converts the internal state into scores for all vocabulary entries, having one row of parameters per entry. At the size of Qwen3-4B, this matrix accounts for about 10% of the total parameters. The input layer only queries the rows of the tokens being read, but the LM head reads and multiplies the entire matrix at every generation step. Since decoding is already memory-bound, an unnecessary row causes more data movement at every step. Entries that are never used in the domain incur the same cost as frequently used ones.
VocabX Methodology: Redesigning Vocabulary Based on Cost Instead of Frequency
Existing vocabulary adaptation techniques—Fast Vocabulary Transfer (FVT), Multi-Word Tokenization (MWT), and Liquid AI's In-Place BPE—all select tokens based on Frequency. They prioritize expressions that frequently appear in the input, overlooking the costs of less frequent but more expensive generated tokens. They also perform either subword merging or phrase-level tokenization, but not both.
VocabX differs in three dimensions.
1. Cost-Based Selection Criteria: Candidate tokens are selected based on the savings in serving costs rather than the number of tokens saved. It prioritizes repetitive patterns on the output side, reflecting generation stage weights (7.4 times, etc.).
2. Simultaneous Optimization of Two Layers: Subword merging (patterns within words) and macro tokens (long repetitive sequences) are optimized under the same cost function. Long repetitive intervals like the JSON envelope of the grader are captured as macros at once.
3. Budget Control: Vocabulary size is controlled in two stages. Subword merging replaces the base vocabulary with a domain-specific small vocabulary, and macros are only added within a fixed budget.
As a result, the number of output layer rows in the grader decreased from about 152,000 to about 65,000, a reduction of 57%.
| Metric | Original Qwen3-4B | After Applying VocabX | Change Rate |
|---|---|---|---|
| Vocabulary Size (Number of Entries) | 151,669 | ~65,000 | -57% |
| LM Head Parameter Proportion | ~10% | ~4.3% | -57% |
| Cost Ratio of Generated Tokens/Input Tokens | 7.4× | Same (Hardware Dependent) | — |
| Measured Inference Cost Reduction | Baseline | 77% Reduction | -77% |
| Original | 151669 |
| VocabX | 65000 |
Limitations of Existing Approaches and Differentiation of VocabX
The research team applied three reference techniques to the same grading task for comparison.
- FVT: Vocabulary transfer through similar token mapping in embedding space. Frequency-based, so it cannot capture output repetitive patterns.
- MWT: Merges frequently occurring n-grams into a single token. Subword level not processed.
- In-Place BPE: Continues to learn BPE from the original vocabulary. Requires numerous merging steps to capture long sequences.
All three techniques reduced fragmentation but did not solve the excess vocabulary problem, and did not reflect the weights of generation costs, limiting actual serving cost reductions. VocabX addresses both issues simultaneously while controlling the explosion of vocabulary size with a budget when adding macro tokens.
Empirical Results: 77% Cost Reduction in Production Grader
According to the current version of the paper (under review, figures may change), the actual inference cost decreased by 77% after applying VocabX. This result was achieved by replacing only the tokenizer while keeping the base model, data, and training recipe the same. The main effects manifested in three pathways.
- Reduction in Output Token Count: Repetitive outputs like JSON envelopes, capability names, and evaluation phrases compressed into 1-2 macro tokens.
- Reduction in LM Head Operations: A 57% reduction in the number of vocabulary rows significantly decreased memory reading and computation at each decode step.
- Improved KV Cache Efficiency: The length of the output sequence decreased, slowing the rate of KV cache increase.
The research team noted that these figures may vary depending on hardware, batch size, context length, and task characteristics.
Implications: The Tokenizer is the Maximum Cost Lever Set Before Training
The key takeaway from this case is that the tokenizer is a cost lever set before any training stage. When deploying domain-specific SLMs in production, vocabulary specialization should be reviewed before model compression, quantization, and inference optimization. This effect is particularly maximized in serving environments with small models (~7B or less) where the LM head proportion is large and decoding bottlenecks are severe (medium batch, long context).
Future tasks include calibrating the cost function of VocabX to various hardware (H100, inference-specific chips, etc.) and building pipelines for dynamically switching domain-specific vocabularies in multilingual and multi-task environments. After the formal publication of the paper, reproducibility and generalization performance should be further validated.