Repository navigation
chat : implement token aware parsing - #29307
Conversation
fdf7580 to
27c2bb4
Compare
27c2bb4 to
8f404f3
Compare
f9df835 to
2f91099
Compare
2f91099 to
f3d2453
Compare
f3d2453 to
f762d79
Compare
|
/bot review |
|
❌ Code review failed. |
|
/bot review |
|
❌ Code review failed. |
|
I swear it never works for me. I think the bot failing illustrates why this is necessary :)
|
|
@aldehir lol it seems like to try it the worst time possible: GH was down (504 error) and for the later I'm not sure, but sometimes (rare) the inference provider stuck and cannot doesn't return any tokens at all let's wait until GH is green again and retry https://www.githubstatus.com/ |
|
It's looking green now, let's give it another shot. /bot review |
|
❌ Code review failed. |
|
/bot review |
Automated code reviewFollow-up review of the delta since Fixes verified as correct (point 1) The (point 2) Removing the dead (point 3) The harmony-xml restore ( (point 4) The gemma4 merge resolution is correct as far as I can verify: the token parsers were re-applied on top of upstream's #31915 refactor, and the old gemma4 Still open / newly raised (point 5) The restored harmony-xml tolerance is untested, and so is the whole parser. No template under (point 6) muse-glimmer
Will slow the review (carried forward, unchanged) (point 7) Unused surface still has no production caller: negated (point 8) The specials table is still copied per request: (point 9) Duplicate trigger starts still throw Nits (carried forward, unchanged) (point 10) (point 11) (point 12) Hard-wrapped mid-sentence comments remain, e.g. (point 13) The test-only hooks in the public No whitespace errors or stray non-ASCII in the delta; the three fix commits are each small, single-purpose, and match their stated intent. This review was generated automatically by pi coding agent using |
|
Alright, I've addressed all that I believe need addressing. Here are justifications for the others (excluding nits):
The original author did not include tests for this parser.
It's fine. We don't typically include the EOG token in our parsers/grammars, they're implicit. The documentation properly documents the model behavior.
We will leverage these unused functions in future parser changes.
Models typically have
Will follow up with a PR to handle duplicate tools. That said, this is a substantial change and should probably land after |
…ng-v4 # Conflicts: # src/llama-grammar.cpp
…nd in the parsed output Input: the server tokenized the rendered chat prompt as one string with parse_special, so a file the agent read and sent back as a tool result turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens. llama_tokenize_with_literals() takes byte ranges in which no special token is matched, not even a user-defined one (Qwen's <think> and <tool_call> are USER_DEFINED and match even with parse_special = false). The text runs between real special tokens are still tokenized as a whole, so the tokens across the part borders are those of the whole text. The jinja input marking (ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special), also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins adjacent text parts and passes the literal ranges). Fork default: disabled, client text is text; a request may set "chat_input_special". Output: with the vocab's special token table, a PEG literal or until delimiter that names a special token matches only that token id in the generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as ordinary tokens in reasoning or content no longer cuts it. A small stand-in for the token-aware parsing of the open upstream PR ggml-org#29307. Text without tokens parses as before. Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ng-v4 # Conflicts: # common/parsers/gemma4.cpp
|
@ggerganov when you have a chance, can you take a look at the grammar changes. Thanks! |
…nd in the parsed output Input: the server tokenized the rendered chat prompt as one string with parse_special, so a file the agent read and sent back as a tool result turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens. llama_tokenize_with_literals() takes byte ranges in which no special token is matched, not even a user-defined one (Qwen's <think> and <tool_call> are USER_DEFINED and match even with parse_special = false). The text runs between real special tokens are still tokenized as a whole, so the tokens across the part borders are those of the whole text. The jinja input marking (ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special), also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins adjacent text parts and passes the literal ranges). Fork default: disabled, client text is text; a request may set "chat_input_special". Output: with the vocab's special token table, a PEG literal or until delimiter that names a special token matches only that token id in the generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as ordinary tokens in reasoning or content no longer cuts it. A small stand-in for the token-aware parsing of the open upstream PR ggml-org#29307. Text without tokens parses as before. Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd in the parsed output Input: the server tokenized the rendered chat prompt as one string with parse_special, so a file the agent read and sent back as a tool result turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens. llama_tokenize_with_literals() takes byte ranges in which no special token is matched, not even a user-defined one (Qwen's <think> and <tool_call> are USER_DEFINED and match even with parse_special = false). The text runs between real special tokens are still tokenized as a whole, so the tokens across the part borders are those of the whole text. The jinja input marking (ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special), also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins adjacent text parts and passes the literal ranges). Fork default: disabled, client text is text; a request may set "chat_input_special". Output: with the vocab's special token table, a PEG literal or until delimiter that names a special token matches only that token id in the generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as ordinary tokens in reasoning or content no longer cuts it. A small stand-in for the token-aware parsing of the open upstream PR ggml-org#29307. Text without tokens parses as before. Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd in the parsed output Input: the server tokenized the rendered chat prompt as one string with parse_special, so a file the agent read and sent back as a tool result turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens. llama_tokenize_with_literals() takes byte ranges in which no special token is matched, not even a user-defined one (Qwen's <think> and <tool_call> are USER_DEFINED and match even with parse_special = false). The text runs between real special tokens are still tokenized as a whole, so the tokens across the part borders are those of the whole text. The jinja input marking (ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special), also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins adjacent text parts and passes the literal ranges). Fork default: disabled, client text is text; a request may set "chat_input_special". Output: with the vocab's special token table, a PEG literal or until delimiter that names a special token matches only that token id in the generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as ordinary tokens in reasoning or content no longer cuts it. A small stand-in for the token-aware parsing of the open upstream PR ggml-org#29307. Text without tokens parses as before. Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd in the parsed output Input: the server tokenized the rendered chat prompt as one string with parse_special, so a file the agent read and sent back as a tool result turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens. llama_tokenize_with_literals() takes byte ranges in which no special token is matched, not even a user-defined one (Qwen's <think> and <tool_call> are USER_DEFINED and match even with parse_special = false). The text runs between real special tokens are still tokenized as a whole, so the tokens across the part borders are those of the whole text. The jinja input marking (ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special), also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins adjacent text parts and passes the literal ranges). Fork default: disabled, client text is text; a request may set "chat_input_special". Output: with the vocab's special token table, a PEG literal or until delimiter that names a special token matches only that token id in the generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as ordinary tokens in reasoning or content no longer cuts it. A small stand-in for the token-aware parsing of the open upstream PR ggml-org#29307. Text without tokens parses as before. Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
If there are no objections, I think I'm going to merge this in soon so I can leverage it to work on #32180 (not strictly necessary, just easier to manage w.r.t merges). |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
(cherry picked from commit 1014775)
Overview
Currently, chat parsing operates purely on text. This makes it impossible to distinguish regular text from special tokens.
For example, Qwen3+ has dedicated tokens for
<think>and</think>. It can, however, also generate these as separate pieces that are not intended to signal reasoning boundaries, e.g.</think>. Operating purely on text loses this information and leads to incorrect parsing of model output.This PR implements token-aware parsing, leveraging the changes from #29876.
Chat parsers can use the
p.token()parser to match a token:Or multiple tokens:
p.token({"<|end|>", "<|call|>"}) // any one of these tokens p.token({"<|end|>", "<|call|>"}, /* negate = */ true) // any single token *except* theseIf a piece isn't registered as a special token,
p.token()falls back top.literal(), so parsers still work with templates/vocabs that don't define it.The
p.until()/p.through()parsers can accept a parser as a delimiter that defines both tokens and literals:Additional Information
Extended grammar to support token sets
To parse heterogeneous grammars with both literals and tokens, the grammar sampler needs support for token grammar sets implemented as:
Additionally, any tokens referenced in the grammar are marked as opaque and cannot be matched against with character rules.
Removed
p.ac()The
p.ac()parser seemed a little unintuitive, so I instead merged it with theuntil()parser and aincludedboolean. Thep.through(p.until(END), END)can be used in place, where the parser will consumeENDto match the GBNF grammar rules.Additionally, delimiters can be defined as parser rules with limitations. This is to support a mixture of literal and token delimiters.
Replacing trigger patterns with a scanning grammar
Trigger words/patterns use
std::regexto determine when to start constraining. This has some downsides:std::regexis known to stack overflow or hit an internal recursion limit on large strings.Instead of using
std::regex, we can leverage the grammar sampler itself.p.trigger_rule(name, start, rest)will produce a "scanning" grammar that scans for every triggerstart. Since the grammar sampler operates a token at a time, it is better thanstd::regexand can differentiate between strings and tokens.Effectively, rules come out to be:
Multiple trigger rules will produce a grammar that finds the first trigger and then constrains the branch associated with that trigger (e.g. the
restparameter).std::regex)Decode speed for gpt-oss-20b on an RTX 3090 over a 16k-token generation with tools offered, master vs. PR.
Other changes
Added token aware parsing to the following parsers:
Added infra in
test-chatto support testing by crafting fake special tokens.There is another side to this problem, which is input sanitization to prevent the model from regurgitating special tokens if included in the system/user prompt. That is out of scope for this PR.
Requirements