Skip to content

Commit bf14467

Browse files
justinchubyCopilot
andcommitted
Harden GGUF tokenizer materialization
Write embedded tokenizer JSON as exact UTF-8 bytes so platform newline handling cannot violate the verbatim-copy contract. Format the pinned census and verify the grouped documentation rows in deterministic registry order. Co-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent 2e4ff0e commit bf14467

3 files changed

Lines changed: 100 additions & 18 deletions

File tree

‎src/mobius/integrations/gguf/_tokenizer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def write_gguf_tokenizer_json(
434434
output = Path(output_dir)
435435
output.mkdir(parents=True, exist_ok=True)
436436
tokenizer_path = output / "tokenizer.json"
437-
tokenizer_path.write_text(metadata["tokenizer.huggingface.json"], encoding="utf-8")
437+
tokenizer_path.write_bytes(metadata["tokenizer.huggingface.json"].encode("utf-8"))
438438
config, special_map = _tokenizer_config(metadata)
439439
(output / "tokenizer_config.json").write_text(
440440
json.dumps(config, indent=2, sort_keys=True, ensure_ascii=False) + "\n",

‎src/mobius/integrations/gguf/_tokenizer_registry.py‎

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,93 @@ class TokenizerPrePolicy:
126126
)
127127

128128
_PINNED_IDENTIFIERS = (
129-
"default", "minicpm5", "llama3", "llama-v3", "llama-bpe", "falcon3",
130-
"falcon-h1", "pixtral", "midm-2.0", "lfm2", "jina-v5-nano", "deepseek-llm",
131-
"deepseek-coder", "deepseek-v3", "youtu", "falcon", "mpt", "starcoder", "gpt-2",
132-
"phi-2", "jina-es", "jina-de", "gigachat", "jina-v2-es", "jina-v2-de",
133-
"a.x-4.0", "mellum", "modern-bert", "jais-2", "gemma4",
134-
"granite-embed-multi-311m", "sarvam-moe", "jina-v1-en", "jina-v2-code",
135-
"roberta-bpe", "whitespace", "refact", "command-r", "qwen2", "deepseek-r1-qwen",
136-
"kormo", "f2llmv2", "qwen35", "stablelm2", "olmo", "dbrx", "smaug-bpe",
137-
"poro-chat", "glm4", "chatglm-bpe", "viking", "jais", "tekken", "smollm",
138-
"codeshell", "bloom", "gpt3-finnish", "exaone", "exaone4", "exaone-moe",
139-
"chameleon", "minerva-7b", "megrez", "gpt-4o", "llama4", "kanana2", "talkie",
140-
"granite-embed-multi-97m", "tiny_aya", "cohere2moe", "superbpe", "trillion",
141-
"granite-docling", "bailingmoe", "bailingmoe2", "llada-moe", "seed-coder",
142-
"hunyuan", "hunyuan-dense", "joyai-llm", "kimi-k2", "grok-2", "afmoe",
143-
"laguna", "minimax-m2", "solar-open", "mellum2",
129+
"default",
130+
"minicpm5",
131+
"llama3",
132+
"llama-v3",
133+
"llama-bpe",
134+
"falcon3",
135+
"falcon-h1",
136+
"pixtral",
137+
"midm-2.0",
138+
"lfm2",
139+
"jina-v5-nano",
140+
"deepseek-llm",
141+
"deepseek-coder",
142+
"deepseek-v3",
143+
"youtu",
144+
"falcon",
145+
"mpt",
146+
"starcoder",
147+
"gpt-2",
148+
"phi-2",
149+
"jina-es",
150+
"jina-de",
151+
"gigachat",
152+
"jina-v2-es",
153+
"jina-v2-de",
154+
"a.x-4.0",
155+
"mellum",
156+
"modern-bert",
157+
"jais-2",
158+
"gemma4",
159+
"granite-embed-multi-311m",
160+
"sarvam-moe",
161+
"jina-v1-en",
162+
"jina-v2-code",
163+
"roberta-bpe",
164+
"whitespace",
165+
"refact",
166+
"command-r",
167+
"qwen2",
168+
"deepseek-r1-qwen",
169+
"kormo",
170+
"f2llmv2",
171+
"qwen35",
172+
"stablelm2",
173+
"olmo",
174+
"dbrx",
175+
"smaug-bpe",
176+
"poro-chat",
177+
"glm4",
178+
"chatglm-bpe",
179+
"viking",
180+
"jais",
181+
"tekken",
182+
"smollm",
183+
"codeshell",
184+
"bloom",
185+
"gpt3-finnish",
186+
"exaone",
187+
"exaone4",
188+
"exaone-moe",
189+
"chameleon",
190+
"minerva-7b",
191+
"megrez",
192+
"gpt-4o",
193+
"llama4",
194+
"kanana2",
195+
"talkie",
196+
"granite-embed-multi-97m",
197+
"tiny_aya",
198+
"cohere2moe",
199+
"superbpe",
200+
"trillion",
201+
"granite-docling",
202+
"bailingmoe",
203+
"bailingmoe2",
204+
"llada-moe",
205+
"seed-coder",
206+
"hunyuan",
207+
"hunyuan-dense",
208+
"joyai-llm",
209+
"kimi-k2",
210+
"grok-2",
211+
"afmoe",
212+
"laguna",
213+
"minimax-m2",
214+
"solar-open",
215+
"mellum2",
144216
)
145217

146218

‎src/mobius/integrations/gguf/_tokenizer_test.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,17 @@ def test_generated_documentation_table_matches_registry(self):
175175
table = documentation.split("| Canonical | Exact identifiers |", 1)[1].split(
176176
"### Metadata audit", 1
177177
)[0]
178-
rows = re.findall(r"^\| `([^`]+)` \| (.+) \|$", table, re.MULTILINE)
178+
documented_rows = re.findall(r"^\| `([^`]+)` \| (.+) \|$", table, re.MULTILINE)
179+
expected_groups: dict[str, list[str]] = {}
180+
for identifier, policy in policies.items():
181+
expected_groups.setdefault(policy.canonical, []).append(identifier)
182+
assert documented_rows == [
183+
(canonical, ", ".join(f"`{identifier}`" for identifier in identifiers))
184+
for canonical, identifiers in expected_groups.items()
185+
]
179186
documented = {
180187
identifier: canonical
181-
for canonical, identifiers in rows
188+
for canonical, identifiers in documented_rows
182189
for identifier in re.findall(r"`([^`]+)`", identifiers)
183190
}
184191
assert documented == {
@@ -295,6 +302,9 @@ def test_write_exact_tokenizer_assets_preserves_templates_and_flags(tmp_path: Pa
295302
result = write_gguf_tokenizer_json(source, output)
296303

297304
assert result == str(output / "tokenizer.json")
305+
assert (output / "tokenizer.json").read_bytes() == metadata[
306+
"tokenizer.huggingface.json"
307+
].encode("utf-8")
298308
config = json.loads((output / "tokenizer_config.json").read_text(encoding="utf-8"))
299309
assert config["add_bos_token"] is True
300310
assert config["add_eos_token"] is False

0 commit comments

Comments
 (0)