/
transformer_main.py
253 lines (222 loc) · 9.71 KB
/
transformer_main.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
# Copyright 2019 The Texar Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Transformer model.
"""
import argparse
import functools
import importlib
import os
import pickle
import random
import torch
from tqdm import tqdm
from torchtext import data
import texar as tx
from texar.modules import Transformer
from bleu_tool import bleu_wrapper
from utils import data_utils, utils
from utils.preprocess import eos_token_id
parser = argparse.ArgumentParser()
parser.add_argument("--config_model",
type=str,
default="config_model",
help="The model config.")
parser.add_argument("--config_data",
type=str,
default="config_iwslt15",
help="The dataset config.")
parser.add_argument("--run_mode",
type=str,
default="train_and_evaluate",
help="Either train_and_evaluate or test.")
parser.add_argument("--model_dir",
type=str,
default="./outputs/",
help="Path to save the trained model and logs.")
parser.add_argument("--model_fn",
type=str,
default="best-model.ckpt",
help="Model filename to save the trained weights")
args = parser.parse_args()
config_model = importlib.import_module(args.config_model)
config_data = importlib.import_module(args.config_data)
utils.set_random_seed(config_model.random_seed)
def main():
"""Entry point.
"""
# Load data
train_data, dev_data, test_data = data_utils.load_data_numpy(
config_data.input_dir, config_data.filename_prefix)
with open(config_data.vocab_file, 'rb') as f:
id2w = pickle.load(f)
beam_width = getattr(config_model, "beam_width", 1)
# Create logging
tx.utils.maybe_create_dir(args.model_dir)
logging_file = os.path.join(args.model_dir, 'logging.txt')
logger = utils.get_logger(logging_file)
print(f"logging file is saved in: {logging_file}")
model = Transformer(config_model, config_data)
if torch.cuda.is_available():
model = model.cuda()
device = torch.cuda.current_device()
else:
device = None
best_results = {'score': 0, 'epoch': -1}
lr_config = config_model.lr_config
if lr_config["learning_rate_schedule"] == "static":
init_lr = lr_config["static_lr"]
scheduler_lambda = lambda x: 1.0
else:
init_lr = lr_config["lr_constant"]
scheduler_lambda = functools.partial(
utils.get_lr_multiplier, warmup_steps=lr_config["warmup_steps"])
optim = torch.optim.Adam(
model.parameters(), lr=init_lr, betas=(0.9, 0.997), eps=1e-9)
scheduler = torch.optim.lr_scheduler.LambdaLR(optim, scheduler_lambda)
def _eval_epoch(epoch, mode):
torch.cuda.empty_cache()
if mode == 'eval':
eval_data = dev_data
elif mode == 'test':
eval_data = test_data
else:
raise ValueError("`mode` should be either \"eval\" or \"test\".")
references, hypotheses = [], []
bsize = config_data.test_batch_size
for i in tqdm(range(0, len(eval_data), bsize)):
sources, targets = zip(*eval_data[i:i + bsize])
with torch.no_grad():
x_block = data_utils.source_pad_concat_convert(
sources, device=device)
predictions = model(
encoder_input=x_block,
is_train_mode=False,
beam_width=beam_width)
if beam_width == 1:
decoded_ids = predictions[0].sample_id
else:
decoded_ids = predictions["sample_id"][:, :, 0]
hypotheses.extend(h.tolist() for h in decoded_ids)
references.extend(r.tolist() for r in targets)
hypotheses = utils.list_strip_eos(hypotheses, eos_token_id)
references = utils.list_strip_eos(references, eos_token_id)
if mode == 'eval':
# Writes results to files to evaluate BLEU
# For 'eval' mode, the BLEU is based on token ids (rather than
# text tokens) and serves only as a surrogate metric to monitor
# the training process
# TODO: Use texar.evals.bleu
fname = os.path.join(args.model_dir, 'tmp.eval')
hwords, rwords = [], []
for hyp, ref in zip(hypotheses, references):
hwords.append([str(y) for y in hyp])
rwords.append([str(y) for y in ref])
hwords = tx.utils.str_join(hwords)
rwords = tx.utils.str_join(rwords)
hyp_fn, ref_fn = tx.utils.write_paired_text(
hwords, rwords, fname, mode='s',
src_fname_suffix='hyp', tgt_fname_suffix='ref')
eval_bleu = bleu_wrapper(ref_fn, hyp_fn, case_sensitive=True)
eval_bleu = 100. * eval_bleu
logger.info("epoch: %d, eval_bleu %.4f", epoch, eval_bleu)
print(f"epoch: {epoch:d}, eval_bleu {eval_bleu:.4f}")
if eval_bleu > best_results['score']:
logger.info("epoch: %d, best bleu: %.4f", epoch, eval_bleu)
best_results['score'] = eval_bleu
best_results['epoch'] = epoch
model_path = os.path.join(args.model_dir, args.model_fn)
logger.info("Saving model to %s", model_path)
print(f"Saving model to {model_path}")
states = {
'model': model.state_dict(),
'optimizer': optim.state_dict(),
'scheduler': scheduler.state_dict(),
}
torch.save(states, model_path)
elif mode == 'test':
# For 'test' mode, together with the cmds in README.md, BLEU
# is evaluated based on text tokens, which is the standard metric.
fname = os.path.join(args.model_dir, 'test.output')
hwords, rwords = [], []
for hyp, ref in zip(hypotheses, references):
hwords.append([id2w[y] for y in hyp])
rwords.append([id2w[y] for y in ref])
hwords = tx.utils.str_join(hwords)
rwords = tx.utils.str_join(rwords)
hyp_fn, ref_fn = tx.utils.write_paired_text(
hwords, rwords, fname, mode='s',
src_fname_suffix='hyp', tgt_fname_suffix='ref')
logger.info("Test output written to file: %s", hyp_fn)
print(f"Test output written to file: {hyp_fn}")
def _train_epoch(epoch: int):
torch.cuda.empty_cache()
random.shuffle(train_data)
train_iter = data.iterator.pool(
train_data,
config_data.batch_size,
key=lambda x: (len(x[0]), len(x[1])),
# key is not used if sort_within_batch is False by default
batch_size_fn=utils.batch_size_fn,
random_shuffler=data.iterator.RandomShuffler())
for _, train_batch in tqdm(enumerate(train_iter)):
optim.zero_grad()
in_arrays = data_utils.seq2seq_pad_concat_convert(
train_batch, device=device)
loss = model(
encoder_input=in_arrays[0],
is_train_mode=True,
decoder_input=in_arrays[1],
labels=in_arrays[2],
)
loss.backward()
optim.step()
scheduler.step()
step = scheduler.last_epoch
if step % config_data.display_steps == 0:
logger.info('step: %d, loss: %.4f', step, loss)
lr = optim.param_groups[0]['lr']
print(f"lr: {lr} step: {step}, loss: {loss:.4}")
if step and step % config_data.eval_steps == 0:
_eval_epoch(epoch, mode='eval')
if args.run_mode == 'train_and_evaluate':
logger.info("Begin running with train_and_evaluate mode")
model_path = os.path.join(args.model_dir, args.model_fn)
if os.path.exists(model_path):
logger.info("Restore latest checkpoint in", model_path)
ckpt = torch.load(model_path)
model.load_state_dict(ckpt['model'])
optim.load_state_dict(ckpt['optimizer'])
scheduler.load_state_dict(ckpt['scheduler'])
_eval_epoch(0, mode='test')
for epoch in range(config_data.max_train_epoch):
_train_epoch(epoch)
_eval_epoch(epoch, mode='eval')
elif args.run_mode == 'eval':
logger.info("Begin running with evaluate mode")
model_path = os.path.join(args.model_dir, args.model_fn)
logger.info("Restore latest checkpoint in %s", model_path)
ckpt = torch.load(model_path)
model.load_state_dict(ckpt['model'])
_eval_epoch(0, mode='eval')
elif args.run_mode == 'test':
logger.info("Begin running with test mode")
model_path = os.path.join(args.model_dir, args.model_fn)
logger.info("Restore latest checkpoint in", model_path)
ckpt = torch.load(model_path)
model.load_state_dict(ckpt['model'])
_eval_epoch(0, mode='test')
else:
raise ValueError(f"Unknown mode: {args.run_mode}")
if __name__ == '__main__':
main()