devmm03 Claude Sonnet 5 commited on
Commit
ff62b77
·
1 Parent(s): 8ac6b2e

fix: use bfloat16 instead of float16 for HAT GPU inference

Browse files

HAT's residual stream isn't scaled between attention blocks, so its
magnitude compounds across the 4 RHAG groups. A real checkpoint run
showed it reaching ~4.6M in fp32, far past float16's ~65504 max. The
production GPU path autocasts to float16, so it silently overflowed to
NaN/Inf partway through the stack; those NaNs then rendered as solid
black PNGs. bfloat16 keeps fp32's exponent range so it doesn't overflow
here, without losing the speed/memory benefit of 16-bit inference.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

Files changed (1) hide show
  1. services/inference.py +6 -1
services/inference.py CHANGED
@@ -122,7 +122,12 @@ class InferenceEngine:
122
 
123
  with torch.inference_mode():
124
  if batch.device.type == "cuda":
125
- with torch.autocast(device_type="cuda", dtype=torch.float16):
 
 
 
 
 
126
  sr_t, unc_t = self.hat_model(batch)
127
  else:
128
  sr_t, unc_t = self.hat_model(batch)
 
122
 
123
  with torch.inference_mode():
124
  if batch.device.type == "cuda":
125
+ # HAT's residual stream isn't scaled between attention blocks, so its
126
+ # magnitude compounds across the 4 RHAG groups (observed to reach ~1e6
127
+ # in fp32 on real inputs). float16's ~65504 max overflows partway through
128
+ # the stack, producing NaNs that render as solid black. bfloat16 keeps
129
+ # fp32's exponent range (same ~3.4e38 max) so it doesn't overflow here.
130
+ with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
131
  sr_t, unc_t = self.hat_model(batch)
132
  else:
133
  sr_t, unc_t = self.hat_model(batch)