text stringlengths 0 2.2M |
|---|
input = saug->forward({input}).front();
|
}
|
auto output = fl::pkg::runtime::forwardSequentialModuleWithPadMask(
|
input, ntwrk, batch[kDurationIdx]);
|
fl::sync();
|
meters.critfwdtimer.resume();
|
auto loss =
|
crit->forward({output, fl::noGrad(batch[kTargetIdx])}).front();
|
fl::sync();
|
meters.fwdtimer.stopAndIncUnit();
|
meters.critfwdtimer.stopAndIncUnit();
|
if (FLAGS_fl_amp_use_mixed_precision) {
|
++scaleCounter;
|
loss = loss * scaleFactor;
|
}
|
if (af::anyTrue<bool>(af::isNaN(loss.array())) ||
|
af::anyTrue<bool>(af::isInf(loss.array()))) {
|
if (FLAGS_fl_amp_use_mixed_precision &&
|
scaleFactor >= fl::kAmpMinimumScaleFactorValue) {
|
scaleFactor = scaleFactor / 2.0f;
|
FL_VLOG(2) << "AMP: Scale factor decreased. New value:\t"
|
<< scaleFactor;
|
scaleCounter = 1;
|
retrySample = true;
|
continue;
|
} else {
|
LOG(FATAL) << "Loss has NaN values. Samples - "
|
<< join(",", readSampleIds(batch[kSampleIdx]));
|
}
|
}
|
if (hasher(join(",", readSampleIds(batch[kSampleIdx]))) % 100 <=
|
FLAGS_pcttraineval) {
|
evalOutput(output.array(), batch[kTargetIdx], meters.train);
|
}
|
// backward
|
meters.bwdtimer.resume();
|
netopt->zeroGrad();
|
loss.backward();
|
if (reducer) {
|
reducer->finalize();
|
}
|
fl::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 * scaleFactor);
|
if (FLAGS_fl_amp_use_mixed_precision) {
|
if (af::anyTrue<bool>(af::isNaN(p.grad().array())) ||
|
af::anyTrue<bool>(af::isInf(p.grad().array()))) {
|
if (scaleFactor >= fl::kAmpMinimumScaleFactorValue) {
|
scaleFactor = scaleFactor / 2.0f;
|
FL_VLOG(2) << "AMP: Scale factor decreased. New value:\t"
|
<< scaleFactor;
|
retrySample = true;
|
}
|
scaleCounter = 1;
|
break;
|
}
|
}
|
}
|
if (retrySample) {
|
meters.optimtimer.stop();
|
continue;
|
}
|
meters.train.loss.add((loss / scaleFactor).array());
|
} while (retrySample);
|
// clamp gradients
|
if (FLAGS_maxgradnorm > 0) {
|
auto params = ntwrk->params();
|
fl::clipGradNorm(params, FLAGS_maxgradnorm);
|
}
|
// update weights
|
netopt->step();
|
fl::sync();
|
meters.optimtimer.stopAndIncUnit();
|
// update scale factor
|
if (FLAGS_fl_amp_use_mixed_precision && scaleFactor < kMaxScaleFactor) {
|
if (scaleCounter % kScaleFactorUpdateInterval == 0) {
|
scaleFactor *= 2;
|
FL_VLOG(2) << "AMP: Scale factor doubled. New value:\t"
|
<< scaleFactor;
|
} else {
|
scaleFactor += 2;
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.