-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathattribution.py
319 lines (281 loc) · 12.4 KB
/
attribution.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
from collections import namedtuple
import torch as t
from tqdm import tqdm
from numpy import ndindex
from loading_utils import Submodule
from activation_utils import SparseAct
from nnsight.envoy import Envoy
from dictionary_learning.dictionary import Dictionary, JumpReluAutoEncoder
from typing import Callable
import types
EffectOut = namedtuple('EffectOut', ['effects', 'deltas', 'grads', 'total_effect'])
def _pe_attrib(
clean,
patch,
model,
submodules: list[Submodule],
dictionaries: dict[Submodule, Dictionary],
metric_fn,
metric_kwargs=dict(),
):
hidden_states_clean = {}
grads = {}
with model.trace(clean):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.get_activation()
x_hat, f = dictionary(x, 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
submodule.set_activation(x_recon)
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 t.no_grad(), model.trace(patch):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.get_activation()
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()
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: list[Submodule],
dictionaries: dict[Submodule, Dictionary],
metric_fn,
steps=10,
metric_kwargs=dict(),
):
hidden_states_clean = {}
with t.no_grad(), model.trace(clean):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.get_activation()
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_clean[submodule] = SparseAct(act=f.save(), res=residual.save()) # type: ignore
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 t.no_grad(), model.trace(patch):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.get_activation()
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_patch[submodule] = SparseAct(act=f.save(), res=residual.save()) # type: ignore
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() as tracer:
metrics = []
fs = []
for step in range(steps):
alpha = step / steps
f = (1 - alpha) * clean_state + alpha * patch_state
f.act.requires_grad_().retain_grad()
f.res.requires_grad_().retain_grad()
fs.append(f)
with tracer.invoke(clean):
submodule.set_activation(dictionary.decode(f.act) + f.res)
metrics.append(metric_fn(model, **metric_kwargs))
metric = sum([m for m in metrics])
metric.sum().backward()
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) # type: ignore
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: list[Submodule],
dictionaries: dict[Submodule, Dictionary],
metric_fn,
):
hidden_states_clean = {}
with t.no_grad(), model.trace(clean):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.get_activation()
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_clean[submodule] = SparseAct(act=f, res=residual).save() # type: ignore
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 t.no_grad(), model.trace(patch):
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.get_activation()
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_patch[submodule] = SparseAct(act=f, res=residual).save() # type: ignore
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.no_grad(), model.trace(clean):
f = clean_state.act.clone()
f[tuple(idx)] = patch_state.act[tuple(idx)]
x_hat = dictionary.decode(f)
submodule.set_activation(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)): # type: ignore
with t.no_grad(), model.trace(clean):
res = clean_state.res.clone()
res[tuple(idx)] = patch_state.res[tuple(idx)] # type: ignore
x_hat = dictionary.decode(clean_state.act)
submodule.set_activation(x_hat + res)
metric = metric_fn(model).save()
effect.resc[tuple(idx)] = (metric.value - metric_clean.value).sum() # type: ignore
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: list[Submodule],
dictionaries: dict[Submodule, Dictionary],
metric_fn: Callable[[Envoy], t.Tensor],
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: SparseAct,
right_vec: SparseAct,
intermediate_stopgrads: list[Submodule] = [],
):
# monkey patching to get around an nnsight bug
for dictionary in dictionaries.values():
if isinstance(dictionary, JumpReluAutoEncoder):
def hacked_forward(self, x):
W_enc, W_dec = self.W_enc.data, self.W_dec.data
b_enc, b_dec = self.b_enc.data, self.b_dec.data
# hacking around an nnsight bug
pre_jump = x @ W_enc + b_enc
f = t.nn.ReLU()(pre_jump * (pre_jump > self.threshold))
f = f * W_dec.norm(dim=1)
f_normed = f / W_dec.norm(dim=1)
x_hat = f_normed @ W_dec + b_dec
return x_hat, f
else:
def hacked_forward(self, x):
return self.forward(x, output_features=True)
dictionary.hacked_forward = types.MethodType(hacked_forward, dictionary)
downstream_dict, upstream_dict = dictionaries[downstream_submod], dictionaries[upstream_submod]
b, s, n_feats = downstream_features.act.shape
if t.all(downstream_features.to_tensor() == 0):
return t.sparse_coo_tensor(
t.zeros((2 * downstream_features.act.dim(), 0), dtype=t.long),
t.zeros(0),
size=(b, s, n_feats+1, b, s, n_feats+1)
).to(model.device)
vjv_values = {}
downstream_feature_idxs = downstream_features.to_tensor().nonzero()
with model.trace(input):
# forward pass modifications
x = upstream_submod.get_activation()
x_hat, f = upstream_dict.hacked_forward(x)
x_res = x - x_hat
upstream_submod.set_activation(x_hat + x_res)
upstream_act = SparseAct(act=f, res=x_res).save()
y = downstream_submod.get_activation()
y_hat, g = downstream_dict.hacked_forward(y)
y_res = y - y_hat
downstream_act = SparseAct(act=g, res=y_res)
to_backprops = (left_vec @ downstream_act).to_tensor()
for downstream_feat_idx in downstream_feature_idxs:
# stop grad
for submodule in intermediate_stopgrads:
submodule.stop_grad()
x_res.grad = t.zeros_like(x_res.grad)
vjv = (upstream_act.grad @ right_vec).to_tensor()
to_backprops[tuple(downstream_feat_idx)].backward(retain_graph=True)
vjv_values[downstream_feat_idx] = vjv.save() # type: ignore
vjv_indices = t.stack(list(vjv_values.keys()), dim=0).T
vjv_values = t.stack([v.value for v in vjv_values.values()], dim=0)
return t.sparse_coo_tensor(vjv_indices, vjv_values, size=(b, s, n_feats+1, b, s, n_feats+1))