"""Tokenizer compatibility check between the draft and each target (CPU only, no MLX). Compares vocab, special tokens, EOS ids, the token ids of a ~2k-token sample text and the chat-templated ids of all 12 benchmark prompts. Picks the primary target: Qwen3.8-27B if it matches the draft exactly, otherwise Qwen3.6-27B. """ import json import os import sys os.environ.setdefault("HF_HUB_OFFLINE", "1") os.environ.setdefault("TRANSFORMERS_OFFLINE", "1") from transformers import AutoTokenizer # noqa: E402 from common import DRAFT, MOE, OUT, TARGET_36, TARGET_38, resolve, short, write_json # noqa: E402 from prompts import CODE, PARAGRAPH, PROMPTS, chat_ids # noqa: E402 SAMPLE = "\n\n".join( [CODE, PARAGRAPH] + [p for _, _, p in PROMPTS] + ["Unicode: नमस्ते दुनिया, こんにちは世界, Grüße, ½ ≤ π ≈ 3.14159, emoji 🚀🔥, tabs\tand spaces."] ) def eos_ids(path): ids = set() for name in ("config.json", "generation_config.json"): f = path / name if f.exists(): e = json.loads(f.read_text()).get("eos_token_id") ids.update(e if isinstance(e, list) else [e] if e is not None else []) return sorted(ids) def describe(repo): path, commit = resolve(repo) tok = AutoTokenizer.from_pretrained(str(path)) tj = json.loads((path / "tokenizer.json").read_text()) return { "repo": repo, "commit": commit, "tok": tok, "pre_tokenizer": tj.get("pre_tokenizer"), "normalizer": tj.get("normalizer"), "vocab": tok.get_vocab(), "special": sorted((t, tok.convert_tokens_to_ids(t)) for t in tok.all_special_tokens), "added": sorted((k, v) for k, v in tok.get_added_vocab().items()), "eos_config": eos_ids(path), "sample_ids": tok.encode(SAMPLE, add_special_tokens=False), "chat_ids": [chat_ids(tok, p) for _, _, p in PROMPTS], } def compare(d, t): sample_match = d["sample_ids"] == t["sample_ids"] first_diff = None if not sample_match: first_diff = next((i for i, (a, b) in enumerate(zip(d["sample_ids"], t["sample_ids"])) if a != b), min(len(d["sample_ids"]), len(t["sample_ids"]))) res = { "draft": d["repo"], "target": t["repo"], "vocab_size_draft": len(d["vocab"]), "vocab_size_target": len(t["vocab"]), "vocab_identical": d["vocab"] == t["vocab"], "special_tokens_identical": d["special"] == t["special"], "added_tokens_identical": d["added"] == t["added"], "sample_tokens": len(t["sample_ids"]), "sample_ids_identical": sample_match, "sample_first_diff_index": first_diff, "chat_template_ids_identical_all_prompts": d["chat_ids"] == t["chat_ids"], "eos_ids_target": t["eos_config"], "pre_tokenizer_identical": d["pre_tokenizer"] == t["pre_tokenizer"], "normalizer_identical": d["normalizer"] == t["normalizer"], } if first_diff is not None: lo = max(first_diff - 3, 0) res["sample_diff_context"] = { "draft": [d["tok"].decode([i]) for i in d["sample_ids"][lo:first_diff + 4]], "target": [t["tok"].decode([i]) for i in t["sample_ids"][lo:first_diff + 4]], } # what matters for speculation: both models must map text to the same ids res["compatible"] = bool(res["vocab_identical"] and res["special_tokens_identical"] and res["sample_ids_identical"]) return res def main(): draft = describe(DRAFT) out = {"pairs": [], "sample_text_tokens": len(draft["sample_ids"])} for repo in (TARGET_38, TARGET_36, MOE): out["pairs"].append(compare(draft, describe(repo))) ok = {p["target"]: p["compatible"] for p in out["pairs"]} if ok[TARGET_38]: out["primary_target"], out["secondary_target"] = TARGET_38, (TARGET_36 if ok[TARGET_36] else None) out["decision"] = "Qwen3.8-27B tokenizes identically to the Qwen3.5-9B draft: it is the primary target." elif ok[TARGET_36]: out["primary_target"], out["secondary_target"] = TARGET_36, None out["decision"] = ("Qwen3.8-27B does NOT tokenize identically to the draft; Qwen3.6-27B is the " "primary target and Qwen3.8 is dropped.") else: out["primary_target"] = out["secondary_target"] = None out["decision"] = "Neither target tokenizes identically to the draft; no valid speculative pair." out["prompt_tokens"] = {name: len(ids) for (_, name, _), ids in zip(PROMPTS, draft["chat_ids"])} write_json(OUT / "compat.json", out) for p in out["pairs"]: print(f"{short(p['target']):24s} compatible={p['compatible']} vocab={p['vocab_identical']} " f"special={p['special_tokens_identical']} sample_ids={p['sample_ids_identical']} " f"({p['sample_tokens']} tok) chat_ids={p['chat_template_ids_identical_all_prompts']}") print(out["decision"]) return 0 if out["primary_target"] else 2 if __name__ == "__main__": sys.exit(main())