Skip to content

Commit 2f4a717

Browse files
Fix training gradient underflow in quantization tests (#13539)
* Fix training gradient underflow in quantization tests Change autocast dtype from float16 to bfloat16 in _test_quantization_training. Float16's limited dynamic range causes gradients to underflow to zero when passing through quantized tensor subclass operations. * fix autocast dtype check Signed-off-by: jiqing-feng <jiqing.feng@intel.com> --------- Signed-off-by: jiqing-feng <jiqing.feng@intel.com> Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
1 parent 0ad0a32 commit 2f4a717

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

tests/models/testing_utils/quantization.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ def _test_quantization_training(self, config_kwargs):
407407
# Step 3: run forward and backward pass
408408
inputs = self.get_dummy_inputs()
409409

410-
with torch.amp.autocast(torch_device, dtype=torch.float16):
410+
# Use bfloat16 on XPU to avoid gradient underflow with quantized layers
411+
autocast_dtype = torch.bfloat16 if torch_device == "xpu" else torch.float16
412+
with torch.amp.autocast(torch_device, dtype=autocast_dtype):
411413
out = model(**inputs, return_dict=False)[0]
412414
out.norm().backward()
413415

0 commit comments

Comments
 (0)