Fix GGUF BPE merge parsing — Qwen3/Llama3 garbage output#21
Merged
Conversation
tq_load_tokenizer_from_gguf() allocated the merge_pairs buffer and set n_merges, but never actually parsed the GGUF merge strings into (id_a, id_b, id_merged) triples. The buffer was zeroed and left unpopulated. BPE tokenizers (Qwen3 248K vocab, Llama 3, GPT-2 style) depend on merge pairs to combine byte tokens into word tokens. Without parsed merges, every byte was emitted as a separate token, producing garbage Unicode output. SentencePiece tokenizers (SmolLM2, Gemma) worked because they use character-level encoding and don't need BPE merges. The fix iterates over the GGUF string array, splits each "tok_a tok_b" merge rule, looks up token IDs, and stores the triple — identical to the existing JSON tokenizer path (tq_tokenizer.c:596-672). Applied to both tq_tokenizer.c (library) and quant.h (single-header / WASM). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Fixes the Qwen3 0.6B WASM demo producing complete garbage output (random Unicode, mixed languages, nonsense bytes).
Root cause
tq_load_tokenizer_from_gguf()had a critical bug: it read thetokenizer.ggml.mergesGGUF key, allocated themerge_pairsbuffer, setn_merges— but never parsed the actual merge strings. The buffer wasmemset(0)and left empty.BPE tokenizers (Qwen3 with 248K vocab, Llama 3, GPT-2 style) depend on merge pairs to combine byte tokens into word tokens. Without parsed merges, every byte was emitted as a separate token → garbage Unicode output.
SmolLM2 worked because it uses SentencePiece (character-level encoding, no BPE merges needed).
Fix
Iterate over the GGUF
tq_gguf_string_tarray, split each"tok_a tok_b"merge rule on space, look up token IDs viastr_lookup(), and store(id_a, id_b, id_merged)triples with priority scores. This is identical to the existing JSON tokenizer path (tq_tokenizer.c:596-672) which already worked correctly.Applied to both:
src/engine/tq_tokenizer.c(library build)quant.h(single-header / WASM build)Test plan
cmake --build build)🤖 Generated with Claude Code