Route byte-level llama tokenizers to TokenizersBackend#47017
Open
subin9 wants to merge 1 commit into
Open
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: auto |
Model type "llama" spans both SentencePiece (Llama-1/2) and byte-level (Llama-3 / tiktoken) tokenizers under one Hub tokenizer_class (LlamaTokenizerFast). In v5, LlamaTokenizer.__init__ unconditionally installs a Metaspace pre-tokenizer/decoder, which silently drops spaces for byte-level repos (see huggingface#45488), e.g. deepseek-ai/DeepSeek-R1-Distill-Llama-*. The existing MODEL_IDS_TO_TOKENIZERS_BACKEND allowlist only covers specific checkpoints (e.g. the 8B) and misses others (the 70B is still broken). Instead, for the small set of dual-scheme model types, inspect the serialized tokenizer.json: if it declares a ByteLevel pre_tokenizer/decoder, route to TokenizersBackend (which respects tokenizer.json). SentencePiece Llama-1/2 stays on LlamaTokenizer unchanged.
bf64c6f to
e073da1
Compare
Contributor
CI recapDashboard: View test results in Grafana |
Member
Contributor
|
This has been an issue since 5.3.0 I believe, would be great to get this fixed. |
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.
Model type "llama" spans both SentencePiece (Llama-1/2) and byte-level (Llama-3 / tiktoken) tokenizers under one Hub tokenizer_class (LlamaTokenizerFast). In v5, LlamaTokenizer.init unconditionally installs a Metaspace pre-tokenizer/decoder, which silently drops spaces for byte-level repos (see #45488), e.g. deepseek-ai/DeepSeek-R1-Distill-Llama-*.
The existing MODEL_IDS_TO_TOKENIZERS_BACKEND allowlist only covers specific checkpoints (e.g. the 8B) and misses others (the 70B is still broken). Instead, for the small set of dual-scheme model types, inspect the serialized tokenizer.json: if it declares a ByteLevel pre_tokenizer/decoder, route to TokenizersBackend (which respects tokenizer.json). SentencePiece Llama-1/2 stays on LlamaTokenizer unchanged.
What does this PR do?
model_type == "llama"covers two incompatible tokenizer schemes under a single Hubtokenizer_class(
LlamaTokenizerFast):▁)deepseek-ai/DeepSeek-R1-Distill-Llama-*) — byte-level BPE (GPT-2 / tiktoken,Ġ)In v5,
LlamaTokenizer.__init__unconditionally installs aMetaspacepre-tokenizer/decoder, overwriting whatevertokenizer.jsondeclares. For byte-level repos this silently drops spaces on both encode and decode(
"Hello world"→"Helloworld"), which is a silent accuracy regression — see #45488.The current mitigation (
MODEL_IDS_TO_TOKENIZERS_BACKEND, from #46091) is a per-checkpoint allowlist. It fixesdeepseek-ai/deepseek-r1-distill-llama-8bbut misses others — for exampleDeepSeek-R1-Distill-Llama-70Bisstill broken on
main(it is not in the list).Instead of enumerating checkpoints, this PR adds a small content-based rule: for the (rare) set of dual-scheme
model types (
{"llama"}), inspect the serializedtokenizer.json; if it declares aByteLevelpre-tokenizer/decoder, route to
TokenizersBackend(which respectstokenizer.json). SentencePiece Llama-1/2(no
ByteLevelintokenizer.json) keeps usingLlamaTokenizerunchanged.LlamaTokenizer.__init__is notmodified, so there is no risk to the SentencePiece path.
This covers any byte-level Llama repo (8B, 70B, future distills) without maintaining a checkpoint allowlist.
Fixes #45488 for Llama-3-derived checkpoints.
Verification
Round-trip on
"Hello world.\nI'm an AI, so I don't have consciousness."(transformersmain):main)DeepSeek-R1-Distill-Llama-70B(not in allowlist)LlamaTokenizer, roundtrip False ("Helloworld.I'manAI.")TokenizersBackend, roundtrip TrueDeepSeek-R1-Distill-Llama-8BTokenizersBackend, TrueTokenizersBackend, TrueNousResearch/Llama-2-7b-hfLlamaTokenizer, TrueLlamaTokenizer, True (unchanged)A no-network unit test for the detection helper is added in
tests/models/auto/test_tokenization_auto.py::AutoTokenizerTest::test_tokenizer_json_is_byte_level.