forked from flashlight/wav2letter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train.cpp
750 lines (680 loc) · 25.1 KB
/
Train.cpp
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cstdlib>
#include <fstream>
#include <random>
#include <string>
#include <vector>
#include <cereal/archives/json.hpp>
#include <cereal/types/unordered_map.hpp>
#include <flashlight/flashlight.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "common/Defines.h"
#include "common/FlashlightUtils.h"
#include "common/Transforms.h"
#include "criterion/criterion.h"
#include "data/Featurize.h"
#include "libraries/common/Dictionary.h"
#include "module/module.h"
#include "runtime/runtime.h"
using namespace w2l;
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
std::string exec(argv[0]);
std::vector<std::string> argvs;
for (int i = 0; i < argc; i++) {
argvs.emplace_back(argv[i]);
}
gflags::SetUsageMessage(
"Usage: \n " + exec + " train [flags]\n or " + exec +
" continue [directory] [flags]\n or " + exec +
" fork [directory/model] [flags]");
/* ===================== Parse Options ===================== */
int runIdx = 1; // current #runs in this path
std::string runPath; // current experiment path
std::string reloadPath; // path to model to reload
std::string runStatus = argv[1];
int64_t startEpoch = 0;
int64_t startUpdate = 0;
if (argc <= 1) {
LOG(FATAL) << gflags::ProgramUsage();
}
if (runStatus == kTrainMode) {
LOG(INFO) << "Parsing command line flags";
gflags::ParseCommandLineFlags(&argc, &argv, false);
if (!FLAGS_flagsfile.empty()) {
LOG(INFO) << "Reading flags from file " << FLAGS_flagsfile;
gflags::ReadFromFlagsFile(FLAGS_flagsfile, argv[0], true);
}
gflags::ParseCommandLineFlags(&argc, &argv, false);
runPath = newRunPath(FLAGS_rundir, FLAGS_runname, FLAGS_tag);
} else if (runStatus == kContinueMode) {
runPath = argv[2];
while (fileExists(getRunFile("model_last.bin", runIdx, runPath))) {
++runIdx;
}
reloadPath = getRunFile("model_last.bin", runIdx - 1, runPath);
LOG(INFO) << "reload path is " << reloadPath;
std::unordered_map<std::string, std::string> cfg;
W2lSerializer::load(reloadPath, cfg);
auto flags = cfg.find(kGflags);
if (flags == cfg.end()) {
LOG(FATAL) << "Invalid config loaded from " << reloadPath;
}
LOG(INFO) << "Reading flags from config file " << reloadPath;
gflags::ReadFlagsFromString(flags->second, gflags::GetArgv0(), true);
if (argc > 3) {
LOG(INFO) << "Parsing command line flags";
LOG(INFO) << "Overriding flags should be mutable when using `continue`";
gflags::ParseCommandLineFlags(&argc, &argv, false);
}
if (!FLAGS_flagsfile.empty()) {
LOG(INFO) << "Reading flags from file " << FLAGS_flagsfile;
gflags::ReadFromFlagsFile(FLAGS_flagsfile, argv[0], true);
}
gflags::ParseCommandLineFlags(&argc, &argv, false);
auto epoch = cfg.find(kEpoch);
if (epoch == cfg.end()) {
LOG(WARNING) << "Did not find epoch to start from, starting from 0.";
} else {
startEpoch = std::stoi(epoch->second);
}
auto nbupdates = cfg.find(kUpdates);
if (nbupdates == cfg.end()) {
LOG(WARNING) << "Did not find #updates to start from, starting from 0.";
} else {
startUpdate = std::stoi(nbupdates->second);
}
} else if (runStatus == kForkMode) {
reloadPath = argv[2];
std::unordered_map<std::string, std::string> cfg;
W2lSerializer::load(reloadPath, cfg);
auto flags = cfg.find(kGflags);
if (flags == cfg.end()) {
LOG(FATAL) << "Invalid config loaded from " << reloadPath;
}
LOG(INFO) << "Reading flags from config file " << reloadPath;
gflags::ReadFlagsFromString(flags->second, gflags::GetArgv0(), true);
if (argc > 3) {
LOG(INFO) << "Parsing command line flags";
LOG(INFO) << "Overriding flags should be mutable when using `fork`";
gflags::ParseCommandLineFlags(&argc, &argv, false);
}
if (!FLAGS_flagsfile.empty()) {
LOG(INFO) << "Reading flags from file" << FLAGS_flagsfile;
gflags::ReadFromFlagsFile(FLAGS_flagsfile, argv[0], true);
}
gflags::ParseCommandLineFlags(&argc, &argv, false);
runPath = newRunPath(FLAGS_rundir, FLAGS_runname, FLAGS_tag);
} else {
LOG(FATAL) << gflags::ProgramUsage();
}
// Only new flags are re-serialized. Copy any values from deprecated flags to
// new flags when deprecated flags are present and corresponding new flags
// aren't
w2l::handleDeprecatedFlags();
af::setSeed(FLAGS_seed);
af::setFFTPlanCacheSize(FLAGS_fftcachesize);
std::shared_ptr<fl::Reducer> reducer = nullptr;
if (FLAGS_enable_distributed) {
initDistributed(
FLAGS_world_rank,
FLAGS_world_size,
FLAGS_max_devices_per_node,
FLAGS_rndv_filepath);
reducer = std::make_shared<fl::CoalescingReducer>(
1.0 / fl::getWorldSize(), true, true);
}
int worldRank = fl::getWorldRank();
int worldSize = fl::getWorldSize();
bool isMaster = (worldRank == 0);
LOG_MASTER(INFO) << "Gflags after parsing \n" << serializeGflags("; ");
LOG_MASTER(INFO) << "Experiment path: " << runPath;
LOG_MASTER(INFO) << "Experiment runidx: " << runIdx;
std::unordered_map<std::string, std::string> config = {
{kProgramName, exec},
{kCommandLine, join(" ", argvs)},
{kGflags, serializeGflags()},
// extra goodies
{kUserName, getEnvVar("USER")},
{kHostName, getEnvVar("HOSTNAME")},
{kTimestamp, getCurrentDate() + ", " + getCurrentDate()},
{kRunIdx, std::to_string(runIdx)},
{kRunPath, runPath}};
auto validSets = split(',', trim(FLAGS_valid));
std::vector<std::pair<std::string, std::string>> validTagSets;
for (const auto& s : validSets) {
// assume the format is tag:filepath
auto ts = splitOnAnyOf(":", s);
if (ts.size() == 1) {
validTagSets.emplace_back(std::make_pair(s, s));
} else {
validTagSets.emplace_back(std::make_pair(ts[0], ts[1]));
}
}
/* ===================== Create Dictionary & Lexicon ===================== */
auto dictPath = pathsConcat(FLAGS_tokensdir, FLAGS_tokens);
if (dictPath.empty() || !fileExists(dictPath)) {
throw std::runtime_error(
"Invalid dictionary filepath specified with --tokensdir and --tokens: \"" +
dictPath + "\"");
}
Dictionary tokenDict(dictPath);
// Setup-specific modifications
for (int64_t r = 1; r <= FLAGS_replabel; ++r) {
tokenDict.addEntry(std::to_string(r));
}
// ctc expects the blank label last
if (FLAGS_criterion == kCtcCriterion) {
tokenDict.addEntry(kBlankToken);
}
if (FLAGS_eostoken) {
tokenDict.addEntry(kEosToken);
}
int numClasses = tokenDict.indexSize();
LOG(INFO) << "Number of classes (network): " << numClasses;
Dictionary wordDict;
LexiconMap lexicon;
if (!FLAGS_lexicon.empty()) {
lexicon = loadWords(FLAGS_lexicon, FLAGS_maxword);
wordDict = createWordDict(lexicon);
LOG(INFO) << "Number of words: " << wordDict.indexSize();
}
DictionaryMap dicts = {{kTargetIdx, tokenDict}, {kWordIdx, wordDict}};
/* =========== Create Network & Optimizers / Reload Snapshot ============ */
std::shared_ptr<fl::Module> network;
std::shared_ptr<SequenceCriterion> criterion;
std::shared_ptr<fl::FirstOrderOptimizer> netoptim;
std::shared_ptr<fl::FirstOrderOptimizer> critoptim;
auto scalemode = getCriterionScaleMode(FLAGS_onorm, FLAGS_sqnorm);
if (runStatus == kTrainMode) {
auto archfile = pathsConcat(FLAGS_archdir, FLAGS_arch);
LOG_MASTER(INFO) << "Loading architecture file from " << archfile;
auto numFeatures = getSpeechFeatureSize();
// Encoder network, works on audio
network = createW2lSeqModule(archfile, numFeatures, numClasses);
if (FLAGS_criterion == kCtcCriterion) {
criterion = std::make_shared<CTCLoss>(scalemode);
} else if (FLAGS_criterion == kAsgCriterion) {
criterion =
std::make_shared<ASGLoss>(numClasses, scalemode, FLAGS_transdiag);
} else if (FLAGS_criterion == kSeq2SeqCriterion) {
criterion = std::make_shared<Seq2SeqCriterion>(
buildSeq2Seq(numClasses, tokenDict.getIndex(kEosToken)));
} else if (FLAGS_criterion == kTransformerCriterion) {
criterion =
std::make_shared<TransformerCriterion>(buildTransformerCriterion(
numClasses,
FLAGS_am_decoder_tr_layers,
FLAGS_am_decoder_tr_dropout,
FLAGS_am_decoder_tr_layerdrop,
tokenDict.getIndex(kEosToken)));
} else {
LOG(FATAL) << "unimplemented criterion";
}
} else if (runStatus == kForkMode) {
std::unordered_map<std::string, std::string> cfg; // unused
W2lSerializer::load(reloadPath, cfg, network, criterion);
} else { // kContinueMode
std::unordered_map<std::string, std::string> cfg; // unused
W2lSerializer::load(
reloadPath, cfg, network, criterion, netoptim, critoptim);
}
LOG_MASTER(INFO) << "[Network] " << network->prettyString();
LOG_MASTER(INFO) << "[Network Params: " << numTotalParams(network) << "]";
LOG_MASTER(INFO) << "[Criterion] " << criterion->prettyString();
if (runStatus == kTrainMode || runStatus == kForkMode) {
netoptim = initOptimizer(
{network}, FLAGS_netoptim, FLAGS_lr, FLAGS_momentum, FLAGS_weightdecay);
critoptim =
initOptimizer({criterion}, FLAGS_critoptim, FLAGS_lrcrit, 0.0, 0.0);
}
LOG_MASTER(INFO) << "[Network Optimizer] " << netoptim->prettyString();
LOG_MASTER(INFO) << "[Criterion Optimizer] " << critoptim->prettyString();
double initLinNetlr = FLAGS_linlr >= 0.0 ? FLAGS_linlr : FLAGS_lr;
double initLinCritlr =
FLAGS_linlrcrit >= 0.0 ? FLAGS_linlrcrit : FLAGS_lrcrit;
std::shared_ptr<LinSegCriterion> linseg;
std::shared_ptr<fl::FirstOrderOptimizer> linNetoptim;
std::shared_ptr<fl::FirstOrderOptimizer> linCritoptim;
if (FLAGS_linseg > startUpdate) {
if (FLAGS_criterion != kAsgCriterion) {
LOG(FATAL) << "linseg may only be used with ASG criterion";
}
linseg = std::make_shared<LinSegCriterion>(numClasses, scalemode);
linseg->setParams(criterion->param(0), 0);
LOG_MASTER(INFO) << "[Criterion] " << linseg->prettyString()
<< " (for first " << FLAGS_linseg - startUpdate
<< " updates)";
linNetoptim = initOptimizer(
{network},
FLAGS_netoptim,
initLinNetlr,
FLAGS_momentum,
FLAGS_weightdecay);
linCritoptim =
initOptimizer({linseg}, FLAGS_critoptim, initLinCritlr, 0.0, 0.0);
LOG_MASTER(INFO) << "[Network Optimizer] " << linNetoptim->prettyString()
<< " (for first " << FLAGS_linseg - startUpdate
<< " updates)";
LOG_MASTER(INFO) << "[Criterion Optimizer] " << linCritoptim->prettyString()
<< " (for first " << FLAGS_linseg - startUpdate
<< " updates)";
}
/* ===================== Meters ===================== */
TrainMeters meters;
for (const auto& s : validTagSets) {
meters.valid[s.first] = DatasetMeters();
}
// best perf so far on valid datasets
std::unordered_map<std::string, double> validminerrs;
for (const auto& s : validTagSets) {
validminerrs[s.first] = DBL_MAX;
}
/* ===================== Logging ===================== */
std::ofstream logFile, perfFile;
if (isMaster) {
dirCreate(runPath);
logFile.open(getRunFile("log", runIdx, runPath));
if (!logFile.is_open()) {
LOG(FATAL) << "failed to open log file for writing";
}
perfFile.open(getRunFile("perf", runIdx, runPath));
if (!perfFile.is_open()) {
LOG(FATAL) << "failed to open perf file for writing";
}
// write perf header
auto perfMsg = getStatus(meters, 0, 0, 0, 0, false, true, "\t").first;
appendToLog(perfFile, "# " + perfMsg);
// write config
std::ofstream configFile(getRunFile("config", runIdx, runPath));
cereal::JSONOutputArchive ar(configFile);
ar(CEREAL_NVP(config));
}
auto logStatus = [&perfFile, &logFile, isMaster](
TrainMeters& mtrs,
int64_t epoch,
int64_t nupdates,
double lr,
double lrcrit) {
syncMeter(mtrs);
if (isMaster) {
auto logMsg =
getStatus(mtrs, epoch, nupdates, lr, lrcrit, true, false, " | ")
.second;
auto perfMsg =
getStatus(mtrs, epoch, nupdates, lr, lrcrit, false, true).second;
LOG_MASTER(INFO) << logMsg;
appendToLog(logFile, logMsg);
appendToLog(perfFile, perfMsg);
}
};
auto saveModels = [&](int iter, int totalUpdates) {
if (isMaster) {
// Save last epoch
config[kEpoch] = std::to_string(iter);
config[kUpdates] = std::to_string(totalUpdates);
std::string filename;
if (FLAGS_itersave) {
filename =
getRunFile(format("model_iter_%03d.bin", iter), runIdx, runPath);
W2lSerializer::save(
filename, config, network, criterion, netoptim, critoptim);
}
// save last model
filename = getRunFile("model_last.bin", runIdx, runPath);
W2lSerializer::save(
filename, config, network, criterion, netoptim, critoptim);
// save if better than ever for one valid
for (const auto& v : validminerrs) {
double verr = meters.valid[v.first].wrdEdit.value()[0];
if (verr < validminerrs[v.first]) {
validminerrs[v.first] = verr;
std::string cleaned_v = cleanFilepath(v.first);
std::string vfname =
getRunFile("model_" + cleaned_v + ".bin", runIdx, runPath);
W2lSerializer::save(
vfname, config, network, criterion, netoptim, critoptim);
}
}
// print brief stats on memory allocation (so far)
auto* curMemMgr =
fl::MemoryManagerInstaller::currentlyInstalledMemoryManager();
if (curMemMgr) {
curMemMgr->printInfo("Memory Manager Stats", 0 /* device id */);
}
}
};
/* ===================== Create Dataset ===================== */
auto trainds = createDataset(
FLAGS_train, dicts, lexicon, FLAGS_batchsize, worldRank, worldSize);
if (FLAGS_noresample) {
LOG_MASTER(INFO) << "Shuffling trainset";
trainds->shuffle(FLAGS_seed);
}
std::map<std::string, std::shared_ptr<W2lDataset>> validds;
for (const auto& s : validTagSets) {
// use batchsize 1 to prevent issues from padded inputs
validds[s.first] =
createDataset(s.second, dicts, lexicon, 1, worldRank, worldSize);
}
/* ===================== Hooks ===================== */
auto evalOutput = [&dicts, &criterion](
const af::array& op,
const af::array& target,
DatasetMeters& mtr) {
auto batchsz = op.dims(2);
for (int b = 0; b < batchsz; ++b) {
auto tgt = target(af::span, b);
auto viterbipath =
afToVector<int>(criterion->viterbiPath(op(af::span, af::span, b)));
auto tgtraw = afToVector<int>(tgt);
// Remove `-1`s appended to the target for batching (if any)
auto labellen = getTargetSize(tgtraw.data(), tgtraw.size());
tgtraw.resize(labellen);
// remap actual, predicted targets for evaluating edit distance error
if (dicts.find(kTargetIdx) == dicts.end()) {
LOG(FATAL) << "Dictionary not provided for target: " << kTargetIdx;
}
auto tgtDict = dicts.find(kTargetIdx)->second;
auto ltrPred = tknPrediction2Ltr(viterbipath, tgtDict);
auto ltrTgt = tknTarget2Ltr(tgtraw, tgtDict);
auto wrdPred = tkn2Wrd(ltrPred);
auto wrdTgt = tkn2Wrd(ltrTgt);
mtr.tknEdit.add(ltrPred, ltrTgt);
mtr.wrdEdit.add(wrdPred, wrdTgt);
}
};
auto test = [&evalOutput](
std::shared_ptr<fl::Module> ntwrk,
std::shared_ptr<SequenceCriterion> crit,
std::shared_ptr<W2lDataset> testds,
DatasetMeters& mtrs) {
ntwrk->eval();
crit->eval();
mtrs.tknEdit.reset();
mtrs.wrdEdit.reset();
mtrs.loss.reset();
for (auto& batch : *testds) {
auto output = ntwrk->forward({fl::input(batch[kInputIdx])}).front();
auto loss =
crit->forward({output, fl::Variable(batch[kTargetIdx], false)})
.front();
mtrs.loss.add(loss.array());
evalOutput(output.array(), batch[kTargetIdx], mtrs);
}
};
auto trainEvalIds =
getTrainEvalIds(trainds->size(), FLAGS_pcttraineval, FLAGS_seed);
int64_t curEpoch = startEpoch;
auto train = [&meters,
&test,
&logStatus,
&saveModels,
&evalOutput,
&validds,
&trainEvalIds,
&curEpoch,
&startUpdate,
reducer](
std::shared_ptr<fl::Module> ntwrk,
std::shared_ptr<SequenceCriterion> crit,
std::shared_ptr<W2lDataset> trainset,
std::shared_ptr<fl::FirstOrderOptimizer> netopt,
std::shared_ptr<fl::FirstOrderOptimizer> critopt,
double initlr,
double initcritlr,
bool clampCrit,
int64_t nbatches) {
if (reducer) {
fl::distributeModuleGrads(ntwrk, reducer);
fl::distributeModuleGrads(crit, reducer);
}
meters.train.loss.reset();
meters.train.tknEdit.reset();
meters.train.wrdEdit.reset();
std::shared_ptr<SpecAugment> saug;
if (FLAGS_saug_start_update >= 0) {
saug = std::make_shared<SpecAugment>(
FLAGS_filterbanks,
FLAGS_saug_fmaskf,
FLAGS_saug_fmaskn,
FLAGS_saug_tmaskt,
FLAGS_saug_tmaskp,
FLAGS_saug_tmaskn);
}
fl::allReduceParameters(ntwrk);
fl::allReduceParameters(crit);
auto resetTimeStatMeters = [&meters]() {
meters.runtime.reset();
meters.stats.reset();
meters.sampletimer.reset();
meters.fwdtimer.reset();
meters.critfwdtimer.reset();
meters.bwdtimer.reset();
meters.optimtimer.reset();
meters.timer.reset();
};
auto runValAndSaveModel = [&](int64_t totalEpochs,
int64_t totalUpdates,
double lr,
double lrcrit) {
meters.runtime.stop();
meters.timer.stop();
meters.sampletimer.stop();
meters.fwdtimer.stop();
meters.critfwdtimer.stop();
meters.bwdtimer.stop();
meters.optimtimer.stop();
// valid
for (auto& vds : validds) {
test(ntwrk, crit, vds.second, meters.valid[vds.first]);
}
// print status
try {
logStatus(meters, totalEpochs, totalUpdates, lr, lrcrit);
} catch (const std::exception& ex) {
LOG(ERROR) << "Error while writing logs: " << ex.what();
}
// save last and best models
try {
saveModels(totalEpochs, totalUpdates);
} catch (const std::exception& ex) {
LOG(FATAL) << "Error while saving models: " << ex.what();
}
// reset meters for next readings
meters.train.loss.reset();
meters.train.tknEdit.reset();
meters.train.wrdEdit.reset();
};
int64_t curBatch = startUpdate;
while (curBatch < nbatches) {
++curEpoch; // counts partial epochs too!
int epochsAfterDecay = curEpoch - FLAGS_lr_decay;
double lrDecayScale =
std::pow(0.5, std::max(epochsAfterDecay, 0) / FLAGS_lr_decay_step);
ntwrk->train();
crit->train();
if (FLAGS_reportiters == 0) {
resetTimeStatMeters();
}
if (!FLAGS_noresample) {
LOG_MASTER(INFO) << "Shuffling trainset";
trainset->shuffle(curEpoch /* seed */);
}
af::sync();
meters.sampletimer.resume();
meters.runtime.resume();
meters.timer.resume();
LOG_MASTER(INFO) << "Epoch " << curEpoch << " started!";
for (auto& batch : *trainset) {
++curBatch;
double lrScheduleScale;
if (FLAGS_lrcosine) {
const double pi = std::acos(-1);
lrScheduleScale =
std::cos(((double)curBatch) / ((double)nbatches) * pi / 2.0);
} else {
lrScheduleScale =
std::pow(FLAGS_gamma, (double)curBatch / (double)FLAGS_stepsize);
}
netopt->setLr(
initlr * lrDecayScale * lrScheduleScale *
std::min(curBatch / double(FLAGS_warmup), 1.0));
critopt->setLr(
initcritlr * lrDecayScale * lrScheduleScale *
std::min(curBatch / double(FLAGS_warmup), 1.0));
af::sync();
meters.timer.incUnit();
meters.sampletimer.stopAndIncUnit();
meters.stats.add(batch[kInputIdx], batch[kTargetIdx]);
if (af::anyTrue<bool>(af::isNaN(batch[kInputIdx])) ||
af::anyTrue<bool>(af::isNaN(batch[kTargetIdx]))) {
LOG(FATAL) << "Sample has NaN values - "
<< join(",", readSampleIds(batch[kSampleIdx]));
}
// forward
meters.fwdtimer.resume();
auto input = fl::input(batch[kInputIdx]);
if (FLAGS_saug_start_update >= 0 &&
curBatch >= FLAGS_saug_start_update) {
input = saug->forward(input);
}
auto output = ntwrk->forward({input}).front();
af::sync();
meters.critfwdtimer.resume();
auto loss =
crit->forward({output, fl::noGrad(batch[kTargetIdx])}).front();
af::sync();
meters.fwdtimer.stopAndIncUnit();
meters.critfwdtimer.stopAndIncUnit();
if (af::anyTrue<bool>(af::isNaN(loss.array()))) {
LOG(FATAL) << "Loss has NaN values. Samples - "
<< join(",", readSampleIds(batch[kSampleIdx]));
}
meters.train.loss.add(loss.array());
int64_t batchIdx = (curBatch - startUpdate - 1) % trainset->size();
int64_t globalBatchIdx = trainset->getGlobalBatchIdx(batchIdx);
if (trainEvalIds.find(globalBatchIdx) != trainEvalIds.end()) {
evalOutput(output.array(), batch[kTargetIdx], meters.train);
}
// backward
meters.bwdtimer.resume();
netopt->zeroGrad();
critopt->zeroGrad();
loss.backward();
if (reducer) {
reducer->finalize();
}
af::sync();
meters.bwdtimer.stopAndIncUnit();
// optimizer
meters.optimtimer.resume();
// scale down gradients by batchsize
for (const auto& p : ntwrk->params()) {
if (!p.isGradAvailable()) {
continue;
}
p.grad() = p.grad() / FLAGS_batchsize;
}
for (const auto& p : crit->params()) {
if (!p.isGradAvailable()) {
continue;
}
p.grad() = p.grad() / FLAGS_batchsize;
}
// clamp gradients
if (FLAGS_maxgradnorm > 0) {
auto params = ntwrk->params();
if (clampCrit) {
auto critparams = crit->params();
params.insert(params.end(), critparams.begin(), critparams.end());
}
fl::clipGradNorm(params, FLAGS_maxgradnorm);
}
// update weights
critopt->step();
netopt->step();
af::sync();
meters.optimtimer.stopAndIncUnit();
meters.sampletimer.resume();
if (FLAGS_reportiters > 0 && curBatch % FLAGS_reportiters == 0) {
runValAndSaveModel(
curEpoch, curBatch, netopt->getLr(), critopt->getLr());
resetTimeStatMeters();
ntwrk->train();
crit->train();
meters.sampletimer.resume();
meters.runtime.resume();
meters.timer.resume();
}
if (curBatch > nbatches) {
break;
}
}
af::sync();
if (FLAGS_reportiters == 0) {
runValAndSaveModel(
curEpoch, curBatch, netopt->getLr(), critopt->getLr());
}
}
};
/* ===================== Train ===================== */
if (FLAGS_linseg - startUpdate > 0) {
train(
network,
linseg,
trainds,
linNetoptim,
linCritoptim,
initLinNetlr,
initLinCritlr,
false /* clampCrit */,
FLAGS_linseg - startUpdate);
startUpdate = FLAGS_linseg;
LOG_MASTER(INFO) << "Finished LinSeg";
}
auto s2s = std::dynamic_pointer_cast<Seq2SeqCriterion>(criterion);
auto trde = std::dynamic_pointer_cast<TransformerCriterion>(criterion);
if (FLAGS_pretrainWindow - startUpdate > 0) {
if (!s2s && !trde) {
LOG(FATAL) << "Window pretraining only allowed for seq2seq.";
}
train(
network,
criterion,
trainds,
netoptim,
critoptim,
FLAGS_lr,
FLAGS_lrcrit,
true,
FLAGS_pretrainWindow - startUpdate);
startUpdate = FLAGS_pretrainWindow;
LOG_MASTER(INFO) << "Finished window pretraining.";
}
if (s2s) {
s2s->clearWindow();
} else if (trde) {
trde->clearWindow();
}
train(
network,
criterion,
trainds,
netoptim,
critoptim,
FLAGS_lr,
FLAGS_lrcrit,
true /* clampCrit */,
FLAGS_iter);
LOG_MASTER(INFO) << "Finished training";
return 0;
}