MLCrack

    DEEP LEARNING ยท INTERVIEW QUESTION

    What is mixed precision training? How does it speed up deep learning?

    ๐Ÿ”ด advanced ยท Training & Optimization

    Hint: Using FP16 for most operations and FP32 for critical ones.

    Answer

    Mixed precision training uses lower precision (FP16 or BF16) for most computations while keeping FP32 for numerically sensitive operations.

    Interview reasoning

    Process:

    1. Keep FP32 master copy of weights.

    2. Use autocast to choose lower precision for eligible forward operations; gains depend on hardware and kernels.

    3. Autocast keeps sensitive losses/reductions in appropriate precision, often FP32. Backward operations follow the precision of corresponding forward operations; gradients and optimizer states depend on parameter/state dtype.

    4. Loss scaling: multiply loss by a large factor before backward to prevent gradient underflow in FP16 range.

    5. Unscale gradients, check nonfinite values, and update FP32 parameters/master weights with the chosen optimizer.

    Benefits: potentially faster training and lower activation memory, enabling larger batches. Total savings vary because optimizer/master states may stay FP32; benchmark speed and peak memory. BF16 (bfloat16): same exponent range as FP32 but reduced mantissa. Usually does not require FP16-style loss scaling; verify numerical stability for the workload. Common in LLM training. FP8: emerging for even faster training (H100 GPUs).

    Tools: PyTorch AMP (torch.amp and torch.autocast), DeepSpeed, FSDP all support mixed precision.

    Mixed precision uses lower-precision tensors and supported fast kernels while often retaining higher-precision accumulation or optimizer state. FP16 can need loss scaling for tiny gradients; BF16 offers a wider exponent range but different precision. Compare actual time-to-quality, not just memory savings or nominal datatype.

    Worked example. Scaling loss by 1,024 scales gradients similarly; unscale before clipping and step only when the gradients are finite.

    Validation

    Compare FP32 and mixed precision on loss, gradient nonfinites, validation quality, peak memory and complete training time.

    Common pitfalls

    Not all operators benefit equally. Scaling cannot repair an excessive learning rate or unstable activations, and clipping before unscaling applies the wrong threshold.