minterp

scripts/behavior.py

56 lines · View on GitHub · Linked from 1. Read

1"""
2Step 1: does the model do the task, and how well? See docs/01-read.md.
3
4For every template and every ordered pair of names, in the three versions
5of minterp/tasks.py, this records the logit of the second name (B) minus
6the logit of the first (A) at the last position, the probability of each,
7and the token the model holds likeliest.
8
9    uv run scripts/behavior.py                  # the model of minllm's stage 3
10    uv run scripts/behavior.py --model seed1    # the same recipe, another seed
11"""
12
13import argparse
14from collections import Counter
15
16import torch
17
18from minterp import tasks
19from minterp.common import load, rounded, save
20
21parser = argparse.ArgumentParser()
22parser.add_argument("--model", default="seed0", help="seed0, seed1, or the path of a checkpoint")
23args = parser.parse_args()
24
25model, tok, meta = load(args.model)
26device = next(model.parameters()).device
27people = tasks.names(tok)
28result = {"model": meta, "names": people, "templates": []}
29
30for batch in tasks.batches(tok, device, people=people):
31    entry = {"template": batch.template, "prompts": len(batch), "labels": batch.labels, "pos": batch.pos}
32    for version in ("clean", "flipped", "neutral"):
33        with torch.no_grad():
34            logits, _ = model(getattr(batch, version))
35        last = logits[:, -1].float()
36        probs = last.softmax(-1)
37        top = last.argmax(-1)
38        entry[version] = {
39            # Always B minus A: above 0 means the model prefers the second name.
40            "logit_diff": rounded(tasks.logit_diff(last, batch.right, batch.wrong)),
41            "p_B": rounded(probs.gather(1, batch.right[:, None])[:, 0]),
42            "p_A": rounded(probs.gather(1, batch.wrong[:, None])[:, 0]),
43            "top_is_B": (top == batch.right).float().mean().item(),
44            "top_is_A": (top == batch.wrong).float().mean().item(),
45            "top_tokens": Counter(tok.decode([t]) for t in top.tolist()).most_common(5),
46        }
47    result["templates"].append(entry)
48    ld = {v: torch.tensor(entry[v]["logit_diff"]) for v in ("clean", "flipped", "neutral")}
49    print(f"{batch.template}\n"
50          f"  clean    B - A = {ld['clean'].mean():+.2f}, above 0 in {100 * (ld['clean'] > 0).float().mean():.0f}%, "
51          f"B is the top token in {100 * entry['clean']['top_is_B']:.0f}%, p(B) = {torch.tensor(entry['clean']['p_B']).mean():.2f}\n"
52          f"  flipped  B - A = {ld['flipped'].mean():+.2f}, below 0 in {100 * (ld['flipped'] < 0).float().mean():.0f}%, "
53          f"A is the top token in {100 * entry['flipped']['top_is_A']:.0f}%\n"
54          f"  neutral  B - A = {ld['neutral'].mean():+.2f}; top tokens {entry['neutral']['top_tokens'][:3]}")
55
56save(f"runs/read/behavior-{meta['name']}.json", result)