-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattribution.py
More file actions
460 lines (416 loc) · 16.8 KB
/
attribution.py
File metadata and controls
460 lines (416 loc) · 16.8 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
from collections import namedtuple
import torch as t
from tqdm import tqdm
from numpy import ndindex
from typing import Dict, Union
from activation_utils import SparseAct
DEBUGGING = False
if DEBUGGING:
tracer_kwargs = {"validate": True, "scan": True}
else:
tracer_kwargs = {"validate": False, "scan": False}
EffectOut = namedtuple("EffectOut", ["effects", "deltas", "grads", "total_effect"])
def _pe_attrib(
clean,
patch,
model,
submodules,
dictionaries,
metric_fn,
metric_kwargs=dict(),
):
# first run through a test input to figure out which hidden states are tuples
is_tuple = {}
with model.trace("_"):
for submodule in submodules:
is_tuple[submodule] = type(submodule.output.shape) == tuple
hidden_states_clean = {}
grads = {}
with model.trace(clean, **tracer_kwargs):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
x = x.to(dtype=model.dtype)
# I have no idea why (x - 0.0) is necessary
# Without it, we get this error from the JumpReluAutoEncoder class
# RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
# It can also be fixed by setting pre_jump = self.encoder(x - 0.0) + self.b_enc in the JumpReluAutoEncoder class
x_hat, f = dictionary((x - 0.0), output_features=True) # x_hat implicitly depends on f
residual = x - x_hat
hidden_states_clean[submodule] = SparseAct(act=f, res=residual).save()
grads[submodule] = hidden_states_clean[submodule].grad.save()
residual.grad = t.zeros_like(residual)
x_recon = x_hat + residual
if is_tuple[submodule]:
submodule.output[0][:] = x_recon
else:
submodule.output = x_recon
x_recon[:, 0, :] = x[:, 0, :]
x.grad = x_recon.grad
metric_clean = metric_fn(model, **metric_kwargs).save()
metric_clean.sum().backward()
hidden_states_clean = {k: v.value for k, v in hidden_states_clean.items()}
grads = {k: v.value for k, v in grads.items()}
if patch is None:
hidden_states_patch = {
k: SparseAct(act=t.zeros_like(v.act), res=t.zeros_like(v.res))
for k, v in hidden_states_clean.items()
}
total_effect = None
else:
hidden_states_patch = {}
with model.trace(patch, **tracer_kwargs), t.inference_mode():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
x = x.to(dtype=model.dtype)
x_hat, f = dictionary(x, output_features=True)
residual = x - x_hat
hidden_states_patch[submodule] = SparseAct(act=f, res=residual).save()
metric_patch = metric_fn(model, **metric_kwargs).save()
total_effect = (metric_patch.value - metric_clean.value).detach()
hidden_states_patch = {k: v.value for k, v in hidden_states_patch.items()}
effects = {}
deltas = {}
for submodule in submodules:
patch_state, clean_state, grad = (
hidden_states_patch[submodule],
hidden_states_clean[submodule],
grads[submodule],
)
delta = (
patch_state - clean_state.detach() if patch_state is not None else -clean_state.detach()
)
# delta.act[:, 0, :] = 0 # zero out the first token
effect = delta @ grad
effects[submodule] = effect
deltas[submodule] = delta
grads[submodule] = grad
total_effect = total_effect if total_effect is not None else None
return EffectOut(effects, deltas, grads, total_effect)
def _pe_ig(
clean,
patch,
model,
submodules,
dictionaries,
metric_fn,
steps=10,
metric_kwargs=dict(),
):
# first run through a test input to figure out which hidden states are tuples
is_tuple = {}
with model.trace("_"):
for submodule in submodules:
is_tuple[submodule] = type(submodule.output.shape) == tuple
hidden_states_clean = {}
with model.trace(clean, **tracer_kwargs), t.no_grad():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
x = x.to(dtype=model.dtype)
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_clean[submodule] = SparseAct(act=f.save(), res=residual.save())
metric_clean = metric_fn(model, **metric_kwargs).save()
hidden_states_clean = {k: v.value for k, v in hidden_states_clean.items()}
if patch is None:
hidden_states_patch = {
k: SparseAct(act=t.zeros_like(v.act), res=t.zeros_like(v.res))
for k, v in hidden_states_clean.items()
}
total_effect = None
else:
hidden_states_patch = {}
with model.trace(patch, **tracer_kwargs), t.no_grad():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
x = x.to(dtype=model.dtype)
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_patch[submodule] = SparseAct(act=f.save(), res=residual.save())
metric_patch = metric_fn(model, **metric_kwargs).save()
total_effect = (metric_patch.value - metric_clean.value).detach()
hidden_states_patch = {k: v.value for k, v in hidden_states_patch.items()}
effects = {}
deltas = {}
grads = {}
for submodule in submodules:
dictionary = dictionaries[submodule]
clean_state = hidden_states_clean[submodule]
patch_state = hidden_states_patch[submodule]
with model.trace(**tracer_kwargs) as tracer:
metrics = []
fs = []
for step in range(steps):
alpha = step / steps
f = (1 - alpha) * clean_state + alpha * patch_state
f.act.retain_grad()
f.res.retain_grad()
fs.append(f)
with tracer.invoke(clean, scan=tracer_kwargs["scan"]):
if is_tuple[submodule]:
submodule.output[0][:] = dictionary.decode(f.act) + f.res
else:
submodule.output = dictionary.decode(f.act) + f.res
metrics.append(metric_fn(model, **metric_kwargs))
metric = sum([m for m in metrics])
metric.sum().backward(
retain_graph=True
) # TODO : why is this necessary? Probably shouldn't be, contact jaden
mean_grad = sum([f.act.grad for f in fs]) / steps
mean_residual_grad = sum([f.res.grad for f in fs]) / steps
grad = SparseAct(act=mean_grad, res=mean_residual_grad)
delta = (
(patch_state - clean_state).detach()
if patch_state is not None
else -clean_state.detach()
)
effect = grad @ delta
effects[submodule] = effect
deltas[submodule] = delta
grads[submodule] = grad
return EffectOut(effects, deltas, grads, total_effect)
def _pe_exact(
clean,
patch,
model,
submodules,
dictionaries,
metric_fn,
):
# first run through a test input to figure out which hidden states are tuples
is_tuple = {}
with model.trace("_"):
for submodule in submodules:
is_tuple[submodule] = type(submodule.output.shape) == tuple
hidden_states_clean = {}
with model.trace(clean, **tracer_kwargs), t.inference_mode():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_clean[submodule] = SparseAct(act=f, res=residual).save()
metric_clean = metric_fn(model).save()
hidden_states_clean = {k: v.value for k, v in hidden_states_clean.items()}
if patch is None:
hidden_states_patch = {
k: SparseAct(act=t.zeros_like(v.act), res=t.zeros_like(v.res))
for k, v in hidden_states_clean.items()
}
total_effect = None
else:
hidden_states_patch = {}
with model.trace(patch, **tracer_kwargs), t.inference_mode():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_patch[submodule] = SparseAct(act=f, res=residual).save()
metric_patch = metric_fn(model).save()
total_effect = metric_patch.value - metric_clean.value
hidden_states_patch = {k: v.value for k, v in hidden_states_patch.items()}
effects = {}
deltas = {}
for submodule in submodules:
dictionary = dictionaries[submodule]
clean_state = hidden_states_clean[submodule]
patch_state = hidden_states_patch[submodule]
effect = SparseAct(
act=t.zeros_like(clean_state.act), resc=t.zeros(*clean_state.res.shape[:-1])
).to(model.device)
# iterate over positions and features for which clean and patch differ
idxs = t.nonzero(patch_state.act - clean_state.act)
for idx in tqdm(idxs):
with t.inference_mode():
with model.trace(clean, **tracer_kwargs):
f = clean_state.act.clone()
f[tuple(idx)] = patch_state.act[tuple(idx)]
x_hat = dictionary.decode(f)
if is_tuple[submodule]:
submodule.output[0][:] = x_hat + clean_state.res
else:
submodule.output = x_hat + clean_state.res
metric = metric_fn(model).save()
effect.act[tuple(idx)] = (metric.value - metric_clean.value).sum()
for idx in list(ndindex(effect.resc.shape)):
with t.inference_mode():
with model.trace(clean, **tracer_kwargs):
res = clean_state.res.clone()
res[tuple(idx)] = patch_state.res[tuple(idx)]
x_hat = dictionary.decode(clean_state.act)
if is_tuple[submodule]:
submodule.output[0][:] = x_hat + res
else:
submodule.output = x_hat + res
metric = metric_fn(model).save()
effect.resc[tuple(idx)] = (metric.value - metric_clean.value).sum()
effects[submodule] = effect
deltas[submodule] = patch_state - clean_state
total_effect = total_effect if total_effect is not None else None
return EffectOut(effects, deltas, None, total_effect)
def patching_effect(
clean,
patch,
model,
submodules,
dictionaries,
metric_fn,
method="attrib",
steps=10,
metric_kwargs=dict(),
):
if method == "attrib":
return _pe_attrib(
clean, patch, model, submodules, dictionaries, metric_fn, metric_kwargs=metric_kwargs
)
elif method == "ig":
return _pe_ig(
clean,
patch,
model,
submodules,
dictionaries,
metric_fn,
steps=steps,
metric_kwargs=metric_kwargs,
)
elif method == "exact":
return _pe_exact(clean, patch, model, submodules, dictionaries, metric_fn)
else:
raise ValueError(f"Unknown method {method}")
def jvp(
input,
model,
dictionaries,
downstream_submod,
downstream_features,
upstream_submod,
left_vec: Union[SparseAct, Dict[int, SparseAct]],
right_vec: SparseAct,
return_without_right=False,
):
"""
Return a sparse shape [# downstream features + 1, # upstream features + 1] tensor of Jacobian-vector products.
"""
if not downstream_features: # handle empty list
if not return_without_right:
return t.sparse_coo_tensor(t.zeros((2, 0), dtype=t.long), t.zeros(0)).to(model.device)
else:
return t.sparse_coo_tensor(t.zeros((2, 0), dtype=t.long), t.zeros(0)).to(
model.device
), t.sparse_coo_tensor(t.zeros((2, 0), dtype=t.long), t.zeros(0)).to(model.device)
# first run through a test input to figure out which hidden states are tuples
is_tuple = {}
with model.trace("_"):
is_tuple[upstream_submod] = type(upstream_submod.output.shape) == tuple
is_tuple[downstream_submod] = type(downstream_submod.output.shape) == tuple
downstream_dict, upstream_dict = dictionaries[downstream_submod], dictionaries[upstream_submod]
vjv_indices = {}
vjv_values = {}
if return_without_right:
jv_indices = {}
jv_values = {}
with model.trace(input, **tracer_kwargs):
# first specify forward pass modifications
x = upstream_submod.output
if is_tuple[upstream_submod]:
x = x[0]
x_hat, f = upstream_dict(x, output_features=True)
x_res = x - x_hat
upstream_act = SparseAct(act=f, res=x_res).save()
if is_tuple[upstream_submod]:
upstream_submod.output[0][:] = x_hat + x_res
else:
upstream_submod.output = x_hat + x_res
y = downstream_submod.output
if is_tuple[downstream_submod]:
y = y[0]
y_hat, g = downstream_dict(y, output_features=True)
y_res = y - y_hat
downstream_act = SparseAct(act=g, res=y_res).save()
for downstream_feat in downstream_features:
if isinstance(left_vec, SparseAct):
to_backprop = (left_vec @ downstream_act).to_tensor().flatten()
elif isinstance(left_vec, dict):
to_backprop = (left_vec[downstream_feat] @ downstream_act).to_tensor().flatten()
else:
raise ValueError(f"Unknown type {type(left_vec)}")
vjv = (upstream_act.grad @ right_vec).to_tensor().flatten()
if return_without_right:
jv = (upstream_act.grad @ right_vec).to_tensor().flatten()
x_res.grad = t.zeros_like(x_res)
to_backprop[downstream_feat].backward(retain_graph=True)
vjv_indices[downstream_feat] = vjv.nonzero().squeeze(-1).save()
vjv_values[downstream_feat] = vjv[vjv_indices[downstream_feat]].save()
if return_without_right:
jv_indices[downstream_feat] = jv.nonzero().squeeze(-1).save()
jv_values[downstream_feat] = jv[vjv_indices[downstream_feat]].save()
# get shapes
d_downstream_contracted = len(
(downstream_act.value @ downstream_act.value).to_tensor().flatten()
)
d_upstream_contracted = len((upstream_act.value @ upstream_act.value).to_tensor().flatten())
if return_without_right:
d_upstream = len(upstream_act.value.to_tensor().flatten())
vjv_indices = t.tensor(
[
[
downstream_feat
for downstream_feat in downstream_features
for _ in vjv_indices[downstream_feat].value
],
t.cat(
[vjv_indices[downstream_feat].value for downstream_feat in downstream_features],
dim=0,
),
]
).to(model.device)
vjv_values = t.cat(
[vjv_values[downstream_feat].value for downstream_feat in downstream_features], dim=0
)
if not return_without_right:
return t.sparse_coo_tensor(
vjv_indices, vjv_values, (d_downstream_contracted, d_upstream_contracted)
)
jv_indices = t.tensor(
[
[
downstream_feat
for downstream_feat in downstream_features
for _ in jv_indices[downstream_feat].value
],
t.cat(
[jv_indices[downstream_feat].value for downstream_feat in downstream_features],
dim=0,
),
]
).to(model.device)
jv_values = t.cat(
[jv_values[downstream_feat].value for downstream_feat in downstream_features], dim=0
)
return (
t.sparse_coo_tensor(
vjv_indices, vjv_values, (d_downstream_contracted, d_upstream_contracted)
),
t.sparse_coo_tensor(jv_indices, jv_values, (d_downstream_contracted, d_upstream)),
)