-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
275 lines (245 loc) · 9.26 KB
/
utils.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
'''
Author: roy
Date: 2020-10-30 22:18:56
LastEditTime: 2020-11-10 09:22:35
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /LAMA/utils.py
'''
from transformers import AutoModelForMaskedLM, AutoTokenizer
import copy
import torch
import torch.nn as nn
import torch.nn.utils.prune as prune
from torch.distributions import Bernoulli
import jsonlines
import prettytable as pt
import sys
from typing import List, Dict
from tqdm import tqdm
class FoobarPruning(prune.BasePruningMethod):
"""
Customized Pruning Method
"""
PRUNING_TYPE = 'unstructured'
def __init__(self, pregenerated_mask) -> None:
super().__init__()
self.pre_generated_mask = pregenerated_mask
def compute_mask(self, t, default_mask):
"""
"""
mask = self.pre_generated_mask
return mask
def Foobar_pruning(module, name, mask=None):
"""
util function for pruning parameters of given module.name using corresponding mask generated by relation-specific mask generator
Parameters:
module: subclass of nn.Module
name: name of parameters to be pruned
id: id for the parameters in the parameters_tobe_pruned list
"""
sub_module = getattr(module, name)
shape = sub_module.size()
if mask is not None and isinstance(mask, (torch.Tensor, torch.nn.Parameter)):
assert shape == mask.size(
), "size of mask and parameters not consistent: {} != {}".format(mask.size(), shape)
FoobarPruning.apply(module, name, pregenerated_mask=mask)
return module
def remove_prune_reparametrization(module, name):
"""
make pruning permanent
"""
prune.remove(module, name)
def restore_init_state(model: torch.nn.Module, init_state):
"""
load copyed initial state dict after prune.remove
"""
model.load_state_dict(init_state)
def freeze_parameters(model):
"""
freeze all parameters of input model
"""
for p in model.parameters():
p.requires_grad = False
def bernoulli_hard_sampler(probs, require_logprob: bool = True):
"""
Hard sampler for bernoulli distribution
"""
Bernoulli_Sampler = Bernoulli(probs=probs)
sample = Bernoulli_Sampler.sample()
if require_logprob:
log_probs_of_sample = Bernoulli_Sampler.log_prob(sample)
return sample, log_probs_of_sample
return sample
def bernoulli_soft_sampler(logits, temperature: float = 0.1):
"""
Soft sampler for bernoulli distribution
"""
device = logits.device
uniform_variables = torch.rand(*logits.size()).to(device)
assert uniform_variables.shape == logits.shape
samples = torch.sigmoid(
(logits + torch.log(uniform_variables) - torch.log(1-uniform_variables)) / temperature)
return samples
def LAMA(model, tokenizer, device, input_w_mask, topk=5):
# model.eval()
if '[MASK]' != tokenizer.mask_token:
input_w_mask = input_w_mask.replace('[MASK]', tokenizer.mask_token)
inputs = tokenizer(input_w_mask, return_tensors='pt')
mask_id = inputs['input_ids'][0].tolist().index(tokenizer.mask_token_id)
inputs.to(device)
outputs = model(**inputs)
logits = outputs.logits
probs = torch.softmax(logits[0, mask_id], dim=-1)
_, indices = torch.topk(probs, k=topk)
predictions = []
for token in tokenizer.decode(indices).strip().split(" "):
predictions.append(token.lower())
return predictions
def save_pruning_masks_generators(args, model_name: str, pruning_masks_generators: List[List], id_to_relation: Dict, save_dir: str):
"""
Save pruning mask generators specified with model name, relation type and number of transformer blocks of interest.
"""
if "/" in model_name:
model_name = model_name.split("/")[-1]
for i in range(len(id_to_relation)):
relation_str = id_to_relation[i]
type = "soft" if args.soft_infer and args.soft_train else "hard"
file_prefix = "{}/{}_{}_{}_{}_{}_init>{}_{}.pickle".format(save_dir,
model_name, relation_str, len(pruning_masks_generators[i]), args.bottom_layer_index, args.top_layer_index, args.init_method, type)
with open(file_prefix, mode='wb') as f:
torch.save(pruning_masks_generators[i], f)
print("Pruning mask generators for {} is saved at {}".format(
relation_str, file_prefix))
def sparsity(model, init_method: str):
# sparsity
try:
if init_method == 'ones':
v = 1.0
else:
v = float(init_method)
except Exception:
return dict()
threshold = torch.sigmoid(torch.tensor(v)).item()
sparsities = dict()
id_to_relation = model.id_to_relation
for i in range(len(model.pruning_mask_generators)):
total_cnt = 0
cnt = 0
pruning_masks = model.pruning_mask_generators[i]
for p in pruning_masks:
bernoulli_p = torch.sigmoid(p.data)
bernoulli_p = bernoulli_p < threshold
cnt = bernoulli_p.int().sum().item()
total_cnt += p.nelement()
sparsities[id_to_relation[i]] = cnt / total_cnt
return sparsities
def relation_miner(context: str):
"""
Return a set of possible commonsense relations given the context
"""
token2rels = {
'use': ['UsedFor'],
'used': ['UsedFor'],
'where': ['AtLocation'],
'Where': ['AtLocation'],
'cause': ['Causes'],
'cause': ['Causes'],
'desire': ['Desires'],
'desires': ['Desires'],
'in': ['AtLocation'],
'happen': ['HasSubevent']
}
raise NotImplementedError
def test(argv):
bert_name = argv[1]
device = torch.device('cuda:{}'.format(argv[2]))
# masks = torch.nn.Parameter(torch.empty(768, 768))
# opt = torch.optim.Adam(masks, lr=3e-4)
# opt.zero_grad()
# torch.nn.init.zeros_(masks)
# soft_samples = bernoulli_soft_sampler(masks, temperature=0.1)
# assert soft_samples.requires_grad == True, "no grad associated with soft samples"
# testing
bert = AutoModelForMaskedLM.from_pretrained(
bert_name, return_dict=True).to(device)
bert.eval()
freeze_parameters(bert)
# init_state = copy.deepcopy(bert.state_dict())
tokenizer = AutoTokenizer.from_pretrained(bert_name, use_fast=True)
print(tokenizer.mask_token_id)
parameters_tobe_pruned = []
# for i in range(8, 12):
# parameters_tobe_pruned.append(
# (bert.bert.encoder.layer[i].attention.self.query, 'weight'))
# parameters_tobe_pruned.append(
# (bert.bert.encoder.layer[i].attention.self.key, 'weight'))
# parameters_tobe_pruned.append(
# (bert.bert.encoder.layer[i].attention.self.value, 'weight'))
# parameters_tobe_pruned.append(
# (bert.bert.encoder.layer[i].attention.output.dense, 'weight'))
# parameters_tobe_pruned.append(
# (bert.bert.encoder.layer[i].intermediate.dense, 'weight'))
# parameters_tobe_pruned.append(
# (bert.bert.encoder.layer[i].output.dense, 'weight'))
# parameters_tobe_pruned = tuple(parameters_tobe_pruned)
# # prune
# for module, name in parameters_tobe_pruned:
# prune.random_unstructured(module, name, amount=0.30)
# Foobar_pruning(module, name, soft_samples[0])
# print(sparsity(bert))
corpus_fileobj = open("./data/ConceptNet/test.jsonl",
mode='r', encoding='utf-8')
total_loss = .0
cnt = 0
top1 = 0
top2 = 0
top3 = 0
for instance in jsonlines.Reader(corpus_fileobj):
cnt += 1
text = instance['masked_sentences'][0].replace(
'[MASK]', tokenizer.mask_token)
obj_label = instance['obj_label'].lower()
input_dict = tokenizer(text, return_tensors='pt').to(device)
mask_index = input_dict['input_ids'][0].tolist().index(
tokenizer.mask_token_id)
labels = input_dict['input_ids'].clone().to(device)
labels.fill_(-100)
labels[0, mask_index] = tokenizer.convert_tokens_to_ids([obj_label])[0]
outputs = bert(**input_dict, labels=labels)
logits = outputs.logits
loss = outputs.loss
probs = torch.softmax(logits[0, mask_index], dim=-1)
_, indices = torch.topk(probs, k=5)
predictions = tokenizer.decode(indices).strip().split()
try:
if obj_label == predictions[0].lower():
top1 += 1
if obj_label in [predictions[0].lower(), predictions[1].lower()]:
top2 += 1
if obj_label in [predictions[0].lower(), predictions[1].lower(), predictions[2].lower()]:
top3 += 1
except Exception:
pass
# if loss.item() <= 2.2:
# print(text)
# print(obj_label)
# print(LAMA(bert, tokenizer, device, text))
# exit()
# print(loss)
total_loss += loss.detach().item()
print(cnt)
# print(LAMA(bert, tokenizer, torch.device('cpu'), text))
p1 = top1 / cnt
p2 = top2 / cnt
p3 = top3 / cnt
print(argv[1])
print(total_loss / cnt)
print('P@1:', p1)
print('P@2:', p2)
print('P@3:', p3)
if __name__ == "__main__":
argv = sys.argv
test(argv)
# for module, name in parameters_tobe_pruned:
# remove_prune_reparametrization(module, name)