Loading content...
In modern deep learning, training efficiency is paramount. Large-scale neural networks can have billions of parameters, making memory bandwidth and computational throughput critical bottlenecks. Hybrid Precision Computation is a powerful technique that strategically combines different numerical precisions—using lower precision (float16 or bfloat16) for speed-critical operations while maintaining higher precision (float32) where numerical stability is essential.
The core insight is that neural network forward passes are robust to reduced precision because the values involved are typically within a reasonable range, and small rounding errors don't significantly affect the outcome. However, gradients during backpropagation can be very small (especially in deep networks), risking underflow to zero in reduced precision formats.
Loss Scaling addresses this challenge elegantly:
This approach preserves representable gradient magnitudes in float16 range while keeping the final gradient values accurate in float32.
Your Task:
Implement a HybridPrecisionTrainer class in Python using NumPy that performs hybrid precision training:
__init__(self, loss_scale=1024.0): Initialize with a loss scaling factor
forward(self, weights, inputs, targets):
backward(self, gradients):
weights = [0.5, -0.3]
inputs = [[1.0, 2.0], [3.0, 4.0]]
targets = [1.0, 0.0]
gradients = [512.0, -256.0]
loss_scale = 1024.0
method = "combined"{"loss": 665.0, "gradients": [0.5, -0.25]}Forward Pass:
Backward Pass:
weights = [1.0, 1.0]
inputs = [[1.0, 0.0], [0.0, 1.0]]
targets = [1.0, 1.0]
loss_scale = 1024.0
method = "forward"0.0Forward Pass:
The predictions exactly match the targets, so the loss is zero.
gradients = [1024.0, 2048.0, 512.0]
loss_scale = 1024.0
method = "backward"[1.0, 2.0, 0.5]Backward Pass:
Constraints