-
Notifications
You must be signed in to change notification settings - Fork 0
/
.train_translation.py@neomake_6955_1.py
403 lines (331 loc) · 15.8 KB
/
.train_translation.py@neomake_6955_1.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import numpy as np
from pathlib import Path
import argparse
import json
import math
import os
import random
import signal
import subprocess
import sys
import time
import torch
from torch import nn, optim
from torch.nn import Transformer
import torchtext
import t_dataset
from t_dataset import Translation_dataset_t
from t_dataset import MyCollate
import translation_utils
from translation_utils import TokenEmbedding, PositionalEncoding
from translation_utils import create_mask
from transformers import BertModel
from transformers import AutoTokenizer
from torch import Tensor
from torchtext.data.metrics import bleu_score
from models import Translator
from models import BarlowTwins
import wandb
#import barlow
os.environ['TRANSFORMERS_OFFLINE'] = 'yes'
os.environ['WANDB_START_METHOD'] = 'thread'
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
MANUAL_SEED = 4444
random.seed(MANUAL_SEED)
np.random.seed(MANUAL_SEED)
torch.manual_seed(MANUAL_SEED)
torch.backends.cudnn.deterministic = True
parser = argparse.ArgumentParser(description = 'Translation')
# Training hyper-parameters:
parser.add_argument('--workers', default=4, type=int, metavar='N',
help='number of data loader workers')
parser.add_argument('--epochs', default=5, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--batch_size', default=4, type=int, metavar='n',
help='mini-batch size')
parser.add_argument('--learning-rate', default=0.2, type=float, metavar='LR',
help='base learning rate')
parser.add_argument('--dropout', default=0.01, type=float, metavar='d',
help='dropout for training translation transformer')
parser.add_argument('--weight-decay', default=1e-6, type=float, metavar='W',
help='weight decay')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum for sgd')
parser.add_argument('--clip', default=1, type=float, metavar='GC',
help='Gradient Clipping')
parser.add_argument('--betas', default=(0.9, 0.98), type=tuple, metavar='B',
help='betas for Adam Optimizer')
parser.add_argument('--eps', default=1e-9, type=float, metavar='E',
help='eps for Adam optimizer')
parser.add_argument('--loss_fn', default='cross_entropy', type=str, metavar='LF',
help='loss function for translation')
parser.add_argument('--optimizer', default='adam', type=str, metavar='OP',
help='selecting optimizer')
# Transformer parameters:
parser.add_argument('--dmodel', default=768, type=int, metavar='T',
help='dimension of transformer encoder')
parser.add_argument('--nhead', default=4, type= int, metavar='N',
help= 'number of heads in transformer')
parser.add_argument('--dfeedforward', default=200, type=int, metavar='F',
help= 'dimension of feedforward layer in transformer encoder')
parser.add_argument('--nlayers', default=3, type=int, metavar= 'N',
help='number of layers of transformer encoder')
parser.add_argument('--projector', default='768-256', type=str,
metavar='MLP', help='projector MLP')
# Tokenizer:
parser.add_argument('--tokenizer', default='bert-base-multilingual-uncased', type=str,
metavar='T', help= 'tokenizer')
parser.add_argument('--mbert-out-size', default=768, type=int, metavar='MO',
help='Dimension of mbert output')
# Paths:
parser.add_argument('--checkpoint_dir', default='./checkpoint/', type=Path,
metavar='DIR', help='path to checkpoint directory')
# to load or barlow or not:
parser.add_argument('--load', default=0, type=int,
metavar='DIR', help='to load barlow twins encoder or not')
# calculate bleu:
parser.add_argument('--checkbleu', default=5 , type=int,
metavar='BL', help='check bleu after these number of epochs')
# train or test dataset
parser.add_argument('--train', default=True , type=bool,
metavar='T', help='selecting train set')
parser.add_argument('--print_freq', default=5 , type=int,
metavar='PF', help='frequency of printing and saving stats')
parser.add_argument('--test_translation', default=0, type=int,
metavar='TT', help='testing translation_score')
''' NOTE:
Transformer and tokenizer arguments would remain constant in training and context enhancement step.
'''
args = parser.parse_args()
# print(args.load)
os.environ["TOKENIZERS_PARALLELISM"] = "true"
def main():
# print("entered main")
args.ngpus_per_node = torch.cuda.device_count()
if 'SLURM_JOB_ID' in os.environ:
# single-node and multi-node distributed training on SLURM cluster
# requeue job on SLURM preemption
signal.signal(signal.SIGUSR1, handle_sigusr1)
signal.signal(signal.SIGTERM, handle_sigterm)
# find a common host name on all nodes
# assume scontrol returns hosts in the same order on all nodes
cmd = 'scontrol show hostnames ' + os.getenv('SLURM_JOB_NODELIST')
stdout = subprocess.check_output(cmd.split())
host_name = stdout.decode().splitlines()[0]
args.rank = int(os.getenv('SLURM_NODEID')) * args.ngpus_per_node
args.world_size = int(os.getenv('SLURM_NNODES')) * args.ngpus_per_node
args.dist_url = f'tcp://{host_name}:58472'
else:
# single-node distributed training
args.rank = 0
args.dist_url = 'tcp://localhost:58472'
args.world_size = args.ngpus_per_node
torch.multiprocessing.spawn(main_worker, (args,), args.ngpus_per_node)
def main_worker(gpu, args):
args.rank += gpu
torch.distributed.init_process_group(
backend='nccl', init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
if args.rank == 0:
'''
wandb.init(config=args, project='translation_test')#############################################
wandb.config.update(args)
config = wandb.config
'''
# exit()
args.checkpoint_dir.mkdir(parents=True, exist_ok=True)
stats_file = open(args.checkpoint_dir / 'stats.txt', 'a', buffering=1)
print(' '.join(sys.argv))
print(' '.join(sys.argv), file=stats_file)
torch.cuda.set_device(gpu)
torch.backends.cudnn.benchmark = True
dataset = Translation_dataset_t(train=args.train)
src_vocab_size = dataset.de_vocab_size
trg_vocab_size = dataset.en_vocab_size
tokenizer = dataset.tokenizer
pad_idx = tokenizer.pad_token_id
sos_idx = tokenizer.cls_token_id
eos_idx = tokenizer.sep_token_id
# transformer1 = nn.TransformerEncoderLayer(d_model = args.dmodel, nhead=args.nhead, dim_feedforward=args.dfeedforward, batch_first=True)
# t_enc = nn.TransformerEncoder(transformer1, num_layers=args.nlayers)
# print(src_vocab_size, trg_vocab_size)
mbert = BertModel.from_pretrained('bert-base-multilingual-uncased')
transformer = Transformer(d_model=args.dmodel,
nhead=args.nhead,
num_encoder_layers=args.nlayers,
num_decoder_layers = args.nlayers,
dim_feedforward=args.dfeedforward,
dropout=args.dropout)
model = Translator(mbert=mbert, transformer= transformer, tgt_vocab_size=trg_vocab_size, emb_size=args.mbert_out_size).cuda(gpu)
# print(model.state_dict)
# model_barlow = barlow.BarlowTwins(projector_layers=args.projector, mbert_out_size=args.mbert_out_size, transformer_enc=model.transformer.encoder, lambd=args.lambd).cuda(gpu)
# args.load = False
if args.load == 1:
# print(args.load)
# print('inside')
print('loading barlow model')
t_enc = model.transformer.encoder
barlow = BarlowTwins(projector_layers=args.projector, mbert_out_size=args.mbert_out_size, transformer_enc=t_enc, mbert=mbert, lambd=0.0051).cuda(gpu)
### note: lambd is just a placeholder
ckpt = torch.load(args.checkpoint_dir/ 'barlow_checkpoint.pth',
map_location='cpu')
barlow.load_state_dict(ckpt['model'])
model.transformer.encoder = barlow.transformer_enc
model.mbert = barlow.mbert
'''
to_do:
if post_train:
torch.load(model.states_dict)
model.transformer.encoder = model_barlow
'''
# model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
param_weights = []
param_biases = []
for param in model.parameters():
if param.ndim == 1:
param_biases.append(param)
else:
param_weights.append(param)
parameters = [{'params': param_weights}, {'params': param_biases}]
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[gpu], find_unused_parameters=True)
###########################################################
if args.optimizer == 'adam':
optimizer =torch.optim.Adam(model.parameters(), lr=args.learning_rate, betas=args.betas, eps=args.eps)
else:
optimizer = torch.optim.SGD(model.parameters(), lr=args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay)
if args.loss_fn == 'cross_entropy':
loss_fn = torch.nn.CrossEntropyLoss(ignore_index=pad_idx)
##############################################################
start_epoch = 0
sampler = torch.utils.data.distributed.DistributedSampler(dataset)
assert args.batch_size % args.world_size == 0
per_device_batch_size = args.batch_size // args.world_size
id2bert_dict = dataset.id2bert_dict
###############################
loader = torch.utils.data.DataLoader(
dataset, batch_size=per_device_batch_size, num_workers=args.workers,
pin_memory=True, sampler=sampler, collate_fn = MyCollate(tokenizer=tokenizer,bert2id_dict=dataset.bert2id_dict))
test_loader = torch.utils.data.DataLoader(
dataset, batch_size=1, num_workers=args.workers,
pin_memory=True, sampler=sampler, collate_fn = MyCollate(tokenizer=tokenizer,bert2id_dict=dataset.bert2id_dict))
#############################
start_time = time.time()
if not args.test_translation:
for epoch in range(start_epoch, args.epochs):
sampler.set_epoch(epoch)
epoch_loss = 0
t = 0
for step, (sent) in enumerate(loader, start=epoch * len(loader)):
src = sent[0].cuda(gpu, non_blocking=True)
tgt_inp = sent[2].cuda(gpu, non_blocking=True)
tgt_out = sent[3].cuda(gpu, non_blocking=True)
src_mask, tgt_mask, src_padding_mask, tgt_padding_mask = create_mask(src, tgt_inp, pad_idx)
logits = model(src, tgt_inp, src_mask, tgt_mask, src_padding_mask, tgt_padding_mask, src_padding_mask)
optimizer.zero_grad()
loss = loss_fn(logits.reshape(-1, logits.shape[-1]), tgt_out.reshape(-1))
loss.backward()
optimizer.step()
# losses += loss.item()
# wandb.log({'iter_loss': loss})
epoch_loss += loss.item()
t += 1
torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
if step % args.print_freq == 0:
if args.rank == 0:
stats = dict(epoch=epoch, step=step,
loss=loss.item(),
time=int(time.time() - start_time))
print(json.dumps(stats))
print(json.dumps(stats), file=stats_file)
if args.rank == 0:
# wandb.log({"epoch_loss":epoch_loss/t})
# save checkpoint
state = dict(epoch=epoch + 1, model=model.module.state_dict(),
optimizer=optimizer.state_dict())
# print(model.state_dict)
torch.save(state, args.checkpoint_dir / 'translation_checkpoint.pth')
print('translation model saved in', args.checkpoint_dir)
##############################################################
if args.rank == 0:
if epoch%args.checkbleu ==0 :
bleu_score = checkbleu(model, tokenizer, test_loader, id2bert_dict, gpu)
# wandb.log({'bleu_score': bleu_score})
# print(bleu_score(predicted, target))
##############################################################
# if epoch%1 ==0 :
# torch.save(model.module.state_dict(),
# 'path.pth')
# print("Model is saved")
# if args.rank == 0:
# # save checkpoint
# state = dict(epoch=epoch + 1, model=model.state_dict(),
# optimizer=optimizer.state_dict())
# torch.save(state, args.checkpoint_dir / f'translation_checkpoint.pth')
# print('saved translation model in', args.checkpoint_dir)
# wandb.finish()
else:
bleu_score = checkbleu(model,tokenizer, test_loader, id2bert_dict, gpu )
print('test_bleu_score', bleu_score)
# if args.rank == 0:
# wandb.log({'bleu_score': bleu_score})
def checkbleu(model, tokenizer, test_loader, id2bert_dict, gpu):
model.eval()
predicted=[]
target=[]
for i in test_loader:
src = i[0].cuda(gpu, non_blocking=True)
# tgt_out = i[1][1:, : ].cuda(gpu, non_blocking=True)
tgt_out = i[3].cuda(gpu, non_blocking=True)
num_tokens = src.shape[0]
src_mask = (torch.zeros(num_tokens, num_tokens)).type(torch.bool).cuda(gpu, non_blocking=True)
out = translate(model, src, tokenizer, src_mask, id2bert, gpu)
predicted.append(out)
target.append([tokenizer.convert_ids_to_tokens(tgt_out)])
print(out)
print(tokenizer.convert_ids_to_tokens(tgt_out))
try:
bleu_score(predicted, target)
except:
predicted.pop()
target.pop()
bleu = bleu_score(predicted, target)
return bleu
'''
todo:
BLEU score
'''
# function to generate output sequence using greedy algorithm
def greedy_decode(model, src, src_mask, max_len, start_symbol, eos_idx, gpu):
src = src
src_mask = src_mask
memory = model.module.encode(src, src_mask)
ys = torch.ones(1, 1).fill_(start_symbol).type(torch.long).cuda(gpu, non_blocking=True)
for i in range(max_len-1):
memory = memory
tgt_mask = (translation_utils.generate_square_subsequent_mask(ys.size(0))
.type(torch.bool)).cuda(gpu, non_blocking=True)
out = model.module.decode(ys, memory, tgt_mask)
out = out.transpose(0, 1)
prob = model.module.generator(out[:, -1])
_, next_word = torch.max(prob, dim=1)
next_word = next_word.item()
ys = torch.cat([ys,
torch.ones(1, 1).type_as(src.data).fill_(next_word)], dim=0)
if next_word == eos_idx:
break
return ys
# actual function to translate input sentence into target language
def translate(model: torch.nn.Module,
src: torch.tensor,
tokenizer,src_mask, id2bert, gpu):
model.eval()
num_tokens = src.shape[0]
tgt_tokens = greedy_decode(
model, src, src_mask, max_len=num_tokens + 5, start_symbol=tokenizer.cls_token_id, eos_idx=tokenizer.sep_token_id, gpu=gpu).flatten()
# for i in len(tgt_tokens):
# tgt_tokens[i] = id2bert[tgt_tokens[i]]
# print(tgt_tokens)
return tokenizer.convert_ids_to_tokens(tgt_tokens)
if __name__ == '__main__':
main()
# wandb.finish()