tests/test_open.py
60 lines · View on GitHub · Linked from 8. Limits
1"""
2Step 8 runs the same code on a published model. Before any measurement on
3it, the identities that every earlier step rests on are checked there: a
4model read from published weights has attention biases and grouped keys
5that the story model lacks.
6"""
7
8import pytest
9import torch
10
11from conftest import needs_open
12from minterp import attention, lens, tasks
13from minterp.common import load
14from minterp.tap import Tap, head_writes
15
16
17@pytest.fixture(scope="module")
18def recorded():
19 model, tok, _ = load("qwen")
20 batch = tasks.batches(tok, next(model.parameters()).device, people=tasks.names(tok))[0]
21 with torch.no_grad(), Tap(model) as tap:
22 logits, _ = model(batch.clean)
23 return model, batch, tap, logits
24
25
26@needs_open
27def test_the_names_are_single_tokens_there_too(recorded):
28 _, batch, _, _ = recorded
29 assert len(batch) == 132
30
31
32@needs_open
33def test_the_recorded_pieces_add_up_to_the_stream(recorded):
34 model, _, tap, _ = recorded
35 n = model.cfg.n_layers
36 total = tap["resid", 0] + sum(tap["attn", i] + tap["mlp", i] for i in range(1, n + 1))
37 size = tap["resid", n].norm(dim=-1).mean()
38 assert (total - tap["resid", n]).abs().max() < 1e-5 * size
39 for i in range(1, n + 1):
40 assert (head_writes(model, tap, i).sum(2) - tap["attn", i]).abs().max() < 1e-5 * size
41
42
43@needs_open
44def test_the_attribution_adds_up_to_the_logit_difference(recorded):
45 model, batch, tap, logits = recorded
46 parts = lens.attribute(model, tap, batch.right, batch.wrong)
47 difference = tasks.logit_diff(logits[:, -1].float(), batch.right, batch.wrong)
48 assert (parts["embed"] + parts["heads"].sum((1, 2)) + parts["mlp"].sum(1) - difference).abs().max() < 1e-3
49
50
51@needs_open
52def test_recomputed_attention_is_the_models_own(recorded):
53 model, _, tap, _ = recorded
54 weights = attention.patterns(model, tap)
55 assert (weights.sum(-1) - 1).abs().max() < 1e-5
56 for i in (1, 12, 24):
57 block = model.layers[i - 1]
58 values = attention.values(model, block.attn, block.attn_norm(tap["resid", i - 1]))
59 again = torch.einsum("bhts,bhsd->bthd", weights[:, i - 1], values)
60 assert (again - tap["heads", i]).abs().max() < 1e-4