okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142c-small into f-142407ea8192

main
2.5 KiB65 linesraw
1#!/usr/bin/env python3
2"""Create a private, pretokenized input for comparable IQ2_XS benchmarking.
3
4This script does NOT upload, distribute or print the input text. It uses the
5tokenizer of the published Strata source checkout and makes exactly 28,912
6tokens. A different text is a comparable test workload, not an exact replay
7of the historical medical prompt.
8"""
9from __future__ import annotations
10
11import argparse
12import hashlib
13from pathlib import Path
14import sys
15
16
17def main() -> int:
18    ap = argparse.ArgumentParser(description=__doc__)
19    ap.add_argument("--source-root", type=Path, required=True,
20                    help="Checkout of pinned benchmark source commit")
21    ap.add_argument("--gguf", type=Path, required=True,
22                    help="IQ2_XS model GGUF shard 1")
23    ap.add_argument("--text", type=Path, required=True,
24                    help="Local UTF-8 text of your choice; never uploaded")
25    ap.add_argument("--out", type=Path, required=True,
26                    help="New output file containing integer token IDs")
27    ap.add_argument("--tokens", type=int, default=28912)
28    args = ap.parse_args()
29    if args.tokens < 2:
30        ap.error("--tokens must be >= 2")
31    source = args.source_root.resolve()
32    sys.path.insert(0, str(source / "tools"))
33    from strata_tokenizer import Tokenizer
34
35    text = args.text.read_text(encoding="utf-8").strip()
36    if not text:
37        ap.error("--text input is empty")
38    tokenizer = Tokenizer.from_gguf(args.gguf.resolve())
39    ids = tokenizer.encode(text)
40    if not ids:
41        ap.error("Tokenizer returned zero tokens")
42    # Repeat a user's locally chosen document if it is shorter than the target.
43    # Re-tokenize the concatenation to preserve BPE boundaries.
44    if len(ids) < args.tokens:
45        chunk = text + "\n\n"
46        count = max(2, args.tokens // len(ids) + 2)
47        while True:
48            ids = tokenizer.encode(chunk * count)
49            if len(ids) >= args.tokens:
50                break
51            count *= 2
52    ids = ids[:args.tokens]
53    target = args.out.resolve()
54    target.parent.mkdir(parents=True, exist_ok=True)
55    data = (" ".join(str(i) for i in ids) + "\n").encode("utf-8")
56    with target.open("xb") as f:
57        f.write(data)
58    print(f"Wrote {len(ids)} tokens to {target}")
59    print(f"SHA256: {hashlib.sha256(data).hexdigest().upper()}")
60    print("Input is local and differs from historical benchmark workload.")
61    return 0
62
63
64if __name__ == "__main__":
65    raise SystemExit(main())