-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt5_ft.py
289 lines (237 loc) · 8.66 KB
/
mt5_ft.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
# -*- coding:utf-8 _*-
import os
import sys
import time
import math
import random
import argparse
import numpy as np
from sklearn.metrics import (
precision_score, recall_score, f1_score)
import torch
from torch import cuda
from transformers import logging
from transformers import (
MT5TokenizerFast,
MT5ForConditionalGeneration)
from polynomial_lr_decay import PolynomialLRDecay
logging.set_verbosity_error()
os.environ["TOKENIZERS_PARALLELISM"] = "false"
device = 'cuda' if cuda.is_available() else 'cpu'
model_name = 'google/mt5-base'
tokenizer = MT5TokenizerFast.from_pretrained(model_name)
def read_insts(mode, lang, form, prompt, max_len=200, ups_num=5000):
"""
Read instances
"""
src, tgt = [], []
literal = tokenizer.encode('Literal')
figure = tokenizer.encode(form.capitalize())
if len(prompt) > 0:
prompt = prompt.format(form.capitalize())
path = 'data/{}_{}_{}.{}'.format(mode, lang, form, '{}')
with open(path.format(0), 'r') as f0, \
open(path.format(1), 'r') as f1:
f0 = f0.readlines()
f1 = f1.readlines()
if mode != 'test':
# Keep label distribution balanced
if len(f0) > len(f1):
f0 = f0[:len(f1)]
else:
f1 = f1[:len(f0)]
# upsample
if mode == 'train' and len(f0) < ups_num:
f0 = (f0 * math.ceil(ups_num / len(f0)))[:ups_num]
f1 = (f1 * math.ceil(ups_num / len(f1)))[:ups_num]
for seqs, label in zip([f0, f1], [literal, figure]):
for seq in seqs:
seq = tokenizer.encode(prompt + seq.strip())
src.append(seq[:min(len(seq) - 1, max_len)] + seq[-1:])
tgt.append(label)
return src, tgt
def collate_fn(insts):
"""
Pad the instance to the max seq length in batch
"""
pad_id = tokenizer.pad_token_id
max_len = max(len(inst) for inst in insts)
batch_seq = [inst + [pad_id] * (max_len - len(inst))
for inst in insts]
batch_seq = torch.LongTensor(batch_seq)
return batch_seq
def paired_collate_fn(insts):
src_inst, tgt_inst = list(zip(*insts))
src_inst = collate_fn(src_inst)
tgt_inst = collate_fn(tgt_inst)
return src_inst, tgt_inst
class MMFLUDataset(torch.utils.data.Dataset):
""" Seq2Seq Dataset """
def __init__(self, src_inst, tgt_inst):
self.src_inst = src_inst
self.tgt_inst = tgt_inst
def __len__(self):
return len(self.src_inst)
def __getitem__(self, idx):
return self.src_inst[idx], self.tgt_inst[idx]
def MMFLUIterator(src, tgt, opt, shuffle=True):
"""
Data iterator for classifier
"""
loader = torch.utils.data.DataLoader(
MMFLUDataset(
src_inst=src,
tgt_inst=tgt),
num_workers=2,
batch_size=opt.batch_size,
collate_fn=paired_collate_fn,
shuffle=shuffle)
return loader
def seq2label(seqs, tokenizer):
pred = []
for ids in seqs:
x = tokenizer.decode(
ids, skip_special_tokens=True,
clean_up_tokenization_spaces=False)
pred.append(x.strip('</s> '))
return pred
def evaluate(model, loader, epoch, tokenizer):
"""
Evaluation function
"""
model.eval()
pred, true = [], []
with torch.no_grad():
for i, batch in enumerate(loader):
src, tgt = map(lambda x: x.to(device), batch)
mask = src.ne(tokenizer.pad_token_id).long()
outs = model.generate(
input_ids=src,
attention_mask=mask,
num_beams=5,
max_length=10)
pred.extend(seq2label(outs, tokenizer))
true.extend(seq2label(tgt, tokenizer))
acc = sum([1 if i == j else 0 for i, j in zip(pred, true)]) / len(pred)
model.train()
print('[Info] {:02d}-valid: acc {:.4f}'.format(epoch, acc))
return acc
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-seed', default=42, type=int, help='random seed')
parser.add_argument(
'-lang', nargs='+', help='language names', required=True)
parser.add_argument(
'-form', nargs='+', help='figure of speech', required=True)
parser.add_argument(
'-prompt', default='', type=str, help='prompt')
parser.add_argument(
'-batch_size', default=32, type=int, help='batch size')
parser.add_argument(
'-lr', default=1e-4, type=float, help='ini. learning rate')
parser.add_argument(
'-log_step', default=100, type=int, help='log every x step')
parser.add_argument(
'-epoch', default=80, type=int, help='force stop at x epoch')
parser.add_argument(
'-eval_step', default=1000, type=int, help='eval every x step')
opt = parser.parse_args()
print('[Info]', opt)
torch.manual_seed(opt.seed)
save_path = 'checkpoints/mt5_{}_{}.chkpt'.format(
'_'.join(opt.lang), '_'.join(opt.form))
# read instances from input file
train_src, train_tgt, valid_src, valid_tgt = [], [], [], []
for lang in opt.lang:
for form in opt.form:
path = 'data/train_{}_{}.0'.format(lang, form)
if not os.path.exists(path):
continue
train_0, train_1 = read_insts(
'train', lang, form, opt.prompt)
valid_0, valid_1 = read_insts(
'valid', lang, form, opt.prompt)
train_src.extend(train_0)
train_tgt.extend(train_1)
valid_src.extend(valid_0)
valid_tgt.extend(valid_1)
print('[Info] {} insts of train set in {}-{}'.format(
len(train_0), lang, form))
print('[Info] {} insts of valid set in {}-{}'.format(
len(valid_0), lang, form))
train_loader = MMFLUIterator(train_src, train_tgt, opt)
valid_loader = MMFLUIterator(valid_src, valid_tgt, opt)
model = MT5ForConditionalGeneration.from_pretrained(model_name)
model = model.to(device).train()
optimizer = torch.optim.Adam(
filter(lambda x: x.requires_grad, model.parameters()),
lr=opt.lr, betas=(0.9, 0.98), eps=1e-09)
scheduler = PolynomialLRDecay(
optimizer,
warmup_steps=1000,
max_decay_steps=10000,
end_learning_rate=5e-5,
power=2)
loss_list = []
start = time.time()
eval_acc, tab = 0, 0
patience = 6
for epoch in range(opt.epoch):
for batch in train_loader:
src, tgt = map(lambda x: x.to(device), batch)
optimizer.zero_grad()
mask = src.ne(tokenizer.pad_token_id).long()
loss = model(src, mask, labels=tgt)[0]
loss.backward()
scheduler.step()
optimizer.step()
loss_list.append(loss.item())
if scheduler.steps % opt.log_step == 0:
lr = optimizer.param_groups[0]['lr']
print('[Info] {:02d}-{:05d}: loss {:.4f} | '
'lr {:.5f} | sec {:.3f}'.format(
epoch, scheduler.steps, np.mean(loss_list),
lr, time.time() - start))
loss_list = []
start = time.time()
if ((len(train_loader) >= opt.eval_step
and scheduler.steps % opt.eval_step == 0)
or (len(train_loader) < opt.eval_step
and scheduler.steps % len(train_loader) == 0
and scheduler.steps > 1000)
or scheduler.steps == 1000):
valid_acc = evaluate(
model,
valid_loader,
epoch,
tokenizer)
if eval_acc < valid_acc:
eval_acc = valid_acc
torch.save(model.state_dict(), save_path)
print('[Info] The checkpoint has been updated.')
tab = 0
else:
tab += 1
if tab == patience:
break
# evaluation
print('[Info] Evaluation')
model.load_state_dict(torch.load(save_path))
for form in opt.form:
for lang in opt.lang:
path = 'data/test_{}_{}.0'.format(lang, form)
if not os.path.exists(path):
continue
test_0, test_1 = read_insts(
'test', lang, form, opt.prompt)
test_loader = MMFLUIterator(test_0, test_1, opt)
print('[Info] {} insts of {}-{}'.format(
len(test_0), lang, form))
evaluate(
model,
test_loader,
0,
tokenizer)
if __name__ == '__main__':
main()