tests/test_steer.py
58 lines · View on GitHub · Linked from 7. Steer
1import torch
2
3from minterp import sae as S
4from minterp import steer, tasks
5from minterp.tap import Tap
6
7
8def test_a_zero_dose_changes_nothing_and_a_dose_adds_exactly_that(model, tok, people):
9 batch = tasks.batches(tok, people=people)[0]
10 torch.manual_seed(0)
11 direction = torch.randn(model.cfg.dim)
12 plain, _ = model(batch.clean)
13 assert torch.allclose(steer.steered(model, batch.clean, 2, direction, [5], 0.0), plain[:, -1], atol=1e-6)
14 with Tap(model, keep=("resid",)) as before:
15 model(batch.clean)
16 with Tap(model, edit={("resid", 2): steer.add(direction, [5], 0.5)}, keep=("resid",)) as after:
17 model(batch.clean)
18 moved = after["resid", 2] - before["resid", 2]
19 assert torch.allclose(moved[:, 5], 0.5 * direction.expand(len(batch), -1), atol=1e-6)
20 assert moved[:, :5].abs().max() == 0 and moved[:, 6:].abs().max() == 0
21
22
23def test_a_direction_per_prompt(model, tok, people):
24 batch = tasks.batches(tok, people=people)[0]
25 torch.manual_seed(0)
26 directions = torch.randn(len(batch), model.cfg.dim)
27 with Tap(model, keep=("resid",)) as before:
28 model(batch.clean)
29 with Tap(model, edit={("resid", 1): steer.add(directions, [3], 2.0)}, keep=("resid",)) as after:
30 model(batch.clean)
31 assert torch.allclose((after["resid", 1] - before["resid", 1])[:, 3], 2.0 * directions, atol=1e-5)
32
33
34def test_the_control_has_the_same_length_and_another_direction():
35 g = torch.Generator().manual_seed(0)
36 d = torch.randn(7, 48)
37 r = steer.like(d, g)
38 assert torch.allclose(r.norm(dim=1), d.norm(dim=1), atol=1e-5)
39 assert torch.nn.functional.cosine_similarity(d, r, dim=1).abs().max() < 0.6
40
41
42def test_elsewhere_ignores_the_tokens_left_out():
43 plain = torch.tensor([[2.0, 1.0, 0.0, -1.0]])
44 changed = plain.clone()
45 changed[0, 0] += 5.0
46 assert steer.elsewhere(plain, changed, torch.tensor([[0]])).abs().max() < 1e-6
47 assert steer.elsewhere(plain, changed, torch.tensor([[3]])).item() > 0.5
48 assert steer.elsewhere(plain, plain, torch.tensor([[0]])).abs().max() < 1e-6
49
50
51def test_a_latent_writes_its_decoder_direction_at_the_streams_scale():
52 torch.manual_seed(0)
53 sae = S.SAE(dim=16, width=40, k=4)
54 sae.scale.fill_(7.0)
55 z = sae.encode(torch.randn(5, 16) * 7.0)
56 more = z.clone()
57 more[:, 11] += 2.5
58 assert torch.allclose(sae.decode(more) - sae.decode(z), 2.5 * steer.writes(sae, 11).expand(5, -1), atol=1e-5)