The Secret to Reducing SLM Inference Costs by 77% with VocabX

The VocabX technique developed by the Hugging Face Dharma AI team redesigns vocabulary based on generation costs rather than token frequency, reducing inference costs by 77% in a Portuguese essay grading system (based on Qwen3-4B). The existing vocabulary of 151,669 was reduced to 65,000, and repetitive patterns like JSON response formats were integrated into macro tokens to alleviate memory bandwidth bottlenecks during the decoding stage. This case demonstrates that the tokenizer is a key cost lever in the practical service of small language models (SLM).

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.

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%.

MetricOriginal Qwen3-4BAfter Applying VocabXChange 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 Tokens7.4×Same (Hardware Dependent)—
Measured Inference Cost ReductionBaseline77% Reduction-77%
Original151669
VocabX65000

Limitations of Existing Approaches and Differentiation of VocabX

The research team applied three reference techniques to the same grading task for comparison.

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.

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.

Verified figures

Our take

VocabX is significant in redefining the tokenizer from a 'fixed inherited asset' to an 'optimizable cost lever'. Considering that decoding bottlenecks in serving small models are related to memory bandwidth, reducing vocabulary size and macroizing repetitive patterns on the output side provide practical solutions that yield immediate ROI. However, the paper is still under review, and validation is only presented for a single task (Portuguese grading), so generalization performance and reproducibility in multilingual and multi-task environments need further validation after formal publication.

Open questions

Source

Back to catalog