Dynamic Loss Scaler for Mixed Precision

Quick Overview

Implement a dynamic loss scaler for mixed-precision training that scales the loss, unscales gradients, skips steps on Inf or NaN, and grows or backs off the scale. Tests fp16 overflow and underflow handling, SGD updates without mutation, and careful state bookkeeping.

Dynamic Loss Scaler for Mixed Precision

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `scale_loss`, `unscale` and `step` on `DynamicLossScaler`. `step(params, scaled_grads, lr)` unscales gradients, applies plain SGD if all are finite, and adapts `self.scale`. It returns `(new_params, applied)`. The constructor and its defaults are given: ```python class DynamicLossScaler: def __init__(self, init_scale=65536.0, growth_factor=2.0, backoff_factor=0.5, growth_interval=2000, min_scale=1.0, max_scale=2.0 ** 24): self.scale = float(init_scale) self.growth_factor = float(growth_factor) self.backoff_factor = float(backoff_factor) self.growth_interval = int(growth_interval) self.min_scale = float(min_scale) self.max_scale = float(max_scale) self.good_steps = 0 self.skipped_steps = 0 ``` Requirements: - `scale_loss` returns `float(loss) * scale`. `unscale` returns a list of float64 arrays `g / scale`. - Finite step: return new arrays `p - lr * g` (never mutate inputs) and `True`, increment `good_steps`. - When `good_steps` reaches `growth_interval`: `scale = min(scale * growth_factor, max_scale)`, reset `good_steps` to 0. - Any Inf/NaN: return copies of `params` and `False`, set `scale = max(scale * backoff_factor, min_scale)`, reset `good_steps`, increment `skipped_steps`. Example: ```python s = DynamicLossScaler(init_scale=4.0, growth_interval=10) new, ok = s.step([np.array([1.0, 2.0])], [np.array([4.0, -8.0])], lr=0.1) # new == [array([0.9, 2.2])], ok is True, s.good_steps == 1, s.scale == 4.0 new, ok = s.step(new, [np.array([np.nan, 0.0])], lr=0.1) # ok is False, params unchanged, s.scale == 2.0, s.good_steps == 0, s.skipped_steps == 1 ``` ```hint Where overflow shows up A single Inf or NaN in any one tensor is enough to poison the update. Decide whether to skip before any parameter is computed. ``` ```hint Counter bookkeeping Trace what `good_steps` should be right after a skipped step, and right after the step that triggers growth. ``` ### Constraints & Assumptions - `params` and `scaled_grads` are equal-length lists of same-shaped arrays. - Finiteness is checked on the unscaled gradients across all tensors. ### What a Strong Answer Covers - Checks finiteness on the unscaled gradients of every tensor and skips the whole step on any Inf or NaN. - Applies backoff and growth with the `min_scale` and `max_scale` clamps, and keeps `good_steps` and `skipped_steps` consistent on both paths. - Returns new arrays on both paths without mutating the inputs. - Traces the example and states the per-step cost. ### Follow-up Questions - Why is finiteness checked on the unscaled gradients, and why is a finite scaled loss not enough? - How would this integrate with gradient clipping? - Why keep master weights in fp32 alongside fp16 activations?

Overview: Implement a dynamic loss scaler for mixed-precision training that scales the loss, unscales gradients, skips steps on Inf or NaN, and grows or backs off the scale. Tests fp16 overflow and underflow handling, SGD updates without mutation, and careful state bookkeeping.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement scale_loss, unscale and step on DynamicLossScaler. step(params, scaled_grads, lr) unscales gradients, applies plain SGD if all are finite, and adapts self.scale. It returns (new_params, applied).

The constructor and its defaults are given:

class DynamicLossScaler:
    def __init__(self, init_scale=65536.0, growth_factor=2.0, backoff_factor=0.5,
                 growth_interval=2000, min_scale=1.0, max_scale=2.0 ** 24):
        self.scale = float(init_scale)
        self.growth_factor = float(growth_factor)
        self.backoff_factor = float(backoff_factor)
        self.growth_interval = int(growth_interval)
        self.min_scale = float(min_scale)
        self.max_scale = float(max_scale)
        self.good_steps = 0
        self.skipped_steps = 0

Requirements:

  • scale_loss returns float(loss) * scale . unscale returns a list of float64 arrays g / scale .
  • Finite step: return new arrays p - lr * g (never mutate inputs) and True , increment good_steps .
  • When good_steps reaches growth_interval : scale = min(scale * growth_factor, max_scale) , reset good_steps to 0.
  • Any Inf/NaN: return copies of params and False , set scale = max(scale * backoff_factor, min_scale) , reset good_steps , increment skipped_steps .

Example:

s = DynamicLossScaler(init_scale=4.0, growth_interval=10)
new, ok = s.step([np.array([1.0, 2.0])], [np.array([4.0, -8.0])], lr=0.1)
# new == [array([0.9, 2.2])], ok is True, s.good_steps == 1, s.scale == 4.0
new, ok = s.step(new, [np.array([np.nan, 0.0])], lr=0.1)
# ok is False, params unchanged, s.scale == 2.0, s.good_steps == 0, s.skipped_steps == 1

Constraints & Assumptions

  • params and scaled_grads are equal-length lists of same-shaped arrays.
  • Finiteness is checked on the unscaled gradients across all tensors.

What a Strong Answer Covers Guidance

  • Checks finiteness on the unscaled gradients of every tensor and skips the whole step on any Inf or NaN.
  • Applies backoff and growth with the min_scale and max_scale clamps, and keeps good_steps and skipped_steps consistent on both paths.
  • Returns new arrays on both paths without mutating the inputs.
  • Traces the example and states the per-step cost.

Follow-up Questions Guidance

  • Why is finiteness checked on the unscaled gradients, and why is a finite scaled loss not enough?
  • How would this integrate with gradient clipping?
  • Why keep master weights in fp32 alongside fp16 activations?