GGUF token ids differ from Hugging Face transformers? Find the cause
A correctly converted GGUF normally gives the same token ids as the Hugging Face tokenizer. llama.cpp b12899 gave the Hugging Face ids on all 708 reference strings of the 15 vocabularies in its own test set. When your ids differ, the cause is in the GGUF header or in the build, and it is usually one of seven things: a different vocabulary, a special token typed NORMAL, a missing tokenizer.ggml.pre, a wrong pre-tokenizer rule, NFC normalization that llama.cpp skips for BPE, a different BOS setting, or whitespace handling around special tokens. tokcheck.py tokenizes the same strings on both sides, shows the first token that differs, and names the cause.
Why does a GGUF tokenize differently from the Hugging Face tokenizer?
Because the GGUF header says something different from the tokenizer files, or because the llama.cpp build implements one rule differently. The table lists the seven causes in the order to rule them out; the first column is the name tokcheck.py prints.
| Cause | What you see | Why | Fix |
|---|---|---|---|
vocabThe vocabulary is not the same | Different vocabulary size, or a different token text at the same id. Everything after it differs. | The GGUF was converted from another revision, or from a fine-tune or merge that added tokens. | Reconvert from the exact model directory you compare against. |
special-typeA special token is typed NORMAL | The token's text is split into pieces (<, |, im, _start ...) even with special-token parsing on. Chat prompts reach the model as fragments, with no error. | llama.cpp only matches a token as a whole when its tokenizer.ggml.token_type is CONTROL (3), USER_DEFINED (4) or UNKNOWN (2). The converter writes NORMAL (1) when the token is missing from the added tokens of the directory it reads. | Reconvert from a directory whose tokenizer.json / tokenizer_config.json still list the token as an added token, or rewrite the type entries. |
pre-missingNo tokenizer.ggml.pre | Contractions, runs of spaces, digits and punctuation split differently. The log says missing pre-tokenizer type, using: 'default' and GENERATION QUALITY WILL BE DEGRADED!. | A BPE GGUF made by an old or third-party converter. llama.cpp falls back to the default rule. | Reconvert, or write the name into a copy: gguf-new-metadata in.gguf out.gguf --pre-tokenizer NAME. |
pre-tokenizerWrong pre-tokenizer name, or a rule that differs in this build | Plain text without special tokens differs, usually only one kind: digits (1234567), whitespace before punctuation, or letters with combining marks. | The converter picks the name from a hash of the Hugging Face tokenizer's output (chkhsh). A hand-mapped hash, a newer transformers that tokenizes the probe text differently, or a regex bug in llama.cpp all end here. | Look up the chkhsh that tokcheck prints, fix the name in a copy, and update llama.cpp (see the table of fixes below). |
unicode-normalizationNFC normalization is skipped | Only text with decomposed characters differs: e + U+0301 stays two tokens where Hugging Face first composes it to é. | The tokenizer.json has an NFC normalizer. llama.cpp reads tokenizer.ggml.normalizer.nfc for Unigram vocabularies only. BPE text is tokenized as given, except pre = raon, where NFC is hard-coded (b12758 and later). | Normalize prompts before sending: unicodedata.normalize("NFC", text). |
bos-eosBOS / EOS are added differently | The ids differ only at the start or the end. | tokenizer.ggml.add_bos_token is missing or wrong, or the pre-tokenizer name brings its own default: llama-bpe turns BOS on. | Set add_bos_token in the header; do not add BOS in the template as well. |
space-after-special / normalizerWhitespace around special tokens, or the SentencePiece space prefix | Ids differ right after a special token, or every string differs in its first token. | Hugging Face decides per token (lstrip / rstrip) and per tokenizer (legacy, slow or fast). GGUF has no field for the per-token flags; llama.cpp hard-codes them for a few model families and uses tokenizer.ggml.add_space_prefix for the prefix. | Compare with the chat template applied, against the Hugging Face variant the model was trained with (--slow). |
How do I check whether my GGUF tokenizes like the original model?
Run tokcheck.py with the GGUF, the original tokenizer and one way to reach llama.cpp. It exits 0 when every string gives the same ids, 1 on a mismatch, 2 when it could not run.
pip install transformers # or only: pip install tokenizers, when --hf is a tokenizer.json
curl -O https://erimaki-kobo.github.io/en/gguf-tokenizer-parity-check/tokcheck.py
# with the llama-tokenize binary from your llama.cpp build
python tokcheck.py --gguf model.gguf --hf ./original-model --llama-tokenize ./build/bin/llama-tokenize
# with a running llama-server that has the GGUF loaded
python tokcheck.py --gguf model.gguf --hf Qwen/Qwen3-0.6B --server http://127.0.0.1:8080
# header only (no llama.cpp needed): vocabulary, token types, chkhsh
python tokcheck.py --gguf model.gguf --hf ./original-model --static
It tokenizes 37 fixed strings (words, whitespace runs, digits, code, non-Latin scripts and emoji), up to six of the model's own added tokens in 8 positions each (alone, glued to text, followed by one space, two spaces, a newline), and two strings with BOS/EOS enabled. Add your own prompts with --text-file. The GGUF reader is built in; the script only needs transformers or tokenizers. This is the output for a Qwen2 vocabulary whose <|im_start|> was changed to NORMAL:
tokcheck 1.0
GGUF ./qwen-im-start-normal.gguf
HF ./refs/qwen2 (transformers)
general.name qwen2
tokenizer.ggml.model gpt2
tokenizer.ggml.pre qwen2
gguf_vocab 151936
hf_vocab 151936
added_tokens 7
chkhsh e636dc30a262dcc0d8c323492e32ae2b70728f4df7dfe9737d9f920a282b8aea
[FAIL] header: 1 added token(s) are not CONTROL/USER_DEFINED in the GGUF: 151644 '<|im_start|>'=NORMAL
[WARN] header: eos_token_id: GGUF 151643, Hugging Face 151645
engine: llama-tokenize, 79 strings
words 6/6 same
space 11/11 same
digits 5/5 same
code 5/5 same
unicode 9/10 same <-- 1 differ
first: 'é vs é' (differs at token 0, looks like: unicode-normalization)
llama.cpp 68:'e' 53839:'Ìģ' 6165:'Ġvs' 3958:'Ġé'
HF 963:'é' 6165:'Ġvs' 3958:'Ġé'
special 32/40 same <-- 8 differ
first: '<|im_start|>' (differs at token 0, looks like: special-type)
llama.cpp 27:'<' 91:'|' 318:'im' 4906:'_start' 91:'|' 29:'>'
HF 151644:'<|im_start|>'
bos-eos 2/2 same
RESULT: MISMATCH - likely causes, most specific first (fix the first one, then run again):
* special-type: A token the Hugging Face tokenizer treats as an added/special token is typed NORMAL in the GGUF, so llama.cpp never matches it as one token and splits its text into pieces. Chat templates then silently feed the model fragments. Fix the token_type entries (3 = CONTROL) or reconvert from a model directory whose tokenizer_config.json / tokenizer.json still lists the token under added tokens.
* unicode-normalization: The Hugging Face tokenizer normalises text to NFC before tokenizing (tokenizer.json "normalizer"), llama.cpp tokenizes the bytes as given. Only text with decomposed characters (a letter followed by a combining mark) is affected, and llama.cpp gives the Hugging Face ids once the input is NFC. Normalise prompts yourself: unicodedata.normalize('NFC', text).
Tested here with --llama-tokenize (all results on this page). The --server path was run against a small relay that answers /tokenize with llama-tokenize's ids, not against llama-server itself; the request fields (content, add_special, parse_special) were checked against the server source in b12899. The llama-cpp-python path was not run. --slow loads the slow SentencePiece tokenizer and needs transformers 4.x.
What does each cause look like in the token ids?
We injected one fault at a time into copies of llama.cpp's vocabulary files and ran tokcheck.py. Each fault moves a different group of strings, which is how the script tells them apart. b12899 and b12738 gave the same result in all 14 cases. In every Qwen2 case four placeholder ids (151665 to 151668) carry the names Qwen3 gives them, so that the vocabulary has added tokens that are not special.
| GGUF header | Strings that differ | First difference with the main cause | tokcheck.py says |
|---|---|---|---|
| Qwen2 vocabulary, nothing changed | unicode 1 of 10 | e\u0301 vs \xe9llama.cpp 68, 53839, 6165, 3958HF 963, 6165, 3958 | unicode-normalization |
Qwen2, tokenizer.ggml.pre removed | words 1 of 6, space 5 of 11, digits 1 of 5, code 5 of 5, unicode 1 of 10 | I've been told he's there, we'll seellama.cpp 40, 6, 586, 1012, 3229, 566, 6, 82, …HF 40, 3003, 1012, 3229, 566, 594, 1052, 11, … | pre-missing, unicode-normalization |
Qwen2, pre = default | words 1 of 6, space 5 of 11, digits 1 of 5, code 5 of 5, unicode 1 of 10 | I've been told he's there, we'll seellama.cpp 40, 6, 586, 1012, 3229, 566, 6, 82, …HF 40, 3003, 1012, 3229, 566, 594, 1052, 11, … | pre-tokenizer, unicode-normalization |
Qwen2, pre = llama-bpe | unicode 1 of 10, bos-eos 2 of 2 | Hello worldllama.cpp 151643, 9707, 1879HF 9707, 1879 | unicode-normalization, bos-eos |
Qwen2, <|im_start|> typed NORMAL | unicode 1 of 10, special 8 of 40 | <|im_start|>llama.cpp 27, 91, 318, 4906, 91, 29HF 151644 | special-type, unicode-normalization |
Qwen2, <think> and </think> typed NORMAL | unicode 1 of 10, special 16 of 48 | <think>llama.cpp 13708, 766, 29HF 151667 | special-type, unicode-normalization |
Qwen2, add_bos_token = true | unicode 1 of 10, bos-eos 2 of 2 | Hello worldllama.cpp 151643, 9707, 1879HF 9707, 1879 | unicode-normalization, bos-eos |
| Qwen2, text of one token changed | words 2 of 6, unicode 1 of 10, bos-eos 1 of 2 | Hello worldllama.cpp 39, 68, 75, 75, 78, 1879HF 9707, 1879 | vocab |
| Llama 3 vocabulary, nothing changed | none | same ids | |
Llama 3, add_bos_token = true | none | same ids | |
Llama 3, add_bos_token = true and pre = smaug-bpe | none | same ids | |
Llama 3, add_bos_token = true and pre = qwen2 | digits 4 of 5 | 1234567llama.cpp 16, 17, 18, 19, 20, 21, 22HF 4513, 10961, 22 | pre-tokenizer |
| Llama 2 SentencePiece vocabulary, nothing changed | none | same ids | |
Llama 2 SentencePiece, add_space_prefix = false | words 6 of 6, space 11 of 11, digits 5 of 5, code 5 of 5, unicode 10 of 10, special 21 of 24, bos-eos 2 of 2 | Hello worldllama.cpp 10994, 3186HF 15043, 3186 | normalizer, space-after-special |
Four things in this table are easy to miss:
- The unchanged Qwen2 file already differs on one string. The Hugging Face side normalizes to NFC (
Qwen2Tokenizerintransformerssets an NFC normalizer) and llama.cpp does not:e+ U+0301 is [68, 53839] in llama.cpp and [963] in Hugging Face. llama.cpp gave [963] once the input was NFC. - A wrong pre-tokenizer name can pass most tests. With BOS kept on by
add_bos_token,qwen2on a Llama 3 vocabulary only changed strings with digits (1234567: seven tokens instead of three), andsmaug-bpechanged none of our strings. - The pre-tokenizer name also sets defaults.
llama-bpeon the Qwen2 vocabulary left the plain strings alone but turned BOS on, because that name setsadd_bosandignore_mergesin llama.cpp. - A NORMAL-typed
<think>fails only in prompts that contain it. The plain strings behave exactly as in the unchanged file, so a test without the tag does not see it.
Which llama.cpp builds fixed known tokenizer mismatches?
Three of the reports below are fixed in current builds and one fix is not merged yet. Three more are in the GGUF or in the converter's environment, so updating llama.cpp does not help, and one we could not reproduce. Build numbers were read from the llama.cpp git history (first tag that contains the fix commit).
| Model or tokenizer | Mismatch | Report | Status on 2026-12-01 |
|---|---|---|---|
| Gemma 4 | \n\n became two tokens (107, 107); Hugging Face gives one (108) | PR #21343 | Fixed in b8648 (2026-04-03). The GGUF does not change; update llama.cpp. |
gpt-oss, Mistral NeMo and others (pre = gpt-4o, tekken, llama4, kanana2, talkie, minimax-m2) | Combining marks are split off their letters: Hindi दी gives [2587, 840] on gpt-oss, Hugging Face [25589]. The report counts 271 of 7,248 test strings. | #30787 | The issue is still open, but commit d8d92973c (PR #30845) is in b11753 (2026-10-20) and later. Update llama.cpp and test again. |
| Inkling 2 | def f(*args, **kwargs): differs because ignore_merges was off | #32798 | Fixed in b12641 (2026-11-22) by PR #32799. |
Falcon-H1 (pre = falcon-h1) | Whitespace runs before punctuation: a # gives [588, 731, 731, 526], Hugging Face [588, 767, 526]. The report counts 190 of 676 non-empty lines. | #32953 | Not fixed in b12899: the fix, PR #33223, is not in the history yet. |
| Gemma 3 exported by Unsloth | <start_of_turn> and <end_of_turn> (ids 105, 106) written as NORMAL, so the chat markers are split into pieces | unsloth #5070 | Reported with unsloth 2026.4.5 and transformers 5.5.0; the maintainers prepared a fix on a branch. Re-export with a version that includes it, or set the two types to 3. |
| BitNet b1.58 2B4T official GGUF | No tokenizer.ggml.pre: a test sentence is 31 tokens instead of 29 | BitNet #684 | Open. The report's fix is tokenizer.ggml.pre = llama-bpe. |
DeepSeek and R1-Distill models converted with transformers 5.x | The pre-tokenizer hash changes, so the converter crashes or picks another name (R1-Distill-Llama as smaug-bpe, DeepSeek-V3 as joyai-llm) | PR #31576 (draft) | Not merged as of b12899. llama.cpp's requirements pin transformers==4.57.6; convert with that version. |
| Llama 2 / TinyLlama / Mistral 7B (SentencePiece) | One space after a special token: </s> XML reported as [2, 259, 9165], Hugging Face [2, 29871, 6560] | #23840 | We could not reproduce it with llama.cpp's own Llama 2 vocabulary file: b12738 and b12899 both give [2, 29871, 6560]. The report used a 2024 TinyLlama GGUF that we could not download here. |
Token ids in this table are quoted from the reports; we did not have those models. For a pre-tokenizer hash the converter does not know (BPE pre-tokenizer was not recognized), see the chkhsh and pre-tokenizer lookup.
How do I compare the two tokenizers by hand?
Give both sides the same bytes, switch BOS off on both, and keep special-token parsing on. These four details produce false mismatches (measured on b12899):
llama-tokenizeprocesses escapes by default, also in files read with-f. A file with a literal backslash andngave [64, 198, 65] (a newline); with--no-escapeit gave [64, 1699, 65].llama-tokenizeadds BOS when the model asks for it. Use--no-bosand compare withtokenizer.encode(text, add_special_tokens=False).llama-tokenizeignores--override-kv. Withtokenizer.ggml.preremoved, the ids were identical with and without--override-kv tokenizer.ggml.pre=str:qwen2. Writing the name into a copy withgguf-new-metadata in.gguf out.gguf --pre-tokenizer qwen2restored the Hugging Face ids.- llama-server's
/tokenizedefaults toadd_special: falseandparse_special: true(server source, b12899).
printf '%s' "<|im_start|>user" > t.txt
llama-tokenize -m model.gguf -f t.txt --ids --no-bos --no-escape --log-disable
python -c "from transformers import AutoTokenizer as A; print(A.from_pretrained('./original-model').encode(open('t.txt').read(), add_special_tokens=False))"
How was this measured?
Everything ran on CPU with the real llama.cpp tokenizer code, and no model weights were involved. The method has three parts.
- Reference set. llama.cpp ships 15 vocabulary-only GGUFs with input strings and the ids the original Hugging Face tokenizer produced for them (
models/ggml-vocab-*.gguf.inpand.out). We ran llama-tokenize from b12899 and b12738 over all of them: 708 of 708 strings matched in each build. - Hugging Face side. The machine could not reach huggingface.co, so the reference tokenizers were rebuilt from those vocabulary files: Qwen2 and Llama 3 as
tokenizersBPE pipelines (the vocabulary and merges are the real ones, the split regex was written by hand), Llama 2 as a SentencePiece model loaded by the slowLlamaTokenizeroftransformers4.57.6. Each rebuild reproduced all 47 lines of its.outfile, and the two BPE rebuilds also reproduce the converter's hash for that tokenizer (chkhshe636dc30… for qwen2 and 0ef9807a… for llama-bpe). The fast Llama 2 tokenizer rebuilt this way did not reproduce the.outfile, so it was not used. - Fault injection. 14 header variants, each run once per build with tokcheck.py (28 runs of 63 to 87 strings; the number depends on how many added tokens the vocabulary has). One run is enough: tokenization is deterministic.
Not measured: real model files from the Hub, llama-server itself, llama-cpp-python, Ollama, and any effect on output quality. Scripts: make_refs.py, check_refs.py, gguf_patch.py, run_matrix.py. Raw results: results.jsonl, inp_out.txt.
Questions people ask about GGUF tokenizer mismatches
- Why does my GGUF give different token ids than Hugging Face transformers?
- A correct conversion normally gives the same ids: llama.cpp b12899 matched the Hugging Face ids on all 708 reference strings of the 15 vocabularies in its test set. When yours differ, the usual causes are a special token typed NORMAL in the GGUF, a missing or wrong tokenizer.ggml.pre, a BOS setting that differs, NFC normalization that llama.cpp does not apply to BPE vocabularies, or a llama.cpp build older than a tokenizer fix.
- How do I compare llama.cpp tokenization with the Hugging Face tokenizer?
- Tokenize the same strings on both sides with special-token parsing on and BOS handled the same way, then find the first id that differs. tokcheck.py does this through llama-tokenize, llama-server /tokenize or llama-cpp-python (only the llama-tokenize path was tested against real llama.cpp), reads the GGUF header, and names the likely cause. By hand, use llama-tokenize -m model.gguf -f text.txt --ids --no-bos --no-escape and tokenizer.encode(text, add_special_tokens=False).
- Why is <|im_start|> or <start_of_turn> split into several tokens in llama.cpp?
- Its token type in the GGUF is NORMAL. llama.cpp matches a token as a whole only when tokenizer.ggml.token_type is CONTROL, USER_DEFINED or UNKNOWN. In our test, changing <|im_start|> to NORMAL split it into six tokens in every prompt that contained it, with no warning. Reconvert from a model directory that still lists the token among its added tokens.
- Does --override-kv tokenizer.ggml.pre work with llama-tokenize?
- No. In b12899 llama-tokenize accepts --override-kv but loads the model with default parameters, so the override is ignored and the ids do not change. To test another pre-tokenizer name, write it into a copy of the file with gguf-new-metadata in.gguf out.gguf --pre-tokenizer NAME; in our test that restored the Hugging Face ids.
- Does llama.cpp apply the tokenizer's NFC normalizer?
- Only for Unigram vocabularies, through the tokenizer.ggml.normalizer.nfc key (b11612 and later), and for the BPE pre-tokenizer raon, where it is hard-coded (b12758 and later). Other BPE vocabularies are tokenized as given. With the Qwen2 vocabulary, e followed by U+0301 gave [68, 53839] in llama.cpp and [963] on the Hugging Face side, where Qwen2Tokenizer applies NFC; after normalizing the input to NFC, llama.cpp gave the same ids.
- Does a tokenizer mismatch change output quality?
- It can. Two reports measured it: with the BitNet b1.58 2B4T GGUF that lacks tokenizer.ggml.pre, WikiText-2 perplexity was 16.74 against 14.963 with the pre-tokenizer set (BitNet issue 684), and llama.cpp issue 7476 measured about 15.5 against 6.2 on an old TinyLlama GGUF. We did not measure quality ourselves.
Related
- llama.cpp pre-tokenizer lookup: every
chkhshand pre-tokenizer name, and the first build that knows it. - Why a model does not stop generating: which tokens each engine stops on.
- Do I need to re-download this GGUF?: what changed between two uploads of the same file.
- Embeddings differ from sentence-transformers: the same kind of check for vectors and rerank scores.
Sources
- llama.cpp b12899: src/llama-vocab.cpp (special-token cache, pre-tokenizer names and their defaults, NFC comment), tools/tokenize/tokenize.cpp, tools/server/server-context.cpp, conversion/base.py (
get_vocab_base_pre), gguf_new_metadata.py, models/ (vocabulary files), requirements - llama.cpp commits: b069b10ab (Gemma 4), d8d92973c (combining marks), d2bceda3d (Inkling 2), 10da8c463 (NFC for Unigram), 98f870c22 (SentencePiece prefix, b12751)
- transformers v5.24.1: tokenization_qwen2.py (NFC normalizer)
- Reports: llama.cpp #23840, #30787, #32798, #32953, #7476, PR #21343, PR #31576, PR #33223; unsloth #5070; BitNet #684