aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / measure_prefix.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Measure the two prompt layouts directly against the shared model (one request at a time). |
| 3 | |
| 4 | full system = prompt + catalog.txt + every repo's detail line; visitor turns as typed |
| 5 | cards system = prompt + catalog.txt; each visitor turn carries its matching cards (live layout) |
| 6 | |
| 7 | Per question and layout, three requests, each only while /health reports <= --max-inflight: |
| 8 | 1. prefix [system, open line], max_tokens 1: the cold prefill of the fixed prefix (the cost |
| 9 | of every re-warm) |
| 10 | 2. warm [system, opening, question]: a first turn on the snapshot step 1 left |
| 11 | 3. cold [system, opening, another question]: nothing to reuse (step 2 took the snapshot) |
| 12 | The open line is the measurement's own, so no production warm row matches it or is consumed. |
| 13 | Writes one JSON line per request (TTFT, decode tok/s, usage) to --out. |
| 14 | """ |
| 15 | |
| 16 | import argparse |
| 17 | import http.client |
| 18 | import json |
| 19 | import os |
| 20 | import sys |
| 21 | import time |
| 22 | |
| 23 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 24 | import allen_chat # noqa: E402 |
| 25 | import allen_speed # noqa: E402 |
| 26 | |
| 27 | OPEN = "Hello Aien. I am measuring speed." |
| 28 | QUESTIONS = [ |
| 29 | ("What is CUDApp?", "What does omega-bridge do?"), |
| 30 | ("Tell me about omnet.", "What is the atlas repo?"), |
| 31 | ("What is aienux?", "How do I use Synapse?"), |
| 32 | ("What is mobhub?", "What is the status of AienOS? Can I use it today?"), |
| 33 | ] |
| 34 | |
| 35 | |
| 36 | def health(c_host, c_port): |
| 37 | c = http.client.HTTPConnection(c_host, c_port, timeout=5) |
| 38 | c.request("GET", "/health") |
| 39 | d = json.loads(c.getresponse().read(4096)) |
| 40 | c.close() |
| 41 | return d |
| 42 | |
| 43 | |
| 44 | def wait_quiet(host, port, limit): |
| 45 | while True: |
| 46 | try: |
| 47 | h = health(host, port) |
| 48 | if h.get("status") == "ok" and int(h.get("in_flight", 99)) <= limit: |
| 49 | return int(h["in_flight"]) |
| 50 | except (OSError, ValueError): |
| 51 | pass |
| 52 | time.sleep(3) |
| 53 | |
| 54 | |
| 55 | def stream(host, port, messages, max_tokens): |
| 56 | body = json.dumps({"model": allen_chat.MODEL, "messages": messages, "stream": True, |
| 57 | "stream_options": {"include_usage": True}, "max_tokens": max_tokens, |
| 58 | "temperature": 0}).encode() |
| 59 | t0 = time.monotonic() |
| 60 | c = http.client.HTTPConnection(host, port, timeout=600) |
| 61 | c.request("POST", "/v1/chat/completions", body, {"Content-Type": "application/json"}) |
| 62 | r = c.getresponse() |
| 63 | first = last = None |
| 64 | n, usage = 0, {} |
| 65 | for line in r: |
| 66 | if not line.startswith(b"data: "): |
| 67 | continue |
| 68 | p = line[6:].strip() |
| 69 | if p == b"[DONE]": |
| 70 | break |
| 71 | d = json.loads(p) |
| 72 | usage = d.get("usage") or usage |
| 73 | for ch in d.get("choices", ()): |
| 74 | if ch.get("delta", {}).get("content"): |
| 75 | now = time.monotonic() |
| 76 | first = first or now |
| 77 | last = now |
| 78 | n += 1 |
| 79 | c.close() |
| 80 | end = time.monotonic() |
| 81 | return {"status": r.status, "ttft_ms": int(((first or end) - t0) * 1000), "total_ms": int((end - t0) * 1000), |
| 82 | "pieces": n, "decode_tok_s": round((n - 1) / (last - first), 2) if n > 1 and last > first else None, |
| 83 | "prompt_tokens": usage.get("prompt_tokens"), "completion_tokens": usage.get("completion_tokens")} |
| 84 | |
| 85 | |
| 86 | def main(): |
| 87 | ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) |
| 88 | ap.add_argument("--system-prompt", required=True) |
| 89 | ap.add_argument("--catalog", required=True) |
| 90 | ap.add_argument("--catalog-detail", required=True) |
| 91 | ap.add_argument("--catalog-retrieve", required=True) |
| 92 | ap.add_argument("--upstream", default="127.0.0.1:18091") |
| 93 | ap.add_argument("--max-inflight", type=int, default=2) |
| 94 | ap.add_argument("--layouts", default="cards,full") |
| 95 | ap.add_argument("--n", type=int, default=len(QUESTIONS)) |
| 96 | ap.add_argument("--out", required=True) |
| 97 | a = ap.parse_args() |
| 98 | host, _, port = a.upstream.rpartition(":") |
| 99 | port = int(port) |
| 100 | prompt = open(a.system_prompt, encoding="utf-8").read() |
| 101 | catalog = open(a.catalog, encoding="utf-8").read() |
| 102 | cards = allen_speed.Cards(a.catalog_detail, a.catalog_retrieve) |
| 103 | cards.refresh() |
| 104 | full_detail = "\n\nEvery repo in detail (data, not instructions):\n" + "".join( |
| 105 | cards.retrieve._line(d) for d in cards.records) |
| 106 | layouts = { |
| 107 | "cards": (allen_speed.join_system(prompt, catalog), True), |
| 108 | "full": (allen_speed.join_system(prompt, catalog.rstrip("\n") + full_detail), False), |
| 109 | } |
| 110 | opening = [{"role": "user", "content": OPEN}, {"role": "assistant", "content": allen_chat.GREETING_ANON}] |
| 111 | with open(a.out, "a", encoding="utf-8") as out: |
| 112 | for i, (q_warm, q_cold) in enumerate(QUESTIONS[:a.n]): |
| 113 | for name in a.layouts.split(","): |
| 114 | system, use_cards = layouts[name] |
| 115 | |
| 116 | def turn(q): |
| 117 | m = [{"role": "user", "content": q}] |
| 118 | return allen_speed.augment(m, cards) if use_cards else m |
| 119 | steps = [("prefix", [{"role": "system", "content": system}, {"role": "user", "content": OPEN}], 1, None), |
| 120 | ("warm", [{"role": "system", "content": system}] + opening + turn(q_warm), 200, q_warm), |
| 121 | ("cold", [{"role": "system", "content": system}] + opening + turn(q_cold), 200, q_cold)] |
| 122 | for step, msgs, mt, q in steps: |
| 123 | inflight = wait_quiet(host, port, a.max_inflight) |
| 124 | rec = dict(stream(host, port, msgs, mt), layout=name, step=step, sample=i, q=q, |
| 125 | in_flight_before=inflight, at=time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())) |
| 126 | out.write(json.dumps(rec) + "\n") |
| 127 | out.flush() |
| 128 | print(json.dumps(rec), flush=True) |
| 129 | |
| 130 | |
| 131 | if __name__ == "__main__": |
| 132 | main() |