-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcausal_search.py
376 lines (319 loc) · 17.4 KB
/
causal_search.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import zstandard as zstd
import json
import os
import io
import random
import argparse
from tqdm import tqdm
from nnsight import LanguageModel
from dictionary_learning.buffer import ActivationBuffer
from dictionary_learning.dictionary import AutoEncoder
from dictionary_learning.training import trainSAE
from datasets import load_dataset
from einops import rearrange
import torch as t
def load_examples(dataset, num_examples, model, seed=12):
examples = []
dataset_items = open(dataset).readlines()
random.seed(seed)
random.shuffle(dataset_items)
for line in dataset_items:
data = json.loads(line)
clean_prefix = model.tokenizer(data["clean_prefix"], return_tensors="pt",
padding=False).input_ids
patch_prefix = model.tokenizer(data["patch_prefix"], return_tensors="pt",
padding=False).input_ids
clean_answer = model.tokenizer(data["clean_answer"], return_tensors="pt",
padding=False).input_ids
patch_answer = model.tokenizer(data["patch_answer"], return_tensors="pt",
padding=False).input_ids
if clean_prefix.shape[1] != patch_prefix.shape[1]:
continue
if clean_answer.shape[1] != 1 or patch_answer.shape[1] != 1:
continue
example_dict = {"clean_prefix": clean_prefix, "patch_prefix": patch_prefix,
"clean_answer": clean_answer.item(), "patch_answer": patch_answer.item()}
examples.append(example_dict)
if len(examples) >= num_examples:
break
return examples
def compare_probs(logits, example_dict):
last_token_logits = logits[:,-1,:]
probs = last_token_logits.softmax(dim=-1)
prob_ratio = t.divide(probs[0][example_dict["clean_answer"]],
probs[0][example_dict["patch_answer"]])
return prob_ratio
def get_dictionary_activation_caches(model, submodule, autoencoder,
example_dict):
clean_input = example_dict["clean_prefix"]
patch_input = example_dict["patch_prefix"]
# 1 clean forward, 1 patch forward
with model.generate(max_new_tokens=1, pad_token_id=model.tokenizer.pad_token_id) as generator:
with generator.invoke(clean_input, scan=False) as invoker:
clean_hidden_states = submodule.output.save()
clean_dictionary_activations = autoencoder.encode(clean_hidden_states.value.to("cuda"))
with model.generate(max_new_tokens=1, pad_token_id=model.tokenizer.pad_token_id) as generator:
with generator.invoke(patch_input, scan=False) as invoker:
patch_hidden_states = submodule.output.save()
patch_dictionary_activations = autoencoder.encode(patch_hidden_states.value.to("cuda"))
return {"clean_activations": clean_dictionary_activations,
"patch_activations": patch_dictionary_activations}
def get_submodule_activation_caches(model, submodule,
example_dict):
clean_input = example_dict["clean_prefix"]
patch_input = example_dict["patch_prefix"]
# 1 clean forward, 1 patch forward
with model.generate(max_new_tokens=1, pad_token_id=model.tokenizer.pad_token_id) as generator:
with generator.invoke(clean_input, scan=False) as invoker:
clean_hidden_states = submodule.output.save()
with model.generate(max_new_tokens=1, pad_token_id=model.tokenizer.pad_token_id) as generator:
with generator.invoke(patch_input, scan=False) as invoker:
patch_hidden_states = submodule.output.save()
return {"clean_activations": clean_hidden_states.value,
"patch_activations": patch_hidden_states.value}
def get_forward_and_backward_caches(model, submodule, autoencoder,
example_dict, metric_fn=compare_probs):
# corrupted forward pass
with model.invoke(example_dict["patch_prefix"],
fwd_args = {"inference": False}) as invoker_patch:
x = submodule.output
f = autoencoder.encode(x)
patch_f_saved = f.save()
x_hat = autoencoder.decode(f)
submodule.output = x_hat
# clean forward passes
with model.invoke(example_dict["clean_prefix"],
fwd_args = {"inference": False}) as invoker_clean:
x = submodule.output
f = autoencoder.encode(x)
clean_f_saved = f.save()
x_hat = autoencoder.decode(f)
submodule.output = x_hat
# clean backward pass
clean_f_saved.value.retain_grad()
logits = invoker_clean.output.logits
metric = metric_fn(logits, example_dict)
metric.backward()
return {"clean_activations": clean_f_saved.value,
"patch_activations": patch_f_saved.value,
"clean_gradients": clean_f_saved.value.grad}
def get_forward_and_backward_caches_neurons(model, submodule, example_dict,
metric_fn=compare_probs):
# corrupted forward pass
with model.invoke(example_dict["patch_prefix"],
fwd_args = {"inference": False}) as invoker_patch:
x = submodule.output
clean_x_saved = x.save()
# clean forward passes
with model.invoke(example_dict["clean_prefix"],
fwd_args = {"inference": False}) as invoker_clean:
x = submodule.output
patch_x_saved = x.save()
# clean backward pass
clean_x_saved.value.retain_grad()
logits = invoker_clean.output.logits
metric = metric_fn(logits, example_dict)
metric.backward()
return {"clean_activations": clean_x_saved.value,
"patch_activations": patch_x_saved.value,
"clean_gradients": clean_x_saved.value.grad}
def get_forward_and_backward_caches_wrt_features(model, submodule_lower, submodule_upper,
autoencoder_lower, autoencoder_upper, feat_idx_upper,
example_dict):
# corrupted forward pass
with model.invoke(example_dict["patch_prefix"],
fwd_args = {"inference": False}) as invoker_patch:
x = submodule_lower.output
f = autoencoder_lower.encode(x)
patch_f_saved = f.save()
x_hat = autoencoder_lower.decode(f)
submodule_lower.output = x_hat
# clean forward pass
upper_autoencoder_acts = None
with model.invoke(example_dict["clean_prefix"],
fwd_args = {"inference": False}) as invoker_clean:
x = submodule_lower.output
f = autoencoder_lower.encode(x)
clean_f_saved = f.save()
x_hat = autoencoder_lower.decode(f)
submodule_lower.output = x_hat
y = submodule_upper.output
g = autoencoder_upper.encode(y)
clean_g_saved = g.save()
# clean backward pass
clean_f_saved.value.retain_grad()
clean_feat_activation = clean_g_saved[:, -1, feat_idx_upper]
clean_feat_activation.backward()
return {"clean_activations": clean_f_saved.value,
"patch_activations": patch_f_saved.value,
"clean_gradients": clean_f_saved.value.grad}
def search_dictionary_for_phenomenon(model, submodule, autoencoder, dataset,
num_return_features=10,
num_examples=100):
examples = load_examples(dataset, num_examples, model)
print(f"Number of valid examples: {len(examples)}")
num_features = autoencoder.dict_size
indirect_effects = t.zeros(len(examples), num_features)
for example_idx, example in tqdm(enumerate(examples), desc="Example", total=len(examples)):
dictionary_activations = get_dictionary_activation_caches(model, submodule, autoencoder,
example["clean_prefix"], example["patch_prefix"])
# get clean logits
with model.forward(example["clean_prefix"]) as invoker_clean:
acts = dictionary_activations["clean_activations"]
clean_reconstructed_activations = autoencoder.decode(acts)
submodule.output = clean_reconstructed_activations
logits = invoker_clean.output.logits
logits = logits[:,-1,:] # logits at final token
clean_probs = logits.softmax(dim=-1)
y_clean = clean_probs[0][example["patch_answer"]].item() / clean_probs[0][example["clean_answer"]].item()
# get logits with single patched feature
for feature_idx in tqdm(range(0, num_features), leave=False, desc="Feature"):
with model.forward(example["clean_prefix"]) as invoker_patch:
# patch single feature in dictionary
acts = dictionary_activations["clean_activations"]
acts[:, -1, feature_idx] = dictionary_activations["patch_activations"][:, -1, feature_idx]
patch_reconstructed_activations = autoencoder.decode(acts)
submodule.output = patch_reconstructed_activations
logits = invoker_patch.output.logits
logits = logits[:,-1,:] # logits at final token
patch_probs = logits.softmax(dim=-1)
y_patch = patch_probs[0][example["patch_answer"]].item() / patch_probs[0][example["clean_answer"]].item()
indirect_effects[example_idx, feature_idx] = (y_patch - y_clean) / y_clean
# take mean across examples
indirect_effects = t.mean(indirect_effects, dim=0)
return indirect_effects
# returns list of tuples of type (indirect_effect, feature_index)
# return t.topk(indirect_effects, num_return_features)
def search_submodule_for_phenomenon(model, submodule, dataset,
num_return_features=10,
num_examples=100):
examples = load_examples(dataset, num_examples, model)
print(f"Number of valid examples: {len(examples)}")
num_features = submodule.out_features
indirect_effects = t.zeros(len(examples), num_features)
for example_idx, example in tqdm(enumerate(examples), desc="Example", total=len(examples)):
dictionary_activations = get_submodule_activation_caches(model, submodule,
example["clean_prefix"], example["patch_prefix"])
# get clean logits
with model.forward(example["clean_prefix"]) as invoker_clean:
pass # no interventions necessary
logits = invoker_clean.output.logits
logits = logits[:,-1,:] # logits at final token
clean_probs = logits.softmax(dim=-1)
y_clean = clean_probs[0][example["patch_answer"]].item() / clean_probs[0][example["clean_answer"]].item()
# get logits with single patched feature
for feature_idx in tqdm(range(0, num_features), leave=False, desc="Feature"):
with model.forward(example["clean_prefix"]) as invoker_patch:
# patch single feature in dictionary
acts = submodule.output
acts[:, -1, feature_idx] = dictionary_activations["patch_activations"][:, -1, feature_idx]
submodule.output = acts
logits = invoker_patch.output.logits
logits = logits[:,-1,:] # logits at final token
patch_probs = logits.softmax(dim=-1)
y_patch = patch_probs[0][example["patch_answer"]].item() / patch_probs[0][example["clean_answer"]].item()
indirect_effects[example_idx, feature_idx] = (y_patch - y_clean) / y_clean
# take mean across examples
indirect_effects = t.mean(indirect_effects, dim=0)
return indirect_effects
# returns list of tuples of type (indirect_effect, feature_index)
# return t.topk(indirect_effects, num_return_features)
def attribution_patching(model, submodule, autoencoder, examples, metric_fn=compare_probs):
num_features = autoencoder.dict_size
indirect_effects = t.zeros(len(examples), num_features)
for example_idx, example in tqdm(enumerate(examples), desc="Example", leave=False, total=len(examples)):
# get forward and backward patches
activations_gradients = get_forward_and_backward_caches(model, submodule,
autoencoder, example,
metric_fn=metric_fn)
act_clean = activations_gradients["clean_activations"][:,-1]
act_patch = activations_gradients["patch_activations"][:,-1]
grad_clean = activations_gradients["clean_gradients"][:,-1]
indirect_effects[example_idx] = grad_clean * (act_patch - act_clean)
indirect_effects = t.mean(indirect_effects, dim=0)
return indirect_effects
def attribution_patching_neurons(model, submodule, examples):
# examples = load_examples(dataset, num_examples, model)
print(f"Number of valid examples: {len(examples)}")
num_features = submodule.out_features
indirect_effects = t.zeros(len(examples), num_features)
for example_idx, example in tqdm(enumerate(examples), desc="Example", total=len(examples)):
# get forward and backward patches
activations_gradients = get_forward_and_backward_caches_neurons(model, submodule,
example)
act_clean = activations_gradients["clean_activations"][:,-1]
act_patch = activations_gradients["patch_activations"][:,-1]
grad_clean = activations_gradients["clean_gradients"][:,-1]
indirect_effects[example_idx] = grad_clean * (act_patch - act_clean)
indirect_effects = t.mean(indirect_effects, dim=0)
return indirect_effects
def attribution_patching_wrt_features(model, submodule_lower, submodule_upper,
autoencoder_lower, autoencoder_upper, feat_idx_upper,
examples):
num_features_lower = autoencoder_lower.dict_size
indirect_effects = t.zeros(len(examples), num_features_lower)
for example_idx, example in tqdm(enumerate(examples), desc="Example", leave=False, total=len(examples)):
# get forward and backward patches
activations_gradients = get_forward_and_backward_caches_wrt_features(model, submodule_lower, submodule_upper,
autoencoder_lower, autoencoder_upper, feat_idx_upper,
example)
act_clean = activations_gradients["clean_activations"][:,-1]
act_patch = activations_gradients["patch_activations"][:,-1]
grad_clean = activations_gradients["clean_gradients"][:,-1]
indirect_effects[example_idx] = grad_clean * (act_patch - act_clean)
indirect_effects = t.mean(indirect_effects, dim=0)
return indirect_effects
def load_submodule(model, submodule_str):
if "." not in submodule_str:
return getattr(model, submodule_str)
submodules = submodule_str.split(".")
curr_module = None
for module in submodules:
if module == "model":
continue
if not curr_module:
curr_module = getattr(model, module)
continue
curr_module = getattr(curr_module, module)
return curr_module
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--autoencoders", "-a", type=str,
default='autoencoders/ae_mlp3_c4_lr0.0001_resample25000_dict32768.pt')
parser.add_argument("--models", "-m", type=str,
default='autoencoders/ae_mlp3_c4_lr0.0001_resample25000_dict32768.pt')
parser.add_argument("--dataset", "-d", type=str,
default="phenomena/vocab/simple.json")
parser.add_argument("--num_examples", "-n", type=int, default=100,
help="Number of example pairs to use in the causal search.")
parser.add_argument("--submodule", "-s", type=str,
default="model.gpt_neox.layers.3.mlp.dense_4h_to_h")
args = parser.parse_args()
# load model and specify submodule
model = LanguageModel("EleutherAI/pythia-70m-deduped", dispatch=True)
model.local_model.requires_grad_(True)
submodule = load_submodule(model, args.submodule)
submodule_width = submodule.out_features
dataset = load_examples(args.dataset, args.num_examples, model)
# load autoencoder
autoencoder_size = 32768
if "_sz" in args.autoencoder:
autoencoder_size = int(args.autoencoder.split("_sz")[1].split("_")[0].split(".")[0])
elif "_dict" in args.autoencoder:
autoencoder_size = int(args.autoencoder.split("_dict")[1].split("_")[0].split(".")[0])
autoencoder = AutoEncoder(submodule_width, autoencoder_size).cuda()
try:
autoencoder.load_state_dict(t.load(args.autoencoder))
except TypeError:
autoencoder.load_state_dict(t.load(args.autoencoder).state_dict())
autoencoder = autoencoder.to("cuda")
indirect_effects = attribution_patching_wrt_features(model,
submodule,
autoencoder,
dataset)
top_effects, top_idxs = t.topk(indirect_effects, 10)
bottom_effects, bottom_idxs = t.topk(indirect_effects, 10, largest=False)
for effect, idx in zip(top_effects, top_idxs):
print(f"Top Feature {idx}: {effect:.5f}")
for effect, idx in zip(bottom_effects, bottom_idxs):
print(f"Bottom Feature {idx}: {effect:.5f}")