通过微调通用开源模型(如 Qwen 和 Gemma)来针对特定领域或任务定制模型时,这些模型会继承一个为众多领域和通用应用构建的词表。
在我们投入生产运行的作文评分系统中,该词表将模型最常生成的文本分割成了大量的 token。本文预览了我们将很快发表的 VocabX 论文中的一些结果:我们如何以每个 token 的成本而非其频率为中心来定制词表,并在不改变基础模型的情况下将测得的推理成本降低了 77%。
分词器通常被视为基础模型的固定属性,被继承下来且不再改动。但对于一个针对单一领域定制的模型来说,它可能是最大的成本杠杆之一,且在任何训练步骤之前就已确定。
我们生产环境中运行的系统之一是基于 Qwen3-4B 构建的葡萄牙语作文自动评分器。它接收作文、提示词和支持性文本,并返回包含每项能力评分和评语的 JSON 评估结果。
该模型本身已经很小,仅有 40 亿参数,并且已经针对该任务进行了训练。然而,其分词器仍然是原始的 Qwen3-4B 分词器,为多种语言和多种类型的文本构建。在评分器中,它仅用于单一语言(葡萄牙语)和单一的响应格式。这带来了两个成本问题:评分器在每个请求中生成的文本所需的 token 数量多于实际需要;并且在生成每个 token 时,模型都需要对包含 151,669 个条目的词表进行打分,其中许多条目该任务几乎从未使用过。
我们使用一种称为 VocabX 的方法重建了该词表,同时保持基础模型、数据和训练配方不变。本文中的数据来自当前版本的论文(仍在审稿中),在发表前可能会发生变化。
为所有人构建的词表
领先的语言模型以 token 为单位进行读写,而分词器决定了文本如何转换为 token。领先通用模型的分词器是在来自众多领域和语言的庞大文本集合上训练的,从源代码到合同再到中文。对于通用模型而言,这种选择是合理的。
但对于用于单一领域的模型来说,这同时造成了两种浪费。
第一种是碎片化。分词器将领域中最常重复的内容分割成许多片段。在评分器中,每个响应都以相同的 JSON 信封、相同的能力名称和相同的评价短语开头。在合同中,是那些样板条款。在代码中,是标识符。这些序列中的每一个都需要多个 token 才能表示,而模型在每个请求中都要为其中的每一个付费。
第二种是冗余。词表携带了领域从未使用过的数万条条目,而模型在生成每个 token 时都要为所有这些条目计算得分。一个经过微调用于评分美国学校生物考试的模型几乎永远不会用中文写作,但它却在每一步都计算每个中文语言 token 的概率。词表将领域中最常重复的内容分割成许多片段,并携带了领域从未使用过的条目。
阅读便宜,写入昂贵
自然的反应是统计 token 数量并试图减少它。但 token 数量并非账单。
要理解为什么生成的 token 成本更高,有助于了解推理的工作原理。它分为两个阶段。在预填充(prefill)阶段,模型通过网络一次性处理整个输入:从 GPU 内存读取的每个权重都会为每个输入 token 重用,此时的限制是计算能力。在解码(decode)阶段,模型一次生成一个 token 的响应,每个 token 都需要完整的一次遍历。每次读取的权重仅被使用几次,GPU 的大部分时间都在等待数据从内存到达:此时的限制变为内存带宽。
这种效应在我们运作的领域最为显著:由中等批次服务的小型模型。对请求进行批处理可以将权重读取分散到各个请求中,从而缓解这一问题,但并不能彻底解决它。在每一个解码步骤中,模型还会读取 KV 缓存,即保存已处理上下文并随其增长的注意力记忆。在我们测量的案例中,评分器(grader)每生成一个令牌的成本是读取一个令牌的 7.4 倍,但该比率会因硬件、问题类型、批次大小、上下文长度及其他因素而异,且可能高于此数值。总体而言,趋势保持不变:生成的成本高于读取的成本。
想象一家按箱子收费的物流公司,但其 outgoing(发出)箱子的收费是 incoming(接收)箱子的七倍。减少箱子总数会有所帮助。减少发出的箱子则能带来七倍于前者的帮助。一种同等压缩输入和输出的方法会错失大部分潜在的节省空间。
账单的第三个组成部分是词汇表本身。模型的最后层,即语言模型头(LM head),将内部状态转化为对每个可能令牌的评分。为此,它为词汇表中的每一项持有一行参数。在 Qwen3-4B 这样规模的模型中,该矩阵占据了近 10% 的参数比例。这是一个大型模型可以吸收而不甚察觉的比例,但它却给小型模型带来了沉重负担。
而且成本并非只支付一次。输入层仅查找正在读取的令牌所在的那一行,而 LM head 则为每个生成的令牌读取并乘以整个矩阵。由于解码过程已经受限于内存带宽,每一行额外的数据都意味着在每个步骤中需要移动更多的数据。一个领域从未使用的条目与一个常用条目的成本完全相同。这就是为什么增加令牌数量可能同时降低计数却提高账单的原因。VocabX 并未将此术语纳入其选择标准,但它通过两个步骤控制词汇表大小:子词合并(sub-word merge)用更小的、特定领域的词汇表替换基础词汇表,而宏(macros)则在固定预算下引入。在评分器中,输出层的行数从约 152,000 行减少到约 65,000 行。
现有方案为何不足
调整模型的词汇表并非新想法。文献中的参考方法,如快速词汇表迁移(FVT)、多词标记化(MWT)以及 Liquid AI 的原位分词器扩展(in-place BPE,即从原始词汇表继续训练 BPE),已经减少了碎片化。当我们在我们的问题上测试这些方法时,发现了两个局限性。
它们根据频率选择令牌,而非根据成本。在输入中频繁出现的表达式对它们来说比在响应中较少出现的表达式更有价值,即使后者是服务成本更高的那个。
它们仅作用于单一层级。它们要么调整单词切分为片段的方式,要么将整个短语合并为一个令牌。这三种方法中的任何一种都无法在同一标准下同时做到这两点。而评分器的最大浪费恰恰在于长重复跨度(如 JSON 信封),子词方法只有在逐个合并每个部分后才能触及这些跨度。
我们如何解决这一问题
VocabX 解决了这两个局限性,以及解决第一个问题之显而易见路径上出现的第三个问题。
首先,我们改变了提问方式。不再询问候选方案能节省多少令牌,而是询问它能节省多少成本和时间。每个被消除的令牌乘以其所在领域的价格:系统提示、用户消息或模型响应。在我们测量的案例中,即评分器,在响应中节省的一个令牌的价值等于在输入中节省的一个令牌的 7.4 倍,词汇表据此构建。
其次,我们同时处理这两个层面。回到那家航运公司的例子:你可以通过更好地包装每个物品来减少箱子数量,或者通过认识到每周都发送同样的十二件套装,并将该套装作为一个整体打包在单个箱子里来减少箱子数量。VocabX 同时采用了这两种策略。在子词层面,它重构了领域词汇被切分成片段的方式。在多词层面,它寻找重复出现的完整跨度,从简短的表达式到完整的行以及 JSON 信封,并将每一个转换为单个标记,我们称之为宏标记(macro token)。这两个层面的选择都基于相同的成本标准,各自拥有独立的预算。
为了找到这些跨度,我们使用了五个挖掘器(miners),即自动搜索语料库中重复跨度的工具,每个挖掘器专门针对一种类型的重复:长单词、整行、表达式、完整句子以及带有标点符号的结构(如 JSON 和标签)。在八个语料库上的平均值显示,最后一个语料库贡献最大,仅在代码领域就占据了 17.5 个点。如果没有这五个挖掘器,评分器上的标记减少量将下降 55.6 个点。
第三,我们估算样本无法看到的部分。根据成本选择标记需要知道每个候选者在已选定的标记之后还能节省多少。精确测量这一点需要在每次选择后重新处理整个语料库,重复数万次。在大规模下常用的方法是在文档样本上进行测量,然后对于样本中不存在的候选者,其增益为零。
一个在 50,000 份文档中出现 37 次的表达式,在 400 份文档的样本中仅约 26% 的情况下会出现。在合同语料库中,只有 16.6% 的候选者出现在 400 份文档的样本中;其余的从未被评估过。
候选者的增益包含两个部分。一部分是它在整个语料库中节省的数量,这部分可以精确计算,无需样本。另一部分是该节省量中尚未被已选定标记覆盖的部分,只有这部分需要估算。对于样本无法看到的候选者,我们使用基于其可见候选者行为的统计估计值,而不是零。在样本中罕见但在语料库中频繁的候选者重新进入了竞争行列。
仅这一项改变,在保持相同样本和相同预算的情况下,就将合同语料库上的标记减少率从 44.7% 提高到了 52.8%。
评分器中发生了什么变化
为了衡量实际效果,我们使用相同的基座模型、相同的数据、相同的配方和相同的随机种子训练了七个版本的评分器。它们之间唯一的区别是分词器。所有版本都在同一台测试机器上运行了相同的 500 个请求,配备 RTX 5090 GPU 和 vLLM 推理服务器,成本是根据测量的运行时间计算的。
模型的平均响应从 350 个标记减少到 101 个标记。成本下降了 77.0%。质量通过基准分数衡量,该分数结合了评分准确性和筛选离题或违反规则文章准确性,从 0.703 上升到 0.810。
达到这一目标需要进行路线调整。词汇表的第一版将每个能力的评分与周围的 JSON 融合为一个单一的宏标记。这是最紧凑的选项,但质量下降了:其基准分数为 0.689,低于生产环境评分器的 0.703。由于评分包含在宏标记内部,每种评分和结构的组合都变成了同一标记的不同变体,模型在一个步骤中同时选择了两者。让评分拥有自己的标记每个响应多花费了 2 个标记,将基准分数提高到 0.810,并且在测量中甚至更便宜:实现了 77.0% 的减少率,而融合版本仅为 73.3%。
这一教训适用于评分器之外的领域。当压缩跨度承载模型旨在做出的关键决策时,压缩是有代价的。目标不是最便宜的词汇表,而是保留重要标记的最便宜词汇表。
超越评分器
为了确定这一增益是否仅适用于我们的案例,我们在五个公共领域(合同、医学论文、科学论文、新闻和 Python 代码)以及三个模型家族(Qwen3、Llama-3.1 和 Gemma-3)上将 VocabX 与三种参考方法进行了比较。所有方法均使用相同的挖掘和测试文档,以及相同的词汇量上限。
在所有十五种组合中,VocabX 的压缩效果均优于最强的参考方法,幅度为 1.2 至 9.3 个百分点。在估算的推理成本方面,VocabX 的剪枝版本也在所有十五项指标中领先,在 Qwen3、Llama-3.1 和 Gemma-3 上的平均优势分别为 10.2、7.5 和 13.6 个百分点。在合同领域,剪枝版实现了 62.6% 的 token 缩减,词汇条目数为 113,426;相比之下,最佳原地 BPE 配置的缩减率为 61.8%,但使用了 207,803 个词汇条目。
只为重要的 Token 付费
VocabX 并不会让模型变得更聪明。它使模型停止逐字支付其无需写入的文本的成本,并停止对其永远不会使用的词汇进行评分。在我们的评估器中,这一措施在保持基础模型和训练配方不变的情况下,将推理成本降低了 77%。
专业化通常始于数据和权重。但它也可以早一层开始,即在词汇层面。
完整的方法、每个实验的数据表以及被弃用的干预措施将收录于我们将很快发布的 VocabX 论文中。
来源
延伸阅读
在 Hugging Face 上探索 Dharma AI,试用我们的演示,下载我们的开源模型,并了解专业化 AI 系统如何在真实的企业应用中超越通用模型。
Models specialized for a domain or a task by fine-tuning general open-source models, such as Qwen and Gemma, inherit a vocabulary built for many domains and general applications.
In an essay grader we run in production, that vocabulary split the text the model writes most into many tokens. This article previews some results from the VocabX paper, which we will publish soon: how we specialized the vocabulary around the cost of each token rather than its frequency, and cut measured inference cost by 77% without changing the base model.
The tokenizer is usually treated as a fixed property of the base model, inherited and left alone. For a model specialized in a single domain, it can be one of the largest cost levers, and it is set before any training step.
One of the systems we run in production is an automated grader for Portuguese essays, built on Qwen3-4B. It receives the essay, the prompt and the supporting texts, and returns a JSON assessment with a grade and a comment for each competency.
The model was already small, at 4 billion parameters, and had already been trained for the task. Its tokenizer, however, was still the original Qwen3-4B one, built for many languages and many kinds of text. In the grader, it was used for a single language, Portuguese, and a single response format. That had two costs: the text the grader writes on every request took more tokens than it needed, and at every generated token the model scored a vocabulary of 151,669 entries, including many this task almost never uses.
We rebuilt that vocabulary with a method we call VocabX, keeping the same base model, the same data and the same training recipe. The numbers in this article come from the current version of the paper, still under review, and may change before publication.
A Vocabulary Built for Everyone
The leading language models read and write in tokens, and the tokenizer decides how text becomes tokens. The tokenizers of the leading general-purpose models are trained on large collections of text from many domains and languages, from source code to contracts to Chinese. For a general-purpose model, that choice makes sense.
For a model used in a single domain, it creates two kinds of waste at once.
The first is fragmentation. The tokenizer splits into many pieces exactly what the domain repeats most. In the grader, every response starts with the same JSON envelope, the same competency names, the same evaluation phrases. In contracts, it is the boilerplate clauses. In code, the identifiers. Each of these sequences costs several tokens where one would do, and the model pays for every one of them on every request.
The second is excess. The vocabulary carries tens of thousands of entries the domain never uses, and the model computes a score for all of them at every token it generates. A model tuned to grade American school biology exams will almost never write in Chinese, yet it computes the probability of every Chinese-language token at every step. The vocabulary splits into many pieces what the domain repeats most, and carries entries the domain never uses.
Reading Is Cheap, Writing Is Expensive
The natural reaction is to count tokens and try to reduce the count. But the count is not the bill.
To see why a generated token costs more, it helps to look at how inference works. It has two phases. In prefill , the model processes the whole input in a single pass through the network: each weight read from GPU memory is reused for every input token, and the limit is compute. In decode , the model generates the response one token at a time, and each token needs a full pass. Each weight read is used only a few times, and the GPU spends most of its time waiting for data to arrive from memory: the limit becomes memory bandwidth.
This effect is strongest exactly where we operate: small models served with moderate batches. Batching requests splits the weight reads across them and eases the problem, but does not solve it. At every decode step, the model also reads the KV cache, the attention memory that holds the context already processed and grows with it. In the case we measured, the grader, a generated token costs 7.4 times a token read, but the ratio varies with the hardware, the problem, the batch size, the context length and other factors, and it can be larger than that. In general, the direction holds: generating costs more than reading.
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. A method that compresses input and output equally misses most of the possible savings.
The third component of the bill is the vocabulary itself. The model's last layer, the LM head, turns the internal state into a score for every possible token. To do that, it holds one row of parameters per vocabulary entry. In a model the size of Qwen3-4B, that matrix accounts for almost 10% of the parameters. It is a share that large models absorb without noticing, but it weighs on a small model.
And the cost is not paid only once. The input layer looks up only the row of the token being read, but the LM head reads and multiplies the entire matrix for every generated token. Since decode is already memory-bound, every extra row is more data to move at every step. An entry the domain never uses costs exactly the same as a frequent one. That is why adding tokens can lower the count and raise the bill at the same time. VocabX does not include this term in its selection criterion, but it controls vocabulary size in two steps: the sub-word merge replaces the base vocabulary with a smaller, domain-specific one, and macros enter under a fixed budget. In the grader, the output layer went from about 152,000 to about 65,000 rows.
Why Existing Solutions Fell Short
Adapting a model's vocabulary is not a new idea. Reference methods in the literature, such as fast vocabulary transfer (FVT), multi-word tokenization (MWT) and Liquid AI's in-place tokenizer expansion (in-place BPE), which continues BPE training from the original vocabulary, already reduce fragmentation. When we tested them on our problem, we found two limitations.
They choose tokens by frequency, not by cost. An expression that appears often in the input is worth more to them than one that appears less often in the response, even if the second is the more expensive one to serve.
They work on a single layer. They either adjust how words are cut into pieces, or merge whole phrases into one token. None of these three does both under the same criterion. And the grader's biggest waste was precisely in long, repeated spans such as the JSON envelope, which a sub-word method only reaches after merging each part, one by one.
How We Solved It
VocabX addresses both limitations, plus a third problem that appears on the obvious path to solving the first.
First, we changed the question. Instead of asking how many tokens a candidate saves, we ask how much cost and time it saves. Each eliminated token is multiplied by the price of the field it appears in: system prompt, user message or model response. In the case we measured, the grader, a token saved in the response is worth 7.4 times one saved in the input, and the vocabulary is built accordingly.
Second, we work on both layers at once. Back to the shipping company: you can cut boxes by packing each item better, or by recognizing that the same twelve-item kit ships every week and sending the kit in a single box. VocabX does both. At the sub-word layer, it rebuilds how domain words are cut into pieces. At the multi-word layer, it looks for whole spans that repeat, from short expressions to full lines and JSON envelopes, and turns each into a single token, which we call a macro token. Both layers are chosen under the same cost criterion, each with its own budget.
To find these spans, we use five miners, automatic searches for spans that repeat across the corpus, each specialized in one kind of repetition: long words, whole lines, expressions, complete sentences, and structures with punctuation such as JSON and tags. Averaged over eight corpora, the last one contributed the most, and on code it alone accounted for 17.5 points. Without all five, token reduction on the grader would drop by 55.6 points.
Third, we estimate what the sample cannot see. Choosing tokens by cost requires knowing how much each candidate still saves after the ones already chosen. Measuring that exactly would require reprocessing the entire corpus after every choice, tens of thousands of times. A common way to do this at scale is to measure on a sample of documents, and then a candidate absent from the sample gets zero gain.
An expression that appears 37 times in 50,000 documents shows up in a 400-document sample only about 26% of the time. In a contracts corpus, only 16.6% of the candidates appear in a 400-document sample; the rest are never evaluated.
A candidate's gain has two parts. One is how much it saves over the entire corpus, and that can be counted exactly, without a sample. The other is how much of that saving is not yet covered by the tokens already chosen, and only that part needs to be estimated. For candidates the sample cannot see, we use a statistical estimate based on the behavior of the ones it can see, instead of zero. A candidate that is rare in the sample but frequent in the corpus is back in the running.
That change alone, with the same sample and the same budget, raised token reduction on a contracts corpus from 44.7% to 52.8%.
What Changed in the Grader
To measure the real effect, we trained seven versions of the grader with the same base model, the same data, the same recipe and the same seed. The only difference between them is the tokenizer. All of them ran the same 500 requests on the same test machine, with an RTX 5090 GPU and the vLLM inference server, and cost was calculated from measured runtime.
The model's response dropped from 350 to 101 tokens on average. Cost dropped 77.0%. Quality, measured by the bench score, which combines grading accuracy and the accuracy of screening off-topic or rule-breaking essays, rose from 0.703 to 0.810.
Getting there required a course correction. The first version of the vocabulary fused each competency's grade with the surrounding JSON into a single macro token. It was the most compact option, but quality dropped: its bench score was 0.689, below the production grader's 0.703. With the grade inside the macro, each combination of grade and structure became a different variant of the same token, and the model chose both in a single step. Giving the grade its own token cost 2 extra tokens per response, raised the bench score to 0.810 and, in the measurement, was even cheaper: a 77.0% reduction, against 73.3% for the fused version.
The lesson applies beyond the grader. Compression has a cost when the compressed span carries the very decision the model exists to make. The goal is not the cheapest possible vocabulary, but the cheapest one that preserves the tokens that matter.
Beyond the Grader
To find out whether the gain was specific to our case, we compared VocabX with the three reference methods on five public domains (contracts, medical papers, scientific papers, news and Python code) and three model families (Qwen3, Llama-3.1 and Gemma-3). All methods used the same mining and test documents, and the same vocabulary ceiling.
VocabX compressed more than the strongest reference method in all fifteen combinations, by 1.2 to 9.3 points. In estimated inference cost, the pruned version of VocabX also led in all fifteen, with average margins of 10.2 points on Qwen3, 7.5 on Llama-3.1 and 13.6 on Gemma-3. On contracts, the pruned version reached 62.6% token reduction with 113,426 vocabulary entries, against 61.8% for the best in-place BPE configuration, which uses 207,803.
Paying Only for the Tokens That Matter
VocabX does not make the model smarter. It makes the model stop paying for text it does not need to write piece by piece, and stop scoring a vocabulary it will never use. In our grader, that cut measured inference cost by 77%, with the same base model and the same training recipe.
Specialization usually starts with data and weights. It can also start one layer earlier, in the vocabulary.
The full method, the tables for every experiment and the discarded interventions will be in the VocabX paper, which we will publish soon.
Sources
Further Reading
Explore Dharma AI on Hugging Face to try our demos, download our open models, and see how specialized AI systems outperform general-purpose models in real enterprise applications.
首次收录 · 2026-10-22 · 10.73 分