fix: add missing pre-tokenizer type for GPT-2 BPE models#508
Open
raphaelbgr wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: add missing pre-tokenizer type for GPT-2 BPE models#508raphaelbgr wants to merge 1 commit intomicrosoft:mainfrom
raphaelbgr wants to merge 1 commit intomicrosoft:mainfrom
Conversation
The GGUF conversion scripts do not write the `tokenizer.ggml.pre`
metadata key for BitNet models, causing llama.cpp to fall back to the
default pre-tokenizer. This produces degraded or incoherent output
with the warning:
"missing pre-tokenizer type, using: 'default'"
"GENERATION QUALITY WILL BE DEGRADED!"
Root cause:
- convert-hf-to-gguf-bitnet.py: BitnetModel.set_vocab() calls
_set_vocab_sentencepiece() which hardcodes pre="default", instead
of _set_vocab_gpt2() which correctly detects and writes the
pre-tokenizer type.
- convert-ms-to-gguf-bitnet.py: add_meta_vocab() writes the
tokenizer model but never writes the pre-tokenizer type.
Fix:
- Change BitnetModel.set_vocab() to call _set_vocab_gpt2()
- Add add_token_pre_type("gpt-2") in add_meta_vocab() for GPT-2 models
Tested on Mac Mini M4 (ARM64) with BitNet-b1.58-2B-4T: reconverted
model produces coherent output at ~41 tokens/sec via bitnet.cpp,
matching the quality seen through HuggingFace transformers.
Author
|
@microsoft-github-policy-service agree |
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.
Summary
BitnetModel.set_vocab()inconvert-hf-to-gguf-bitnet.pyto call_set_vocab_gpt2()instead of_set_vocab_sentencepiece()add_token_pre_type("gpt-2")inconvert-ms-to-gguf-bitnet.pyfor GPT-2 BPE modelsProblem
The GGUF conversion scripts do not write the
tokenizer.ggml.premetadata key, causing llama.cpp to fall back to the default pre-tokenizer with the warning:This results in incoherent/garbage output from
llama-cliandrun_inference.py, even though the model weights are correct. The issue affects all users running inference via bitnet.cpp.Root cause
convert-hf-to-gguf-bitnet.py:BitnetModel.set_vocab()calls_set_vocab_sentencepiece(), which hardcodestokenizer.ggml.pre = "default"and setstokenizer.ggml.model = "llama". BitNet-b1.58-2B-4T uses a GPT-2 BPE tokenizer (128K vocab, tiktoken-based), not SentencePiece.convert-ms-to-gguf-bitnet.py:add_meta_vocab()writesadd_tokenizer_model()but never callsadd_token_pre_type(), leaving the pre-tokenizer field empty in the GGUF.The default pre-tokenizer uses different regex rules for text splitting, causing incorrect tokenization boundaries that produce nonsensical output.
Fix
_set_vocab_sentencepiece()to_set_vocab_gpt2()in the HF converter — this correctly detects the pre-tokenizer type via hash matching and writes it to the GGUF.add_token_pre_type("gpt-2")in the MS converter for GPT-2 models.Test plan
tokenizer.ggml.pre = "gpt-2"is present in the output GGUFRelated issues
The pre-built GGUF on HuggingFace (
microsoft/BitNet-b1.58-2B-4T-gguf) was also converted without this metadata and has a non-standard chat template. Regenerating it with these fixes would resolve the output quality issues.