okami.mom/strata

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

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
2.5 KiB46 linesraw
1"""q36_serve_handoff.py PORT [--n 300] : a request that runs alone (the solo path, with MTP drafts), another one arrives while it
2runs (the server stops it and continues it in a batch slot from its held state), the second one ends (the first is alone again and
3goes back to the solo path): the first request's text must be the text of the same request run entirely alone.  Greedy, thinking
4off, every expert in VRAM (in the hybrid mode a different window mix can round differently: see docs/QWEN36.md).
5
6  python tools/qwen35/q36_serve_handoff.py 8199 [--n 300] [--delay 1.0]
7"""
8import argparse, json, threading, time, urllib.request
9
10A = "Write a long, detailed story about a lighthouse keeper who finds a message in a bottle and follows it across the sea."
11B = "List the first twenty prime numbers and explain how you found them."
12
13
14def ask(port, text, n, out, key):
15    body = {"model": "x", "max_tokens": n, "temperature": 0, "chat_template_kwargs": {"enable_thinking": False},
16            "messages": [{"role": "user", "content": text}]}
17    t0 = time.time()
18    r = json.loads(urllib.request.urlopen(urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions",
19                   json.dumps(body).encode(), {"Content-Type": "application/json"}), timeout=900).read())
20    out[key] = (r["choices"][0]["message"]["content"], r["usage"]["completion_tokens"], time.time() - t0)
21
22
23ap = argparse.ArgumentParser()
24ap.add_argument("port", type=int)
25ap.add_argument("--n", type=int, default=300)
26ap.add_argument("--delay", type=float, default=1.0)
27a = ap.parse_args()
28alone = {}
29ask(a.port, A, a.n, alone, "A")
30ask(a.port, B, 60, alone, "B")
31for rnd in range(2):
32    mix = {}
33    ta = threading.Thread(target=ask, args=(a.port, A, a.n, mix, "A"))
34    tb = threading.Thread(target=lambda: (time.sleep(a.delay), ask(a.port, B, 60, mix, "B")))
35    ta.start()
36    tb.start()
37    ta.join()
38    tb.join()
39    ea, eb = mix["A"][0] == alone["A"][0], mix["B"][0] == alone["B"][0]
40    print("round %d: A %d tokens in %.1fs, B %d tokens in %.1fs; A equals its run alone: %s; B equals its run alone: %s"
41          % (rnd, mix["A"][1], mix["A"][2], mix["B"][1], mix["B"][2], ea, eb), flush=True)
42    if not (ea and eb):
43        x, y = mix["A"][0], alone["A"][0]
44        k = next((i for i in range(min(len(x), len(y))) if x[i] != y[i]), min(len(x), len(y)))
45        print("  A first differs from its run alone at character %d of %d: %r vs %r" % (k, len(y), x[max(0, k - 20):k + 30],
46                                                                                    y[max(0, k - 20):k + 30]))