Fix detokenizer for byte-level tokenizers with an SPM-style decoder (fixes #1041)#1329
Open
robertlangdonn wants to merge 1 commit into
Open
Fix detokenizer for byte-level tokenizers with an SPM-style decoder (fixes #1041)#1329robertlangdonn wants to merge 1 commit into
robertlangdonn wants to merge 1 commit into
Conversation
Mistral tekken v13 tokenizers (Devstral-Small-2, etc.) carry an SPM-style
decoder ("Replace ▁->space, ByteFallback, Fuse, Strip") but a ByteLevel
pre-tokenizer, so their vocabulary uses GPT-2 byte markers. load() selected
the detokenizer from the decoder field alone, matching _is_spm_decoder and
routing them to SPMStreamingDetokenizer, which only strips the "▁" marker and
left the byte-level space marker "Ġ" (U+0120) in the output.
Trust the pre-tokenizer: a ByteLevel vocabulary must use BPEStreamingDetokenizer
regardless of the decoder shape. Genuine SPM tokenizers (no ByteLevel
pre-tokenizer) are unaffected.
Fixes ml-explore#1041.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1041.
Summary
mlx_lm.serveremits the byte-level space markerĠ(U+0120) instead of spaces for Mistral's tekken v13 tokenizers (Devstral-Small-2, etc.):Root cause
tokenizer_utils.load()selects the streaming detokenizer by inspecting only thedecoderfield oftokenizer.json. The tekken v13 tokenizer is a hybrid:decoderis the classic SentencePiece sequence (Replace ▁→space, ByteFallback, Fuse, Strip), so it matches_is_spm_decoderand is routed toSPMStreamingDetokenizer;pre_tokenizerisByteLevel, so the vocabulary actually uses GPT-2-style byte markers (Ġfor spaces).SPMStreamingDetokenizeronly strips the▁marker, so theĠmarkers pass through untouched. (Notably,transformers' owntokenizer.decode()also mis-decodes this tokenizer;BPEStreamingDetokenizerproduces correct output where HF does not.)Fix
A
ByteLevelpre-tokenizer is authoritative: the vocabulary is byte-level and must useBPEStreamingDetokenizer, regardless of thedecodershape. Added_has_byte_level_pretokenizer()and prefer the BPE detokenizer when it (or aByteLeveldecoder) is present. Genuine SPM tokenizers (noByteLevelpre-tokenizer) are unaffected.Testing
test_byte_level_vocab_with_spm_decoder— regression for Devstral (tekken v13 tokenizer) producesĠcharacters instead of spaces in output #1041: asserts BPE routing and exact,Ġ-free output for the tekken v13 tokenizer (downloads tokenizer files only).test_has_byte_level_pretokenizer— unit test for the new helper.test_tokenizersdetokenizer-class expectations still hold: the SPM tokenizers (Mistral-7B-v0.2/v0.3, Phi-3.5) staySPMStreamingDetokenizerand the BPE ones stayBPEStreamingDetokenizer; only the tekken case changes.