From b57039188431b37dbee538628649e52478ae0e01 Mon Sep 17 00:00:00 2001 From: Lee <12025119019@stu.ynu.edu.cn> Date: Fri, 4 Sep 2026 12:06:04 +0800 Subject: [PATCH] fix(quantization): fix nvfp4 availability check --- modelopt/torch/quantization/backends/nvfp4_gemm.py | 12 +++--------- modelopt/torch/quantization/backends/utils.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/modelopt/torch/quantization/backends/nvfp4_gemm.py b/modelopt/torch/quantization/backends/nvfp4_gemm.py index bc6d8b8a86e..ed83fdf6c2c 100644 --- a/modelopt/torch/quantization/backends/nvfp4_gemm.py +++ b/modelopt/torch/quantization/backends/nvfp4_gemm.py @@ -20,7 +20,7 @@ import modelopt.torch.quantization as mtq from modelopt.torch.quantization.backends.gemm_registry import gemm_registry -from modelopt.torch.quantization.backends.utils import fp4_compatible +from modelopt.torch.quantization.backends.utils import check_attributes, fp4_compatible from modelopt.torch.quantization.qtensor import NVFP4QTensor, QTensorWrapper from modelopt.torch.quantization.utils import reduce_amax @@ -234,20 +234,14 @@ def _nvfp4_availability_check(module, input, args, kwargs): for key, value in input_cfg.items(): if key == "enable": continue - if ( - not hasattr(module.input_quantizer, key) - or getattr(module.input_quantizer, key) != value - ): + if not check_attributes(module.input_quantizer, key, value): return False # Check weight quantizer config for key, value in weight_cfg.items(): if key == "enable": continue - if ( - not hasattr(module.weight_quantizer, key) - or getattr(module.weight_quantizer, key) != value - ): + if not check_attributes(module.weight_quantizer, key, value): return False # When the input.shape[1] is not the multiple of 64, GEMM will sometimes output NaN. diff --git a/modelopt/torch/quantization/backends/utils.py b/modelopt/torch/quantization/backends/utils.py index 6ed133f5d6c..0067747062e 100644 --- a/modelopt/torch/quantization/backends/utils.py +++ b/modelopt/torch/quantization/backends/utils.py @@ -30,3 +30,15 @@ def fp4_compatible(): if not torch.cuda.is_available(): return False return torch.cuda.get_device_capability(0) >= (10, 0) + + +def check_attributes(obj: object, key: str, value: object) -> bool: + """Check the attributes of objects such as quantizers.""" + _key = f"_{key}" + + if hasattr(obj, key): + return getattr(obj, key) == value + elif hasattr(obj, _key): + return getattr(obj, _key) == value + + return False