Download training/code-rank/scripts/decide/parity_probe.py from paintedwolfcode/bialy-dataset: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/datasets/paintedwolfcode/bialy-dataset/resolve/main/training/code-rank/scripts/decide/parity_probe.py
- Command line
-
hf download hf://datasets/paintedwolfcode/bialy-dataset/training/code-rank/scripts/decide/parity_probe.py
-
curl -L -o parity_probe.py https://huggingface.co/datasets/paintedwolfcode/bialy-dataset/resolve/main/training/code-rank/scripts/decide/parity_probe.py
4.3 kB
| #!/usr/bin/env python3 | |
| """Score turns through the trainer's own forward and, optionally, through a pw-decide | |
| launcher, so a head can be checked before the engine is blamed: the two paths agree | |
| to the fourth decimal when the engine is right, and a head that scores nothing here | |
| was never trained, whatever its training log said. | |
| Usage: parity_probe.py --model ID --head FILE --examples FILE [--engine LAUNCHER] [--n 6] | |
| [--corpus <decide dir>/corpus.json] | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| os.environ.setdefault("TRANSFORMERS_OFFLINE", "1") | |
| os.environ.setdefault("TOKENIZERS_PARALLELISM", "false") | |
| import torch # noqa: E402 | |
| import laya # noqa: E402 | |
| from safetensors.torch import load_file # noqa: E402 | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| import rows as rowfile # noqa: E402 | |
| from corpus import Corpus, decide_dir # noqa: E402 | |
| from train import forward_head, multi_item, precompute, state_text # noqa: E402 | |
| def torch_probs(agent, model, corpus, ex, device): | |
| context = int(agent.cfg.get("max_len", 512)) | |
| head_max_len = min(int(corpus.state_spec.get("head_tokens", 512)), context - 64) | |
| tools_q = corpus.multi_question("tool", ex["offered"]["loadable"]) | |
| item = multi_item(agent.tok, state_text(ex["state"]), tools_q, {k: 0 for k in tools_q["options"]}, context, head_max_len, "tools") | |
| names = list(tools_q["options"].keys())[: len(item["markers"])] | |
| f = precompute(agent, [item], device)[0] | |
| with torch.no_grad(): | |
| logits = forward_head(model, f["h"][None].to(device), f["att"][None].to(device), f["marker_pos"][None].to(device), | |
| f["marker_mask"][None].to(device), torch.tensor([item["qtype"]], device=device))[0].cpu() | |
| return dict(zip(names, torch.sigmoid(logits[: len(names)]).tolist())) | |
| def engine_probs(proc, corpus, ex): | |
| req = {"method": "decide", "head": "turn-load", "state": ex["state"], "questions": corpus.row_questions(ex)} | |
| proc.stdin.write(json.dumps(req) + "\n") | |
| proc.stdin.flush() | |
| return json.loads(proc.stdout.readline())["answers"]["tools"]["probabilities"] | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--model", required=True) | |
| ap.add_argument("--head", required=True) | |
| ap.add_argument("--examples", required=True) | |
| ap.add_argument("--engine", default=os.environ.get("LYCAON_DECIDE_ENGINE")) | |
| ap.add_argument("--corpus", default=str(decide_dir() / "corpus.json")) | |
| ap.add_argument("--n", type=int, default=6) | |
| args = ap.parse_args() | |
| device = os.environ.get("LAYA_DEVICE") or ("mps" if torch.backends.mps.is_available() else "cpu") | |
| agent = laya.load(args.model, device=device) | |
| model = agent.model | |
| tensors = load_file(args.head, device=device) | |
| for module in ("head", "scorer", "type_emb"): | |
| state = {k[len(module) + 1:]: v for k, v in tensors.items() if k.startswith(module + ".")} | |
| if state: | |
| getattr(model, module).load_state_dict(state) | |
| model.eval() | |
| corpus = Corpus.load(args.corpus) | |
| proc = None | |
| if args.engine: | |
| proc = subprocess.Popen([args.engine], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1) | |
| proc.stdin.write(json.dumps({"method": "hello"}) + "\n") | |
| proc.stdin.flush() | |
| proc.stdout.readline() | |
| worst = 0.0 | |
| examples = [ex for ex in rowfile.load(args.examples) if not ex["partial"]][: args.n] | |
| for ex in examples: | |
| pt = torch_probs(agent, model, corpus, ex, device) | |
| top = sorted(pt.items(), key=lambda kv: -kv[1])[:5] | |
| truth = sorted(rowfile.truth_tools(ex)) | |
| print("truth", truth) | |
| print(" trainer top", [(k, round(v, 3)) for k, v in top], "| truth p", [(t, round(pt.get(t, -1), 3)) for t in truth]) | |
| if proc is not None: | |
| pe = engine_probs(proc, corpus, ex) | |
| gap = max(abs(pe.get(k, 0.0) - v) for k, v in pt.items()) | |
| worst = max(worst, gap) | |
| print(" engine top", [(k, round(pe.get(k, 0.0), 3)) for k, _ in top], "| max |engine - trainer| %.4f" % gap) | |
| if proc is not None: | |
| proc.stdin.close() | |
| proc.wait() | |
| print("parity: worst gap %.4f over %d turns" % (worst, len(examples))) | |
| if __name__ == "__main__": | |
| main() | |