tests/test_read.py
139 lines · View on GitHub · Linked from 1. Read
1"""
2What docs/01-read.md claims, checked against the record under runs/read/
3and, where the story model is present, against the model itself. If a run
4is repeated and a number moves, these fail and the note has to follow.
5"""
6
7import json
8
9import numpy as np
10import pytest
11import torch
12
13from conftest import needs_model
14from minterp import lens, tasks
15from minterp.common import eot, load
16from minterp.tap import Tap
17
18VERSIONS = ("clean", "flipped", "neutral")
19
20
21def pooled(seed):
22 with open(f"runs/read/behavior-{seed}.json") as f:
23 run = json.load(f)
24 return {v: np.concatenate([np.array(t[v]["logit_diff"]) for t in run["templates"]]) for v in VERSIONS}, run
25
26
27def attribution(seed):
28 with open(f"runs/read/lens-{seed}.json") as f:
29 return json.load(f)
30
31
32@pytest.mark.parametrize("seed, clean, flipped, medians, neutral, swing", [
33 ("seed0", 99.8, 84.5, (7.42, -4.03), (1.84, 59.4), (11.84, 99.8)),
34 ("seed1", 95.5, 75.8, (5.36, -2.79), (1.28, 62.7), (7.49, 99.2)),
35])
36def test_both_models_do_the_task(seed, clean, flipped, medians, neutral, swing):
37 ld, run = pooled(seed)
38 assert len(ld["clean"]) == 660 and len(run["names"]) == 12
39 assert round(100 * (ld["clean"] > 0).mean(), 1) == clean
40 assert round(100 * (ld["flipped"] < 0).mean(), 1) == flipped
41 assert (round(np.median(ld["clean"]), 2), round(np.median(ld["flipped"]), 2)) == medians
42
43 assert (round(ld["neutral"].mean(), 2), round(100 * (ld["neutral"] > 0).mean(), 1)) == neutral
44 paired = ld["clean"] - ld["flipped"]
45 assert (round(paired.mean(), 2), round(100 * (paired > 0).mean(), 1)) == swing
46
47
48@pytest.mark.parametrize("seed, clean, flipped", [("seed0", (99, 100), (82, 88)), ("seed1", (86, 99), (67, 84))])
49def test_every_pattern_is_done_in_most_prompts(seed, clean, flipped):
50 _, run = pooled(seed)
51 c = [round(100 * (np.array(t["clean"]["logit_diff"]) > 0).mean()) for t in run["templates"]]
52 f = [round(100 * (np.array(t["flipped"]["logit_diff"]) < 0).mean()) for t in run["templates"]]
53 assert (min(c), max(c)) == clean and (min(f), max(f)) == flipped
54
55
56def test_a_logit_difference_is_not_the_top_token():
57 _, run = pooled("seed0")
58 entry = next(t for t in run["templates"] if t["template"].endswith("showed the map to"))
59 assert round(100 * (np.array(entry["clean"]["logit_diff"]) > 0).mean()) == 99
60 assert round(100 * entry["clean"]["top_is_B"]) == 17
61
62
63def blocks(seed, version):
64 """What each block writes into B minus A, mean over the prompts: (blocks,)."""
65 a = attribution(seed)["attribution"][version]
66 return np.array(a["heads"]["mean"]).sum(1) + np.array(a["mlp"]["mean"])
67
68
69@pytest.mark.parametrize("seed, total, late, early", [("seed0", 7.90, 7.42, 0.48), ("seed1", 5.25, 4.74, 0.51)])
70def test_the_last_two_blocks_write_most_of_the_clean_difference(seed, total, late, early):
71 b = blocks(seed, "clean")
72 embed = attribution(seed)["attribution"]["clean"]["embed"]["mean"]
73 assert b.sum() + embed == pytest.approx(total, abs=0.011)
74 assert b[6:].sum() == pytest.approx(late, abs=0.011) and b[:6].sum() == pytest.approx(early, abs=0.011)
75
76
77def test_the_models_divide_the_work_differently():
78 by = lambda seed, block: [round(float(blocks(seed, v)[block - 1]), 2) for v in VERSIONS]
79
80 assert by("seed1", 2) == [0.45, 0.52, 0.50] and by("seed1", 7) == [1.45, 1.08, 1.17]
81 assert by("seed1", 8) == [3.29, -3.75, -0.37]
82
83 assert by("seed0", 7) == [2.42, -2.36, -0.0] and by("seed0", 8) == [5.00, -1.69, 1.53]
84 assert by("seed0", 3) == [0.49, 0.04, 0.28]
85
86
87def test_single_parts_in_the_three_versions():
88 a0, a1 = attribution("seed0")["attribution"], attribution("seed1")["attribution"]
89 head = lambda a, block, h: [round(a[v]["heads"]["mean"][block - 1][h], 2) for v in VERSIONS]
90 mlp = lambda a, block: [round(a[v]["mlp"]["mean"][block - 1], 2) for v in VERSIONS]
91 assert head(a0, 7, 0) == [1.79, -2.08, -0.12] and head(a1, 8, 0) == [1.85, -1.95, -0.10]
92 assert head(a0, 8, 4) == [2.57, -1.23, 0.56] and head(a0, 8, 3) == [1.09, 0.06, 0.57]
93 assert mlp(a0, 8) == [1.18, -0.51, 0.32] and mlp(a1, 8) == [1.45, -0.52, 0.37]
94 assert head(a1, 7, 1) == [1.07, 0.87, 0.91] and head(a1, 2, 0) == [0.37, 0.39, 0.39]
95 assert head(a1, 8, 5) == [-0.53, -0.56, -0.53]
96 q = a1["clean"]["heads"]
97 assert (round(q["q25"][7][5], 2), round(q["q75"][7][5], 2)) == (-1.12, 0.13)
98
99
100def lens_of(seed, version):
101 run = attribution(seed)
102 return np.concatenate([np.array(t["lens"][version]) for t in run["templates"]], axis=1)
103
104
105def test_the_first_step_differs_between_the_models():
106 med = lambda seed, version, depth: round(float(np.median(lens_of(seed, version)[depth])), 2)
107 assert [med("seed0", v, 3) for v in VERSIONS] == [1.38, 0.12, 0.79]
108 assert [med("seed1", v, 2) for v in VERSIONS] == [1.58, 1.91, 1.77]
109 assert [med("seed0", v, 8) for v in VERSIONS] == [7.42, -4.03, 1.33]
110 assert [med("seed1", v, 7) for v in VERSIONS] == [2.79, 2.03, 2.33] and med("seed1", "flipped", 8) == -2.79
111
112
113def test_early_on_the_stream_leans_to_the_name_that_acts():
114 above = lambda seed, depth: round(100 * float((lens_of(seed, "clean")[depth] > lens_of(seed, "flipped")[depth]).mean()))
115 assert [above("seed1", d) for d in (2, 3, 4, 5)] == [12, 1, 2, 17]
116 assert above("seed0", 2) == 7 and above("seed0", 3) == 92
117
118
119def test_the_stream_grows_with_depth():
120 size = np.mean([t["size"] for t in attribution("seed0")["templates"]], axis=0)
121 assert [round(float(size[d]), 2) for d in (0, 3, 8)] == [0.05, 1.10, 3.46]
122
123
124@needs_model
125def test_on_the_story_model_the_split_is_exact_and_the_task_is_done():
126 model, tok, _ = load("seed0", device="cpu")
127 batch = tasks.batches(tok)[0]
128 with Tap(model) as tap:
129 logits, _ = model(batch.clean)
130 final = tasks.logit_diff(logits, batch.right, batch.wrong)
131 parts = lens.attribute(model, tap, batch.right, batch.wrong)
132 total = parts["embed"] + parts["heads"].sum((1, 2)) + parts["mlp"].sum(1)
133 assert torch.allclose(total, final, atol=1e-3)
134 assert (final > 0).float().mean() > 0.9
135 assert len(tasks.names(tok)) == 12
136
137 ids = torch.tensor([[eot(tok)] + tok.encode_ordinary("One day, Lily and Tom were playing in the garden. Lily said hello to")])
138 probs = model(ids)[0][0, -1].softmax(-1)
139 assert tok.decode([probs.argmax().item()]) == " Tom" and round(probs.max().item(), 2) == 0.99