minterp

tests/test_sae.py

59 lines · View on GitHub · Linked from 5. Features

1import torch
2
3from minterp import sae as S
4
5
6def planted(rows=20000, dim=24, directions=60, active=3, seed=0):
7    """Vectors that really are sums of a few unit directions out of many more than there are dimensions."""
8    g = torch.Generator().manual_seed(seed)
9    D = torch.randn(directions, dim, generator=g)
10    D = D / D.norm(dim=1, keepdim=True)
11    z = torch.zeros(rows, directions)
12    for r in range(active):
13        idx = torch.randint(0, directions, (rows,), generator=g)
14        z[torch.arange(rows), idx] = 1 + torch.rand(rows, generator=g)
15    return z @ D + 5.0, D  # an offset, so that the bias has something to do
16
17
18def test_at_most_k_latents_are_active_and_none_is_negative():
19    torch.manual_seed(0)
20    sae = S.SAE(dim=16, width=64, k=5)
21    z = sae.encode(torch.randn(10, 16))
22    assert z.shape == (10, 64) and (z >= 0).all() and ((z > 0).sum(1) <= 5).all()
23    rebuilt, again = sae(torch.randn(10, 16))
24    assert rebuilt.shape == (10, 16) and again.shape == (10, 64)
25
26
27def test_it_finds_directions_that_were_planted():
28    x, D = planted()
29    sae = S.train(x[:18000], width=120, k=3, steps=1500, batch=1024, lr=2e-3)
30    assert S.unexplained(sae, x[18000:]) < 0.1  # on vectors it did not train on
31    assert torch.allclose(sae.dec.norm(dim=1), torch.ones(120), atol=1e-5)
32    # Most planted directions have a dictionary direction close to them, though the dimension is 24 and they are 60.
33    closest = (D @ sae.dec.T).max(1).values
34    assert (closest > 0.9).float().mean() > 0.8
35
36
37def test_the_scale_makes_it_blind_to_the_size_of_the_vectors():
38    x, _ = planted(rows=6000)
39    small = S.train(x, width=120, k=3, steps=300, batch=1024, lr=2e-3)
40    large = S.train(60 * x, width=120, k=3, steps=300, batch=1024, lr=2e-3)
41    assert abs(large.scale / small.scale - 60) < 1e-3
42    assert abs(S.unexplained(small, x) - S.unexplained(large, 60 * x)) < 1e-3
43    assert torch.allclose(small.encode(x[:50]), large.encode(60 * x[:50]), atol=1e-3)
44
45
46def test_save_and_load(tmp_path):
47    x, _ = planted(rows=3000)
48    sae = S.train(x, width=120, k=3, steps=50, batch=512)
49    S.save(sae, tmp_path / "sae.pt")
50    again = S.load(tmp_path / "sae.pt")
51    assert again.k == 3 and again.width == 120
52    assert torch.equal(sae(x[:20])[0], again(x[:20])[0])
53
54
55def test_two_runs_with_one_seed_agree():
56    x, _ = planted(rows=3000)
57    a = S.train(x, width=120, k=3, steps=40, batch=512, seed=3)
58    b = S.train(x, width=120, k=3, steps=40, batch=512, seed=3)
59    assert torch.equal(a.dec, b.dec)