forked from AmeenAli/XAI_Transformers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxai_transformer.py
386 lines (268 loc) · 12.5 KB
/
xai_transformer.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
377
378
379
380
381
382
383
384
385
386
from torch import nn
import torch
import sys
import copy
root_dir = './../'
sys.path.append(root_dir)
from utils import LayerNorm
class LNargs(object):
def __init__(self):
self.lnv = 'nowb'
self.sigma = None
self.hidden = None
self.adanorm_scale = 1.
self.nowb_scale = None
self.mean_detach = False
self.std_detach = False
class LNargsDetach(object):
def __init__(self):
self.lnv = 'nowb'
self.sigma = None
self.hidden = None
self.adanorm_scale = 1.
self.nowb_scale = None
self.mean_detach = True
self.std_detach = True
class LNargsDetachNotMean(object):
def __init__(self):
self.lnv = 'nowb'
self.sigma = None
self.hidden = None
self.adanorm_scale = 1.
self.nowb_scale = None
self.mean_detach = False
self.std_detach = True
def make_p_layer(layer, gamma):
player = copy.deepcopy(layer)
player.weight = torch.nn.Parameter(layer.weight+gamma*layer.weight.clamp(min=0))
player.bias = torch.nn.Parameter(layer.bias +gamma*layer.bias.clamp(min=0))
return player
class BertPooler(nn.Module):
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = nn.Tanh()
def forward(self, hidden_states):
# We "pool" the model by simply taking the hidden state corresponding
# to the first token.
self.first_token_tensor = hidden_states[:, 0]
self.pooled_output1 = self.dense(self.first_token_tensor)
self.pooled_output2 = self.activation(self.pooled_output1)
return self.pooled_output2
class BertSelfOutput(nn.Module):
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.config = config
if self.config.train_mode == True:
self.dropout = torch.nn.Dropout(p=0.1, inplace=False)
if config.detach_layernorm == True:
assert config.train_mode==False
if config.detach_mean==False:
print('Detach LayerNorm only Norm')
largs = LNargsDetachNotMean()
else:
print('Detach LayerNorm Mean+Norm')
largs = LNargsDetach()
else:
largs = LNargs()
self.LayerNorm = LayerNorm(config.hidden_size, eps=config.layer_norm_eps, args=largs)
self.detach = config.detach_layernorm
def forward(self, hidden_states, input_tensor):
hidden_states = self.dense(hidden_states)
if self.config.train_mode == True:
hidden_states = self.dropout(hidden_states)
hidden_states = self.LayerNorm(hidden_states + input_tensor)
return hidden_states
def pforward(self, hidden_states, input_tensor, gamma):
pdense = make_p_layer(self.dense, gamma)
hidden_states = pdense(hidden_states)
#hidden_states = self.dense(hidden_states)
if self.config.train_mode == True:
hidden_states = self.dropout(hidden_states)
hidden_states = self.LayerNorm(hidden_states + input_tensor)
return hidden_states
class AttentionBlock(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.query = nn.Linear(config.hidden_size, config.all_head_size)
self.key = nn.Linear(config.hidden_size, config.all_head_size)
self.value = nn.Linear(config.hidden_size, config.all_head_size)
self.output = BertSelfOutput(config)
self.detach = config.detach_kq
if self.config.train_mode == True:
self.dropout = torch.nn.Dropout(p=0.1, inplace=False)
if self.detach == True:
assert self.config.train_mode==False
print('Detach K-Q-softmax branch')
def save_attn_gradients(self, attn_gradients):
self.attn_gradients = attn_gradients
def get_attn_gradients(self):
return self.attn_gradients
def transpose_for_scores(self, x):
# x torch.Size([1, 10, 768])
# xout torch.Size([1, 10, 12, 64])
new_x_shape = x.size()[:-1] + (self.config.num_attention_heads, self.config.attention_head_size)
x = x.view(*new_x_shape)
X= x.permute(0, 2, 1, 3)
return X
def un_transpose_for_scores(self, x, old_shape):
x = x.permute(0, 1, 2, 3)
return x.reshape(old_shape)
@staticmethod
def pproc(layer,player,x):
z = layer(x)
zp = player(x)
return zp * (z / zp).data
def forward(self, hidden_states, gamma=0 , method=None):
# print('PKQ gamma', gamma)
pquery = make_p_layer(self.query, gamma)
pkey = make_p_layer(self.key, gamma)
pvalue = make_p_layer(self.value, gamma)
n_nodes= hidden_states.shape[1]
if self.config.train_mode:
query_ = self.query(hidden_states)
key_ = self.key(hidden_states)
val_ = self.value(hidden_states)
else:
query_ = self.pproc(self.query, pquery, hidden_states)
key_ = self.pproc(self.key, pkey, hidden_states)
val_ = self.pproc(self.value, pvalue, hidden_states)
# [1, senlen, 768] -> [1, 12, senlen, 64]
query_t = self.transpose_for_scores(query_)
key_t = self.transpose_for_scores(key_)
val_t = self.transpose_for_scores(val_)
# torch.Size([1, 12, 10, 64]) , torch.Size([1, 12, 64, 10]) -> torch.Size([1, 12, 10, 10])
attention_scores = torch.matmul(query_t, key_t.transpose(-1, -2))
#if torch.isnan(attention_scores).any():
# import pdb;pdb.set_trace()
if self.detach:
assert self.config.train_mode==False
attention_probs = nn.Softmax(dim=-1)(attention_scores).detach()
else:
attention_probs = nn.Softmax(dim=-1)(attention_scores)
if self.config.train_mode:
attention_probs = self.dropout(attention_probs)
if method == 'GAE':
attention_probs.register_hook(self.save_attn_gradients)
context_layer = torch.matmul(attention_probs, val_t)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
old_context_layer_shape = context_layer.shape
new_context_layer_shape = context_layer.size()[:-2] + (self.config.all_head_size,)
context_layer = context_layer.view(*new_context_layer_shape)
if self.config.train_mode:
output = self.output(context_layer, hidden_states)
else:
#
# print('Out gamma', gamma)
output = self.output.pforward(context_layer, hidden_states, gamma=gamma)
return output, attention_probs #, (attention_scores, hidden_states) #, query_t, key_t, val_t)
class BertAttention(nn.Module):
def __init__(self, config, embeddings):
super().__init__()
n_blocks = config.n_blocks
self.n_blocks=n_blocks
self.embeds = embeddings
self.config = config
self.attention_layers = torch.nn.Sequential(*[AttentionBlock(config) for i in range(n_blocks)])
self.output = BertSelfOutput(config)
self.pooler = BertPooler(config)
self.classifier = nn.Linear(in_features=config.hidden_size, out_features=config.n_classes, bias=True)
self.device = config.device
self.attention_probs = {i: [] for i in range(n_blocks)}
self.attention_debug = {i: [] for i in range(n_blocks)}
self.attention_gradients = {i: [] for i in range(n_blocks)}
self.attention_cams = {i: [] for i in range(n_blocks)}
self.attention_lrp_gradients = {i: [] for i in range(n_blocks)}
def forward(self, input_ids,
attention_mask=None,
token_type_ids=None,
position_ids=None,
inputs_embeds=None,
labels=None,
past_key_values_length=0):
hidden_states = self.embeds(input_ids=input_ids,
token_type_ids=token_type_ids,
position_ids=None,
inputs_embeds=None,
past_key_values_length=0).to(self.config.device)
attn_input = hidden_states
for i,block in enumerate(self.attention_layers):
output, attention_probs = block(attn_input)
self.attention_probs[i] = attention_probs
# self.attention_debug[i] = debug_data + (output,)
attn_input = output
pooled = self.pooler(output)
logits = self.classifier(pooled)
self.output_=output
self.pooled_ = pooled
self.logits_=logits
if labels is not None:
loss = torch.nn.CrossEntropyLoss()(logits,labels)
else:
loss = None
return {'loss': loss, 'logits': logits}
def prep_lrp(self, x):
x = x.data
x.requires_grad_(True)
return x
def forward_and_explain(self, input_ids,
cl,
attention_mask=None,
token_type_ids=None,
position_ids=None,
inputs_embeds=None,
labels=None,
past_key_values_length=0,
gammas=None,
method=None):
# Forward
A = {}
hidden_states= self.embeds(input_ids=input_ids,
token_type_ids=token_type_ids,
position_ids=None,
inputs_embeds=None,
past_key_values_length=0).to(self.config.device)
A['hidden_states'] = hidden_states
attn_input = hidden_states
for i,block in enumerate(self.attention_layers):
# [1, 12, 768] -> [1, 12, 768]
attn_inputdata = attn_input.data
attn_inputdata.requires_grad_(True)
A['attn_input_{}_data'.format(i)] =attn_inputdata
A['attn_input_{}'.format(i)] = attn_input
gamma = 0. if gammas is None else gammas[i]
# print('using gamma', gamma)
output, attention_probs = block(A['attn_input_{}_data'.format(i)], gamma=gamma, method=method)
self.attention_probs[i] = attention_probs
attn_input = output
# (1, 12, 768) -> (1x768)
outputdata = output.data
outputdata.requires_grad_(True)
pooled = self.pooler(outputdata) #A['attn_output'] )
# (1x768) -> (1,nclasses)
pooleddata = pooled.data
pooleddata.requires_grad_(True)
logits = self.classifier(pooleddata)
A['logits'] = logits
# Through clf layer
Rout = A['logits'][:,cl]
self.R0 = Rout.detach().cpu().numpy()
Rout.backward()
((pooleddata.grad)*pooled).sum().backward()
Rpool = ((outputdata.grad)*output)
R_ = Rpool
for i,block in list(enumerate(self.attention_layers))[::-1]:
R_.sum().backward()
R_grad = A['attn_input_{}_data'.format(i)].grad
R_attn = (R_grad)*A['attn_input_{}'.format(i)]
if method == 'GAE':
self.attention_gradients[i] = block.get_attn_gradients().squeeze()
R_ = R_attn
R = R_.sum(2).detach().cpu().numpy()
if labels is not None:
loss = torch.nn.CrossEntropyLoss()(logits,labels)
else:
loss = None
return {'loss': loss, 'logits': logits, 'R': R}