Skip to content

Emit only the new scalars when a token extends the previous character - #613

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
spokvulcan:fix/streaming-detokenizer-grapheme
Sep 10, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
spokvulcan:fix/streaming-detokenizer-grapheme

Conversation

@spokvulcan

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes #612.

NaiveStreamingDetokenizer.next() measured the common prefix between the previous decode and the new one in Characters, so a token that appended a combining scalar (U+FE0F, a zero-width joiner, an accent) to the previous character turned the whole merged grapheme cluster into "new" text and re-emitted its base. The prefix is now measured in Unicode scalars, which compares exactly and keeps the #465 behaviour for tokenizers whose decode is not append-only. Four tests in StreamingDetokenizerTests stream a ZWJ flag sequence, a variation selector after an ASCII quote, a combining accent, and a variation selector after a multi-byte character split across two tokens, and compare the scalar sequence with the full decode; they fail on main and pass with this change.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: the diagnosis, the fix, the tests and this description were written with Claude Code (Anthropic) under the author's direction, and are posted from the author's account.

NaiveStreamingDetokenizer.next() measured the common prefix between the
previous decode and the new one in Characters, so a token that appended a
combining scalar (U+FE0F, a zero-width joiner, an accent) to the previous
character re-emitted the whole merged cluster: a ZWJ flag sequence
streamed as its base flag repeated, a quote followed by a variation
selector as two quotes. The prefix is now measured in Unicode scalars,
which compares exactly and keeps the fallback for tokenizers whose decode
is not append-only.

Four tests in StreamingDetokenizerTests stream a ZWJ flag sequence, a
variation selector after an ASCII quote, a combining accent, and a
variation selector after a multi-byte character split across two tokens,
and compare the scalar sequence with the full decode.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, nice find! Thank you.

@davidkoski
davidkoski merged commit 4c3d793 into ml-explore:main Sep 10, 2026
4 checks passed
wballard added a commit to swissarmyhammer/mlx-swift-lm-old that referenced this pull request Sep 14, 2026
Apply the upstream commits e3d4a20..3e6ea1e as a 3-way patch. CLAUDE.md is
excluded.

- db05290 Let training differentiate the gated-delta recurrence (ml-explore#616)
- 4c3d793 Emit only the new scalars when a token extends the previous character (ml-explore#613)
- 1012230 Gemma 4 VLM: fuse float32 logit softcapping (ml-explore#615)
- 9ce5b4f Fix generation loop blocking Swift cooperative workers (ml-explore#611)
- 238ad74 Add configuration-based LoRA metadata discovery (ml-explore#597)
- 604fae7 RotatingKVCache: make trim wrap-aware instead of corrupting the ring (ml-explore#584)
- a56aee3 Add bounded cross-dialect tool-call recovery (ml-explore#548)
- 3e6ea1e Reuse the KV cache for append-only media turns (ml-explore#515)

Conflict resolutions:

- Evaluate.swift: keep the shared generateSpeculativeStream helper. It now
  receives parameters and tools, and passes tools and toolCallPolicy to
  TextToolTokenLoopHandler for the draft-model and MTP paths.
- PromptCacheReusePolicy.swift: keep the package access of the turn fields,
  and make canSplitPreparedMedia package also. reconcilePromptCache has no
  prepared input to split, thus it answers .appendMediaSuffix with nil.
- ToolCallFormat.swift: keep buffersEntireResponse and add supportsBareJSON.
- ToolCallProcessor.swift: the buffersEntireResponse check stays first in
  processChunk. The recovery scanner path comes after it.

Follow-up changes:

- AllowedToolOutputRouter: join adjacent response outputs into one event.
  The recovery scanner can divide response text at a "<" that does not
  start a call.
- Gemma4FusionTests: delete testFusionLatency. It runs only when an
  environment variable is set, and stable runs no skipped tests.

Tests: all five bundles pass with no skipped test (MLXLMTests 1170 Swift
Testing + 693 XCTest, MLXGuidedGenerationTests 70, MLXFoundationModelsTests
213, CXGrammarTests 7, MLXHuggingFaceMacrosTests 5).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TEgdBA8Ap14pU1EfmHkazD
djandrewgibbons pushed a commit to djandrewgibbons/mlx-swift-lm that referenced this pull request Sep 26, 2026
…ml-explore#613)

NaiveStreamingDetokenizer.next() measured the common prefix between the
previous decode and the new one in Characters, so a token that appended a
combining scalar (U+FE0F, a zero-width joiner, an accent) to the previous
character re-emitted the whole merged cluster: a ZWJ flag sequence
streamed as its base flag repeated, a quote followed by a variation
selector as two quotes. The prefix is now measured in Unicode scalars,
which compares exactly and keeps the fallback for tokenizers whose decode
is not append-only.

Four tests in StreamingDetokenizerTests stream a ZWJ flag sequence, a
variation selector after an ASCII quote, a combining accent, and a
variation selector after a multi-byte character split across two tokens,
and compare the scalar sequence with the full decode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] NaiveStreamingDetokenizer repeats the previous character when a token adds a combining scalar

3 participants