okami.mom/strata

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

Niko1221Merge f-dsv41-rel (0be62ccad) into v0.2.3.1: DeepSeek V4.1-Flash on top of the released 0.2.3 line28ce98bf1

main
2.8 KiB76 linesraw
1"""sanity_greedy.py - a few greedy answers from an engine config (the same config files tools/quant_quality.py uses), through the
2normal batched prompt path (not the yardstick's decode windows).
3
4  python3 tools/trellis/sanity_greedy.py --config CFG.json --out answers.txt [--wrap "flock ... env HIP_VISIBLE_DEVICES=3"]
5"""
6from __future__ import annotations
7
8import argparse
9import json
10import os
11import pathlib
12import subprocess
13import sys
14
15HERE = pathlib.Path(__file__).resolve().parent
16sys.path.insert(0, str(HERE.parent))
17sys.path.insert(0, str(HERE))
18import make_corpus  # noqa: E402
19
20PROMPTS = [
21    "What is the capital of Australia, and what is it known for? Answer in two sentences.",
22    "Write a Python function that returns the n-th Fibonacci number iteratively, with a docstring.",
23    "Translate into German: 'The library opens at nine and closes at five, except on Sundays.'",
24    "A train leaves at 14:20 and arrives at 17:05. How long is the trip? Think step by step, then give the answer.",
25]
26
27
28def main() -> int:
29    ap = argparse.ArgumentParser()
30    ap.add_argument("--config", required=True)
31    ap.add_argument("--out", required=True)
32    ap.add_argument("--wrap", default="")
33    ap.add_argument("--new", type=int, default=80)
34    a = ap.parse_args()
35    cfg = json.load(open(a.config))
36    tok = make_corpus.load_tokenizer(cfg["tokenizer"])
37    env = dict(os.environ)
38    env.update(cfg.get("env", {}))
39    env["LD_LIBRARY_PATH"] = ":".join(cfg.get("lib_dirs", []) + [env.get("LD_LIBRARY_PATH", "")])
40    args = [cfg["exe"], "--serve"] + [x for x in cfg["args"] if x != "--serve"]
41    if a.wrap:
42        args = a.wrap.split() + args
43    p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=open(a.out + ".stderr", "w"), text=True,
44                         cwd=cfg.get("cwd"), env=env, bufsize=1)
45    for line in p.stdout:
46        if line.startswith("READY"):
47            break
48    else:
49        raise SystemExit("the engine ended before READY")
50    out = []
51    for q in PROMPTS:
52        text = "<|im_start|>user\n%s<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" % q
53        ids = tok.encode(text, parse_special=True)
54        p.stdin.write("GEN %d %s\n" % (a.new, ",".join(map(str, ids))))
55        p.stdin.flush()
56        gen = []
57        for line in p.stdout:
58            if line.startswith("T "):
59                gen.append(int(line.split()[1]))
60            elif line.startswith(("DONE", "ERR")):
61                done = line.strip()
62                break
63        out.append("### %s\n%s\n[%s]\n" % (q, tok.decode(gen), done))
64        print(out[-1], flush=True)
65    p.stdin.write("QUIT\n")
66    p.stdin.flush()
67    try:
68        p.wait(timeout=300)
69    except subprocess.TimeoutExpired:
70        p.kill()
71    pathlib.Path(a.out).write_text("\n".join(out), encoding="utf-8")
72    return 0
73
74
75if __name__ == "__main__":
76    sys.exit(main())