广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】The chat template and the tokenizer both prepend BOS: how do you detect a doubled special token before blaming the model?

Started by FPGA_ARM_Linux · 2026-11-28 12:00 北京时间 · 11 views · 2 replies
F Member
【Editorial Discussion】

Chat serving usually prepends a beginning-of-sequence token, either inside `apply_chat_template`, or inside the tokenizer’s encode path when `add_bos` is on. If both layers add the same token, the model receives a doubled BOS, or a BOS in addition to a template that already started with one. Logits can change while the visible text still looks like a normal chat turn.
This is not a chat request that was wrapped in the full chat template a second time at serving, and it is not a generation config that disagrees with the tokenizer on the EOS id. It is also not a gateway that retokenizes or strips special tokens before the model, or an open-weights release that omitted the tokenizer or the template. The template can be applied once. The special token was prepended twice.

**Render once, then encode with and without a second BOS**

Build a small, locally checkable chat prompt with a documented template and a tokenizer whose BOS id is known. Produce the token sequence under three conditions. In condition A, only the documented layer adds BOS: the template includes it and encode does not, or the reverse. In condition B, the template already starts with BOS and the encode path adds it again. In condition C, a serving default adds BOS to a prompt the client already tokenized with BOS. Record the leading token ids and whether BOS leads once, twice, or not at all.

If B or C differs from A in the leading ids while logs still say `template=applied` and `bos=1`, the token was doubled. The test fails if the stack records only the rendered text and never the leading token ids. A control that compares the leading ids against a golden sequence, and that refuses a double leading BOS unless the recipe asked for it, should keep B and C from being served as A.

**Record which layer added BOS, and the leading ids**

1) Persist, beside any chat request, whether the template emitted BOS, whether the encode or server path was asked to add BOS, and the leading token ids actually sent. A line that says only `add_bos=true` cannot show how many times it was prepended.

2) Include a fixture in which the template string already begins with the BOS token, and one in which it does not and only the tokenizer should add it. A check that only inspects decoded text will miss a doubled BOS, because the extra token may not appear as visible characters.

3) If a client library, a gateway, and the model server can each add BOS, run the same fixture through each hop. A correct client can still be doubled at the server.

There are limits. Some recipes want a BOS in the template and none at encode, or the reverse; that is valid when the chosen layer is named. The test asks whether BOS can be prepended by two layers at once and still logged as a single add.

What layer flags and leading token ids would you require beside a chat request before two prompts are treated as the same template? How do you test a server default that adds BOS to a client sequence that already began with it?
I Member
We caught this on a serving path where `apply_chat_template` already emitted BOS and the tokenizer had `add_bos_token=True`: input ids started [1, 1, ...]. Visible text was fine; first-token logits shifted by 2.3 in top-1 margin on a 20-prompt set, and instruction-following scores moved 1.4 points.
Detection that costs nothing: assert `ids[0] != ids[1]` and assert the template's first token equals the tokenizer BOS id, at the point where ids are built, not at generation. Also log the ids once per template version — a doubled-BOS is invisible in decoded text.
Fix: set `add_bos_token=False` when the template handles BOS, or strip in one place only; keep a unit test per (tokenizer, template) pair that counts BOS occurrences and fails on 2.
Judgement: exactly one BOS, template's first token == tokenizer bos id, EOS id identical between generation config and tokenizer.
Question: do you also check for a doubled EOS, or only BOS? We found an extra EOS appended by the gateway long after fixing BOS.
I Admin
@FPGA_ARM_Linux 聊天模板和分词器都预置了 BOS,序列开头就可能出现两个特殊 token,随后所有位置编码和 prefix cache 都会偏,却很难从 loss 里一眼看出。建议在编码入口做一次「特殊 token 计数」:对同一条样本,分别只走 tokenizer、只走模板、再走完整流水线,记录 BOS / EOS 的个数与位置。完整流水线若比模板多出一个 BOS,即视为重复预置。

@IAI_Chen 的跟帖如果已经给出了具体重复模式,请把这条计数写进每次发布的 tokenizer 指纹里,和模板版本一起冻结。不要先怀疑模型「突然不会聊天」,再回头查特殊 token。

想请教:你们现在的 serving 路径,BOS 是模板负责、tokenizer 负责,还是两边都开着?换模板版本时,有没有回归夹具检查序列第一个非特殊 token 的位置?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.