Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

GGUF tokenizer: map every pre-tokenizer from the table generated off llama.cpp llama-vocab.cpp (qwen35 included)

YanissAmz0fd0a4f7ff23
diff --git a/exllamav3/loader/gguf_pre.py b/exllamav3/loader/gguf_pre.py
new file mode 100644
index 0000000..adecac9
--- /dev/null
+++ b/exllamav3/loader/gguf_pre.py
@@ -0,0 +1,352 @@
+# SPDX-License-Identifier: Apache-2.0
+"""GENERATED by tools/gen_gguf_pre.py from llama.cpp src/llama-vocab.cpp at commit 29c720eaf72fcf8e4e61a56a88b994d92668261d. Do not edit: regenerate.
+PRE_REGEX: pre_type -> (byte_encode, the regex_exprs list) of llm_tokenizer_bpe (split in sequence, each on the pieces of the one before).
+PRE_NAMES: tokenizer.ggml.pre -> (pre_type, ignore_merges, add_bos default, clean_spaces, bos_is_null)."""
+from __future__ import annotations
+
+LLAMA_CPP_COMMIT = "29c720eaf72fcf8e4e61a56a88b994d92668261d"
+
+PRE_REGEX = {
+ 'AFMOE': (True, [
+ '\\p{AFMoE_digits}',
+ '[一-鿿㐀-䶿豈-\ufaff\u3040-ゟ゠-ヿ・-゚⼀-\u2fdfเ-\u0e7f\u0e80-\u0effក-\u17ffက-႟ꩠ-ꩿꧠ-\ua9ff가-\ud7afᄀ-ᇿ]+',
+ '[!"#$%&\'()*+,\\-./:;<=>?@\\[\\\\\\]^_`{|}~][A-Za-z]+|[^\\r\\n\\p{L}\\p{P}\\p{S}]?[\\p{L}\\p{M}]+| ?[\\p{P}\\p{S}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+',
+ ]),
+ 'BAILINGMOE': (True, [
+ "'(?:[sSdDmMtT]|[lL][lL]|[vV][eE]|[rR][eE])|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L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{|}~][A-Za-z]+|[^\r\n\\p{L}\\p{P}\\p{S}]?[\\p{L}\\p{M}]+| ?[\\p{P}\\p{S}]+[\r\n]*|\\s*[\r\n]+|\\s+(?!\\S)|\\s+',
+ ]),
+ 'DEEPSEEK_CODER': (True, [
+ '[\r\n]',
+ '\\s?\\p{L}+',
+ '\\s?\\p{P}+',
+ '[一-龥ࠀ-一가-\ud7ff]+',
+ '\\p{N}',
+ ]),
+ 'DEEPSEEK_LLM': (True, [
+ '[\r\n]',
+ '\\s?[A-Za-zµÀ-ÖØ-öø-ƺƼ-ƿDŽ-ʓʕ-ʯͰ-ͳͶͷͻ-ͽͿΆΈ-ΊΌΎ-ΡΣ-ϵϷ-ҁҊ-ԯԱ-ՖႠ-ჅᎠ-Ᏽᏸ-ᏽᲐ-ᲺᲽ-Ჿᴀ-ᴫᵫ-ᵷᵹ-ᶚḀ-ἕἘ-Ἕἠ-ὅὈ-Ὅὐ-ὗὙὛὝὟ-ώᾀ-ᾴᾶ-ᾼιῂ-ῄῆ-ῌῐ-ΐῖ-Ίῠ-Ῥῲ-ῴῶ-ῼℂℇℊ-ℓℕℙ-ℝℤΩℨK-ℭℯ-ℴℹℼ-ℿⅅ-ⅉⅎↃↄⰀ-ⱻⱾ-ⳤⳫ-ⳮⳲⳳꙀ-ꙭꚀ-ꚛꜢ-ꝯꝱ-ꞇꞋ-ꞎꭰ-ꮿff-stﬓ-ﬗA-Za-z𐐀-𐑏𐒰-𐓓𐓘-𐓻𐲀-𐲲𐳀-𐳲𑢠-𑣟𞤀-𞥃]+',
+ '\\s?[!-/:-~!-/:-~‘-‟\u3000-。]+',
+ '\\s+$',
+ '[一-龥ࠀ-一가-\ud7ff]+',
+ '\\p{N}+',
+ ]),
+ 'DEFAULT': (True, [
+ '[\\p{P}\\$\\+<=>\\^~\\|]+',
+ "'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)",
+ '\\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True, [
+ "'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)",
+ ]),
+ 'UFAKZEKA': (True, [
+ '[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+',
+ ]),
+ 'VIKING': (True, [
+ ' ?[^(\\s|.,!?…。,、।۔،)]+',
+ '\\p{N}',
+ ]),
+ 'WHITESPACE': (False, [
+ '\\S+',
+ ]),
+ 'YOUTU': (True, [
+ '[가-힣ㄱ-ㆎ]+|[!…“”‘’—:;,、-〿︰-﹏]+|[ㄅ-ㄯ]+|[一-龥\u3040-ゟ゠-ヿ]+',
+ "[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}]*[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}]+(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])?|[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}]+[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}]*(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])?|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n/]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
+ ]),
+}
+
+PRE_NAMES = {
+ 'a.x-4.0': ('GPT2', False, False, True, False),
+ 'afmoe': ('AFMOE', False, False, False, False),
+ 'bailingmoe': ('BAILINGMOE', False, False, False, False),
+ 'bailingmoe2': ('BAILINGMOE', False, False, False, False),
+ 'bloom': ('BLOOM', False, False, True, False),
+ 'chameleon': ('CHAMELEON', False, True, False, False),
+ 'chatglm-bpe': ('CHATGLM4', False, False, False, True),
+ 'codeshell': ('CODESHELL', False, False, True, False),
+ 'cohere2moe': ('TINY_AYA', False, False, False, False),
+ 'command-r': ('COMMAND_R', False, False, False, False),
+ 'dbrx': ('DBRX', False, False, False, False),
+ 'deepseek-coder': ('DEEPSEEK_CODER', False, False, False, False),
+ 'deepseek-llm': ('DEEPSEEK_LLM', False, False, False, False),
+ 'deepseek-r1-qwen': ('QWEN2', False, False, False, False),
+ 'deepseek-v3': ('DEEPSEEK3_LLM', False, False, False, False),
+ 'default': ('DEFAULT', False, False, True, False),
+ 'domyn-small': ('DOMYN_SMALL', False, False, True, False),
+ 'exaone': ('EXAONE', False, False, True, False),
+ 'exaone-moe': ('EXAONE_MOE', False, False, True, False),
+ 'exaone4': ('GPT2', False, False, False, False),
+ 'f2llmv2': ('QWEN2', False, False, False, False),
+ 'falcon': ('FALCON', False, False, True, False),
+ 'falcon-h1': ('LLAMA3', True, True, True, False),
+ 'falcon3': ('LLAMA3', True, True, True, False),
+ 'flint': ('FLINT', False, False, False, False),
+ 'gemma4': ('GEMMA4', False, False, False, False),
+ 'gigachat': ('GPT2', False, False, True, False),
+ 'glm4': ('CHATGLM4', True, False, False, True),
+ 'glm5': ('CHATGLM4', True, False, False, True),
+ 'gpt-2': ('GPT2', False, False, True, False),
+ 'gpt-4o': ('GPT4O', False, False, False, False),
+ 'gpt3-finnish': ('GPT3_FINNISH', False, False, True, False),
+ 'granite-docling': ('GRANITE_DOCLING', False, False, False, False),
+ 'granite-embed-multi-311m': ('GEMMA4', False, False, False, False),
+ 'granite-embed-multi-97m': ('GRANITE_EMB_MULTI', True, False, False, False),
+ 'grok-2': ('GROK_2', False, False, False, False),
+ 'hunyuan': ('HUNYUAN', False, False, False, False),
+ 'hunyuan-dense': ('HUNYUAN_DENSE', False, False, False, False),
+ 'hy_v4': ('HY_V4', False, False, False, False),
+ 'jais': ('JAIS', False, False, True, False),
+ 'jais-2': ('JAIS2', False, False, True, False),
+ 'jina-de': ('GPT2', False, False, True, False),
+ 'jina-es': ('GPT2', False, False, True, False),
+ 'jina-v1-en': ('GPT2', False, False, True, False),
+ 'jina-v2-code': ('GPT2', False, False, True, False),
+ 'jina-v2-de': ('GPT2', False, False, True, False),
+ 'jina-v2-es': ('GPT2', False, False, True, False),
+ 'jina-v5-nano': ('LLAMA3', True, True, True, False),
+ 'joyai-llm': ('JOYAI_LLM', False, False, False, False),
+ 'k2-horizon': ('K2_HORIZON', False, False, False, False),
+ 'kanana2': ('GPT4O', False, False, False, False),
+ 'kimi-k2': ('KIMI_K2', False, False, False, False),
+ 'kormo': ('QWEN2', False, False, False, False),
+ 'laguna': ('LAGUNA', False, False, False, False),
+ 'lfm2': ('LLAMA3', True, True, False, False),
+ 'llada-moe': ('BAILINGMOE', False, False, False, False),
+ 'llama-bpe': ('LLAMA3', True, True, True, False),
+ 'llama-v3': ('LLAMA3', True, True, True, False),
+ 'llama3': ('LLAMA3', True, True, True, False),
+ 'llama4': ('GPT4O', False, False, False, False),
+ 'megrez': ('QWEN2', False, False, True, False),
+ 'mellum': ('GPT2', False, False, True, False),
+ 'mellum2': ('MELLUM2', False, False, False, False),
+ 'midm-2.0': ('LLAMA3', True, True, True, False),
+ 'minerva-7b': ('MINERVA', False, False, True, False),
+ 'minicpm5': ('MINICPM5', True, False, False, False),
+ 'minimax-m2': ('MINIMAX_M2', False, False, False, False),
+ 'mmbert': ('MMBERT', False, False, True, False),
+ 'modern-bert': ('GPT2', False, False, True, False),
+ 'mpt': ('MPT', False, False, True, False),
+ 'nemotron-3.5-embed': ('NEMOTRON_3_5_EMBD', True, True, True, False),
+ 'olmo': ('OLMO', False, False, True, False),
+ 'phi-2': ('GPT2', False, False, True, False),
+ 'pixtral': ('TEKKEN', True, True, False, False),
+ 'poro-chat': ('PORO', False, False, False, False),
+ 'qwen2': ('QWEN2', False, False, False, False),
+ 'qwen35': ('QWEN35', False, False, False, False),
+ 'refact': ('REFACT', False, False, True, False),
+ 'roberta-bpe': ('GPT2', False, False, True, False),
+ 'sarvam-moe': ('SARVAM_MOE', False, False, False, False),
+ 'seed-coder': ('SEED_CODER', False, False, False, False),
+ 'smaug-bpe': ('SMAUG', False, False, True, False),
+ 'smollm': ('SMOLLM', False, False, False, False),
+ 'solar-open': ('SOLAR_OPEN', False, False, False, False),
+ 'soprano': ('SOPRANO', False, False, True, False),
+ 'spark2_5': ('SPARK2_5', False, False, False, False),
+ 'stablelm2': ('STABLELM2', False, False, True, False),
+ 'starcoder': ('STARCODER', False, False, True, False),
+ 'superbpe': ('SUPERBPE', False, False, False, False),
+ 'talkie': ('GPT4O', False, False, False, False),
+ 'tekken': ('TEKKEN', True, True, False, False),
+ 'tekken-num3': ('TEKKEN_NUM3', True, True, False, False),
+ 'tiny_aya': ('TINY_AYA', False, False, False, False),
+ 'trillion': ('TRILLION', False, False, False, False),
+ 'ufakzeka': ('UFAKZEKA', False, False, False, False),
+ 'viking': ('VIKING', False, False, False, False),
+ 'whitespace': ('WHITESPACE', False, False, True, False),
+ 'youtu': ('YOUTU', True, False, False, False),
+}
diff --git a/exllamav3/loader/gguf_tokenizer.py b/exllamav3/loader/gguf_tokenizer.py
index b905ee2..eb8b439 100644
--- a/exllamav3/loader/gguf_tokenizer.py
+++ b/exllamav3/loader/gguf_tokenizer.py
@@ -1,21 +1,25 @@
-"""tokenizer.json + tokenizer_config.json from the tokenizer.ggml.* metadata of a GGUF (byte-level BPE: gpt2 model with
-the qwen2 pre-tokenizer). Other tokenizer models / pre-tokenizers raise: nothing is guessed."""
+"""tokenizer.json + tokenizer_config.json from the tokenizer.ggml.* metadata of a GGUF (byte-level BPE: gpt2 model, any
+tokenizer.ggml.pre listed in gguf_pre.py, the table generated from llama.cpp's llama-vocab.cpp). Other tokenizer models /
+pre-tokenizers raise: nothing is guessed."""
from __future__ import annotations
import json
import os
from .gguf_collection import GGUFCollection
+from .gguf_pre import PRE_NAMES, PRE_REGEX
# ggml token types: 1 normal, 2 unknown, 3 control, 4 user-defined, 5 unused, 6 byte
-_QWEN2_REGEX = (r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+")
-_PRE = {"qwen2": _QWEN2_REGEX}
def write_tokenizer_files(g: GGUFCollection, outdir: str) -> None:
m = g.meta
model, pre = m.get("tokenizer.ggml.model"), m.get("tokenizer.ggml.pre")
- if model != "gpt2" or pre not in _PRE:
- raise ValueError(f"GGUF tokenizer model={model!r} pre={pre!r} is not mapped (have gpt2 + {sorted(_PRE)})")
+ if model != "gpt2" or pre not in PRE_NAMES:
+ raise ValueError(f"GGUF tokenizer model={model!r} pre={pre!r} is not mapped (gpt2 with a pre in gguf_pre.PRE_NAMES)")
+ ptype, ignore_merges = PRE_NAMES[pre][0], PRE_NAMES[pre][1]
+ byte_encode, regexes = PRE_REGEX[ptype]
+ if not byte_encode or any("{AFMoE_" in r for r in regexes):
+ raise ValueError(f"GGUF pre-tokenizer {pre!r} ({ptype}) is not expressible as split regexes + byte-level: not mapped")
toks, types = m["tokenizer.ggml.tokens"], m["tokenizer.ggml.token_type"]
merges = [tuple(x.split(" ", 1)) for x in m["tokenizer.ggml.merges"]]
added = [{"id": i, "content": t, "single_word": False, "lstrip": False, "rstrip": False, "normalized": False,
@@ -23,12 +27,12 @@ def write_tokenizer_files(g: GGUFCollection, outdir: str) -> None:
tj = {
"version": "1.0", "truncation": None, "padding": None, "added_tokens": added, "normalizer": None,
"pre_tokenizer": {"type": "Sequence", "pretokenizers": [
- {"type": "Split", "pattern": {"Regex": _PRE[pre]}, "behavior": "Isolated", "invert": False},
+ *[{"type": "Split", "pattern": {"Regex": r}, "behavior": "Isolated", "invert": False} for r in regexes],
{"type": "ByteLevel", "add_prefix_space": False, "trim_offsets": False, "use_regex": False}]},
"post_processor": None,
"decoder": {"type": "ByteLevel", "add_prefix_space": False, "trim_offsets": False, "use_regex": False},
"model": {"type": "BPE", "dropout": None, "unk_token": None, "continuing_subword_prefix": "",
- "end_of_word_suffix": "", "fuse_unk": False, "byte_fallback": False, "ignore_merges": False,
+ "end_of_word_suffix": "", "fuse_unk": False, "byte_fallback": False, "ignore_merges": ignore_merges,
"vocab": {t: i for i, t in enumerate(toks)}, "merges": [list(x) for x in merges]},
}
with open(os.path.join(outdir, "tokenizer.json"), "w", encoding="utf8") as f:
diff --git a/tests/test_gguf_qwen35moe_decode.py b/tests/test_gguf_qwen35moe_decode.py
index 90d76a0..8889f2c 100644
--- a/tests/test_gguf_qwen35moe_decode.py
+++ b/tests/test_gguf_qwen35moe_decode.py
@@ -9,12 +9,8 @@ import torch
ENGINE = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, ENGINE)
from exllamav3 import Model, Config, Cache, Generator, Job, GreedySampler, Tokenizer
-from exllamav3.loader import gguf_tokenizer
from exllamav3.loader.gguf_config import write_config_dir
-# Throughput test only: the qwen35 pre-tokenizer regex is not verified yet, so it is NOT mapped in the engine; the test borrows
-# qwen2's (token ids are meaningless on a 4-block cut anyway).
-gguf_tokenizer._PRE.setdefault("qwen35", gguf_tokenizer._PRE["qwen2"])
src = os.environ["FMOE_GGUF"]
d = write_config_dir(src, os.path.join(os.environ.get("FMOE_CFG", "/tmp"), "qwen35moe_cfg"))