-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransformer_lm_sci.py
345 lines (297 loc) · 11.3 KB
/
transformer_lm_sci.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
# compute sci / weight norms / attention sparsity for an LM
import argparse
import os
from data_utils import (
build_datasets_dyck,
build_datasets_lm,
build_datasets_tense_inflection,
)
from train_transformers import get_base_transformer_lm
import torch
import numpy as np
from data_utils.dyck_helpers import read_dyck_data
from data_utils.lm_dataset_helpers import read_lm_data
from data_utils.tense_inflection_helpers import read_ti_data
from tree_projections import (
get_tree_projection,
get_parsing_accuracy,
get_sparsity_scores_helper,
)
from tqdm import tqdm
from util import set_seed
import random
from parse_q_and_tense import parse_question, parse_tense, convert_to_parse
def get_gold_parse(dataset_type, sent):
if dataset_type == "lm":
parse = convert_to_parse(sent, parse_question(sent))
elif dataset_type == "tense":
parse = convert_to_parse(sent, parse_tense(sent))
else:
raise Exception
return parse
def process(sents, split_by_words):
def remove_fullstop(sent_list):
if sent_list[-1] == ".":
return sent_list[:-1]
new_sents = []
target_words = []
for sent in sents:
split_word = None
sent_words = sent.split(" ")
for word in split_by_words:
if word in sent_words:
split_word = word
break
if split_word is None:
continue
idx = sent_words.index(split_word)
target_words.append(sent_words[idx + 1])
new_sents.append(" ".join(remove_fullstop(sent_words[:idx])))
return new_sents, target_words
def get_norm(model):
total_norm = 0
parameters = [p for p in model.parameters()]
for p in parameters:
param_norm = p.detach().data.norm(2)
total_norm += param_norm.item() ** 2
total_norm = total_norm**0.5
return total_norm
def compute_attention_sparsity(args, model_name):
args.vec_dim = 512
args.n_heads = 4
args.gpu_id = 0
mname = model_name.split("/")[-2]
folder_name = "MODEL_SPARSITY/{}_sparsity".format(mname)
checkpoint = model_name.split("/")[-1].split(".")[0]
if os.path.exists("{}/{}.txt".format(folder_name, checkpoint)):
return
if args.dataset == "dyck":
_, in_vocab, _ = build_datasets_dyck()
in_sentences = read_dyck_data([args.split], 20)
if len(in_sentences) > 500:
idxs = random.sample(
[
idx
for idx, sent in enumerate(in_sentences)
if len(sent.split(" ")) < 200
],
k=500,
)
in_sentences = [in_sentences[idx] for idx in idxs]
elif args.dataset == "lm":
_, in_vocab, _ = build_datasets_lm()
in_sentences, _ = read_lm_data([args.split])
in_sentences, targets = process(in_sentences, split_by_words=["quest"])
if len(in_sentences) > 10000:
idxs = random.sample([idx for idx, _ in enumerate(in_sentences)], k=10000)
in_sentences = [in_sentences[idx] for idx in idxs]
elif args.dataset == "tense":
_, in_vocab, _ = build_datasets_tense_inflection()
in_sentences, _ = read_ti_data([args.split])
in_sentences, targets = process(
in_sentences, split_by_words=["PRESENT", "PAST"]
)
if len(in_sentences) > 10000:
idxs = random.sample([idx for idx, _ in enumerate(in_sentences)], k=10000)
in_sentences = [in_sentences[idx] for idx in idxs]
else:
raise Exception
lm, _ = get_base_transformer_lm(args, in_vocab, model_name=model_name)
device = torch.device("cuda:{}".format(args.gpu_id))
lm.to(device)
def tokenizer(s, add_special_tokens=True):
if add_special_tokens:
return [lm.encoder_sos] + in_vocab(s)
else:
return in_vocab(s)
attn_sparsity = get_sparsity_scores_helper(lm, tokenizer, in_sentences)
if not os.path.exists(folder_name):
os.makedirs(folder_name)
with open("{}/{}.txt".format(folder_name, checkpoint), "w") as writer:
writer.write(str(attn_sparsity))
writer.write("\n")
def compute_model_norm(args, model_name):
args.vec_dim = 512
args.n_heads = 4
args.gpu_id = 0
if args.dataset == "dyck":
_, in_vocab, _ = build_datasets_dyck()
elif args.dataset == "lm":
_, in_vocab, _ = build_datasets_lm()
elif args.dataset == "tense":
_, in_vocab, _ = build_datasets_tense_inflection()
else:
raise Exception
lm, _ = get_base_transformer_lm(args, in_vocab, model_name=model_name)
device = torch.device("cuda:{}".format(args.gpu_id))
lm.to(device)
model_norm = get_norm(lm)
mname = model_name.split("/")[-2]
folder_name = "MODEL_NORM/{}_norm".format(mname)
checkpoint = model_name.split("/")[-1].split(".")[0]
if not os.path.exists(folder_name):
os.makedirs(folder_name)
with open("{}/{}.txt".format(folder_name, checkpoint), "w") as writer:
writer.write(str(model_norm))
writer.write("\n")
def compute_sci_helper_fn(
args, in_vocab, model_name, in_sentences, targets, gold_parses, ret_vals
):
lm, _ = get_base_transformer_lm(args, in_vocab, model_name=model_name)
device = torch.device("cuda:{}".format(args.gpu_id))
lm.to(device)
mname = model_name.split("/")[-2]
folder_name = "SCI_SCORES/{}_sci".format(mname)
checkpoint = model_name.split("/")[-1].split(".")[0]
print(checkpoint)
if not os.path.exists(folder_name):
os.makedirs(folder_name)
if os.path.exists("{}/{}.txt".format(folder_name, checkpoint)) and not ret_vals:
return
def tokenizer(s, add_special_tokens=True):
if add_special_tokens:
return [lm.encoder_sos] + in_vocab(s)
else:
return in_vocab(s)
total_sci_score = 0.0
pred_parses = []
batch_size = 1
st = 0
with tqdm(total=len(in_sentences)) as progress_bar:
while st < len(in_sentences):
en = min(len(in_sentences), st + batch_size)
output = get_tree_projection(
in_sentences[st:en][0],
lm,
tokenizer,
st_threshold=0,
verbose=True,
sim_fn="cosine",
normalize=True,
layer_id=-1,
is_lm=True,
)
pred_parses += [output["pred_parse"]]
total_sci_score += np.sum([output["pred_parse_score"]])
progress_bar.update(en - st)
st = en
score = total_sci_score / len(in_sentences)
if gold_parses is not None:
parsing_acc = get_parsing_accuracy(pred_parses, gold_parses)["f1"]
else:
parsing_acc = 0.0
if ret_vals:
return score, pred_parses, gold_parses
else:
with open("{}/{}.txt".format(folder_name, checkpoint), "w") as writer:
writer.write(str(score))
writer.write("\n")
writer.write(str(parsing_acc))
writer.write("\n")
writer.write("\n")
def compute_sci(args, model_name, ret_vals=False):
args.vec_dim = 512
args.n_heads = 4
args.gpu_id = 0
if args.dataset == "dyck":
_, in_vocab, _ = build_datasets_dyck()
in_sentences = read_dyck_data([args.split], 20)
if len(in_sentences) > 500:
idxs = random.sample(
[idx for idx, sent in enumerate(in_sentences)],
k=500,
)
in_sentences = [in_sentences[idx] for idx in idxs]
targets = None
gold_parses = None
elif args.dataset == "lm":
_, in_vocab, _ = build_datasets_lm()
in_sentences, _ = read_lm_data([args.split])
in_sentences, targets = process(in_sentences, split_by_words=["quest"])
if len(in_sentences) > 10000:
idxs = random.sample([idx for idx, _ in enumerate(in_sentences)], k=10000)
in_sentences = [in_sentences[idx] for idx in idxs]
targets = [targets[idx] for idx in idxs]
gold_parses = [
get_gold_parse("lm", "{} . quest".format(sent)) for sent in in_sentences
]
elif args.dataset == "tense":
_, in_vocab, _ = build_datasets_tense_inflection()
in_sentences, _ = read_ti_data([args.split])
in_sentences, targets = process(
in_sentences, split_by_words=["PRESENT", "PAST"]
)
if len(in_sentences) > 10000:
idxs = random.sample([idx for idx, _ in enumerate(in_sentences)], k=10000)
in_sentences = [in_sentences[idx] for idx in idxs]
targets = [targets[idx] for idx in idxs]
gold_parses = [
get_gold_parse("tense", "{} . present".format(sent))
for sent in in_sentences
]
else:
raise Exception
return compute_sci_helper_fn(
args, in_vocab, model_name, in_sentences, targets, gold_parses, ret_vals
)
def get_idxs_res(dataset, res):
if dataset == "dyck":
return [10000] + [10000 * idx for idx in range(res, 51, res)]
else:
return [3000] + [3000 * idx for idx in range(res, 101, res)]
def flatten(l_o_l):
return [x for l in l_o_l for x in l]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", type=str)
parser.add_argument("--split", type=str, default="train")
parser.add_argument("--dataset", type=str, default="dyck")
parser.add_argument("--encoder_n_layers", default=4, type=int)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--dummy", action="store_true")
parser.add_argument("--resolution", type=int, default=-1)
parser.add_argument("--compute_norm", action="store_true")
parser.add_argument("--compute_sparsity", action="store_true")
args = parser.parse_args()
# main(args)
# eval2(args)
set_seed(args.seed)
if args.compute_norm:
idxs = get_idxs_res(args.dataset, args.resolution)
all_model_names = [
"{}/checkpoint_{}.pickle".format(args.model_name, idx) for idx in idxs
]
for model_name in all_model_names:
compute_model_norm(args, model_name)
elif args.compute_sparsity:
idxs = get_idxs_res(args.dataset, args.resolution)
all_model_names = [
"{}/checkpoint_{}.pickle".format(args.model_name, idx) for idx in idxs
]
for model_name in all_model_names:
compute_attention_sparsity(args, model_name)
elif not args.dummy:
if args.resolution == -1:
compute_sci(args, args.model_name)
else:
### args.model_name is now the path
res_list = [10, 5]
if args.resolution != 1:
idxs = get_idxs_res(args.dataset, args.resolution)
else:
idxs = range(3000, 303000, 3000)
other_res = set(
flatten(
[
get_idxs_res(args.dataset, res)
for res in res_list
if res > args.resolution
]
)
)
idxs = [idx for idx in idxs if idx not in other_res]
all_model_names = [
"{}/checkpoint_{}.pickle".format(args.model_name, idx) for idx in idxs
]
for model_name in all_model_names:
compute_sci(args, model_name)