ATOM (atom-ai): code snapshot (100fae0 + batched-train), README with measured numbers, curated checkpoints (part 3)
969c687 verified Download docs/artifacts/root_cause/judge.py from thefinalboss/atom-ai: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/thefinalboss/atom-ai/resolve/main/docs/artifacts/root_cause/judge.py
- Command line
-
hf download hf://thefinalboss/atom-ai/docs/artifacts/root_cause/judge.py
-
curl -L -o judge.py https://huggingface.co/thefinalboss/atom-ai/resolve/main/docs/artifacts/root_cause/judge.py
3.81 kB
| """Free-run quality: score generated replies with an external judge (GRU f100, val CE 0.10 on this corpus) | |
| and with ATOM itself (self-NLL along its own generation vs teacher-forced CE on corpus text). | |
| Also prompt-dependence: fraction of seeds whose reply is identical for all 3 prompts.""" | |
| import json, sys | |
| from pathlib import Path | |
| import torch, torch.nn.functional as F | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from common import CKPT, PROMPTS, load_model | |
| from gru_baseline import GRULM | |
| from src.io.atomizer import Atomizer | |
| from src.speech_lock import format_dialogue_prompt | |
| OUT = Path(__file__).resolve().parent | |
| torch.set_num_threads(2) | |
| def gru_nll(m, prompt, reply): | |
| ctx = f"Utilisateur: {prompt}\nAssistant: ".encode(); r = reply.encode() | |
| if not r: return None | |
| x = torch.tensor(list(ctx + r))[None]; lg, _ = m(x[:, :-1]) | |
| lp = F.cross_entropy(lg[0], x[0, 1:], reduction="none")[len(ctx) - 1:] | |
| return float(lp.mean()) | |
| def atom_nll(m, prompt, reply, train_labels): | |
| orig = Atomizer._infer_commit_boundary | |
| if train_labels: | |
| Atomizer._infer_commit_boundary = classmethod(lambda cls, p: "newline" if p[-1] in (10, 13) else "max_span") | |
| try: | |
| m.eval(); m.reset_state(reset_atomizer=True) | |
| pp = m.atomizer.encode(format_dialogue_prompt(prompt), reset=True) | |
| for q in pp[:-1]: m.forward_packet(q) | |
| cur = pp[-1]; tot = 0.0; r = reply.encode() | |
| for b in r: | |
| tot += -float(torch.log_softmax(m.forward_packet(cur)["surface"]["byte_logits"][0], -1)[b]) | |
| cur = m.atomizer.packet_from_payload(bytes([b])) | |
| finally: | |
| Atomizer._infer_commit_boundary = orig | |
| return tot / max(len(r), 1) | |
| def mean(xs): | |
| xs = [x for x in xs if x is not None]; return sum(xs) / len(xs) if xs else None | |
| def main(): | |
| judge = GRULM(512, 2); judge.load_state_dict(torch.load(OUT / "gru_f100.pt")); judge.eval() | |
| atom = load_model(CKPT) | |
| dec = json.loads((OUT / "decode_consistency.json").read_text()) | |
| ng = json.loads((OUT / "ngram_results.json").read_text()) | |
| sets = { | |
| "corpus_gold_replies": {p: [g] for p, g in [("Bonjour, comment ça va ?", "Ça va très bien, et toi ?\n"), ("Qui es-tu ?", "Je suis Atom, un petit modèle qui apprend à parler.\n"), ("Il était une fois", "Il était une fois un petit robot qui apprenait des mots.\n")]}, | |
| "atom_original_decode": dec["C_chat_original_nogate"], | |
| "atom_label_fixed_decode": dec["C_chat_patched_nogate"], | |
| "gru_f100": json.loads((OUT / "gru_f100.json").read_text())["samples"], | |
| "gru_small128": json.loads((OUT / "gru_small128.json").read_text())["samples"], | |
| } | |
| for row in ng["rows"]: | |
| if "samples" in row: sets[f"ngram{row['n']}_heldout"] = row["samples"] | |
| res = {} | |
| for name, s in sets.items(): | |
| # score first reply line only (up to first newline) — what a chat shows | |
| first = {p: [v.split("\n")[0] + ("\n" if "\n" in v else "") for v in vs] for p, vs in s.items()} | |
| r = {"judge_gru_nll": mean([gru_nll(judge, p, v) for p, vs in first.items() for v in vs])} | |
| if name.startswith("atom") or name == "corpus_gold_replies": | |
| r["atom_self_nll_train_labels"] = mean([atom_nll(atom, p, v, True) for p, vs in first.items() for v in vs]) | |
| r["atom_self_nll_orig_labels"] = mean([atom_nll(atom, p, v, False) for p, vs in first.items() for v in vs]) | |
| vals = list(s.values()) | |
| if len(vals[0]) > 1: | |
| r["frac_seeds_identical_across_3_prompts"] = sum(len({vals[k][i] for k in range(3)}) == 1 for i in range(len(vals[0]))) / len(vals[0]) | |
| res[name] = r; print(name, r, flush=True) | |
| (OUT / "judge_results.json").write_text(json.dumps(res, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |