-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivation_patch.py
147 lines (121 loc) · 4.62 KB
/
activation_patch.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Code to reproduce activation patching of residual stream of GPT2-Small on IOI task
"""
# %%
from transformer_lens import HookedTransformer
from transformer_lens import patching
from rich import print as rprint
from rich.table import Table
import torch as t
from torch import Tensor
from jaxtyping import Float, Int
from attribution_patching.plotly_utils import imshow
# %%
model = HookedTransformer.from_pretrained(
"gpt2-small",
center_unembed=True,
center_writing_weights=True,
fold_ln=True,
refactor_factored_attn_matrices=True,
)
# %%
prompt_format = [
"When John and Mary went to the shops,{} gave the bag to",
"When Tom and James went to the park,{} gave the ball to",
"When Dan and Sid went to the shops,{} gave an apple to",
"After Martin and Amy went to the park,{} gave a drink to",
]
name_pairs = [
(" John", " Mary"),
(" Tom", " James"),
(" Dan", " Sid"),
(" Martin", " Amy"),
]
# Define 8 prompts, in 4 groups of 2 (with adjacent prompts having answers swapped)
prompts = [
prompt.format(name)
for (prompt, names) in zip(prompt_format, name_pairs) for name in names[::-1]
]
# Define the answers for each prompt, in the form (correct, incorrect)
answers = [names[::i] for names in name_pairs for i in (1, -1)]
# Define the answer tokens (same shape as the answers)
answer_tokens = t.concat([
model.to_tokens(names, prepend_bos=False).T for names in answers
])
rprint(prompts)
rprint(answers)
rprint(answer_tokens)
table = Table("Prompt", "Correct", "Incorrect", title="Prompts & Answers:")
for prompt, answer in zip(prompts, answers):
table.add_row(prompt, repr(answer[0]), repr(answer[1]))
rprint(table)
# %%
tokens = model.to_tokens(prompts, prepend_bos=True)
# Move the tokens to the GPU
tokens = tokens.to(model.cfg.device)
# Run the model and cache all activations
# original_logits, cache = model.run_with_cache(tokens)
clean_tokens = tokens
# Swap each adjacent pair to get corrupted tokens
indices = [i+1 if i % 2 == 0 else i-1 for i in range(len(tokens))]
corrupted_tokens = clean_tokens[indices]
# %%
def logits_to_ave_logit_diff(
logits: Float[Tensor, "batch seq d_vocab"],
answer_tokens: Float[Tensor, "batch 2"] = answer_tokens,
per_prompt: bool = False
):
'''
Returns logit difference between the correct and incorrect answer.
If per_prompt=True, return the array of differences rather than the average.
'''
# SOLUTION
# Only the final logits are relevant for the answer
final_logits: Float[Tensor, "batch d_vocab"] = logits[:, -1, :]
# Get the logits corresponding to the indirect object / subject tokens respectively
answer_logits: Float[Tensor, "batch 2"] = final_logits.gather(dim=-1, index=answer_tokens)
# Find logit difference
correct_logits, incorrect_logits = answer_logits.unbind(dim=-1)
answer_logit_diff = correct_logits - incorrect_logits
return answer_logit_diff if per_prompt else answer_logit_diff.mean()
# %%
clean_tokens = tokens
# Swap each adjacent pair to get corrupted tokens
indices = [i+1 if i % 2 == 0 else i-1 for i in range(len(tokens))]
corrupted_tokens = clean_tokens[indices]
print(
"Clean string 0: ", model.to_string(clean_tokens[0]), "\n"
"Corrupted string 0:", model.to_string(corrupted_tokens[0])
)
clean_logits, clean_cache = model.run_with_cache(clean_tokens)
corrupted_logits, corrupted_cache = model.run_with_cache(corrupted_tokens)
clean_logit_diff = logits_to_ave_logit_diff(clean_logits, answer_tokens)
print(f"Clean logit diff: {clean_logit_diff:.4f}")
corrupted_logit_diff = logits_to_ave_logit_diff(corrupted_logits, answer_tokens)
print(f"Corrupted logit diff: {corrupted_logit_diff:.4f}")
# %%
def ioi_metric(
logits: Float[Tensor, "batch seq d_vocab"],
answer_tokens: Float[Tensor, "batch 2"] = answer_tokens,
corrupted_logit_diff: float = corrupted_logit_diff,
clean_logit_diff: float = clean_logit_diff,
) -> Float[Tensor, ""]:
'''
Linear function of logit diff, calibrated so that it equals 0 when performance is
same as on corrupted input, and 1 when performance is same as on clean input.
'''
# SOLUTION
patched_logit_diff = logits_to_ave_logit_diff(logits, answer_tokens)
return (patched_logit_diff - corrupted_logit_diff) / (clean_logit_diff - corrupted_logit_diff)
# %%
act_patch_resid_pre = patching.get_act_patch_resid_pre(model, corrupted_tokens, clean_cache, ioi_metric)
# %%
labels = [f"{tok} {i}" for i, tok in enumerate(model.to_str_tokens(clean_tokens[0]))]
imshow(
act_patch_resid_pre,
labels={"x": "Position", "y": "Layer"},
x=labels,
title="resid_pre Activation Patching",
width=600
)
# %%