Download scripts/bench/kl_probe.py from jaytonde05/kv-cache-quantization-commands: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/datasets/jaytonde05/kv-cache-quantization-commands/resolve/main/scripts/bench/kl_probe.py
- Command line
-
hf download hf://datasets/jaytonde05/kv-cache-quantization-commands/scripts/bench/kl_probe.py
-
curl -L -o kl_probe.py https://huggingface.co/datasets/jaytonde05/kv-cache-quantization-commands/resolve/main/scripts/bench/kl_probe.py
2.03 kB
| """Exp 0 KL probe: get top-20 prompt logprobs for a few long SWE prefixes. | |
| Sends token-id prompts to /v1/completions (max_tokens=1, prompt_logprobs=20) and | |
| stores the top-20 (id, logprob) per position for positions in [START, END). | |
| compare_kl.py then computes KL between configs on identical token sequences. | |
| """ | |
| import argparse | |
| import json | |
| import time | |
| import urllib.request | |
| from pathlib import Path | |
| from transformers import AutoTokenizer | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--n", type=int, default=3) # trajectories | |
| ap.add_argument("--prefix_tokens", type=int, default=24000) | |
| ap.add_argument("--start", type=int, default=4000) # measured window | |
| ap.add_argument("--url", default="http://localhost:8000/v1/completions") | |
| a = ap.parse_args() | |
| tok = AutoTokenizer.from_pretrained(Path.home() / "models" / "Qwen3.8-27B") | |
| rows = [json.loads(l) for l in open(Path.home() / "bench" / "data" / "swe_kl_50.jsonl")][: a.n] | |
| out = [] | |
| for r in rows: | |
| ids = tok.apply_chat_template(r["messages"], tools=r["tools"], tokenize=True) | |
| if not isinstance(ids, list): | |
| ids = ids["input_ids"] | |
| ids = ids[: a.prefix_tokens] | |
| body = {"model": "Qwen/Qwen3.8-27B", "prompt": ids, "max_tokens": 1, "temperature": 0, | |
| "prompt_logprobs": 20} | |
| t0 = time.time() | |
| req = urllib.request.Request(a.url, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) | |
| resp = json.loads(urllib.request.urlopen(req, timeout=1800).read()) | |
| pl = resp["choices"][0]["prompt_logprobs"] | |
| pos = [] | |
| for i in range(a.start, len(ids)): | |
| d = pl[i] or {} | |
| top = sorted(((int(k), v["logprob"]) for k, v in d.items()), key=lambda x: -x[1])[:21] | |
| pos.append({"t": ids[i], "top": top}) | |
| out.append({"id": r["id"], "n_tokens": len(ids), "start": a.start, "positions": pos}) | |
| print(f"{r['id']}: {len(ids)} tokens, {time.time() - t0:.1f}s", flush=True) | |
| Path(a.out).write_text(json.dumps(out)) | |
| print("KL_PROBE_OK", a.out) | |