/
language_model_trainer.py
494 lines (409 loc) · 17.6 KB
/
language_model_trainer.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import time, datetime
import random
import sys
from pathlib import Path
from typing import Union
from torch import cuda
from torch.utils.data import Dataset, DataLoader
from torch.optim.sgd import SGD
try:
from apex import amp
except ImportError:
amp = None
import flair
from flair.data import Dictionary
from flair.models import LanguageModel
from flair.optim import *
from flair.training_utils import add_file_handler
log = logging.getLogger("flair")
class TextDataset(Dataset):
def __init__(
self,
path: Union[str, Path],
dictionary: Dictionary,
expand_vocab: bool = False,
forward: bool = True,
split_on_char: bool = True,
random_case_flip: bool = True,
document_delimiter: str = '\n',
shuffle: bool = True,
):
if type(path) is str:
path = Path(path)
assert path.exists()
self.files = None
self.path = path
self.dictionary = dictionary
self.split_on_char = split_on_char
self.forward = forward
self.random_case_flip = random_case_flip
self.expand_vocab = expand_vocab
self.document_delimiter = document_delimiter
self.shuffle = shuffle
if path.is_dir():
self.files = sorted([f for f in path.iterdir() if f.exists()])
else:
self.files = [path]
def __len__(self):
return len(self.files)
def __getitem__(self, index=0) -> torch.tensor:
"""Tokenizes a text file on character basis."""
if type(self.files[index]) is str:
self.files[index] = Path(self.files[index])
assert self.files[index].exists()
with self.files[index].open("r", encoding="utf-8") as fin:
lines = (doc + self.document_delimiter for doc in fin.read().split(self.document_delimiter) if doc)
lines = map(self.random_casechange, lines)
lines = list(map(list if self.split_on_char else str.split, lines))
log.info(f"read text file with {len(lines)} lines")
if self.shuffle:
random.shuffle(lines)
log.info(f"shuffled")
if self.expand_vocab:
for chars in lines:
for char in chars:
self.dictionary.add_item(char)
ids = torch.tensor(
[self.dictionary.get_idx_for_item(char) for chars in lines for char in chars],
dtype=torch.long
)
if not self.forward:
ids = ids.flip(0)
return ids
def random_casechange(self, line: str) -> str:
if self.random_case_flip:
no = random.randint(0, 99)
if no == 0:
line = line.lower()
if no == 1:
line = line.upper()
return line
class TextCorpus(object):
def __init__(
self,
path: Union[Path, str],
dictionary: Dictionary,
forward: bool = True,
character_level: bool = True,
random_case_flip: bool = True,
document_delimiter: str = '\n',
):
self.dictionary: Dictionary = dictionary
self.forward = forward
self.split_on_char = character_level
self.random_case_flip = random_case_flip
self.document_delimiter: str = document_delimiter
if type(path) == str:
path = Path(path)
self.train = TextDataset(
path / "train",
dictionary,
False,
self.forward,
self.split_on_char,
self.random_case_flip,
document_delimiter=self.document_delimiter,
shuffle=True,
)
# TextDataset returns a list. valid and test are only one file, so return the first element
self.valid = TextDataset(
path / "valid.txt",
dictionary,
False,
self.forward,
self.split_on_char,
self.random_case_flip,
document_delimiter=document_delimiter,
shuffle=False,
)[0]
self.test = TextDataset(
path / "test.txt",
dictionary,
False,
self.forward,
self.split_on_char,
self.random_case_flip,
document_delimiter=document_delimiter,
shuffle=False,
)[0]
class LanguageModelTrainer:
def __init__(
self,
model: LanguageModel,
corpus: TextCorpus,
optimizer: Optimizer = SGD,
test_mode: bool = False,
epoch: int = 0,
split: int = 0,
loss: float = 10000,
optimizer_state: dict = None,
):
self.model: LanguageModel = model
self.optimizer: Optimizer = optimizer
self.corpus: TextCorpus = corpus
self.test_mode: bool = test_mode
self.loss_function = torch.nn.CrossEntropyLoss()
self.log_interval = 100
self.epoch = epoch
self.split = split
self.loss = loss
self.optimizer_state = optimizer_state
def train(
self,
base_path: Union[Path, str],
sequence_length: int,
learning_rate: float = 20,
mini_batch_size: int = 100,
anneal_factor: float = 0.25,
patience: int = 10,
clip=0.25,
max_epochs: int = 1000,
checkpoint: bool = False,
grow_to_sequence_length: int = 0,
num_workers: int = 2,
use_amp: bool = False,
amp_opt_level: str = "O1",
**kwargs,
):
if use_amp:
if sys.version_info < (3, 0):
raise RuntimeError("Apex currently only supports Python 3. Aborting.")
if amp is None:
raise RuntimeError(
"Failed to import apex. Please install apex from https://www.github.com/nvidia/apex "
"to enable mixed-precision training."
)
# cast string to Path
if type(base_path) is str:
base_path = Path(base_path)
add_file_handler(log, base_path / "training.log")
number_of_splits: int = len(self.corpus.train)
val_data = self._batchify(self.corpus.valid, mini_batch_size)
# error message if the validation dataset is too small
if val_data.size(0) == 1:
raise RuntimeError(
f"ERROR: Your validation dataset is too small. For your mini_batch_size, the data needs to "
f"consist of at least {mini_batch_size * 2} characters!"
)
base_path.mkdir(parents=True, exist_ok=True)
loss_txt = base_path / "loss.txt"
savefile = base_path / "best-lm.pt"
try:
best_val_loss = self.loss
optimizer = self.optimizer(
self.model.parameters(), lr=learning_rate, **kwargs
)
if self.optimizer_state is not None:
optimizer.load_state_dict(self.optimizer_state)
if isinstance(optimizer, (AdamW, SGDW)):
scheduler: ReduceLRWDOnPlateau = ReduceLRWDOnPlateau(
optimizer, verbose=True, factor=anneal_factor, patience=patience
)
else:
scheduler: ReduceLROnPlateau = ReduceLROnPlateau(
optimizer, verbose=True, factor=anneal_factor, patience=patience
)
if use_amp:
self.model, optimizer = amp.initialize(
self.model, optimizer, opt_level=amp_opt_level
)
training_generator = DataLoader(
self.corpus.train, shuffle=False, num_workers=num_workers
)
for epoch in range(self.epoch, max_epochs):
epoch_start_time = time.time()
# Shuffle training files randomly after serially iterating through corpus one
if epoch > 0:
training_generator = DataLoader(
self.corpus.train, shuffle=True, num_workers=num_workers
)
self.model.save_checkpoint(
base_path / f"epoch_{epoch}.pt",
optimizer,
epoch,
0,
best_val_loss,
)
# iterate through training data, starting at self.split (for checkpointing)
for curr_split, train_slice in enumerate(
training_generator, self.split
):
if sequence_length < grow_to_sequence_length:
sequence_length += 1
log.info(f"Sequence length is {sequence_length}")
split_start_time = time.time()
# off by one for printing
curr_split += 1
train_data = self._batchify(train_slice.flatten(), mini_batch_size)
log.info(
"Split %d" % curr_split
+ "\t - ({:%H:%M:%S})".format(datetime.datetime.now())
)
for group in optimizer.param_groups:
learning_rate = group["lr"]
# go into train mode
self.model.train()
# reset variables
hidden = self.model.init_hidden(mini_batch_size)
# not really sure what this does
ntokens = len(self.corpus.dictionary)
total_loss = 0
start_time = time.time()
for batch, i in enumerate(
range(0, train_data.size(0) - 1, sequence_length)
):
data, targets = self._get_batch(train_data, i, sequence_length)
if not data.is_cuda and cuda.is_available():
log.info(
"Batch %d is not on CUDA, training will be very slow"
% (batch)
)
raise Exception("data isnt on cuda")
self.model.zero_grad()
optimizer.zero_grad()
# do the forward pass in the model
output, rnn_output, hidden = self.model.forward(data, hidden)
# try to predict the targets
loss = self.loss_function(output.view(-1, ntokens), targets)
# Backward
if use_amp:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm_(self.model.parameters(), clip)
optimizer.step()
total_loss += loss.data
# We detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
hidden = self._repackage_hidden(hidden)
# explicitly remove loss to clear up memory
del loss, output, rnn_output
if batch % self.log_interval == 0 and batch > 0:
cur_loss = total_loss.item() / self.log_interval
elapsed = time.time() - start_time
log.info(
"| split {:3d} /{:3d} | {:5d}/{:5d} batches | ms/batch {:5.2f} | "
"loss {:5.2f} | ppl {:8.2f}".format(
curr_split,
number_of_splits,
batch,
len(train_data) // sequence_length,
elapsed * 1000 / self.log_interval,
cur_loss,
math.exp(cur_loss),
)
)
total_loss = 0
start_time = time.time()
log.info(
"%d seconds for train split %d"
% (time.time() - split_start_time, curr_split)
)
###############################################################################
self.model.eval()
val_loss = self.evaluate(val_data, mini_batch_size, sequence_length)
scheduler.step(val_loss)
log.info("best loss so far {:5.2f}".format(best_val_loss))
log.info(self.model.generate_text())
if checkpoint:
self.model.save_checkpoint(
base_path / "checkpoint.pt",
optimizer,
epoch,
curr_split,
best_val_loss,
)
# Save the model if the validation loss is the best we've seen so far.
if val_loss < best_val_loss:
self.model.best_score = best_val_loss
self.model.save(savefile)
best_val_loss = val_loss
###############################################################################
# print info
###############################################################################
log.info("-" * 89)
summary = (
"| end of split {:3d} /{:3d} | epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | "
"valid ppl {:8.2f} | learning rate {:3.4f}".format(
curr_split,
number_of_splits,
epoch + 1,
(time.time() - split_start_time),
val_loss,
math.exp(val_loss),
learning_rate,
)
)
with open(loss_txt, "a") as myfile:
myfile.write("%s\n" % summary)
log.info(summary)
log.info("-" * 89)
log.info("Epoch time: %.2f" % (time.time() - epoch_start_time))
except KeyboardInterrupt:
log.info("-" * 89)
log.info("Exiting from training early")
###############################################################################
# final testing
###############################################################################
test_data = self._batchify(self.corpus.test, mini_batch_size)
test_loss = self.evaluate(test_data, mini_batch_size, sequence_length)
summary = "TEST: valid loss {:5.2f} | valid ppl {:8.2f}".format(
test_loss, math.exp(test_loss)
)
with open(loss_txt, "a") as myfile:
myfile.write("%s\n" % summary)
log.info(summary)
log.info("-" * 89)
def evaluate(self, data_source, eval_batch_size, sequence_length):
# Turn on evaluation mode which disables dropout.
self.model.eval()
with torch.no_grad():
total_loss = 0
ntokens = len(self.corpus.dictionary)
hidden = self.model.init_hidden(eval_batch_size)
for i in range(0, data_source.size(0) - 1, sequence_length):
data, targets = self._get_batch(data_source, i, sequence_length)
prediction, rnn_output, hidden = self.model.forward(data, hidden)
output_flat = prediction.view(-1, ntokens)
total_loss += len(data) * self.loss_function(output_flat, targets).data
hidden = self._repackage_hidden(hidden)
return total_loss.item() / len(data_source)
@staticmethod
def _batchify(data, batch_size):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // batch_size
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * batch_size)
# Evenly divide the data across the bsz batches.
data = data.view(batch_size, -1).t().contiguous()
return data
@staticmethod
def _get_batch(source, i, sequence_length):
seq_len = min(sequence_length, len(source) - 1 - i)
data = source[i : i + seq_len].clone().detach()
target = source[i + 1 : i + 1 + seq_len].view(-1).clone().detach()
data = data.to(flair.device)
target = target.to(flair.device)
return data, target
@staticmethod
def _repackage_hidden(h):
"""Wraps hidden states in new tensors, to detach them from their history."""
return tuple(v.clone().detach() for v in h)
@staticmethod
def load_from_checkpoint(
checkpoint_file: Union[str, Path], corpus: TextCorpus, optimizer: Optimizer = SGD
):
if type(checkpoint_file) is str:
checkpoint_file = Path(checkpoint_file)
checkpoint = LanguageModel.load_checkpoint(checkpoint_file)
return LanguageModelTrainer(
checkpoint["model"],
corpus,
optimizer,
epoch=checkpoint["epoch"],
split=checkpoint["split"],
loss=checkpoint["loss"],
optimizer_state=checkpoint["optimizer_state_dict"],
)