Datasets:
Download code/scripts_seqroute/score_objective.py from divy123/sequential-router-v2: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/datasets/divy123/sequential-router-v2/resolve/main/code/scripts_seqroute/score_objective.py
- Command line
-
hf download hf://datasets/divy123/sequential-router-v2/code/scripts_seqroute/score_objective.py
-
curl -L -o score_objective.py https://huggingface.co/datasets/divy123/sequential-router-v2/resolve/main/code/scripts_seqroute/score_objective.py
2.84 kB
| """Objective scoring of generated responses (everything except judge / COMET / simplification). Resumable: skips (model, query_id) already scored. | |
| Usage: python score_objective.py [--models a,b] [--procs 96] | |
| Writes /mnt/localssd/seqroute1m/scores/obj/<model>.jsonl | |
| """ | |
| import argparse | |
| import json | |
| import sys | |
| import time | |
| from multiprocessing import Pool | |
| from pathlib import Path | |
| import pandas as pd | |
| sys.path.insert(0, "src") | |
| sys.path.insert(0, ".") | |
| ROOT = Path("/mnt/localssd/seqroute1m") | |
| Q: dict = {} | |
| def init(qpath): | |
| global Q | |
| df = pd.read_parquet(qpath, columns=["query_id", "prompt", "evaluator_type", "payload"]) | |
| Q = {r.query_id: (r.prompt, r.evaluator_type, json.loads(r.payload)) for r in df.itertuples()} | |
| def work(rec): | |
| from llmroute.seqroute.evaluate import evaluate_row | |
| prompt, ev, payload = Q[rec["id"]] | |
| try: | |
| r = evaluate_row(ev, prompt, payload, rec["text"], rec.get("reasoning"), rec.get("finish_reason")) | |
| except Exception as e: | |
| r = dict(Q=0.0, raw=0.0, raw_name="error", parse_ok=False, answer=None, sub={}, truncated=False, empty_final=False, eval_error=repr(e)[:200]) | |
| if r is None: | |
| return None | |
| r["query_id"] = rec["id"] | |
| r["sub"] = json.dumps(r.get("sub", {})) | |
| r["answer"] = None if r.get("answer") is None else str(r["answer"])[:2000] | |
| return r | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--models", default="") | |
| ap.add_argument("--procs", type=int, default=96) | |
| ap.add_argument("--queries", default=str(ROOT / "queries/master_queries.parquet")) | |
| a = ap.parse_args() | |
| out_dir = ROOT / "scores/obj" | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| models = [m["id"] for m in json.load(open(ROOT / "manifests/models.json"))] | |
| if a.models: | |
| models = [m for m in models if m in a.models.split(",")] | |
| with Pool(a.procs, initializer=init, initargs=(a.queries,)) as pool: | |
| for m in models: | |
| rf = ROOT / "responses" / f"{m}.jsonl" | |
| if not rf.exists(): | |
| continue | |
| of = out_dir / f"{m}.jsonl" | |
| done = set() | |
| if of.exists(): | |
| done = {json.loads(l)["query_id"] for l in open(of)} | |
| recs = [] | |
| for l in open(rf): | |
| r = json.loads(l) | |
| if r["id"] not in done: | |
| recs.append(r) | |
| t0 = time.time() | |
| n = 0 | |
| with open(of, "a") as f: | |
| for res in pool.imap_unordered(work, recs, chunksize=16): | |
| if res is None: | |
| continue | |
| f.write(json.dumps(res, ensure_ascii=False) + "\n") | |
| n += 1 | |
| print(f"{m}: scored {n} new ({len(done)} cached) in {time.time() - t0:.0f}s", flush=True) | |
| if __name__ == "__main__": | |
| main() | |