minterp

tests/test_attention.py

21 lines · View on GitHub · Linked from 4. Circuit

1import torch
2
3from minterp import attention
4from minterp.tap import Tap
5
6
7def test_the_weights_rebuild_what_the_attention_wrote(model):
8    torch.manual_seed(2)
9    tokens = torch.randint(0, model.cfg.vocab_size, (3, 14))
10    with Tap(model) as tap:
11        model(tokens)
12    w = attention.patterns(model, tap)  # (batch, blocks, heads, seq, seq)
13    assert w.shape == (3, model.cfg.n_layers, model.cfg.n_heads, 14, 14)
14    assert torch.allclose(w.sum(-1), torch.ones(3, model.cfg.n_layers, model.cfg.n_heads, 14), atol=1e-5)
15    assert (w.triu(1) == 0).all()  # no position reads from a later one
16    for i, block in enumerate(model.layers, start=1):
17        x = block.attn_norm(tap["resid", i - 1])
18        v = attention.values(model, block.attn, x)  # (batch, heads, seq, head_dim)
19        heads = (w[:, i - 1] @ v).transpose(1, 2)  # (batch, seq, heads, head_dim): each head's weighted sum
20        assert torch.allclose(heads, tap["heads", i], atol=1e-5)
21        assert torch.allclose(block.attn.o_proj(heads.flatten(-2)), tap["attn", i], atol=1e-5)