From 01a0f6b356bce04e1abcedea95c2b31da6855b85 Mon Sep 17 00:00:00 2001 From: Disty0 Date: Sat, 29 Nov 2025 01:34:54 +0300 Subject: [PATCH] Warn and disable quantized matmul if triton is not available --- modules/model_quant.py | 10 ++++++++++ modules/sd_models.py | 4 ++++ modules/sdnq/common.py | 2 +- modules/sdnq/loader.py | 4 +++- modules/sdnq/quantizer.py | 2 +- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/model_quant.py b/modules/model_quant.py index ca4a89287..940594890 100644 --- a/modules/model_quant.py +++ b/modules/model_quant.py @@ -166,6 +166,11 @@ def create_sdnq_config(kwargs = None, allow: bool = True, module: str = 'Model', from modules import shared if allow and (shared.opts.sdnq_quantize_mode in {'pre', 'auto'}) and (module == 'any' or module in shared.opts.sdnq_quantize_weights): from modules.sdnq import SDNQConfig + from modules.sdnq.common import use_torch_compile as sdnq_use_torch_compile + + if shared.opts.sdnq_use_quantized_matmul and not sdnq_use_torch_compile: + shared.log.warning('SDNQ Quantized MatMul requires a working Triton install. Disabling Quantized MatMul.') + shared.opts.sdnq_use_quantized_matmul = False if weights_dtype is None: if module in {"TE", "LLM"} and shared.opts.sdnq_quantize_weights_mode_te not in {"Same as model", "default"}: @@ -492,6 +497,11 @@ def sdnq_quantize_model(model, op=None, sd_model=None, do_gc: bool = True, weigh global quant_last_model_name, quant_last_model_device # pylint: disable=global-statement from modules import devices, shared, timer from modules.sdnq import sdnq_post_load_quant + from modules.sdnq.common import use_torch_compile as sdnq_use_torch_compile + + if shared.opts.sdnq_use_quantized_matmul and not sdnq_use_torch_compile: + shared.log.warning('SDNQ Quantized MatMul requires a working Triton install. Disabling Quantized MatMul.') + shared.opts.sdnq_use_quantized_matmul = False if weights_dtype is None: if (op is not None) and ("text_encoder" in op or op in {"TE", "LLM"}) and (shared.opts.sdnq_quantize_weights_mode_te not in {"Same as model", "default"}): diff --git a/modules/sd_models.py b/modules/sd_models.py index 1d4a87775..a8d501105 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -167,6 +167,10 @@ def set_diffuser_options(sd_model, vae=None, op:str='model', offload:bool=True, for module_name in get_module_names(sd_model): module = getattr(sd_model, module_name, None) if hasattr(module, "quantization_config") and getattr(module.quantization_config, "quant_method", None) == "sdnq": + from modules.sdnq.common import use_torch_compile as sdnq_use_torch_compile + if shared.opts.sdnq_use_quantized_matmul and not sdnq_use_torch_compile: + shared.log.warning('SDNQ Quantized MatMul requires a working Triton install. Disabling Quantized MatMul.') + shared.opts.sdnq_use_quantized_matmul = False if module.quantization_config.use_quantized_matmul != shared.opts.sdnq_use_quantized_matmul: from modules.sdnq.loader import apply_sdnq_options_to_model shared.log.debug(f'Setting {op} {module_name}: sdnq_use_quantized_matmul={shared.opts.sdnq_use_quantized_matmul}') diff --git a/modules/sdnq/common.py b/modules/sdnq/common.py index 6f7d6ba50..e8d3c9ae4 100644 --- a/modules/sdnq/common.py +++ b/modules/sdnq/common.py @@ -5,7 +5,7 @@ import torch from modules import shared, devices -sdnq_version = "0.1.0" +sdnq_version = "0.1.1" dtype_dict = { "int32": {"min": -2147483648, "max": 2147483647, "num_bits": 32, "sign": 1, "exponent": 0, "mantissa": 31, "target_dtype": torch.int32, "torch_dtype": torch.int32, "storage_dtype": torch.int32, "is_unsigned": False, "is_integer": True, "is_packed": False}, diff --git a/modules/sdnq/loader.py b/modules/sdnq/loader.py index 0821e62ad..9120dd19d 100644 --- a/modules/sdnq/loader.py +++ b/modules/sdnq/loader.py @@ -3,7 +3,7 @@ import json import torch from diffusers.models.modeling_utils import ModelMixin -from .common import dtype_dict, use_tensorwise_fp8_matmul +from .common import dtype_dict, use_tensorwise_fp8_matmul, use_torch_compile from .quantizer import SDNQConfig, sdnq_post_load_quant, prepare_weight_for_matmul, prepare_svd_for_matmul, get_quant_args_from_config from .forward import get_forward_func from .file_loader import load_files @@ -206,6 +206,8 @@ def apply_sdnq_options_to_module(model, dtype: torch.dtype = None, dequantize_fp def apply_sdnq_options_to_model(model, dtype: torch.dtype = None, dequantize_fp32: bool = None, use_quantized_matmul: bool = None): + if use_quantized_matmul and not use_torch_compile: + raise ValueError("SDNQ Quantized MatMul requires a working Triton install.") model = apply_sdnq_options_to_module(model, dtype=dtype, dequantize_fp32=dequantize_fp32, use_quantized_matmul=use_quantized_matmul) if hasattr(model, "quantization_config"): if use_quantized_matmul is not None: diff --git a/modules/sdnq/quantizer.py b/modules/sdnq/quantizer.py index 58d85ebe8..6ac45b58b 100644 --- a/modules/sdnq/quantizer.py +++ b/modules/sdnq/quantizer.py @@ -909,7 +909,7 @@ class SDNQConfig(QuantizationConfigMixin): Safety checker that arguments are correct """ if self.use_quantized_matmul and not use_torch_compile: - raise ValueError("Quantized MatMul requires a working Triton install.") + raise ValueError("SDNQ Quantized MatMul requires a working Triton install.") if self.weights_dtype not in accepted_weight_dtypes: raise ValueError(f"SDNQ only support weight dtypes in {accepted_weight_dtypes} but found {self.weights_dtype}") if self.quantized_matmul_dtype is not None and self.quantized_matmul_dtype not in accepted_matmul_dtypes: