Improve TorchAO quantization test coverage and XPU support#13530
Improve TorchAO quantization test coverage and XPU support#13530jiqing-feng wants to merge 11 commits intohuggingface:mainfrom
Conversation
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
|
Hi @sayakpaul . Would you please review this PR? Thanks! |
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
|
There are a bunch of things going on in this PR. I would suggest breaking the PR into smaller PRs. |
|
Hi @sayakpaul . Thanks for the review! I've split this PR into 5 smaller independent PRs as suggested:
Each PR is independent and can be reviewed/merged separately. Will close this PR once the split PRs are up. |
| def is_compileable(self) -> bool: | ||
| return True | ||
|
|
||
| def _dequantize(self, model): |
There was a problem hiding this comment.
We shouldn't have dequantize here in this PR right?
There was a problem hiding this comment.
Yes, please review the change here: #13538
| return { | ||
| "hidden_states": randn_tensor( | ||
| (1, 36, 21, 64, 64), generator=self.generator, device=torch_device, dtype=self.torch_dtype | ||
| (1, 36, 5, 16, 16), generator=self.generator, device=torch_device, dtype=self.torch_dtype |
There was a problem hiding this comment.
It's for avoiding OOM, details see: #13541. Please let me know if you want comments in the code.
| def _get_dummy_inputs_for_model(self, model): | ||
| inputs = self.get_dummy_inputs() | ||
| model_dtype = next(model.parameters()).dtype | ||
| return { | ||
| k: v.to(model_dtype) if isinstance(v, torch.Tensor) and v.is_floating_point() else v | ||
| for k, v in inputs.items() | ||
| } |
There was a problem hiding this comment.
QuantizationCompileTesterMixin is an independent mixin that doesn't inherit from QuantizationTesterMixin. Test classes may use either one or both, so the method needs to be defined in both places.
Alternatively, I can extract it into a shared base class or a standalone utility function to avoid code duplication. Let me know which approach you prefer. Please review this change in #13539
|
Hi @sayakpaul . I have separated this PR into 5 small PRs, please review them 1 by 1 if it is easier for you. Thanks! |
What does this PR do?
This PR improves the TorchAO quantization testing infrastructure with several fixes: enabling
int4wotests on Intel XPU, implementing_dequantizefor TorchAO, fixing input dtype mismatches, and fixing training gradient underflow.Changes
Enable int4wo tests on XPU: Removed the
_int4wo_skipmarker that restrictedint4wotests to CUDA only, allowing them to run on all accelerator backends.XPU-specific int4 packing format: Added XPU-specific handling in
_get_quant_config()— Intel XPU requiresint4_packing_format="plain_int32"forInt4WeightOnlyConfig.Fix input dtype casting: Introduced
_get_dummy_inputs_for_model(model)helper inQuantizationTesterMixinto automatically cast floating-point input tensors to the model's parameter dtype, preventing dtype mismatches during quantized model inference.Implement
_dequantizefor TorchAO: Added_dequantize()method inTorchAoHfQuantizerthat iterates allnn.Linearmodules, callsweight.dequantize()onTorchAOBaseTensorweights, and replaces them with standardnn.Parameter. Also fixed_verify_if_layer_quantizedto checkisinstance(module.weight, TorchAOBaseTensor)so dequantized layers are correctly detected as non-quantized.Fix training gradient underflow: Changed autocast dtype from
float16tobfloat16in_test_quantization_training. Float16's limited dynamic range (max ~65504, min subnormal ~5.96e-8) causes gradients to underflow to zero when passing through quantized tensor subclass operations; bfloat16 shares float32's exponent range and avoids this issue.Reduce WanAnimate TorchAO test input sizes: Shrunk dummy inputs in
TestWanAnimateTransformer3DTorchAoto avoid OOM on devices without FlashAttention (e.g. XPU, which falls back to math SDPA and materializes the full O(S²) attention matrix). Reducedhidden_statesfrom (1,36,21,64,64) to (1,36,5,16,16) andface_pixel_valuesfrom (1,3,77,512,512) to (1,3,13,512,512), bringing self-attention sequence length from 21,504 to 320 and peak attention memory from ~74 GiB to ~16 MB. Face frame count (13) is chosen so the face encoder's two stride-2 convolutions produce temporal output 4, plus 1 padding = 5, matchinghidden_statestemporal dim.