okami.mom/strata

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

Niko1221Merge f-142b-serve into f-1426e1085c1d

main
2.0 KiB49 linesraw
1"""Build the bench prompts at exact token counts with Strata's own tokenizer (run in the runner's venv).
2
3usage: gen.py SRC_ROOT TOKENIZER_DIR > prompts.jsonl
4Each prompt is "Run id: <uuid>" + a slice of the repository text (tools/needle_bench.py's haystack) +
5one question. The slice length is binary-searched so that the server-side prompt (content + the chat
6template's 12 tokens) lands on the target.
7"""
8
9import json
10import sys
11import uuid
12from pathlib import Path
13
14src, tok_dir = Path(sys.argv[1]), Path(sys.argv[2])
15sys.path.insert(0, str(src / "tools"))
16import needle_bench  # noqa: E402
17import strata_tokenizer as ST  # noqa: E402
18
19TEMPLATE_TOKENS = 12
20ASK = "\n\nSummarize the material above: what it is, its main parts, and how they fit together."
21PLAN = [("cold-first-request", 32768, False), ("warmup-short", 4096, False), ("warmup-long", 32768, False)]
22PLAN += [(f"{t}-run{i}", t, True) for t in (4096, 32768, 128000) for i in (1, 2, 3)]
23
24vocab = json.loads((tok_dir / "vocab.json").read_text(encoding="utf-8"))
25tokens = [None] * len(vocab)
26for t, i in vocab.items():
27    tokens[i] = t
28tk = ST.Tokenizer(tokens, (tok_dir / "merges.txt").read_text(encoding="utf-8").split("\n"),
29                  json.loads((tok_dir / "token_type.json").read_text()))
30text = needle_bench.haystack(1_000_000)
31
32for n, (label, target, counted) in enumerate(PLAN):
33    head = f"Run id: {uuid.uuid4()}\n\n"
34    offset = n * 7919
35    goal = target - TEMPLATE_TOKENS
36
37    def count(c: int) -> int:
38        return len(tk.encode(head + text[offset:offset + c] + ASK))
39
40    lo, hi = 0, len(text) - offset
41    while lo < hi:                      # largest slice whose prompt is <= goal tokens
42        mid = (lo + hi + 1) // 2
43        if count(mid) <= goal:
44            lo = mid
45        else:
46            hi = mid - 1
47    content = head + text[offset:offset + lo] + ASK
48    print(json.dumps({"label": label, "target": target, "counted": counted, "offset": offset,
49                      "content_tokens": len(tk.encode(content)), "content": content}), flush=True)