Download eval_sample.py from tts-sft/round4-sft-eval: direct link, hf CLI and curl.
- Browser
- Download file 4.13 kB
-
https://huggingface.co/datasets/tts-sft/round4-sft-eval/resolve/main/eval_sample.py
- Command line
-
hf download hf://datasets/tts-sft/round4-sft-eval/eval_sample.py
-
curl -L -o eval_sample.py https://huggingface.co/datasets/tts-sft/round4-sft-eval/resolve/main/eval_sample.py
4.13 kB
| #!/usr/bin/env python3 | |
| """Sample K candidates per eval problem from an OpenAI-compatible endpoint. | |
| Protocol = arm B / SFT_DESIGN §7: temp 1.0, top_p 0.95, configurable | |
| completion cap (legacy default 16384), reasoning medium, per-sample seed = | |
| base_seed + k, reasoning_content preserved. | |
| Shard-native: --shard i/N (deterministic by sorted id) or --ids file. | |
| Resumable: existing (id,k) rows in --out are skipped. | |
| python3 eval_sample.py --testset testset.jsonl --model <name> \ | |
| --base-url http://localhost:8000/v1 --k 16 --shard 0/4 --out samples_s0.jsonl | |
| """ | |
| import argparse, asyncio, json, os | |
| async def main(a): | |
| from openai import AsyncOpenAI | |
| rows = [json.loads(l) for l in open(a.testset)] | |
| rows.sort(key=lambda r: r["id"]) | |
| if a.ids: | |
| want = {l.strip() for l in open(a.ids) if l.strip()} | |
| rows = [r for r in rows if r["id"] in want] | |
| if a.shard: | |
| i, n = map(int, a.shard.split("/")) | |
| rows = rows[i::n] | |
| done = set() | |
| if os.path.exists(a.out): | |
| for l in open(a.out): | |
| try: | |
| r = json.loads(l) | |
| done.add((r["id"], r["k"])) | |
| except Exception: | |
| pass | |
| todo = [(r, k) for r in rows for k in range(a.k) if (r["id"], k) not in done] | |
| print(f"{len(rows)} problems x k={a.k}; {len(done)} done, {len(todo)} to sample", flush=True) | |
| client = AsyncOpenAI(base_url=a.base_url, api_key=a.api_key, timeout=a.timeout) | |
| sem = asyncio.Semaphore(a.concurrency) | |
| outf = open(a.out, "a") | |
| lock = asyncio.Lock() | |
| async def one(r, k): | |
| async with sem: | |
| for attempt in range(5): | |
| try: | |
| resp = await client.chat.completions.create( | |
| model=a.model, | |
| messages=[{"role": "user", "content": r["question"]}], | |
| temperature=1.0, top_p=0.95, | |
| max_completion_tokens=a.max_completion_tokens, | |
| reasoning_effort="medium", | |
| seed=a.base_seed + k, timeout=a.timeout) | |
| ch = resp.choices[0] | |
| u = resp.usage | |
| rec = {"id": r["id"], "k": k, "seed": a.base_seed + k, | |
| "text": ch.message.content or "", | |
| "rt": getattr(ch.message, "reasoning_content", None), | |
| "finish": ch.finish_reason, | |
| "pt": u.prompt_tokens if u else None, | |
| "ct": u.completion_tokens if u else None, | |
| "max_completion_tokens": a.max_completion_tokens} | |
| async with lock: | |
| outf.write(json.dumps(rec) + "\n"); outf.flush() | |
| return | |
| except Exception as e: # noqa: BLE001 | |
| if attempt == 4: | |
| print(f"GIVEUP {r['id']} k={k}: {type(e).__name__}", flush=True) | |
| return | |
| await asyncio.sleep(min(60, 2 ** attempt * 5)) | |
| CH = 2000 | |
| for i in range(0, len(todo), CH): | |
| await asyncio.gather(*(one(r, k) for r, k in todo[i:i + CH])) | |
| print(f"{min(i + CH, len(todo))}/{len(todo)}", flush=True) | |
| outf.close() | |
| print("SAMPLING DONE", flush=True) | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--testset", required=True) | |
| ap.add_argument("--model", required=True) | |
| ap.add_argument("--base-url", default="http://localhost:8000/v1") | |
| ap.add_argument("--api-key", default="EMPTY") | |
| ap.add_argument("--k", type=int, default=16) | |
| ap.add_argument("--max-completion-tokens", type=int, default=16384) | |
| ap.add_argument("--shard", default=None, help="i/N over sorted problem ids") | |
| ap.add_argument("--ids", default=None, help="file of ids (one per line)") | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--base-seed", type=int, default=1234) | |
| ap.add_argument("--concurrency", type=int, default=128) | |
| ap.add_argument("--timeout", type=float, default=1800) | |
| args = ap.parse_args() | |
| asyncio.run(main(args)) | |