tests/test_attribute.py
53 lines · View on GitHub · Linked from 6. Feature circuit
1import torch
2
3from minterp import attribute, tasks
4from minterp import sae as S
5from minterp.tap import Tap
6
7
8def setup(model, tok, people):
9 torch.manual_seed(0)
10 batch = tasks.batches(tok, people=people)[0]
11 sae = S.SAE(model.cfg.dim, 4 * model.cfg.dim, k=8).eval().requires_grad_(False)
12 measure = lambda logits, tap: tasks.logit_diff(logits, batch.right, batch.wrong)
13 return batch, sae, measure
14
15
16def test_the_dictionary_in_the_pass_changes_nothing(model, tok, people):
17 batch, sae, measure = setup(model, tok, people)
18 plain, _ = model(batch.clean)
19 z, err, gz, gerr, value = attribute.through(model, sae, batch.clean, 2, measure)
20 assert torch.allclose(value, tasks.logit_diff(plain, batch.right, batch.wrong), atol=1e-4)
21 assert z.shape == (len(batch), batch.clean.shape[1], sae.width) and gz.shape == z.shape and gerr.shape == err.shape
22 with Tap(model, edit={("resid", 2): attribute.rebuilt_with(sae, z, err)}, keep=()):
23 again, _ = model(batch.clean)
24 assert torch.allclose(again, plain, atol=1e-4)
25 assert not any(p.requires_grad for p in model.parameters())
26
27
28def test_the_gradient_predicts_a_small_change_and_not_exactly_a_large_one(model, tok, people):
29 batch, sae, measure = setup(model, tok, people)
30 z, err, gz, gerr, value = attribute.through(model, sae, batch.clean, 2, measure)
31 torch.manual_seed(1)
32 with Tap(model, keep=("resid",)) as tap:
33 model(batch.clean)
34 step = torch.randn_like(err) * tap["resid", 2].std()
35
36 def moved(size):
37 with torch.no_grad(), Tap(model, edit={("resid", 2): attribute.rebuilt_with(sae, z, err + size * step)}, keep=()):
38 logits, _ = model(batch.clean)
39 return tasks.logit_diff(logits, batch.right, batch.wrong) - value
40
41 predicted = (step * gerr).sum((1, 2))
42 small, large = moved(1e-3) / 1e-3, moved(1.0)
43 off_small, off_large = (small - predicted).abs().mean(), (large - predicted).abs().mean()
44 assert off_small < 0.1 * predicted.abs().mean()
45 assert off_large > 5 * off_small
46
47
48def test_a_position_cannot_matter_to_an_earlier_one(model, tok, people):
49 batch, sae, _ = setup(model, tok, people)
50 early = lambda logits, tap: logits[:, 3, 0]
51 _, _, gz, gerr, _ = attribute.through(model, sae, batch.clean, 2, early)
52 assert gerr[:, 4:].abs().max() == 0 and gz[:, 4:].abs().max() == 0
53 assert gerr[:, :4].abs().max() > 0