tests/test_tap.py
88 lines · View on GitHub · Linked from 1. Read
1import pytest
2import torch
3
4from minterp.tap import Tap, head_writes
5
6
7@pytest.fixture
8def tokens(model):
9 torch.manual_seed(1)
10 return torch.randint(0, model.cfg.vocab_size, (3, 12))
11
12
13def n_hooks(model):
14 return sum(len(m._forward_hooks) + len(m._forward_pre_hooks) for m in model.modules())
15
16
17def test_reading_changes_nothing_and_leaves_no_hooks(model, tokens):
18 plain, _ = model(tokens)
19 with Tap(model) as tap:
20 assert n_hooks(model) > 0
21 seen, _ = model(tokens)
22 assert torch.equal(plain, seen)
23 assert n_hooks(model) == 0
24 assert tap["resid", 0].shape == (3, 12, model.cfg.dim)
25 assert tap["heads", 1].shape == (3, 12, model.cfg.n_heads, model.cfg.dim // model.cfg.n_heads)
26
27
28def test_the_stream_is_the_sum_of_what_was_written(model, tokens):
29 with Tap(model) as tap:
30 logits, _ = model(tokens)
31 for i in range(1, model.cfg.n_layers + 1):
32 rebuilt = tap["resid", i - 1] + tap["attn", i] + tap["mlp", i]
33 assert torch.allclose(rebuilt, tap["resid", i], atol=1e-5)
34 final = tap["resid", model.cfg.n_layers]
35 assert torch.allclose(model.lm_head(model.norm(final)), logits, atol=1e-5)
36
37
38def test_the_heads_sum_to_the_attention(model, tokens):
39 with Tap(model) as tap:
40 model(tokens)
41 for i in range(1, model.cfg.n_layers + 1):
42 writes = head_writes(model, tap, i)
43 assert writes.shape == (3, 12, model.cfg.n_heads, model.cfg.dim)
44 assert torch.allclose(writes.sum(2), tap["attn", i], atol=1e-5)
45
46
47def test_an_edit_reaches_everything_after_it_and_nothing_before(model, tokens):
48 plain, _ = model(tokens)
49
50 def at_position_5(x):
51 x = x.clone()
52 x[:, 5] = 0
53 return x
54
55 with Tap(model, edit={("resid", 1): at_position_5}) as tap:
56 changed, _ = model(tokens)
57 assert torch.equal(tap["resid", 1][:, 5], torch.zeros(3, model.cfg.dim))
58 assert torch.allclose(changed[:, :5], plain[:, :5], atol=1e-6)
59 assert not torch.allclose(changed[:, 5:], plain[:, 5:], atol=1e-3)
60 with Tap(model, edit={("resid", 1): lambda x: x}):
61 same, _ = model(tokens)
62 assert torch.equal(same, plain)
63
64
65def test_editing_heads_changes_what_the_attention_writes(model, tokens):
66 with Tap(model, edit={("heads", 2): torch.zeros_like}) as tap:
67 model(tokens)
68 assert torch.equal(tap["attn", 2], torch.zeros_like(tap["attn", 2]))
69
70 def without_head_0(y):
71 y = y.clone()
72 y[:, :, 0] = 0
73 return y
74
75 with Tap(model) as plain:
76 model(tokens)
77 with Tap(model, edit={("heads", 2): without_head_0}) as cut:
78 model(tokens)
79 removed = plain["attn", 2] - cut["attn", 2]
80 assert torch.allclose(removed, head_writes(model, plain, 2)[:, :, 0], atol=1e-5)
81
82
83def test_keep_limits_what_is_stored(model, tokens):
84 with Tap(model, keep=("resid",)) as tap:
85 model(tokens)
86 assert {k for k, _ in tap.store} == {"resid"}
87 with pytest.raises(AssertionError):
88 Tap(model, edit={("attn", 0): lambda x: x})