"""Held-out decision accuracy (data/i1/heldout.jsonl, never trained on): the first assistant action of each case, balanced between 'should call' and 'should not call'. python dec_eval.py CKPT OUT.json""" import collections import json import os import sys import torch from tokenizers import Tokenizer from chatfmt import Fmt, parse_calls from model_io import generate, load f = Fmt(Tokenizer.from_file(os.environ["SLAYER_TOKENIZER"])) m, _, _ = load(sys.argv[1], device="cuda", dtype=torch.bfloat16) m.eval() c, per = collections.Counter(), collections.defaultdict(collections.Counter) for line in open("data/i1/heldout.jsonl", encoding="utf-8"): x = json.loads(line) ms = x["messages"] i = next((i for i, y in enumerate(ms) if y["role"] == "assistant" and y.get("trainable", True)), None) if i is None: continue p = f.prompt_ids(ms[:i], x.get("tools") or []) if len(p) > 3600: continue ref = ms[i].get("tool_calls") or [] o, _ = generate(m, p, stop_ids={f.ime, f.bos}, max_new_tokens=160) cs, _ = parse_calls(f.tok.decode(o, skip_special_tokens=False)) k = "call" if ref else "nocall" ok = (bool(cs) and cs[0]["name"] == ref[0]["name"]) if ref else not cs c[k] += 1 c[k + "_ok"] += ok c["any_call_when_needed"] += bool(ref) and bool(cs) c["multi"] += len(cs) > 1 per[x["task"]][k + ("_ok" if ok else "_bad")] += 1 p1, p2 = c["call_ok"] / c["call"], c["nocall_ok"] / c["nocall"] out = {"ckpt": sys.argv[1], "call_right_tool": round(100 * p1, 1), "called_when_needed": round(100 * c["any_call_when_needed"] / c["call"], 1), "nocall_correct": round(100 * p2, 1), "balanced": round(50 * (p1 + p2), 1), "parallel_turns": c["multi"], "n": [c["call"], c["nocall"]], "per_task": {k: dict(v) for k, v in per.items()}} json.dump(out, open(sys.argv[2], "w")) print(json.dumps({k: v for k, v in out.items() if k != "per_task"}))