prefill_distinct.py 1.7 KB

1234567891011121314151617181920
  1. #!/usr/bin/env python3
  2. """Needle prompt of ~<target> tokens with content salted by <seed> (no radix-cache prefix hits), max_tokens 20."""
  3. import json, urllib.request, time, sys, random
  4. URL = "http://10.0.0.1:8888/v1/chat/completions"
  5. target = int(sys.argv[1]); seed = int(sys.argv[2]) if len(sys.argv) > 2 else 1
  6. rnd = random.Random(seed)
  7. words = ["harbour","lantern","meadow","copper","violin","orchard","compass","thistle","ember","granite","willow","saffron","anchor","quartz","ledger","falcon"]
  8. def para(i):
  9. return f"Entry {seed}-{i}: the {rnd.choice(words)} keeper noted {rnd.randint(1,999)} {rnd.choice(words)} crates by the {rnd.choice(words)} gate before the {rnd.choice(words)} bell rang at dusk. "
  10. secret = f"The secret code word is PELICAN-{seed}."
  11. def ask(p, n=20):
  12. body = dict(model="deepseek-v4.1-flash", temperature=0, max_tokens=n, stream=False, chat_template_kwargs={"thinking": False}, messages=[{"role": "user", "content": p}])
  13. t0 = time.time()
  14. r = json.load(urllib.request.urlopen(urllib.request.Request(URL, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}), timeout=3000))
  15. return r["usage"]["prompt_tokens"], time.time() - t0, r["choices"][0]["message"]["content"].strip()
  16. sample = "".join(para(i) for i in range(50)); n50, _, _ = ask(sample, 1); per = (n50 - 20) / 50.0
  17. reps = int((target - 200) / per)
  18. p = "".join(para(i) for i in range(reps)) + "\n" + secret + "\n" + "".join(para(reps + i) for i in range(20)) + "\nWhat is the secret code word? Answer with the code word only."
  19. n, dt, txt = ask(p)
  20. print(f"prompt_tokens={n} prefill+20tok={dt:.1f}s ({n/dt:.0f} tok/s) answer={txt[:40]!r} expected=PELICAN-{seed}", flush=True)