-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupplementary.py
58 lines (44 loc) · 1.68 KB
/
supplementary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from functools import partial
import argparse
import time
import einops
import torch
from IPython.display import clear_output
# TransformerLens
from transformer_lens import HookedTransformer, patching
from dataset import *
from mechanistic_utils import *
from plotly_utils import imshow, scatter
import plotly.express as px
# My libraries
from utils import *
torch.set_grad_enabled(False)
device = "cuda" if torch.cuda.is_available() else "cpu"
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=50, help="number of samples to use")
args = parser.parse_args()
model = HookedTransformer.from_pretrained(
"gpt2-small",
center_unembed=True,
center_writing_weights=True,
fold_ln=True,
refactor_factored_attn_matrices=True
)
with open("acronyms_2_common.txt", "r") as f:
prompts, acronyms = list(zip(*[line.split(", ") for line in f.read().splitlines()]))
n = args.n
prompts = prompts[:n]
acronyms = acronyms[:n]
clean_tokens = model.to_tokens(prompts)
answer_tokens = model.to_tokens(acronyms, prepend_bos=False)
clean_logits, clean_cache = model.run_with_cache(clean_tokens)
heads_to_visualize = [[1, 0], [2, 2], [4, 11]]
labels = ["BOS", "The", "C1", "T1", "C2", "T2", "C3", "T3", " (", "A1", "A2"]
mean_attn_patterns = torch.stack(
[clean_cache["pattern", layer][:, head].mean(0) for layer, head in heads_to_visualize],
dim=0)
imshow(
mean_attn_patterns, facet_col=0, facet_labels=[f"{layer}.{head}" for layer, head in heads_to_visualize],
title="Mean Attention Patterns for Fuzzy Previous Heads", width=800, height=400, x=labels, y=labels,
labels={"y": "Destination", "x":"Source"}, save_path="images/supplementary/attn_patterns_fuzzy.pdf"
)