text stringlengths 0 2.2M |
|---|
};
|
auto runValAndSaveModel =
|
[&](int64_t totalEpochs, int64_t totalUpdates, double lr) {
|
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);
|
} 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;
|
double scaleFactor =
|
FLAGS_fl_amp_use_mixed_precision ? FLAGS_fl_amp_scale_factor : 1.;
|
unsigned int kScaleFactorUpdateInterval =
|
FLAGS_fl_amp_scale_factor_update_interval;
|
unsigned int kMaxScaleFactor = FLAGS_fl_amp_max_scale_factor;
|
unsigned short scaleCounter = 1;
|
while (curBatch < nbatches) {
|
++curEpoch; // counts partial epochs too!
|
int64_t epochsAfterDecay = curEpoch - FLAGS_lr_decay;
|
double lrDecayScale = std::pow(
|
0.5,
|
(epochsAfterDecay < 0 ? 0
|
: 1 + epochsAfterDecay / FLAGS_lr_decay_step));
|
ntwrk->train();
|
if (FLAGS_reportiters == 0) {
|
resetTimeStatMeters();
|
}
|
std::hash<std::string> hasher;
|
FL_LOG_MASTER(INFO) << "Shuffling trainset";
|
auto curTrainset = loadPrefetchDataset(
|
trainset, FLAGS_nthread, true /* shuffle */, curEpoch /* seed */);
|
fl::sync();
|
meters.sampletimer.resume();
|
meters.runtime.resume();
|
meters.timer.resume();
|
FL_LOG_MASTER(INFO) << "Epoch " << curEpoch << " started!";
|
for (auto& batch : *curTrainset) {
|
++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));
|
fl::sync();
|
meters.timer.incUnit();
|
meters.sampletimer.stopAndIncUnit();
|
meters.stats.add(batch[kDurationIdx], batch[kTargetSizeIdx]);
|
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]));
|
}
|
// Ensure no samples are skipped while adjusting the loss scale factor.
|
// When gradient values are Inf/NaN, the model update is skipped and the
|
// scale factor is adjusted accordingly for determinism.
|
// The AMP algorithm implemented here mirrors:
|
// - https://arxiv.org/abs/1710.03740
|
// - https://bit.ly/35F5GqX
|
// - https://bit.ly/3mn2qr0
|
bool retrySample = false;
|
do {
|
retrySample = false;
|
// forward
|
meters.fwdtimer.resume();
|
auto input = fl::input(batch[kInputIdx]);
|
if (FLAGS_saug_start_update >= 0 &&
|
curBatch >= FLAGS_saug_start_update) {
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.