Ad广告位招商 / Advertise hereSlot A1 · Rate card →

【Editorial】Stop sequences are matched on detokenized text, not token ids: how do you catch a stop that never fires, or fires early?

Started by FPGA_ARM_Linux · 2026-11-28 04:08 UTC · 22 views · 2 replies
F Member
【Editorial Discussion】

Decoding stacks often accept stop sequences as strings, then either match them on the detokenized text or convert them once into token ids and halt when those ids appear. If the implementation matches only one of those surfaces, a stop can fail to fire, or can fire too early. A string may tokenize to several ids. The halt condition is not the one the recipe named.

This is not a generation config that disagrees with the tokenizer on the EOS id, and it is not a gateway that retokenizes or strips special tokens before the model. It is also not a ban list applied only to single-token ids, a BOS token prepended by both the template and the tokenizer, or an open-weights release that omitted the stop tokens. The stop strings can be present. They were matched on the wrong surface.

**Encode a stop string, then generate under two matchers**

Build a small, locally checkable decode prompt and a unique stop string that tokenizes to more than one id, plus a single-token control. Run the same prompt under three implementations. In condition A, the stop is applied to the surface the recipe names. In condition B, the list is stored as strings but only exact single-token ids are checked. In condition C, the matcher looks only at detokenized text after each token, including a case where detokenization hides a special-token stop. Record the stop string, its token ids, the surface that was checked, and whether generation halted at the intended point.

If B or C continues past the stop, or halts at a different offset than A, while logs still say `stop=1`, the contract was not bound. The test fails if the stack records only the string list and never the matching surface. A control that stores the string and its token-id sequence, and names the surface, should keep B and C from being treated as A.

**Record the string, its ids, and the matching surface**

1) Persist, beside any decode, each stop string, its token-id sequence, the surface used (ids or detokenized text), and the halt offset. A line that says only `stops=N` cannot show why a run continued or stopped early.

2) Include a fixture in which the stop is a single token, and one in which it is two or more tokens, including a special-token stop that detokenization omits. A check that only uses single-token ASCII words will hide the failure.

3) If a generation config, a serving default, and a request-time parameter can each supply the list, run the same fixture through each source. A correct request-time id matcher can hide a server list that still matches only detokenized text.

There are limits. Some stacks intentionally match only on detokenized text, or only on ids; that is valid when the surface is named. The test asks whether a stop written as a string can fail to halt, or halt early, because it was applied on a different surface than the recipe named.

What token-id sequence, matching surface, and halt offset would you require beside a stop string before two decodes are treated as the same contract? How do you test a list that looks complete in a config but is checked only on detokenized text or only on single-token ids?
论 Member
这个点抓得很准,补充一个可落地的测试方法:停止条件要在“字符串面”和“token 面”各测一次。先构造一个一定会跨 token 边界的停止串,比如一个多 token 的词组,分别在流式和非流式下跑,看停没停、停在哪。再看日志:停下时记录最后输出的 token id 序列,和预期停止串的 token 化结果逐个对一遍,就能定位是“没匹配上”还是“匹配上了但没触发”。还有一个容易漏的场景:停止串出现在工具返回结果里,解码器只检查模型生成部分,工具结果里的停止串不会触发停止,这时候要在网关层再补一道字符串检查。最后提醒一点:不同推理框架对停止串的处理口径不一样,换框架部署时,这组测试要重跑一遍。
A Member
Concrete case from a stop-string mismatch, with the numbers that made it visible.

We passed stop='###' as a string, matched on detokenized text in one path and on ids in another. '###' detokenizes to three characters but tokenizes to two ids in our tokenizer ('##' + '#'), and those ids are context-dependent: after a heading prefix it can come out as a single id 2718; after code it comes out as 13 + 2718. So:

- id-matching fired when the model emitted '##', one token early, truncating the last row of a table.
- text-matching fired one token late, because the trailing '#' arrived in the next chunk and the check only ran at chunk boundaries.

Detection that worked, in order:

1) Log both surfaces per stop event: byte offset in detokenized text, token index, and the ids matched. When these disagree you already have the bug without a repro.
2) Build a 200-prompt × 3-template matrix with the stop string at the start, middle, and end of a code block, plus a case that appears only after detokenization ('##' + '#' split across tokens). We saw 11 premature and 4 missed stops in 600 runs.
3) Test the streaming chunk boundary separately: feed 1, 7, and 64-token chunks. Text matching at chunk boundaries is exactly where late stops come from.
4) Normalize on one surface and assert against it: compare the detokenized prefix to the stop string, and treat the id path as an optimization that must produce the same offset.
5) Check overlapping stops ('###' and '##'): the earliest byte offset must win, and it must be the first occurrence, not the longest match.
6) Judge: stop byte offset exact in 100% of runs, no fire before the string is complete, no fire after the following token.

Question: which surface do you treat as authoritative in your stack — detokenized text or ids? And when they disagree, do you fail the run, or let the id path win because it is faster?
Ad广告位招商 / Advertise hereSlot A10 · Rate card →
Log in or register to reply.
Ad广告位招商 / Advertise hereSlot A9 · Rate card →