Files
leleka/scripts/compare-real-tokenizer.py
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

65 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Compare native token IDs and roundtrips with the pinned upstream Python oracle.
Offline evaluation only. Requires `regex` for engine/strata/tools/strata_tokenizer.py.
The real GGUF and extracted tokenizer assets must stay outside Git.
"""
import argparse
import json
import pathlib
import subprocess
import sys
CORPUS = (
"Hello world!",
"Привіт, світе! Як справи?",
"e\u0301 café — naïve",
"😀👨‍👩‍👧‍👦",
"空白 漢字\n数字123",
"\t\r\nTwo spaces\x00end",
"<|im_start|>user\nSay hi<|im_end|>",
'<tool_call>{"name":"calc","arguments":{"x":1}}</tool_call>',
"a" * 70 + " next",
"print(1 + 2) # ✓",
)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--binary", type=pathlib.Path, required=True)
parser.add_argument("--gguf", type=pathlib.Path, required=True)
parser.add_argument("--assets", type=pathlib.Path, required=True)
args = parser.parse_args()
binary = args.binary.resolve(strict=True)
gguf = args.gguf.resolve(strict=True)
assets = args.assets.resolve(strict=True)
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent / "engine/strata/tools"))
from strata_tokenizer import Tokenizer # pylint: disable=import-outside-toplevel
oracle = Tokenizer.from_gguf(gguf)
for special in (False, True):
for number, value in enumerate(CORPUS):
command = [binary, "model", "tokenize", assets, "--stdin"]
if special:
command.append("--special")
native = json.loads(subprocess.run(
command, input=value, text=True, capture_output=True, check=True,
).stdout)
expected = oracle.encode(value, parse_special=special)
if native != expected:
raise ValueError(f"token IDs differ for case {number}, special={special}")
decoded = json.loads(subprocess.run(
[binary, "model", "detokenize", assets, ",".join(map(str, native))],
text=True, capture_output=True, check=True,
).stdout)
if decoded != value or oracle.decode(expected) != value:
raise ValueError(f"roundtrip differs for case {number}, special={special}")
print(json.dumps({"cases": len(CORPUS) * 2, "vocab": len(oracle.tokens),
"merges": len(oracle.ranks), "result": "exact native/upstream parity"}))
if __name__ == "__main__":
main()