-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
167 lines (143 loc) · 7.29 KB
/
run.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
import os
import numpy as np
import torch
from transformers import BartTokenizer, BartConfig
from transformers import AdamW, get_linear_schedule_with_warmup
from dataset import MyDatasetCollection
from bart import MyBart
from utils import freeze_embeds, trim_batch
from tqdm import tqdm
def run(args, logger):
tokenizer = BartTokenizer.from_pretrained(args.model)
train_data = MyDatasetCollection(logger, args, args.train_file, True)
dev_data = MyDatasetCollection(logger, args, args.predict_file, False)
train_data.load_dataset(tokenizer)
train_data.load_dataloader()
dev_data.load_dataset(tokenizer)
dev_data.load_dataloader()
if args.do_train:
if args.checkpoint is not None:
def convert_to_single_gpu(state_dict):
def _convert(key):
if key.startswith('module.'):
return key[7:]
return key
return {_convert(key):value for key, value in state_dict.items()}
model = MyBart.from_pretrained(args.model,
state_dict=convert_to_single_gpu(torch.load(args.checkpoint)))
else:
model = MyBart.from_pretrained(args.model)
if args.freeze_embeds:
logger.info("Freezing embeddings")
freeze_embeds(model)
if args.n_gpu>1:
model = torch.nn.DataParallel(model)
if torch.cuda.is_available():
model.to(torch.device("cuda"))
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': args.weight_decay},
{'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
num_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
logger.info("#Params: {}".format(num_parameters))
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps=args.warmup_steps,
num_training_steps=args.total_steps)
train(args, logger, model, train_data, dev_data, optimizer, scheduler)
if args.do_predict:
checkpoint = os.path.join(args.output_dir, args.predict_checkpoint)
def convert_to_single_gpu(state_dict):
def _convert(key):
if key.startswith('module.'):
return key[7:]
return key
return {_convert(key):value for key, value in state_dict.items()}
model = MyBart.from_pretrained(args.model,
state_dict=convert_to_single_gpu(torch.load(checkpoint)))
logger.info("Loading checkpoint from {}".format(checkpoint))
if torch.cuda.is_available():
model.to(torch.device("cuda"))
model.eval()
ems = inference(model, dev_data, save_predictions=True, verbose=True)
logger.info("%s on %s data: %.2f" % (dev_data.metric, dev_data.data_type, np.mean(ems)*100))
def train(args, logger, model, train_data, dev_data, optimizer, scheduler):
model.train()
global_step = 0
train_losses = []
best_accuracy = (-1.0, -1.0, -1.0) if args.dataset == "zest" else -1.0
stop_training=False
logger.info("Starting training!")
for epoch in range(int(args.num_train_epochs)):
for batch in tqdm(train_data.dataloader, desc="Epoch {}".format(epoch)):
global_step += 1
if torch.cuda.is_available():
batch = [b.to(torch.device("cuda")) for b in batch]
pad_token_id = train_data.tokenizer.pad_token_id
batch[0], batch[1] = trim_batch(batch[0], pad_token_id, batch[1])
batch[2], batch[3] = trim_batch(batch[2], pad_token_id, batch[3])
loss = model(input_ids=batch[0], attention_mask=batch[1],
decoder_input_ids=batch[2], decoder_attention_mask=batch[3],
is_training=True)
if args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu.
if torch.isnan(loss).data:
logger.info("Stop training because loss=%s" % (loss.data))
stop_training=True
break
train_losses.append(loss.detach().cpu())
loss.backward()
if global_step % args.gradient_accumulation_steps == 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step() # We have accumulated enough gradients
scheduler.step()
model.zero_grad()
if global_step % args.eval_period == 0:
model.eval()
curr_em = inference(model if args.n_gpu==1 else model.module, dev_data)
logger.info("Step %d Train loss %.2f %s %s on epoch=%d" % (
global_step,
np.mean(train_losses),
dev_data.metric,
curr_em,
epoch))
train_losses = []
if best_accuracy < curr_em:
model_state_dict = {k:v.cpu() for (k, v) in model.state_dict().items()}
torch.save(model_state_dict, os.path.join(args.output_dir, "best-model.pt"))
logger.info("Saving model with best %s: %s -> %s on epoch=%d, global_step=%d" % \
(dev_data.metric, best_accuracy, curr_em, epoch, global_step))
best_accuracy = curr_em
wait_step = 0
stop_training = False
else:
wait_step += 1
if wait_step >= args.wait_step:
stop_training = True
break
model.train()
if stop_training:
break
model_state_dict = {k:v.cpu() for (k, v) in model.state_dict().items()}
torch.save(model_state_dict, os.path.join(args.output_dir, "last-model.pt"))
def inference(model, dev_data, save_predictions=False, verbose=False):
predictions = []
bos_token_id = dev_data.tokenizer.bos_token_id
for i, batch in enumerate(dev_data.dataloader):
if torch.cuda.is_available():
batch = [b.to(torch.device("cuda")) for b in batch]
pad_token_id = dev_data.tokenizer.pad_token_id
batch[0], batch[1] = trim_batch(batch[0], pad_token_id, batch[1])
outputs = model.generate(input_ids=batch[0],
attention_mask=batch[1],
num_beams=dev_data.args.num_beams,
max_length=dev_data.args.max_output_length,
decoder_start_token_id=model.config.bos_token_id,
early_stopping=dev_data.gen_early_stop,)
for input_, output in zip(batch[0], outputs):
pred = dev_data.decode(output)
predictions.append(pred)
if save_predictions:
dev_data.save_predictions(predictions)
return dev_data.evaluate(predictions, verbose=verbose)