mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
include/strata/image/qwen2_tok.hpp, src/image/te/qwen2_tok.cpp build the token ids of Qwen-Image-2.1's text encoder
the way sd.cpp 2988060 does, with no regex library and no sd.cpp:
- sd-cli's <lora:NAME:WEIGHT> removal (its --lora-model-dir defaults to "."), parse_prompt_attention as its code runs
(only "\(" loses its backslash, "\]" stays), the system / user / assistant template, each piece encoded alone;
- the 31 special literals with sd.cpp's ids (151643 + index; at 5 of those ids the GGUF lists [PAD151669] ..
[PAD151673], sd.cpp names them <|boi_token|> .. <|tms_token|>; we follow sd.cpp);
- the Qwen2 pre-tokenizer regex as a scanner; \p{L}, \p{N}, \s and the contraction case folds (s also matches U+017F)
are Oniguruma's own, dumped from sd.cpp's build over every code point (tools/image/gate/te_tok_probe.cpp ->
te_tok_tables.py -> src/image/te/qwen2_tok_unicode.inc);
- byte-level BPE with the GGUF's merges (equal to sd.cpp's compiled-in 151,387, checked); invalid UTF-8 refused as
sd.cpp refuses it.
No engine tokenizer to reuse: the engine takes ids from the Python server (tools/strata_tokenizer.py, qwen35 rule).
Gate: 408 prompts (tools/image/gate/te_tok_cases.py: the 8 gate prompts, the 1,025- and 4,002-token prompts, every
special literal and the 5 GGUF pad literals, 19 prompt-weight cases, 22 unicode cases (CJK, kana, hangul, emoji with
joiners / flags / skin tones, combining marks, right-to-left, other digits and spaces, case-fold traps), empty and
whitespace-only prompts, long words, 5 invalid UTF-8, 300 random prompts from a fixed seed); sd.cpp's ids, weights and
system-token count recorded through the stage hook (stage_hooks.py: tokens.weights and OWN5_EXIT_AFTER_TOKENS, which
exits before any model loads; te_tok_collect.py). GB10 with the full merges: 408 of 408 identical (sd.cpp refuses the
5 invalid-UTF-8 prompts, so do we).
Test: tools/test_te_tokenizer.py compiles the tokenizer with the system C++ compiler and checks all 408 cases from
tools/image/gate/data/te_tok_expected.json with the 2,557 merges they use (te_tok_merges.txt, ranks kept): no GPU, no
sd.cpp, no model file. Passed on the GB10 (aarch64) and p1004 (x86).
91 lines
4.3 KiB
Python
91 lines
4.3 KiB
Python
"""tools/test_te_tokenizer.py - our Qwen-Image text-encoder tokenizer (src/image/te/qwen2_tok.cpp) against the token ids
|
|
stable-diffusion.cpp 2988060 built for the same prompts.
|
|
|
|
The expected ids were recorded from sd.cpp itself (tools/image/gate/te_tok_collect.py: a stage-hook build of sd-cli that
|
|
dumps the ids LLMEmbedder::encode_prompt hands its text encoder) for every case of tools/image/gate/te_tok_cases.py and
|
|
stored in tools/image/gate/data/te_tok_expected.json. The test needs no GPU, no sd.cpp and no model file: it compiles
|
|
the tokenizer with the system C++ compiler and gives it only the merges those cases use
|
|
(tools/image/gate/data/te_tok_merges.txt, with their ranks in the full 151,387-merge list).
|
|
|
|
python -m pytest tools/test_te_tokenizer.py"""
|
|
import base64
|
|
import json
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DATA = ROOT / "tools" / "image" / "gate" / "data"
|
|
CXX = shutil.which("g++") or shutil.which("c++") or shutil.which("clang++")
|
|
|
|
|
|
@unittest.skipUnless(CXX, "no C++ compiler")
|
|
class TokenizerMatchesSdcpp(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.tmp = tempfile.TemporaryDirectory()
|
|
d = Path(cls.tmp.name)
|
|
cls.exe = d / "strata-te-tokens"
|
|
r = subprocess.run([CXX, "-std=c++17", "-O2", "-I", str(ROOT / "include"), "-I", str(ROOT / "src" / "image" / "te"),
|
|
str(ROOT / "src" / "image" / "te" / "qwen2_tok.cpp"),
|
|
str(ROOT / "src" / "image" / "te" / "qwen2_tok_main.cpp"), "-o", str(cls.exe)],
|
|
capture_output=True, text=True, timeout=600)
|
|
if r.returncode != 0:
|
|
raise AssertionError("build failed:\n" + r.stderr[-4000:])
|
|
cls.cases = json.loads((DATA / "te_tok_expected.json").read_text())
|
|
lines = [f"{c['name']} {base64.b64decode(c['prompt_b64']).hex()}" for c in cls.cases]
|
|
(d / "cases.txt").write_text("\n".join(lines) + "\n")
|
|
r = subprocess.run([str(cls.exe), "run", str(DATA / "te_tok_merges.txt"), str(d / "cases.txt"), str(d / "out.txt")],
|
|
capture_output=True, text=True, timeout=600)
|
|
if r.returncode != 0:
|
|
raise AssertionError("run failed:\n" + r.stderr[-4000:])
|
|
cls.got = {}
|
|
for line in (d / "out.txt").read_text().splitlines():
|
|
parts = line.split(" ", 2)
|
|
cls.got[parts[0]] = parts[1:]
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.tmp.cleanup()
|
|
|
|
def test_case_set_is_wide(self):
|
|
names = [c["name"] for c in self.cases]
|
|
self.assertGreaterEqual(len(names), 380)
|
|
for prefix in ("gate_", "long_", "special_", "gguf_literal_", "w_", "u_", "e_", "l_", "x_", "r_"):
|
|
self.assertTrue(any(n.startswith(prefix) for n in names), prefix)
|
|
|
|
def test_every_case_matches(self):
|
|
bad = []
|
|
for c in self.cases:
|
|
g = self.got.get(c["name"])
|
|
if g is None:
|
|
bad.append((c["name"], "no output"))
|
|
continue
|
|
if "error" in c:
|
|
if g[0] != "error":
|
|
bad.append((c["name"], "sd.cpp refused the prompt, we did not"))
|
|
continue
|
|
if g[0] != "ok":
|
|
bad.append((c["name"], f"we refused: {g[1]}"))
|
|
continue
|
|
n_sys, ids, w = g[1].split(" ")
|
|
ids = [int(x) for x in ids.split(",")] if ids else []
|
|
want_w = [struct.unpack("<I", struct.pack("<f", x))[0] for x in c["weights"]]
|
|
got_w = [int(x, 16) for x in w.split(",")] if w else []
|
|
if int(n_sys) != c["n_system"]:
|
|
bad.append((c["name"], f"n_system {n_sys} vs {c['n_system']}"))
|
|
elif ids != c["ids"]:
|
|
k = next((i for i, (a, b) in enumerate(zip(ids, c["ids"])) if a != b), min(len(ids), len(c["ids"])))
|
|
bad.append((c["name"], f"ids differ at {k}: ours {ids[k:k + 5]} sd.cpp {c['ids'][k:k + 5]} "
|
|
f"({len(ids)} vs {len(c['ids'])} ids)"))
|
|
elif got_w != want_w:
|
|
bad.append((c["name"], "weights differ"))
|
|
self.assertEqual(bad, [], f"{len(bad)} of {len(self.cases)} cases differ")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|