Enable or disable quantized matmul on pre-quant models

This commit is contained in:
Disty0
2025-11-26 21:08:15 +03:00
parent 86a506f549
commit 48b5d56ba4
2 changed files with 13 additions and 4 deletions
+10 -1
View File
@@ -14,7 +14,7 @@ from modules import timer, paths, shared, shared_items, modelloader, devices, sc
from modules.memstats import memory_stats
from modules.modeldata import model_data
from modules.sd_checkpoint import CheckpointInfo, select_checkpoint, list_models, checkpoints_list, checkpoint_titles, get_closest_checkpoint_match, model_hash, update_model_hashes, setup_model, write_metadata, read_metadata_from_safetensors # pylint: disable=unused-import
from modules.sd_offload import disable_offload, set_diffuser_offload, apply_balanced_offload, set_accelerate # pylint: disable=unused-import
from modules.sd_offload import get_module_names, disable_offload, set_diffuser_offload, apply_balanced_offload, set_accelerate # pylint: disable=unused-import
from modules.sd_models_utils import NoWatermark, get_signature, get_call, path_to_repo, patch_diffuser_config, convert_to_faketensors, read_state_dict, get_state_dict_from_checkpoint, apply_function_to_model # pylint: disable=unused-import
@@ -164,6 +164,15 @@ def set_diffuser_options(sd_model, vae=None, op:str='model', offload:bool=True,
shared.log.quiet(quiet, f'Setting {op}: channels-last=True')
sd_model.unet.to(memory_format=torch.channels_last)
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":
if module.quantization_config.use_quantized_matmul != shared.opts.sdnq_use_quantized_matmul:
from 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}')
module = apply_sdnq_options_to_model(module, use_quantized_matmul=shared.opts.sdnq_use_quantized_matmul)
setattr(sd_model, module_name, module)
if offload:
set_diffuser_offload(sd_model, op, quiet)
+3 -3
View File
@@ -134,7 +134,7 @@ def load_sdnq_model(model_path: str, model_cls: ModelMixin = None, file_name: st
model = post_process_model(model)
if (dtype is not None) or (dequantize_fp32 is not None) or (use_quantized_matmul is not None):
model = apply_options_to_model(model, dtype=dtype, dequantize_fp32=dequantize_fp32, use_quantized_matmul=use_quantized_matmul)
model = apply_sdnq_options_to_model(model, dtype=dtype, dequantize_fp32=dequantize_fp32, use_quantized_matmul=use_quantized_matmul)
return model
@@ -154,7 +154,7 @@ def post_process_model(model):
return model
def apply_options_to_model(model, dtype: torch.dtype = None, dequantize_fp32: bool = None, use_quantized_matmul: bool = None):
def apply_sdnq_options_to_model(model, dtype: torch.dtype = None, dequantize_fp32: bool = None, use_quantized_matmul: bool = None):
has_children = list(model.children())
if not has_children:
if dtype is not None and getattr(model, "dtype", torch.float32) != torch.float32:
@@ -199,5 +199,5 @@ def apply_options_to_model(model, dtype: torch.dtype = None, dequantize_fp32: bo
module.forward = module.forward.__get__(module, module.__class__)
setattr(model, module_name, module)
else:
setattr(model, module_name, apply_options_to_model(module, dtype=dtype, dequantize_fp32=dequantize_fp32, use_quantized_matmul=use_quantized_matmul))
setattr(model, module_name, apply_sdnq_options_to_model(module, dtype=dtype, dequantize_fp32=dequantize_fp32, use_quantized_matmul=use_quantized_matmul))
return model