okami.mom/strata

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

Niko1221Merge f-q36-vp (Qwen3.6 parallel requests and pictures)db4dd65e5

main
3.3 KiB65 linesraw
1"""q36_serve_par.py PORT [--n 128] [--conc 4] [--img PATH] : N concurrent chat requests through the server (parallel: N), against
2the same requests one after the other (the solo path).  Greedy, thinking off: the texts must be equal.  Prints the
3aggregate rate of the concurrent round.  With --img: half of the requests carry the picture (and ask about it)."""
4import argparse, base64, json, sys, threading, time, urllib.request
5
6PROMPTS = json.load(open(sys.argv[0].rsplit("/", 1)[0] + "/parity_prompts.json")) if False else [
7    "Write a short story about a lighthouse keeper who finds a message in a bottle.",
8    "Implement an LRU cache in Python with get and put in O(1), and add three unit tests.",
9    "What is 17 * 23 + 144 / 12? Show the steps.",
10    "Explain how a refrigerator works to a ten-year-old.",
11    "Translate into German: The train to Munich leaves at half past seven, platform four.",
12    "List five differences between TCP and UDP.",
13    "Summarize the plot of Romeo and Juliet in three sentences.",
14    "Write a JSON object describing a book with title, author, year and three tags.",
15]
16
17
18def ask(port, content, n, out, i, stream=False):
19    body = {"model": "x", "max_tokens": n, "temperature": 0, "chat_template_kwargs": {"enable_thinking": False},
20            "messages": [{"role": "user", "content": content}]}
21    t0 = time.time()
22    try:
23        r = json.loads(urllib.request.urlopen(urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions",
24                       json.dumps(body).encode(), {"Content-Type": "application/json"}), timeout=900).read())
25        out[i] = (r["choices"][0]["message"]["content"], r["usage"]["completion_tokens"], time.time() - t0)
26    except Exception as e:  # noqa: BLE001
27        out[i] = ("ERROR %s" % e, 0, time.time() - t0)
28
29
30ap = argparse.ArgumentParser()
31ap.add_argument("port", type=int)
32ap.add_argument("--n", type=int, default=128)
33ap.add_argument("--conc", type=int, default=4)
34ap.add_argument("--img", default=None)
35a = ap.parse_args()
36contents = []
37for i in range(a.conc):
38    p = PROMPTS[i % len(PROMPTS)]
39    if a.img and i % 2 == 1:
40        b64 = base64.b64encode(open(a.img, "rb").read()).decode()
41        contents.append([{"type": "image_url", "image_url": {"url": "data:image/png;base64," + b64}},
42                         {"type": "text", "text": "What text is written in the image? Answer with the text only."}])
43    else:
44        contents.append(p)
45solo = [None] * a.conc
46for i, c in enumerate(contents):
47    ask(a.port, c, a.n, solo, i)
48print("one after the other: %d requests, %d tokens, %.1fs" % (a.conc, sum(x[1] for x in solo), sum(x[2] for x in solo)), flush=True)
49for rnd in range(2):
50    conc = [None] * a.conc
51    ts = [threading.Thread(target=ask, args=(a.port, c, a.n, conc, i)) for i, c in enumerate(contents)]
52    t0 = time.time()
53    for t in ts:
54        t.start()
55    for t in ts:
56        t.join()
57    wall = time.time() - t0
58    tot = sum(x[1] for x in conc)
59    same = [conc[i][0] == solo[i][0] for i in range(a.conc)]
60    print("concurrent round %d: %d requests, %d tokens in %.1fs = %.1f tok/s aggregate; texts equal to the solo run: %s"
61          % (rnd, a.conc, tot, wall, tot / wall, same), flush=True)
62    for i, ok in enumerate(same):
63        if not ok:
64            print("  request %d: %r vs %r" % (i, conc[i][0][:80], solo[i][0][:80]))
65sys.exit(0)