Struct std::LossScaler
struct LossScaler {
float scale;
float growthFactor;
float backoffFactor;
int growthInterval;
int goodSteps;
}Fields
multiply the loss by this before Tensor::backward()
scale *= growthFactor after growthInterval clean steps
scale *= backoffFactor after an overflow
clean steps since the last change
Methods
Call after Tensor::backward() on (loss * scale): divides p.grad by the scale in place and returns 1 if every gradient is finite, or 0 if any gradient overflowed (inf/NaN) — skip the optimizer step in that case.
LossScaler::unscale over every tensor in a list; 1 if all are finite.
Call once per step with the combined result of LossScaler::unscale over all parameters (1 = all finite). Grows or backs off the scale.
scale 65536, growth 2x every 2000 clean steps, backoff 0.5x — the defaults PyTorch's GradScaler uses.