okami.mom/strata

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

Niko1221Merge branch 'f-1402-w' into f-140253623e56f

main
3.0 KiB47 linesraw
1#!/usr/bin/env python3
2"""Parallel-Last: python3 parlast.py OUT.json LEVELS(1,2,4) ROUNDS PROMPT_TOK OUT_TOK. Streamt, misst TTFT, Dauer, Token je Strom und gesamt."""
3import json, sys, time, threading, pathlib, urllib.request, statistics
4OUT = pathlib.Path(sys.argv[1]); LEVELS = [int(x) for x in sys.argv[2].split(",")]
5ROUNDS = int(sys.argv[3]); PTOK = int(sys.argv[4]); OTOK = int(sys.argv[5])
6ROOT = pathlib.Path.home() / "ai/Strata-0.1.36"
7files = []
8for d, g in [("docs", "*.md"), ("src", "*.cpp"), ("src", "*.cu"), ("include", "*.hpp"), ("serve", "*.py"), ("tools", "*.py"), ("third_party/llama.cpp/docs", "*.md")]:
9    files += sorted((ROOT / d).rglob(g))
10corp = "".join(f"\n### {f.name}\n" + f.read_text(errors="ignore") for f in files)
11SEQ = [int(time.time()) % 1000000]
12def prompt():
13    SEQ[0] += 1
14    n = int(PTOK * 3.2); off = (SEQ[0] * 37000) % max(1, len(corp) - n)
15    return f"[nonce {SEQ[0]}]\nHere is source code:\n" + corp[off:off + n] + "\n\nWrite a short summary of what this code does, then propose two refactorings with example code."
16def one(res, i):
17    body = {"model": "swift-1.5-iq3_xxs", "stream": True, "stream_options": {"include_usage": True},
18            "messages": [{"role": "user", "content": prompt()}], "max_tokens": OTOK,
19            "temperature": 0.7, "top_p": 0.95, "top_k": 20}
20    r = urllib.request.Request("http://127.0.0.1:8080/v1/chat/completions", json.dumps(body).encode(), {"Content-Type": "application/json"})
21    t0 = time.time(); first = None; chunks = 0; usage = None; last = t0
22    try:
23        with urllib.request.urlopen(r, timeout=1800) as f:
24            for line in f:
25                line = line.decode().strip()
26                if not line.startswith("data:") or line.endswith("[DONE]"): continue
27                j = json.loads(line[5:])
28                if j.get("usage"): usage = j["usage"].get("completion_tokens")
29                ch = j.get("choices") or []
30                if ch and (ch[0].get("delta", {}).get("content") or ch[0].get("delta", {}).get("reasoning_content") or ch[0].get("delta", {}).get("reasoning")):
31                    chunks += 1; last = time.time()
32                    if first is None: first = last
33    except Exception as e:
34        res[i] = {"error": repr(e)}; return
35    toks = usage or chunks
36    res[i] = {"ttft": round((first or last) - t0, 2), "total_s": round(last - t0, 2), "tokens": toks, "chunks": chunks,
37              "decode_tps": round(toks / max(0.01, last - (first or t0)), 1) if first else None}
38out = []
39one({}, 0) if False else None
40for c in LEVELS:
41    for rd in range(ROUNDS):
42        res = [None] * c; th = [threading.Thread(target=one, args=(res, i)) for i in range(c)]
43        t0 = time.time(); [t.start() for t in th]; [t.join() for t in th]; wall = time.time() - t0
44        tot = sum((r or {}).get("tokens", 0) or 0 for r in res)
45        row = {"streams": c, "round": rd + 1, "wall_s": round(wall, 1), "total_tokens": tot, "total_tps": round(tot / wall, 1), "per_stream": res}
46        out.append(row); print(json.dumps(row), flush=True)
47json.dump(out, open(OUT, "w"), indent=1)