Repository navigation
fix(provider): Gemma 4 replies keep characters written as byte tokens - #1703
Conversation
swift-transformers' ByteFallbackDecoder releases <0xHH> tokens only when a later token arrives, so a decode ending in them drops them and shows nothing pending. Gemma 4 writes characters outside its vocabulary that way: a reply ending in one lost it, and DetokenizerV2 could restart its segment inside a run and corrupt the character. LocalTokenizerBridge.decode now releases a trailing run itself. Token IDs, sampling, model state and cache keys do not change; only the text decoded from the same tokens does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests only and the changelog, no source change: a reply of byte tokens alone longer than any walk bound, a control character written as a byte, and the unknown-token test loads the tokenizer repeatedly because the probe reads whichever byte token a dictionary lists first. The changelog now states the stop-string exception, the streamed logprobs entries, logprobs bytes and /detokenize. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Threat model review — advisoryReviewed head Reviewers: anthropic/claude-sonnet-5.5: first pass completed; anthropic/claude-opus-5.5: selected depth completed (2 batches); openai/gpt-6.1-sol: not needed by escalation policy No actionable findings in the reviewed text. This is not a security approval. Models: budgeted Sonnet / selective Opus / selective Sol 6.1. Findings require human validation; this review never requests changes or blocks merging. Scan completed. Coverage: 56/56 source units; cross-file integration complete. 0 selected depth batch(es) pending. All threat definitions indexed; relevant full definitions selected. Unchanged callers are not scanned. Cost this run: $0.0000 reported, $0.0000 reserved with unknown cost; 0 paid request(s), 0 reused batch(es), 0 provider cache-hit tokens. Saved findings and earlier report · Historical findings are not confirmed resolved. Bedrock: 25 request(s), 1182442 uncached + 0 cache-read + 0 cache-write input / 15261 output tokens; 0 request(s) with unknown usage. OpenRouter backup used 0 time(s). Dollar amounts above cover OpenRouter only; AWS billing is reconciled separately. |
davidtai
left a comment
There was a problem hiding this comment.
Approved for the 0.10.7 release (David's order, 2026-11-03). Deferred lane, released by David 2026-11-03. Full-diff review: provider-lane-review.md, section Deferred lane. Composition check: draft #1738, CI run 41631482989 green.
Summary
A Gemma 4 reply loses the characters its vocabulary writes as byte tokens when they come last, and a long reply can corrupt one mid-stream. A reply made only of such characters comes back empty.
all zipped up 🗜all zipped upthe sky today is clear 🩵the sky today is clearthe last syllable 갂the last syllablecube root sign ∛cube root signswift-transformers'
ByteFallbackDecoderreleases<0xHH>tokens only when a later token arrives. A decode ending in them drops them and shows nothing pending, so:DetokenizerV2thinks the segment is clean and restarts it at its 256-token soft cap inside the run, keeping only the last byte token, so the character comes out as U+FFFD.Gemma 4's published
tokenizer.jsondeclares that decoder, and characters outside its 262,144-token vocabulary are written that way: 90 of 114 assigned code points in U+1FA70–1FAFF (🩵,🪩), 170 of 768 in U+1F300–1F5FF, 11,554 of 20,992 in U+4E00–9FFF, 9,439 of 11,172 Hangul syllables.LocalTokenizerBridge.decodenow releases a trailing run itself. A partial run reads as U+FFFD, whichDetokenizerV2already holds back, so its restart can no longer land inside a character. A probe at load turns this on only when a lone byte token decodes to nothing, so other decoders are untouched and a fixed upstream turns it off.Token IDs, numeric logprobs, sampling, model state and cache keys do not change, with one exception below: a stop string a trailing run completes now matches one token sooner. This changes served text on purpose, so it needs the owner's acceptance.
Target: 0.10.7. It is provider-only, needs no SDK repin, and sits on v0.10.6 (
84ddfdba6b). Merges with #1693, #1650, #1668 and #1669 with no conflict. Merge it after #1693: see Limitations.Linked issue
None; found by the verifier of #1693 during the 0.10.7 sweep, then confirmed on a running provider.
Before And After
flowchart TB subgraph B["Before"] direction LR B1["tokens end in <0xF0> <0x9F> <0x97> <0x9C>"] --> B2["ByteFallbackDecoder: bytes wait for a later token"] B2 --> B3["no later token: bytes dropped"] B3 --> B4["client: all zipped up"] end subgraph A["After"] direction LR A1["tokens end in <0xF0> <0x9F> <0x97> <0x9C>"] --> A2["LocalTokenizerBridge.decode: releases the trailing run"] A2 --> A3["partial run reads as U+FFFD, held back by DetokenizerV2"] A3 --> A4["client: all zipped up 🗜"] end B ~~~ ATest plan
Arms: v0.10.6
84ddfdba6b(binarye8cba71e…) and this commit (binary01784ed0…, built from16d59d9ce; the later commit on this branch changes only a test and the changelog). SDK7933a0fa, mlx-swiftc633d221, mlx72dffa2d, same metallib. Gemma 4 26B QAT, weights verified against the published manifest, isolatedstart --localproviders on an M3 Ultra, temperature 0, MTP off. Plan fixed before the data:candidates/N7-13-byte-fallback-flush/PLAN.mdin the sweep folder.Red/green: new
LocalTokenizerByteFallbackTests(7 tests, 9 cases): 42 issues with master's bridge; all pass with the change.Mutations caught: unknown names read as bytes 1 issue, probe removed 2,
skipSpecialTokenswalk removed 1.Live, 30 exact-repeat lines ending in a character confirmed outside the vocabulary from the published file, fixed before the build:
Live, 6 long replies, 838 to 1,400 tokens, Hangul and CJK with such characters, no newlines: U+FFFD in client text 12 on master, 1 here, streaming and non-streaming. The one left is the last character of a reply stopped by
max_tokensinside a character, whichDetokenizerV2's header says happens. Characters delivered 3,425 → 3,642 streaming.Same tokens, non-streaming: per-token numeric logprobs bitwise equal on 36 of 36 requests (6,723 tokens). Completion tokens and finish reasons equal on 72 of 72, streaming included.
Same tokens, streaming: the plan's gate failed as written. Numeric logprob lists differ on all 36 streaming requests, by length: master's stream omits the entries of tokens whose text never appeared, the same bytes this change releases, so this PR's list is longer (6,593 → 6,868 entries). Master's list is a subsequence of this PR's on 36 of 36. Reported as a failed gate, not reinterpreted.
Frozen 11-request agentic conversation × 10 units with logprobs on: logprobs bitwise equal, content, usage, cached tokens and finish equal, 110 of 110 (6,219 tokens). It has no byte-fallback token, so its text does not move.
Other tokenizers: bridge decode equals upstream decode on the published gpt-oss, Nemotron Lightning and Qwen3.5 tokenizers, 2,000 sequences each, both
skipSpecialTokensvalues, 0 differing. None has<0xHH>tokens.Nearby suites on this commit: 112 tests in 17 suites pass.
make docs-impact-check BASE=84ddfdba6b…,make docs-check,make changelog-check: pass.Independent verifier (fresh subagent, its own prompts, comparison code and tests, gates fixed in advance, report in the sweep folder): PARTIAL. The fix reproduces and the identity claim needed the two corrections now in this description.
tokenizerson the published file, 102,994 comparisons, every ID alone and all 65,536 byte pairs: no wrong text for valid UTF-8. For invalid bytes this writes one U+FFFD per bad sequence, as upstream does mid-run, where Python writes one per byte.Timing: no-regression set under the sweep's quiet rule, kept 2026-11-02 18:59 UTC: the frozen 11-request Gemma 4 26B QAT conversation,
--mtp off, same-text A/A 5 pairs then ABBA 10 pairs, v0.10.6 against16d59d9ce6(this head adds only tests and the changelog). Total −0.37%, follow-up TTFT −0.40%, first-request TTFT −0.85%, decode +0.30%, non-donor waits −1.54%; no pair beyond its threshold (3.00% on total and follow-up TTFT); outputs and cached tokens equal on 110/110.make provider-teststeps on a local combination of v0.10.6, fix(provider): Gemma 4 text keeps its spaces before punctuation (git add . came back as git add.) #1693, Gemma 4 and <think> models: client stop sequences skip reasoning and tool calls #1650, perf(provider): prepare the checkpoint row encoder once per segment (Gemma donor stream end 1.7–2.0 s → 1.0–1.2 s) #1668, perf(provider): check each restored checkpoint row once for non-finite values (Gemma cached follow-up TTFT −26.6%, ~0.2 s per SSD restore) #1669, this PR and the Gemma decode limits candidate (never pushed): 644 XCTest, 129 skipped, 0 failures; 6,736 Swift Testing tests plus 9 isolated runs pass, 0 crashes. The metallib was staged by hand: the Metal Toolchain is not installed here. CI runs the suite on this head. Combined timed A/B against v0.10.6, 2026-11-01, frozen 11-request Gemma conversation, MTP off, same-text A/A 5 pairs then 10 ABBA pairs, under the 25% background-CPU rule with the owner's waiver for iCloud'sfileproviderdandbird: summed follow-up TTFT 8.911 → 6.336 s, −28.87%, 10/10 beyond 4.33%; donor waits 5.633 → 2.639 s, −53.07%, 10/10 beyond 4.34%; summed time 20.219 → 14.213 s, −29.74%, 10/10 beyond 3.01%; decode +11.41%; outputs and cached tokens equal on 110/110. One no-regression clause fails as written: non-donor stream-end waits +27.67% against a 13.21% threshold, on all 10 pairs, which is 12 → 16 ms per request.Refactor pass: dedicated subagent; probe renamed so it no longer shares a name with the stored property, one comment on the non-obvious branch, unused import removed, docs and changelog tightened. No behaviour change. Tests and doc checks re-run on the result.
Limitations
max_tokensnow ends in U+FFFD instead of dropping it./detokenizekeeps a trailing run too.completion_tokensfalls by one (stop: ["🗜"]: 9 → 8). This is the one case where the tokens generated change.logprobs: a streamed reply gains the entries of tokens whose text never appeared. A byte token'stokenandbytesmove from empty to U+FFFD or its ASCII character, not the real byte.a ..z). The encoder never writes those tokens and none reached the top 20 alternatives at the 10 positions checked. fix(provider): Gemma 4 text keeps its spaces before punctuation (git add . came back as git add.) #1693 turns that cleanup off.DetokenizerV2would close it for every tokenizer; not done here.StripunderskipSpecialTokens: true, and a byte token flagged special.#huggingFaceTokenizerLoaderand the cluster worker'sPromptTokenizerdon't go through this bridge and keep the old behaviour. The worker's tokenizer serves qualification tools and only prints readable text. The defect itself is upstream in swift-transformers 1.3.3; no upstream PR was opened.Components touched
Protocol / interface changes
Documentation impact
docs/architecture/inference.md,docs/developer/test.md,changelog.d/gemma-byte-fallback-flush.md)AI usage: the code, tests, measurements and this description were produced by Claude (Claude Code, Opus 5.5) during an unattended sweep, at the repository owner's request. A human has not yet reviewed them.
🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need.