-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_bert_ci.py
180 lines (159 loc) · 8.16 KB
/
utils_bert_ci.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
from operator import itemgetter
from copy import deepcopy
import heapq
import numpy
import torch
import torch.optim as optim
from allennlp.common.util import lazy_groups_of
from allennlp.data.iterators import BucketIterator
from allennlp.nn.util import move_to_device
from allennlp.modules.text_field_embedders import TextFieldEmbedder
import random
def get_embedding_weight(model):
"""
Extracts and returns the token embedding weight matrix from the model.
"""
for module in model.modules():
if isinstance(module, TextFieldEmbedder):
for embed in module._token_embedders.keys():
embedding_weight = module._token_embedders[embed].weight.cpu().detach()
return embedding_weight
# hook used in add_hooks()
extracted_grads = []
def extract_grad_hook(module, grad_in, grad_out):
extracted_grads.append(grad_out[0])
def add_hooks(model):
"""
Finds the token embedding matrix on the model and registers a hook onto it.
When loss.backward() is called, extracted_grads list will be filled with
the gradients w.r.t. the token embeddings
"""
for module in model.modules():
if isinstance(module, TextFieldEmbedder):
for embed in module._token_embedders.keys():
module._token_embedders[embed].weight.requires_grad = True
module.register_backward_hook(extract_grad_hook)
def create_empty_batch(model, batch, trigger_token_ids=None, snli=False):
batch = move_to_device(batch, cuda_device=0)
trigger_sequence_tensor = torch.LongTensor(deepcopy(trigger_token_ids))
trigger_sequence_tensor = trigger_sequence_tensor.repeat(len(batch['label']), 1).cuda()
batch['tokens']['tokens'] = trigger_sequence_tensor
return batch
def get_accuracy(model, dev_dataset, vocab, trigger_token_ids=None, snli=False):
model.get_metrics(reset=True)
model.eval()
if trigger_token_ids is None:
evaluate_batch(model, dev_dataset, trigger_token_ids, snli)
print("Without Triggers: " + str(model.get_metrics()['accuracy']))
else:
print_string = ""
for idx in trigger_token_ids:
print_string = print_string + vocab.get_token_from_index(idx) + ', '
evaluate_batch(model, dev_dataset, trigger_token_ids, snli)
print("Current Triggers: " + print_string + " : " + str(model.get_metrics()['accuracy']))
return model.get_metrics()['accuracy']
def evaluate_batch(model, batch, trigger_token_ids=None, snli=False):
"""
Takes a batch of classification examples (SNLI or SST), and runs them through the model.
If trigger_token_ids is not None, then it will append the tokens to the input.
This funtion is used to get the model's accuracy and/or the loss with/without the trigger.
"""
batch = move_to_device(batch, cuda_device=0)
if trigger_token_ids is None:
output_dict = model(batch['tokens'], batch['label'])
return output_dict
else:
if isinstance(trigger_token_ids, dict):
original_tokens = batch['tokens']['tokens'].clone()
batch['tokens']['tokens'] = trigger_token_ids['tokens']['tokens']
output_dict = model(batch['tokens'], batch['label'])
batch['tokens']['tokens'] = original_tokens
else:
original_tokens = batch['tokens']['tokens'].clone()
batch['tokens']['tokens'] = torch.tensor([trigger_token_ids]).cuda()
output_dict = model(batch['tokens'], batch['label'])
batch['tokens']['tokens'] = original_tokens
return output_dict
def get_average_grad(model, batch, trigger_token_ids, target_label=None, snli=False, is_df = False):
"""
Computes the average gradient w.r.t. the trigger tokens when prepended to every example
in the batch. If target_label is set, that is used as the ground-truth label.
"""
# create an dummy optimizer for backprop
optimizer = optim.Adam(model.parameters())
optimizer.zero_grad()
# prepend triggers to the batch
original_labels = batch['label'].clone()
if target_label is not None:
# set the labels equal to the target (backprop from the target class, not model prediction)
batch['label'] = int(target_label) * torch.ones_like(batch['label']).cuda()
global extracted_grads
extracted_grads = [] # clear existing stored grads
loss = evaluate_batch(model, batch, trigger_token_ids, snli)['loss']
loss.backward()
# print(loss)
# index 0 has the hypothesis grads for SNLI. For SST, the list is of size 1.
grads = extracted_grads[0].cpu()
batch['label'] = original_labels # reset labels
# average grad across batch size, result only makes sense for trigger tokens at the front
averaged_grad = torch.sum(grads, dim=0)
# print(averaged_grad.size())
trig_length = get_trig_length(trigger_token_ids)
averaged_grad = averaged_grad[0:trig_length] # return just trigger grads
return averaged_grad
def get_trig_length(trigger):
all_tokens = trigger['tokens']['tokens'][0].detach().cpu().numpy()
return len(all_tokens)
def get_best_candidates(model, batch, trigger_token_ids, cand_trigger_token_ids, \
snli=False, beam_size=1, increase_loss=False, is_df=False):
""""
Given the list of candidate trigger token ids (of number of trigger words by number of candidates
per word), it finds the best new candidate trigger.
This performs beam search in a left to right fashion.
"""
# first round, no beams, just get the loss for each of the candidates in index 0.
# (indices 1-end are just the old trigger)
if increase_loss:
beamer = heapq.nlargest
else:
beamer = heapq.nsmallest
trigger_tokens = list(trigger_token_ids['tokens']['tokens'][0].cpu().detach().numpy())
loss_per_candidate = get_loss_per_candidate(0, model, batch, trigger_tokens,
cand_trigger_token_ids, snli, is_df)
# maximize the loss
rand_ind = random.randint(0,beam_size-1)
# rand_ind = 0
top_candidates = [beamer(beam_size, loss_per_candidate, key=itemgetter(1))[rand_ind]]
# top_candidates now contains beam_size trigger sequences, each with a different 0th token
for idx in range(1, len(trigger_tokens)): # for all trigger tokens, skipping the 0th (we did it above)
loss_per_candidate = []
for cand, _ in top_candidates: # for all the beams, try all the candidates at idx
loss_per_candidate.extend(get_loss_per_candidate(idx, model, batch, cand,
cand_trigger_token_ids, snli, is_df))
# print(loss_per_candidate)
top_candidates = [beamer(beam_size, loss_per_candidate, key=itemgetter(1))[rand_ind]]
# print(max(top_candidates, key=itemgetter(1)))
if increase_loss:
output = max(top_candidates, key=itemgetter(1))
else:
output = min(top_candidates, key=itemgetter(1))
return output[0], output[1]
def get_loss_per_candidate(index, model, batch, trigger_token_ids, cand_trigger_token_ids, snli=False, is_df=False):
"""
For a particular index, the function tries all of the candidate tokens for that index.
The function returns a list containing the candidate triggers it tried, along with their loss.
"""
if isinstance(cand_trigger_token_ids[0], (numpy.int64, int)):
print("Only 1 candidate for index detected, not searching")
return trigger_token_ids
model.get_metrics(reset=True)
loss_per_candidate = []
# loss for the trigger without trying the candidates
curr_loss = evaluate_batch(model, batch, trigger_token_ids, snli)['loss'].cpu().detach().numpy()
loss_per_candidate.append((deepcopy(trigger_token_ids), curr_loss))
for cand_id in range(len(cand_trigger_token_ids[0])):
trigger_token_ids_one_replaced = deepcopy(trigger_token_ids) # copy trigger
trigger_token_ids_one_replaced[index] = cand_trigger_token_ids[index][cand_id] # replace one token
loss = evaluate_batch(model, batch, trigger_token_ids_one_replaced, snli)['loss'].cpu().detach().numpy()
loss_per_candidate.append((deepcopy(trigger_token_ids_one_replaced), loss))
return loss_per_candidate