prefill.py 1.7 KB

1234567891011121314151617181920212223242526272829303132
  1. """Cold-cache prefill throughput: unique filler each time so nothing is cached."""
  2. import json, sys, time, urllib.request, uuid
  3. BASE, MODEL = sys.argv[1], sys.argv[2]
  4. def once(target):
  5. nonce = uuid.uuid4().hex
  6. unit = f"Reading {nonce} note {{n}}: the pump was serviced and the filter replaced.\n"
  7. text = "".join(unit.format(n=i) for i in range(target // 16))
  8. body = {"model": MODEL, "messages": [{"role": "user", "content": text + "\nReply with OK."}],
  9. "max_tokens": 4, "temperature": 0, "stream": True,
  10. "stream_options": {"include_usage": True}}
  11. req = urllib.request.Request(BASE + "/chat/completions", json.dumps(body).encode(),
  12. {"Content-Type": "application/json"})
  13. t0 = time.time(); ttft = None; usage = None
  14. with urllib.request.urlopen(req, timeout=3600) as r:
  15. for raw in r:
  16. line = raw.decode("utf-8", "replace").strip()
  17. if not line.startswith("data:"): continue
  18. d = line[5:].strip()
  19. if d == "[DONE]": break
  20. ev = json.loads(d)
  21. if ev.get("usage"): usage = ev["usage"]
  22. for ch in ev.get("choices", []):
  23. d = ch.get("delta") or {}
  24. if (d.get("content") or d.get("reasoning") or d.get("reasoning_content")) and ttft is None:
  25. ttft = time.time() - t0
  26. n = (usage or {}).get("prompt_tokens", 0)
  27. if ttft:
  28. print(f" prompt={n:>7} ttft={ttft:6.1f}s prefill={n/ttft:8.0f} tok/s", flush=True)
  29. else:
  30. print(f" prompt={n:>7} no first token seen (usage={usage})", flush=True)
  31. for t in ([int(a) for a in sys.argv[3:]] or [8000, 42000, 128000]):
  32. once(t)