Repository navigation
Conversation
|
Prioritized P1 frontend correctness and categorized for design review. The report shows that special-token text supplied as user content can become protocol token IDs. The proposed repair carries trust provenance through the template VM and uses ordinary tokenization for untrusted spans. Review provenance through containers, macros, captures and function results, ordinary-text tokenization across span boundaries, and multimodal placeholder accounting. The reported 580 fixture tests support that review; preserve legitimate template-emitted protocol markers while ensuring quoted user markers cannot create them. No specific affected model is established by the PR description. |
original-el8
left a comment
There was a problem hiding this comment.
Requesting changes: the shared frontend is the appropriate place for this repair, but the implementation still allows request content to become trusted and introduces regressions in ordinary tokenization and image prompts. The four inline findings were reproduced at 6b44d78. They violate the prompt-renderer requirements to preserve ordinary token IDs, distinguish literal request markers from protocol markers, and retain image placeholders.
Validation: all 21 existing Jinja unit tests passed with the pinned external templates prepared by tests/frontend/prepare_templates.py. Additional temporary regression tests exercised the actual VM and Frontend::render_chat; Qwen tokenization checks used the recipe's pinned nvidia/Qwen3.8-Flash-Next-FP4-Local template and tokenizer at b9ce18a6887e18c089fdea8509a33e63429a3300. The Gemma provenance reproduction used the pinned chat_template.google fixture. These checks reproduced the failures described inline; the existing Jinja tests do not catch them. I did not run the full reported 580-test suite, image preprocessing, or GPU serving.
Please preserve provenance through indexing, containers, calls, and string transformations, and preserve ordinary BPE tokenization across provenance boundaries. Add regression tests for the concrete cases below before merging.
| } | ||
| let value = self.hinted(v.index(&key)); | ||
| self.stack.push(value); | ||
| self.stack_trust.push(trust || key_trust); |
There was a problem hiding this comment.
[P1] Preserve request provenance when indexing
trust || key_trust makes an untrusted container's value trusted whenever the index is a template literal. For example, rendering {{ messages[0]['content'] }} with request content <|image_pad|> produces [(13, true)]. This also affects the pinned Gemma template: a user message containing <turn|> is emitted in a trusted run through message['content'], so it still reaches the tokenizer that recognizes protocol tokens. A trusted lookup key must not confer trust on the selected request value. Add both the minimal indexing case and an actual recipe-template regression.
| let (v, trust) = self.pop_trusted(); | ||
| input_trust = trust; | ||
| filter(name, v, args, &kw_named, !self.hint_texts.is_empty())? | ||
| } | ||
| _ => { | ||
| let v = self.pop(); | ||
| method(name, v, args, &kwargs)? | ||
| let (v, trust) = self.pop_trusted(); | ||
| input_trust = trust; | ||
| method(name, v, args, &kw_named)? | ||
| } | ||
| }; | ||
| let trusted = match op { | ||
| I::Test(_) => true, | ||
| I::Call(_) => !result.carries_untrusted(), | ||
| _ => arguments_trusted && input_trust && !result.carries_untrusted(), | ||
| }; | ||
| let result = Self::force_trust(result, trusted); |
There was a problem hiding this comment.
[P1] Preserve mixed trust runs through trimming
The trim implementation reconstructs a changed string with Value::marked, which drops its trust runs. This filter path then makes the whole result untrusted when its input contained request text, including legitimate template-emitted image markers. Reproduced with the pinned Qwen template and tokenizer: a user message containing one image followed by text hello retains one image placeholder token, but changing the text to hello produces zero placeholder tokens, while the rendered text still contains the image envelope. expand_prompt consequently rejects the placeholder count for that valid image request. Trim must slice and remap provenance alongside text and checkpoint hints instead of discarding it.
| let mut segments = Vec::with_capacity(runs.len()); | ||
| let mut cursor = 0usize; | ||
| for (end, allow_special) in runs { | ||
| if end > cursor { | ||
| segments.push(TokenSegment { | ||
| text: rendered[cursor..end].to_owned(), | ||
| allow_special, | ||
| image_index: None, | ||
| }); | ||
| cursor = end; |
There was a problem hiding this comment.
[P1] Preserve ordinary BPE merges across trust boundaries
Turning every trust run into an independently encoded segment changes token IDs even when the prompt contains no user-supplied protocol markers. With the pinned Qwen template/tokenizer, render a user message followed by an assistant lookup tool call with arguments {"x":"hello"} and options enable_thinking=false, add_generation_prompt=true. In the resulting <parameter=x> text, full-text encoding uses token 24582 for =x, while this path emits [28, 87] because = is trusted and x is untrusted. A minimal BPE vocabulary containing a, b, and ab reproduces the same problem with template text a followed by request text b. Merging only adjacent runs with equal trust cannot fix this: special-token eligibility must be enforced without introducing ordinary-text tokenization boundaries.
| pub fn carries_untrusted(&self) -> bool { | ||
| match self { | ||
| Self::String(s) => s.2.iter().any(|(_, trusted)| !trusted), | ||
| _ => false, | ||
| } | ||
| } | ||
| pub fn baked_untrusted(value: Self) -> Self { | ||
| match value { | ||
| Self::Str(s) => { | ||
| let runs = vec![(s.len(), false)]; | ||
| Self::marked_runs(s.to_owned(), Vec::new(), runs) | ||
| } | ||
| Self::String(s) => Self::String(Rc::new((s.0.clone(), s.1.clone(), vec![( | ||
| s.0.len(), | ||
| false, | ||
| )]))), | ||
| other => other, |
There was a problem hiding this comment.
[P1] Carry provenance through containers and function results
Both helpers handle strings only, and I::Call derives result trust solely from carries_untrusted() without preserving argument provenance. As a result, {% set ns = namespace(body=m) %}{{ ns.body }} renders request value m='<|image_pad|>' as trusted. A separate reproduction assigns the request messages array to ns.body and iterates it; each message's content is also emitted as trusted because baked_untrusted leaves the array unchanged. This contradicts the claimed recursive provenance and leaves special-token injection possible through normal template operations. Preserve provenance through container construction, assignment, traversal, and builtin results; fixing literal indexing alone will not resolve these cases.
6b44d78 to
2118c84
Compare
|
All four findings are fixed in P1 indexing (vm.rs) — P2 trim (vm.rs/value.rs) — P3 ordinary tokenization (mod.rs) — implemented your proposed shape: the full rendered text is encoded in one pass with the template tokenizer (ordinary prompts are byte-identical to pre-patch encoding, merges across trust boundaries preserved), then tokens whose ids are in the added-token decoder and whose byte offsets overlap an untrusted span are grouped into maximal windows; each window's byte range is re-encoded with the ordinary tokenizer and spliced with byte-offset accounting. Regression: P4 provenance (value.rs/vm.rs) — Validation: fixture suite with Two minor follow-ups found in our own second-pass review, disclosed for completeness (both over-taint — fail toward untrusted, no injection path): |
…mplate VM Rendered chat templates interleave template-emitted text with user content. Special tokens arriving in user content were tokenized with the same tokenizer as template output, so a prompt quoting a marker such as <|vision_start|> could inject a real special-token id into the model stream. The renderer records, per output span, whether the template VM produced it or user content flowed through it: - The jinja VM emits trust runs: trusted spans for template literals and untrusted spans for injected values. Provenance travels inside values, so namespace and dict construction, attribute assignment, list and map literals, macro captures, iteration, and slicing preserve it, and an indexing or lookup key only selects a value; it never confers trust on it. - Text-transform filters that shrink text (trim and the strip family) slice both the checkpoint hints and the trust runs to the surviving byte range instead of discarding them, so a template marker emitted next to user content keeps its special-token id across a trim. - The whole rendered prompt encodes in one pass with the template tokenizer, preserving the pre-patch token ids for ordinary prompts, including BPE merges that cross trust boundaries. Tokens whose id comes from the added vocabulary and whose byte range overlaps an untrusted span are then re-encoded with an ordinary tokenizer that has no added vocabulary, and the replacement ids are spliced in with byte offset accounting, so user-quoted markers cannot produce protocol ids. - expand_prompt counts and splices image placeholder ids from prompt_token_ids instead of re-splitting the text, which keeps placeholder accounting correct across trust boundaries and lets ImagePrompt drop its write-only text field (the field had no runtime readers; its single reader sat in a test driver this patch fixes in the same motion). The fixture suite passes; ordinary prompts render byte-identical token ids to full-text encoding while prompts carrying real image placeholders keep their expansion through trim and mixed-trust paths. Signed-off-by: Carlos Augusto <mb@lab.how>
2118c84 to
33d2dff
Compare
|
Rebased onto current |
|
@original-el8 rebased onto current main — the Inkling 2 conflicts are resolved (new layouts keep working through the reduced |
Rendered chat templates interleave template-emitted text with user content. Special tokens arriving in user content were tokenized with the same tokenizer as template output, so a prompt quoting a marker such as
<|vision_start|>could inject a real special-token id into the model stream.The renderer now records, per output span, whether the template VM produced it or user content flowed through it:
TokenSegmentcarries a trust bit; jinja emits trusted spans for template literals and untrusted spans for injected values, including values reaching the output through namespace containers, function results, and macro captures (the bit recurses with the value).expand_promptcounts and splices image placeholder ids fromprompt_token_idsinstead of re-splitting the text, which keeps placeholder accounting correct across trust boundaries and letsImagePromptdrop its write-onlytextfield (the field had no runtime readers; its single reader sat in a test driver this patch fixes in the same motion).Testing:
flare-heliosfixture suite (FLARE_TEST_TEMPLATE_DIRprepared viatests/frontend/prepare_templates.py): 580 passed;test_imagesfixture driver builds again (it had been left unbuildable by the signature change this patch supersedes).