-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinfluence_extractor.py
More file actions
1355 lines (1254 loc) · 59.8 KB
/
Copy pathinfluence_extractor.py
File metadata and controls
1355 lines (1254 loc) · 59.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import logging
import os
import pickle as pkl
import numpy as np
import tensorflow as tf
from tqdm import tqdm
from examples import Example, examples_in_batches
from metadata_analysis import *
from official.nlp.bert import tokenization
from utils import load_marvin, write_pickle
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # FATAL
logging.getLogger('tensorflow').setLevel(logging.FATAL)
from configs import USERNAME
class InfluenceExtractor():
def __init__(self,
bert_config,
embedding_model,
transformer_encoder,
decoder_model,
tokenizer,
use_stop_gradient=False,
model_compression=False,
decoder_type='mlm',
precision=tf.float32):
self.embedding_model = embedding_model
self.tokenizer = tokenizer
self.embedding_mat = self.embedding_model.layers[1].weights[0]
self.mask_embedding = self.get_embedding_from_word(
'[MASK]') ## baseline emb of ['mask']
self.cls_embedding = self.get_embedding_from_word(
'[CLS]') ## baseline emb of ['CLS']
self.sep_embedding = self.get_embedding_from_word(
'[SEP]') ## baseline emb of ['SEP']
self.pad_embedding = self.get_embedding_from_word(
'[PAD]') ## baseline emb of ['PAD']
self.transformer_encoder = transformer_encoder
self.decoder_model = decoder_model
self.bert_config = bert_config
self.hidden_size = self.bert_config.hidden_size
self.num_hidden_layers = self.bert_config.num_hidden_layers
self.num_attention_heads = self.bert_config.num_attention_heads
self.attention_size = int(self.hidden_size / self.num_attention_heads)
self.use_stop_gradient = use_stop_gradient
self.model_compression = model_compression
self.seq_length = self.transformer_encoder.input[0].shape[1]
self.indice_array = np.eye(self.seq_length)[np.newaxis,
np.newaxis, :, :]
self.indice_attn_array = self.indice_array[..., np.newaxis].repeat(
self.num_attention_heads, -1)
self.decoder_type = decoder_type
self.precision = precision
assert not (self.use_stop_gradient and self.model_compression)
def get_embedding_from_word(self, word):
token_id = self.tokenizer.convert_tokens_to_ids([word])[0]
return self.embedding_mat[token_id, :]
def get_baseline(self, baseline, input_mask):
actual_seq_length = tf.reduce_sum(input_mask)
if baseline == 'zero':
embedding_baseline = tf.zeros(self.hidden_size)
elif baseline == 'mask' or baseline == 'mask_i' or 'mask' in baseline:
embedding_baseline = self.mask_embedding
elif baseline == 'pad' or 'pad' in baseline:
embedding_baseline = self.pad_embedding
else:
embedding_baseline = self.get_embedding_from_word(baseline)
embedding_baseline = tf.expand_dims(embedding_baseline, 0)
if 'cls' in baseline and 'sep' in baseline:
return tf.concat([
tf.expand_dims(self.cls_embedding, 0),
tf.repeat(embedding_baseline, actual_seq_length - 2, axis=0),
tf.expand_dims(self.sep_embedding, 0),
tf.repeat(embedding_baseline,
self.seq_length - actual_seq_length,
axis=0),
], 0)
elif 'cls' in baseline:
return tf.concat([
tf.expand_dims(self.cls_embedding, 0),
tf.repeat(embedding_baseline, actual_seq_length - 1, axis=0),
tf.repeat(embedding_baseline,
self.seq_length - actual_seq_length,
axis=0),
], 0)
elif 'sep' in baseline:
return tf.concat([
tf.repeat(embedding_baseline, actual_seq_length - 1, axis=0),
tf.expand_dims(self.sep_embedding, 0),
tf.repeat(embedding_baseline,
self.seq_length - actual_seq_length,
axis=0),
], 0)
else:
return tf.repeat(embedding_baseline, self.seq_length, axis=0)
def get_tensor_input_batch(self, examples):
batch_size = len(examples)
make_constant = lambda m: tf.constant(m, tf.int32)
input_word_ids = make_constant(
np.vstack([e.input_ids for e in examples]))
input_mask = make_constant(np.vstack([e.input_mask for e in examples]))
if self.decoder_type == 'mlm':
masked_lm_positions = make_constant(
np.vstack([[e.target_idx] for e in examples]))
else:
masked_lm_positions = None
segment_ids = make_constant(np.vstack([e.segment_ids for e in examples
]))
index = np.vstack([e.word_ids for e in examples]).astype(np.int32)
index = np.vstack((np.arange(batch_size).repeat(2),
index.reshape(1, -1))).T.reshape(len(index), 2,
2).astype(np.int32)
return input_word_ids, input_mask, segment_ids, index, masked_lm_positions
def construct_qoi(self, word_ids, res):
"""
docstring
"""
index = np.array(word_ids).astype(np.int32)
index = tf.constant(
np.hstack([
np.array([[r] * len(word_ids), index]) for r in range(res)
]).T.reshape(res, len(word_ids), 2).astype(np.int32))
return index
def get_tensor_input(self, e, res=1, agg_verb=False):
make_constant = lambda m: tf.repeat(
tf.constant([m], tf.int32), res, axis=0)
input_word_ids = tf.constant([e.input_ids])
segment_ids = make_constant(e.segment_ids)
input_mask = make_constant(e.input_mask)
qoi_index = self.construct_qoi(e.word_ids, res)
if self.decoder_type == 'mlm':
masked_lm_positions = make_constant([e.target_idx])
else:
masked_lm_positions = None
return input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions
def get_alphas(self, res):
return tf.linspace(0.0, 1.0, res)
def get_path_embeddings(self, input_ids, embedding_baseline, alphas):
# Expand dimensions for vectorized computation of interpolations.
input_x = self.embedding_model(input_ids)
alphas_x = alphas[:, tf.newaxis, tf.newaxis]
baseline_x = tf.expand_dims(embedding_baseline, axis=0)
# print(baseline_x.shape, alphas_x.shape, input_x.shape)
# print(alphas_x * (input_x - baseline_x))
path_embeddings = baseline_x + alphas_x * (input_x - baseline_x)
return path_embeddings
def get_index_node_mask(self,
index1,
index2,
ei,
top_indices,
top_attention_indices,
incl_skip_nodes,
res,
start=0,
end=1,
endpoint=True):
# print('ti', top_indices)
if index1 is None:
#if no word index is given, assume end to end calculation.
assert top_attention_indices is None
assert top_indices is None
if top_indices is not None:
# print('top indice not None')
emb_node_mask = self.get_emb_node_mask(index1, index2, ei,
top_indices, res)
else:
emb_node_mask = tf.ones((res, self.num_hidden_layers + 1,
self.seq_length, self.hidden_size))
if top_attention_indices is not None:
# print('top attn indice not None')
attn_node_mask = self.get_attn_node_mask(index1, index2, ei,
top_attention_indices, res)
else:
attn_node_mask = tf.ones(
(res, self.num_hidden_layers, self.seq_length,
self.num_attention_heads,
int(self.hidden_size / self.num_attention_heads)))
if incl_skip_nodes is None:
# print('top incl None')
incl_skip_node = tf.ones((1, self.num_hidden_layers))
else:
incl_skip_node = tf.constant(incl_skip_nodes[ei,
index2, :][None, :])
return emb_node_mask, attn_node_mask, incl_skip_node
def get_emb_node_mask(self, index1, index2, ei, top_indices, res):
"""
top_indices: np.ones((len(examples), len(indices),
self.num_hidden_layers, self.seq_length))
"""
top_indices_e = np.expand_dims(top_indices[ei], 0)
emb_node_mask = np.expand_dims(
np.concatenate(
(self.indice_array[:, :,
index1, :], top_indices_e[:, index2, :, :]),
axis=1).repeat(res, 0), -1).repeat(self.hidden_size, -1)
emb_node_mask = tf.constant(emb_node_mask)
return emb_node_mask
def get_attn_node_mask(self, index1, index2, ei, top_attention_indices,
res):
"""
top_attention_indices: np.ones((len(examples), len(indices),
self.num_hidden_layers, self.seq_length), len(num_of_heads))
"""
top_indices_e = np.expand_dims(top_attention_indices[ei], 0)
attn_node_mask = np.expand_dims(
top_indices_e[:, index2, :, :, :].repeat(res, 0),
-1).repeat(self.attention_size, -1)
attn_node_mask = tf.constant(attn_node_mask)
return attn_node_mask
def tf_forward_pass(
self,
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
qoi_index,
masked_lm_positions=None,
emb_node_mask=None,
attn_node_mask=None,
incl_skip_node=None,
emb_node_mask_rep=None,
attn_node_mask_rep=None,
incl_skip_mask_rep=None,
):
return tf.function(self.forward_pass)(
alphas, input_word_ids, embedding_baseline, input_mask, segment_ids,
qoi_index, masked_lm_positions, emb_node_mask, attn_node_mask,
incl_skip_node, emb_node_mask_rep, attn_node_mask_rep,
incl_skip_mask_rep)
# @tf.function
def forward_pass(
self,
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
qoi_index,
masked_lm_positions=None,
emb_node_mask=None,
attn_node_mask=None,
incl_skip_node=None,
emb_node_mask_rep=None,
attn_node_mask_rep=None,
incl_skip_mask_rep=None,
):
path_embeddings = self.get_path_embeddings(input_word_ids,
embedding_baseline, alphas)
if self.use_stop_gradient:
sequence_output, attention_output, attention_probs, cls_output = self.transformer_encoder(
[
input_mask, segment_ids, path_embeddings, emb_node_mask,
attn_node_mask, incl_skip_node
])
elif self.model_compression:
sequence_output, attention_output, attention_probs, cls_output = self.transformer_encoder(
[
input_mask, segment_ids, path_embeddings, emb_node_mask,
attn_node_mask, incl_skip_node, emb_node_mask_rep,
attn_node_mask_rep, incl_skip_mask_rep
])
else:
sequence_output, attention_output, attention_probs, cls_output = self.transformer_encoder(
[input_mask, segment_ids, path_embeddings])
if self.decoder_type == 'cls':
decoder_output = self.decoder_model([cls_output])
elif self.decoder_type == 'mlm':
decoder_output = tf.squeeze(
self.decoder_model([sequence_output[-1], masked_lm_positions]),
1)
decoder_output_difference = tf.gather_nd(decoder_output, qoi_index)
return decoder_output_difference, decoder_output, attention_output, sequence_output, attention_probs
@tf.function
def get_reverse_gradient(self, alphas, input_word_ids, embedding_baseline,
input_mask, segment_ids, use_attention_output,
layer, qoi_index, **kwargs):
with tf.GradientTape(watch_accessed_variables=False,
persistent=False) as tape:
tape.watch(alphas)
decoder_output_difference, _, attention_output, sequence_output, _ = self.forward_pass(
alphas, input_word_ids, embedding_baseline, input_mask,
segment_ids, qoi_index, **kwargs)
decoder_output_difference_qoi = tf.reduce_sum(
decoder_output_difference[:, 0] -
decoder_output_difference[:, 1])
lm_diff = sequence_output[0][-1] - embedding_baseline
if layer == 'alpha':
return tape.gradient(decoder_output_difference_qoi, alphas)
if layer is not None:
attention_output = attention_output[layer]
sequence_output = sequence_output[layer + 2]
if use_attention_output:
grad = tape.gradient(decoder_output_difference_qoi,
attention_output)
else:
grad = tape.gradient(decoder_output_difference_qoi,
sequence_output)
if layer is None:
grad = tf.stack(grad, axis=1)
# grad = (grad[:-1] + grad[1:]) / tf.constant(2.0)
return grad, decoder_output_difference, lm_diff
@tf.function
def get_forward_gradient(self, alphas, input_word_ids, embedding_baseline,
input_mask, segment_ids, use_attention_output,
layer, qoi_index, **kwargs):
with tf.autodiff.ForwardAccumulator(
primals=alphas, tangents=tf.ones_like(alphas)) as acc:
_, _, attention_output, sequence_output, _ = self.forward_pass(
alphas, input_word_ids, embedding_baseline, input_mask,
segment_ids, qoi_index, **kwargs)
if layer is not None:
attention_output = attention_output[layer]
sequence_output = sequence_output[layer + 2]
if use_attention_output:
jvps = acc.jvp(attention_output)
else:
jvps = acc.jvp(sequence_output)
if layer is None:
jvps = tf.stack(jvps, axis=1)
return jvps
def get_reverse_influence_optimality_test(
self,
examples,
res,
indices,
num_random=100,
# top_indices=None,
# top_attention_indices=None,
# incl_skip_nodes=None,
use_attention_output=False,
reverse_qoi=None,
baseline='zero',
fix_top_indice=None,
fix_attention_indice=None):
"""[summary]
Args:
examples ([type]): [description]
res ([type]): [description]
indices ([type]): word indices to calculate influence of
layer ([type], optional): [description]. Defaults to None.
top_indices ([type], optional): [description]. Defaults to None.
top_attention_indices ([type], optional): [description]. Defaults to None.
incl_skip_nodes ([type], optional): [description]. Defaults to None.
use_attention_output (bool, optional): [description]. Defaults to False.
reverse_qoi ([type], optional): [description]. Defaults to None.
baseline (str, optional): [description]. Defaults to 'zero'.
Returns:
[type]: [description]
"""
assert self.use_stop_gradient
i2, i1 = np.meshgrid(np.arange(len(indices)), np.arange(len(examples)))
## all attention indices mask default 1 (allow flow of all heads)
# if not use_attention_output:
top_attention_indices = np.ones(
(num_random, len(examples), len(indices), self.num_hidden_layers,
self.seq_length, self.num_attention_heads))
## in the beginning, start with including all skip connections.
incl_skip_nodes = np.ones(
(num_random, len(examples), len(indices), self.num_hidden_layers))
# else:
# top_attention_indices = np.zeros(
# (num_random, len(examples), len(indices),
# self.num_hidden_layers, self.seq_length,
# self.num_attention_heads))
# ## in the beginning, start with including all skip connections.
# incl_skip_nodes = np.zeros((num_random, len(examples), len(indices),
# self.num_hidden_layers))
if fix_top_indice is None:
### getting embedding level path
top_indices = np.zeros((num_random, len(examples), len(indices),
self.num_hidden_layers, self.seq_length))
reverse_qoi = np.zeros((len(examples), len(indices)))
else:
fix_top_indice = fix_top_indice.astype(int)
top_indices = np.ones((num_random, len(examples), len(indices),
self.num_hidden_layers, self.seq_length))
# if use_attention_output:
if use_attention_output:
layer_range = np.arange(0, self.num_hidden_layers)
else:
layer_range = np.arange(0, self.num_hidden_layers)
for r in np.arange(num_random):
for l in layer_range:
top_indices[r, :, :, l, :] = 0
top_indices[r, i1, i2, l, fix_top_indice[r, l, :, :, 0]] = 1
if use_attention_output:
top_attention_indices[r, :, :, l, :, :] = 0
# top_attention_indices[r, i1, i2, l,
# fix_top_indice[r, l, :, :, 0], :] = 1
for ie in range(len(examples)):
for ii in range(len(indices)):
# print(r, ie, ii, l, fix_attention_indice[r, l, ie,
# ii])
if fix_attention_indice[r, l, ie, ii] == -1:
incl_skip_nodes[r, ie, ii, l] = 1
top_attention_indices[r, ie, ii, l,
fix_top_indice[r, l, ie,
ii,
0], :] = 0
else:
incl_skip_nodes[r, ie, ii, l] = 0
top_attention_indices[
r, ie, ii, l, fix_top_indice[r, l, ie, ii,
0],
fix_attention_indice[r, l, ie, ii]] = 1
# top_attention_indices[:, :, l, :, :] = 0
# top_attention_indices[i1, i2, l, fix_top_indice[l, :, :,
# 0], :] = 1
# print(top_attention_indices[0, 0, 0], top_attention_indices.shape)
# print(incl_skip_nodes, incl_skip_nodes.shape)
# print(top_indices[0, 0, 0])
all_grads = []
for ei, e in tqdm(enumerate(examples), position=0, leave=True):
# e_grad = []
input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions = self.get_tensor_input(
e,
res + 1,
)
embedding_baseline = self.get_baseline(baseline, e.input_mask)
alphas = self.get_alphas(res + 1)
for index2, index1 in enumerate(indices):
if index1 == None or e.input_mask[index1] != 0:
# print(index1)
r_grad = []
for t in range(num_random):
# for t in range(len(top_indices)):
emb_node_mask, attn_node_mask, incl_skip_node = self.get_index_node_mask(
index1, index2, ei, top_indices[t],
top_attention_indices[t], incl_skip_nodes[t],
res + 1)
grad = self.get_reverse_gradient(
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
use_attention_output,
'alpha',
qoi_index,
emb_node_mask=emb_node_mask,
attn_node_mask=attn_node_mask,
incl_skip_node=incl_skip_node,
masked_lm_positions=masked_lm_positions,
)
if reverse_qoi[ei, index2]:
grad = -grad
grad = grad.numpy().mean()
# gradr, _, _ = self.get_reverse_gradient(
# alphas,
# input_word_ids,
# embedding_baseline,
# input_mask,
# segment_ids,
# use_attention_output,
# 5,
# qoi_index,
# emb_node_mask=emb_node_mask,
# attn_node_mask=attn_node_mask,
# incl_skip_node=incl_skip_node,
# masked_lm_positions=masked_lm_positions,
# )
# gradf = self.get_forward_gradient(
# alphas,
# input_word_ids,
# embedding_baseline,
# input_mask,
# segment_ids,
# use_attention_output,
# 5,
# qoi_index,
# emb_node_mask=emb_node_mask,
# attn_node_mask=attn_node_mask,
# incl_skip_node=incl_skip_node,
# masked_lm_positions=masked_lm_positions,
# )
# print(gradr.shape, gradf.shape)
# grad = (gradf * gradr).numpy()
# grad = grad.sum(-1).mean(0)
# print(grad.sum())
# if reverse_qoi[ei, index2]:
# grad = -grad
# grad = grad.
# print(grad.mean())
# else:
##padding
# print(index2, index1)
# if use_attention_output:
# grad = np.zeros(
# (res + 1, self.seq_length, self.num_hidden_layers,
# self.attention_size))
# else:
# grad = np.zeros(
# (res + 1, self.seq_length, self.hidden_size))
r_grad.append(grad)
# e_grad.append(r_grad)
all_grads.append(r_grad)
return np.array(all_grads)
def get_reverse_influence(self,
examples,
res,
indices,
layer=None,
top_indices=None,
top_attention_indices=None,
incl_skip_nodes=None,
use_attention_output=False,
reverse_qoi=None,
baseline='zero'):
"""[summary]
Args:
examples ([type]): [description]
res ([type]): [description]
indices ([type]): word indices to calculate influence of
layer ([type], optional): [description]. Defaults to None.
top_indices ([type], optional): [description]. Defaults to None.
top_attention_indices ([type], optional): [description]. Defaults to None.
incl_skip_nodes ([type], optional): [description]. Defaults to None.
use_attention_output (bool, optional): [description]. Defaults to False.
reverse_qoi ([type], optional): [description]. Defaults to None.
baseline (str, optional): [description]. Defaults to 'zero'.
Returns:
[type]: [description]
"""
assert self.use_stop_gradient
if not isinstance(reverse_qoi, np.ndarray):
if reverse_qoi == True:
reverse_qoi = np.ones((len(examples), len(indices)))
elif reverse_qoi == False or reverse_qoi is None:
reverse_qoi = np.zeros((len(examples), len(indices)))
all_grads = []
# use_attention_output = tf.constant(use_attention_output)
for ei, e in tqdm(enumerate(examples), position=0, leave=True):
e_grad = []
input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions = self.get_tensor_input(
e,
res + 1,
)
embedding_baseline = self.get_baseline(baseline, e.input_mask)
alphas = self.get_alphas(res + 1)
for index2, index1 in enumerate(indices):
if index1 == None or e.input_mask[index1] != 0:
emb_node_mask, attn_node_mask, incl_skip_node = self.get_index_node_mask(
index1, index2, ei, top_indices, top_attention_indices,
incl_skip_nodes, res + 1)
grad, _, _ = self.get_reverse_gradient(
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
use_attention_output,
layer,
qoi_index,
emb_node_mask=emb_node_mask,
attn_node_mask=attn_node_mask,
incl_skip_node=incl_skip_node,
masked_lm_positions=masked_lm_positions,
)
if reverse_qoi[ei, index2]:
grad = -grad
grad = grad.numpy()
else:
##padding
# print(index2, index1)
if use_attention_output:
grad = np.zeros(
(res + 1, self.seq_length, self.num_hidden_layers,
self.attention_size))
else:
grad = np.zeros(
(res + 1, self.seq_length, self.hidden_size))
e_grad.append(grad)
all_grads.append(e_grad)
return np.array(all_grads)
def get_forward_influence(self,
examples,
res,
indices,
layer=None,
top_indices=None,
top_attention_indices=None,
incl_skip_nodes=None,
use_attention_output=False,
baseline='zero'):
assert self.use_stop_gradient
all_forward_grads = []
for ei, e in tqdm(enumerate(examples), position=0, leave=True):
forward_grads = []
input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions = self.get_tensor_input(
e,
res + 1,
)
embedding_baseline = self.get_baseline(baseline, e.input_mask)
alphas = self.get_alphas(res + 1)
for index2, index1 in enumerate(indices):
if index1 == None or e.input_mask[index1] != 0:
emb_node_mask, attn_node_mask, incl_skip_node = self.get_index_node_mask(
index1, index2, ei, top_indices, top_attention_indices,
incl_skip_nodes, res + 1)
forward_grad = self.get_forward_gradient(
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
use_attention_output,
layer,
qoi_index,
emb_node_mask=emb_node_mask,
attn_node_mask=attn_node_mask,
incl_skip_node=incl_skip_node,
masked_lm_positions=masked_lm_positions,
)
forward_grad = forward_grad.numpy()
# print(forward_grad.shape)
else:
if use_attention_output:
forward_grad = np.zeros(
(res + 1, self.seq_length, self.num_hidden_layers,
self.attention_size))
else:
forward_grad = np.zeros(
(res + 1, self.seq_length, self.hidden_size))
forward_grads.append(forward_grad)
all_forward_grads.append(forward_grads)
return np.array(all_forward_grads)
def get_lm_prob_batch(self,
examples,
batch_size,
return_attention_prob=False,
return_decoder_output=False,
return_decoder_output_mask=False):
assert not self.use_stop_gradient and not self.model_compression
decoder_outputs = np.zeros((0, 2))
category_features = []
attention_prob_lst = []
sequence_output_all = np.zeros(
(0, self.num_hidden_layers + 1, self.seq_length, self.hidden_size))
attn_output_all = np.zeros(
(0, self.num_hidden_layers, self.seq_length,
self.num_attention_heads, self.attention_size))
for ei, e in tqdm(enumerate(examples_in_batches(examples, batch_size)),
position=0,
leave=True):
actual_batch_size = len(e)
input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions = self.get_tensor_input_batch(
e)
alphas = tf.ones(actual_batch_size, self.precision)
embedding_baseline = tf.zeros((self.seq_length, self.hidden_size))
decoder_output_difference, decoder_output, attention_output, sequence_output, attention_probs = self.forward_pass(
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
qoi_index,
masked_lm_positions=masked_lm_positions)
if not return_decoder_output and not return_decoder_output_mask:
sequence_output_all = np.concatenate(
(sequence_output_all, tf.stack(sequence_output[1:],
axis=1).numpy()), 0)
# print(tf.stack(attention_output, axis=1).numpy().shape)
attn_output_all = np.concatenate(
(attn_output_all, tf.stack(attention_output,
axis=1).numpy()), 0)
if return_attention_prob:
attention_probs = np.array([a.numpy() for a in attention_probs])
attention_probs = np.swapaxes(attention_probs, 0, 1)
attention_prob_lst.append(attention_probs)
if return_decoder_output_mask:
assert len(examples) == 1
assert actual_batch_size == 1
return np.array(tf.squeeze(decoder_output_difference).numpy())
# print(decoder_output.shape, decoder_outputs.shape)
decoder_outputs = np.vstack(
(decoder_outputs, decoder_output_difference))
if self.decoder_type == 'mlm':
category_features.extend([
(eii.features[0], eii.features[1]) for eii in e
])
if return_decoder_output:
return np.array(decoder_outputs)
if return_attention_prob:
attention_prob_result = np.concatenate(attention_prob_lst)
else:
attention_prob_result = None
return np.array(
decoder_outputs
), category_features, attention_prob_result, sequence_output_all, attn_output_all
def get_e2e_influence(self,
examples,
res,
start=0,
end=1,
endpoint=True,
return_all_resolutions=False,
return_wrt2_embedding=False,
baseline='zero',
agg_verb=False,
ig=True):
assert not self.use_stop_gradient
all_grad_is = []
decoder_output_differences = []
for ei, e in tqdm(enumerate(examples)):
# if ei % 100 == 0:
# print(ei)
grad_is = []
decoder_output_difference_lst = []
input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions = self.get_tensor_input(
e,
res + 1,
)
embedding_baseline = self.get_baseline(baseline, e.input_mask)
alphas = self.get_alphas(res + 1)
grad_i, decoder_output_difference, lm_diff = self.get_reverse_gradient(
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
use_attention_output=False,
layer=-2,
qoi_index=qoi_index,
masked_lm_positions=masked_lm_positions)
grad_i = tf.reduce_sum((grad_i * lm_diff), -1).numpy()
grad_is.append(grad_i)
all_grad_is.append(grad_is)
decoder_output_difference_lst.append(
decoder_output_difference.numpy())
decoder_output_differences.append(decoder_output_difference_lst)
return np.array(all_grad_is), np.array(decoder_output_differences)
def get_model_compression(self,
examples,
batch_size,
ind_heatmap_e=None,
sequence_output_e=None,
attn_heatmap_e=None,
attn_output_e=None,
ind_skip_e=None):
assert self.model_compression and not self.use_stop_gradient
decoder_outputs = np.zeros((0, 2))
if ind_heatmap_e is None:
ind_heatmap_e = np.ones((len(examples), self.num_hidden_layers + 1,
self.seq_length, self.hidden_size))
if sequence_output_e is None:
sequence_output_e = np.zeros(
(self.num_hidden_layers + 1, self.seq_length, self.hidden_size))
if attn_output_e is None:
attn_output_e = np.zeros(
(self.num_hidden_layers, self.seq_length,
self.num_attention_heads, self.attention_size))
if attn_heatmap_e is None:
attn_heatmap_e = np.ones(
(len(examples), self.num_hidden_layers, self.seq_length,
self.num_attention_heads, self.attention_size))
if ind_skip_e is None:
ind_skip_e = np.ones((len(examples), self.num_hidden_layers,
self.seq_length, self.hidden_size))
# for ei, e in tqdm(enumerate(examples_in_batches(examples, batch_size))):
for i in tqdm(range(1 + ((len(examples) - 1) // batch_size)),
position=0,
leave=True):
e = examples[i * batch_size:(i + 1) * batch_size]
actual_batch_size = len(e)
input_word_ids, input_mask, segment_ids, qoi_index, masked_lm_positions = self.get_tensor_input_batch(
e)
alphas = tf.ones(actual_batch_size, self.precision)
embedding_baseline = tf.zeros((self.seq_length, self.hidden_size))
decoder_output_difference, _, _, _, _ = self.forward_pass(
alphas,
input_word_ids,
embedding_baseline,
input_mask,
segment_ids,
qoi_index,
emb_node_mask_rep=tf.constant(
sequence_output_e[None, ...].repeat(actual_batch_size, 0)),
attn_node_mask_rep=tf.constant(attn_output_e[None, ...].repeat(
actual_batch_size, 0)),
incl_skip_mask_rep=tf.constant(
ind_skip_e[i * batch_size:(i + 1) * batch_size]),
emb_node_mask=tf.constant(ind_heatmap_e[i * batch_size:(i + 1) *
batch_size]),
attn_node_mask=tf.constant(
attn_heatmap_e[i * batch_size:(i + 1) * batch_size]),
incl_skip_node=tf.constant(np.zeros(
(1, self.num_hidden_layers))),
masked_lm_positions=masked_lm_positions,
)
# input_mask, segment_ids, path_embeddings, emb_node_mask,
# attn_node_mask, incl_skip_node, emb_node_mask_rep,
# attn_node_mask_rep, incl_skip_mask_rep
# sequence_output, attention_output, _, _ = self.transformer_encoder([
# input_word_ids,
# input_mask,
# segment_ids,
# embedding_end,
# embedding_baseline,
# alpha,
# tf.constant(ind_heatmap_e[i * batch_size:(i + 1) * batch_size]),
# tf.constant(attn_heatmap_e[i * batch_size:(i + 1) *
# batch_size]),
# tf.constant(np.zeros((1, self.num_hidden_layers))),
# tf.constant(sequence_output_e[None,
# ...].repeat(actual_batch_size,
# 0)),
# tf.constant(attn_output_e[None,
# ...].repeat(actual_batch_size, 0)),
# tf.constant(ind_skip_e[i * batch_size:(i + 1) * batch_size]),
# ])
decoder_outputs = np.vstack(
(decoder_outputs, decoder_output_difference))
return np.array(decoder_outputs)
def get_greedy_influence_test(self,
examples,
res,
indices,
threshold=0,
use_attention_output=False,
reverse_qoi=None,
baseline='zero',
fix_top_indice=None):
i2, i1 = np.meshgrid(np.arange(len(indices)), np.arange(len(examples)))
## all attention indices mask default 1 (allow flow of all heads)
top_attention_indices = np.ones(
(len(examples), len(indices), self.num_hidden_layers,
self.seq_length, self.num_attention_heads))
## in the beginning, start with including all skip connections.
incl_skip_nodes = np.ones(
(len(examples), len(indices), self.num_hidden_layers))
if fix_top_indice is None:
### getting embedding level path
top_indices = np.ones((len(examples), len(indices),
self.num_hidden_layers, self.seq_length))
assert reverse_qoi is None
reverse_qoi = np.zeros((len(examples), len(indices)))
else:
assert threshold == 0 ## for attention-level path, assume picking only 1 node per layer
fix_top_indice = fix_top_indice.astype(int)
top_indices = np.ones((len(examples), len(indices),
self.num_hidden_layers, self.seq_length))
for l in np.arange(0, self.num_hidden_layers):
top_indices[:, :, l, :] = 0
top_indices[i1, i2, l, fix_top_indice[l, :, :, 0]] = 1
top_attention_indices[:, :, l, :, :] = 0
top_attention_indices[i1, i2, l, fix_top_indice[l, :, :,
0], :] = 1
reverse_qoi_idx = np.where(reverse_qoi == 1)
expanding_indices = []
combs = []
if fix_top_indice is None:
# if getting embedding level path, last layer's node is fixed (to mask)
layer_range = np.arange(0, self.num_hidden_layers - 1)
# layer_range = np.arange(1)
else:
layer_range = np.arange(0, self.num_hidden_layers)
for layer in [0]:
print('=' * 50)
print('layer', layer)
fwd = self.get_forward_influence(
examples,
res,
indices,
layer,
top_indices,
top_attention_indices=top_attention_indices,
incl_skip_nodes=incl_skip_nodes,
use_attention_output=use_attention_output,
baseline=baseline)
# print(fwd.shape)
# print('backward influence')
rvs = self.get_reverse_influence(
examples,
res,
indices,
layer,
top_indices,
top_attention_indices=top_attention_indices,
incl_skip_nodes=incl_skip_nodes,
use_attention_output=use_attention_output,
reverse_qoi=reverse_qoi,
baseline=baseline)
comb = fwd * rvs # (E, 10, 50, 11, 512)
comb = np.mean(comb.sum(-1), axis=2)
if fix_top_indice is None or not use_attention_output:
# pass
# if layer ==1 and
if layer == 0:
# modify reverse qoi based on layer
neg_ind = np.where(comb.sum(-1) < 0)
reverse_qoi[neg_ind[0], neg_ind[1]] = 1
comb[neg_ind[0], neg_ind[1], :] *= -1
# argsort_index = np.argsort(-comb, axis=-1)
# expanding_indices.append(argsort_index[:, :, :threshold + 1])
# print(comb.shape)
combs.append(comb)
for layer in [1]:
print('=' * 50)
print('layer', layer)
for tmp_index in range(self.seq_length):
top_indices[:, :, 0, :] = 0
top_indices[i1, i2, 0, tmp_index] = 1
fwd = self.get_forward_influence(
examples,
res,
indices,
layer,
top_indices,
top_attention_indices=top_attention_indices,
incl_skip_nodes=incl_skip_nodes,
use_attention_output=use_attention_output,
baseline=baseline)
# print(fwd.shape)
# print('backward influence')
rvs = self.get_reverse_influence(
examples,
res,
indices,
layer,
top_indices,
top_attention_indices=top_attention_indices,
incl_skip_nodes=incl_skip_nodes,
use_attention_output=use_attention_output,
reverse_qoi=reverse_qoi,
baseline=baseline)
comb = fwd * rvs # (E, 10, 50, 11, 512)
comb = np.mean(comb.sum(-1), axis=2)