Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

add sdxl svdquant (#2251)

Wang, Chang1c3a55182ccc
diff --git a/auto_round/algorithms/transforms/svdquant/apply.py b/auto_round/algorithms/transforms/svdquant/apply.py
index 1bc582f95..8257e7ceb 100644
--- a/auto_round/algorithms/transforms/svdquant/apply.py
+++ b/auto_round/algorithms/transforms/svdquant/apply.py
@@ -118,10 +118,15 @@ class SVDQuantTransform(BasePreprocessor):
self._block_groups: dict[str, list[SmoothSearchGroup]] = {}
self._smooth_calibration: dict[str, SmoothGroupCalibration] = {}
self._target_modules = config.target_modules
- if self._target_modules is None and config.model_adapter == "flux":
- from auto_round.export.svdquant_adapters import FLUX_SVDQUANT_TARGET_MODULES
+ if self._target_modules is None and config.model_adapter in {"flux", "sdxl"}:
+ from auto_round.export.svdquant_adapters import (
+ FLUX_SVDQUANT_TARGET_MODULES,
+ SDXL_SVDQUANT_TARGET_MODULES,
+ )
- self._target_modules = FLUX_SVDQUANT_TARGET_MODULES
+ self._target_modules = (
+ FLUX_SVDQUANT_TARGET_MODULES if config.model_adapter == "flux" else SDXL_SVDQUANT_TARGET_MODULES
+ )
def bind(self, orchestrator) -> None:
super().bind(orchestrator)
@@ -150,18 +155,18 @@ class SVDQuantTransform(BasePreprocessor):
def _resolve_model_adapter(self, model: torch.nn.Module | None, block: torch.nn.Module | None = None) -> str:
model_adapter = self.config.model_adapter or "auto"
if model_adapter == "auto":
- model_config = getattr(model, "config", None)
- class_name = (
- model_config.get("_class_name", type(model).__name__)
- if hasattr(model_config, "get")
- else type(model).__name__
- )
- if "fluxtransformer" in str(class_name).lower():
- model_adapter = "flux"
- elif block is not None and block.__class__.__name__ in {
- "FluxTransformerBlock",
- "FluxSingleTransformerBlock",
- }:
+ from auto_round.export.svdquant_adapters import detect_svdquant_model_adapter
+
+ model_adapter = detect_svdquant_model_adapter(model) if model is not None else "identity"
+ if (
+ model_adapter == "identity"
+ and block is not None
+ and block.__class__.__name__
+ in {
+ "FluxTransformerBlock",
+ "FluxSingleTransformerBlock",
+ }
+ ):
model_adapter = "flux"
if model is not None:
model._autoround_svdquant_model_adapter = model_adapter
@@ -171,10 +176,15 @@ class SVDQuantTransform(BasePreprocessor):
if model is not None:
warn_if_unverified_flux_model(model)
self._target_modules = self.config.target_modules
- if self._target_modules is None and model_adapter == "flux":
- from auto_round.export.svdquant_adapters import FLUX_SVDQUANT_TARGET_MODULES
+ if self._target_modules is None and model_adapter in {"flux", "sdxl"}:
+ from auto_round.export.svdquant_adapters import (
+ FLUX_SVDQUANT_TARGET_MODULES,
+ SDXL_SVDQUANT_TARGET_MODULES,
+ )
- self._target_modules = FLUX_SVDQUANT_TARGET_MODULES
+ self._target_modules = (
+ FLUX_SVDQUANT_TARGET_MODULES if model_adapter == "flux" else SDXL_SVDQUANT_TARGET_MODULES
+ )
return model_adapter
def register_fp_input_forward_hooks(self, block) -> list:
diff --git a/auto_round/algorithms/transforms/svdquant/config.py b/auto_round/algorithms/transforms/svdquant/config.py
index 6f67cd1ad..8a7670b93 100644
--- a/auto_round/algorithms/transforms/svdquant/config.py
+++ b/auto_round/algorithms/transforms/svdquant/config.py
@@ -102,7 +102,7 @@ class SVDQuantConfig(QuantizationConfig):
"--svdquant-model-adapter",
field="model_adapter",
default="auto",
- choices=["auto", "identity", "flux"],
+ choices=["auto", "identity", "flux", "sdxl"],
help="Architecture adapter used by SVDQuant export.",
)
diff --git a/auto_round/algorithms/transforms/svdquant/smooth_adapters/__init__.py b/auto_round/algorithms/transforms/svdquant/smooth_adapters/__init__.py
index c8613c305..02d79ab0e 100644
--- a/auto_round/algorithms/transforms/svdquant/smooth_adapters/__init__.py
+++ b/auto_round/algorithms/transforms/svdquant/smooth_adapters/__init__.py
@@ -22,12 +22,15 @@ from auto_round.algorithms.transforms.svdquant.smooth_adapters.base import (
generic_linear_groups,
)
from auto_round.algorithms.transforms.svdquant.smooth_adapters.flux import discover_flux_groups, supports_flux_block
+from auto_round.algorithms.transforms.svdquant.smooth_adapters.sdxl import discover_sdxl_groups, supports_sdxl_block
def discover_svdquant_groups(block: torch.nn.Module, is_target: TargetPredicate) -> list[SmoothSearchGroup]:
"""Discover shared-input projection groups for one quantization block."""
if supports_flux_block(block):
return discover_flux_groups(block, is_target)
+ if supports_sdxl_block(block):
+ return discover_sdxl_groups(block, is_target)
return generic_linear_groups(block, is_target)
diff --git a/auto_round/algorithms/transforms/svdquant/smooth_adapters/sdxl.py b/auto_round/algorithms/transforms/svdquant/smooth_adapters/sdxl.py
new file mode 100644
index 000000000..b10f52e46
--- /dev/null
+++ b/auto_round/algorithms/transforms/svdquant/smooth_adapters/sdxl.py
@@ -0,0 +1,67 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Projection grouping for SDXL BasicTransformerBlock SVDQuant."""
+
+from __future__ import annotations
+
+import torch
+
+from auto_round.algorithms.transforms.svdquant.smooth_adapters.base import (
+ SmoothSearchGroup,
+ TargetPredicate,
+ generic_linear_groups,
+ module_global_name,
+ resolve_module,
+)
+
+_SELF_ATTENTION_QKV = ("attn1.to_q", "attn1.to_k", "attn1.to_v")
+
+
+def supports_sdxl_block(block: torch.nn.Module) -> bool:
+ return block.__class__.__name__ == "BasicTransformerBlock"
+
+
+def discover_sdxl_groups(block: torch.nn.Module, is_target: TargetPredicate) -> list[SmoothSearchGroup]:
+ selected = []
+ for path in _SELF_ATTENTION_QKV:
+ module = resolve_module(block, path)
+ if isinstance(module, torch.nn.Linear) and is_target(path, module):
+ selected.append((path, module))
+
+ groups = []
+ consumed = set()
+ if len(selected) == len(_SELF_ATTENTION_QKV):
+ attention = resolve_module(block, "attn1")
+ if attention is None:
+ raise ValueError(f"SDXL SVDQuant group {module_global_name(block, 'attn1.qkv')!r} has no attention module")
+ names = tuple(module_global_name(block, path) for path, _ in selected)
+ projections = tuple(module for _, module in selected)
+ groups.append(
+ SmoothSearchGroup(
+ key=module_global_name(block, "attn1.qkv"),
+ projection_names=names,
+ projections=projections,
+ projection_input_key=names[0],
+ projection_input_module=projections[0],
+ evaluation_input_key=module_global_name(block, "attn1"),
+ evaluation_module=attention,
+ )
+ )
+ consumed.update(id(module) for module in projections)
+ groups.extend(generic_linear_groups(block, is_target, consumed=consumed))
+ return groups
+
+
+__all__ = ["discover_sdxl_groups", "supports_sdxl_block"]
diff --git a/auto_round/export/formats/backends/svdquant_nunchaku.py b/auto_round/export/formats/backends/svdquant_nunchaku.py
index 2b58f9abf..d8c36c424 100644
--- a/auto_round/export/formats/backends/svdquant_nunchaku.py
+++ b/auto_round/export/formats/backends/svdquant_nunchaku.py
@@ -115,7 +115,15 @@ class SVDQuantNunchakuFormat(OutputFormat):
def check_and_reset_format(self, scheme: QuantizationScheme, ctx: Any):
self._validate_svd_layer_overrides(ctx.model, ctx.layer_config)
- return None, scheme, ctx.layer_config, ctx.quant_block_list
+ quant_block_list = ctx.quant_block_list
+ if quant_block_list is None and ctx.model is not None:
+ from auto_round.export.svdquant_adapters import resolve_svdquant_model_adapter
+
+ model_adapter = resolve_svdquant_model_adapter("auto", ctx.model)
+ adapter_quant_block_list = getattr(model_adapter, "quant_block_list", None)
+ if callable(adapter_quant_block_list):
+ quant_block_list = adapter_quant_block_list(ctx.model)
+ return None, scheme, ctx.layer_config, quant_block_list
def pack_layer(self, *args, **kwargs):
return None
diff --git a/auto_round/export/svdquant_adapters/__init__.py b/auto_round/export/svdquant_adapters/__init__.py
index e20874ead..1404640b9 100644
--- a/auto_round/export/svdquant_adapters/__init__.py
+++ b/auto_round/export/svdquant_adapters/__init__.py
@@ -28,6 +28,11 @@ from auto_round.export.svdquant_adapters.flux import (
FluxSVDQuantNunchakuAdapter,
flux_onefile_tensor_count,
)
+from auto_round.export.svdquant_adapters.sdxl import (
+ SDXL_SVDQUANT_TARGET_MODULES,
+ SDXLSVDQuantNunchakuAdapter,
+ is_sdxl_unet_config,
+)
def _model_config(model: torch.nn.Module) -> dict:
@@ -42,6 +47,18 @@ def _model_config(model: torch.nn.Module) -> dict:
return {}
+def detect_svdquant_model_adapter(model: torch.nn.Module) -> str:
+ """Return the runtime adapter name implied by a model's architecture config."""
+
+ config = _model_config(model)
+ class_name = str(config.get("_class_name", type(model).__name__)).lower()
+ if "fluxtransformer" in class_name:
+ return "flux"
+ if is_sdxl_unet_config(config, type(model).__name__):
+ return "sdxl"
+ return "identity"
+
+
def resolve_svdquant_model_adapter(
name: str,
model: torch.nn.Module,
@@ -51,25 +68,33 @@ def resolve_svdquant_model_adapter(
"""Resolve a registered architecture adapter without runtime dependencies."""
normalized = name.strip().lower()
- if normalized not in {"auto", "identity", "flux"}:
- raise ValueError(f"unknown SVDQuant model adapter {name!r}; expected auto, identity, or flux")
+ if normalized not in {"auto", "identity", "flux", "sdxl"}:
+ raise ValueError(f"unknown SVDQuant model adapter {name!r}; expected auto, identity, flux, or sdxl")
config = _model_config(model)
- class_name = str(config.get("_class_name", type(model).__name__)).lower()
if normalized == "auto":
- normalized = "flux" if "fluxtransformer" in class_name else "identity"
+ normalized = detect_svdquant_model_adapter(model)
if normalized == "flux":
return FluxSVDQuantNunchakuAdapter(
config=config or None,
decomposition_device=decomposition_device,
require_complete_model=True,
)
+ if normalized == "sdxl":
+ return SDXLSVDQuantNunchakuAdapter(
+ config=config or None,
+ decomposition_device=decomposition_device,
+ require_complete_model=True,
+ )
return IdentitySVDQuantModelAdapter()
__all__ = [
"FLUX_TOP_LEVEL_TENSOR_KEYS",
"FLUX_SVDQUANT_TARGET_MODULES",
+ "SDXL_SVDQUANT_TARGET_MODULES",
+ "SDXLSVDQuantNunchakuAdapter",
"FluxSVDQuantNunchakuAdapter",
+ "detect_svdquant_model_adapter",
"flux_onefile_tensor_count",
"resolve_svdquant_model_adapter",
]
diff --git a/auto_round/export/svdquant_adapters/sdxl.py b/auto_round/export/svdquant_adapters/sdxl.py
new file mode 100644
index 000000000..3d520afda
--- /dev/null
+++ b/auto_round/export/svdquant_adapters/sdxl.py
@@ -0,0 +1,389 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""SDXL model mapping for runtime-loadable SVDQuant Nunchaku artifacts."""
+
+from __future__ import annotations
+
+import json
+import os
+import re
+from collections.abc import Iterable, Mapping
+from dataclasses import dataclass
+from typing import Any
+
+import torch
+
+from auto_round.export.svdquant_nunchaku import SourceLinearRecord, SVDQuantExportRecord
+
+_BLOCK_RE = re.compile(r"^(.*\.transformer_blocks\.\d+)\.(.+)$")
+_QKV_SOURCES = ("attn1.to_q", "attn1.to_k", "attn1.to_v")
+_DIRECT_SOURCES = (
+ "attn1.to_out.0",
+ "attn2.to_q",
+ "attn2.to_out.0",
+ "ff.net.0.proj",
+ "ff.net.2",
+)
+_SDXL_BASE_SEQUENCE_FIELDS = {
+ "block_out_channels": (320, 640, 1280),
+ "down_block_types": ("DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D"),
+ "up_block_types": ("CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D"),
+ "transformer_layers_per_block": (1, 2, 10),
+ "attention_head_dim": (5, 10, 20),
+}
+_SDXL_BASE_SCALAR_FIELDS = {
+ "addition_embed_type": "text_time",
+ "cross_attention_dim": 2048,
+ "projection_class_embeddings_input_dim": 2816,
+ "layers_per_block": 2,
+ "sample_size": 128,
+ "in_channels": 4,
+ "out_channels": 4,
+ "use_linear_projection": True,
+}
+
+SDXL_SVDQUANT_TARGET_MODULES = (
+ "attn1.to_q",
+ "attn1.to_k",
+ "attn1.to_v",
+ "attn1.to_out.0",
+ "attn2.to_q",
+ "attn2.to_out.0",
+ "ff.net.0.proj",
+ "ff.net.2",
+)
+
+
+def _config_dict(value: Any) -> dict[str, Any]:
+ if isinstance(value, Mapping):
+ return dict(value)
+ to_dict = getattr(value, "to_dict", None)
+ if callable(to_dict):
+ result = to_dict()
+ if isinstance(result, Mapping):
+ return dict(result)
+ if hasattr(value, "__dict__"):
+ return {key: item for key, item in vars(value).items() if not key.startswith("_")}
+ raise ValueError("SDXL config must be a mapping or serialize to a JSON object")
+
+
+def _normalize_config_paths(value: Any) -> Any:
+ if isinstance(value, os.PathLike):
+ return os.fspath(value)
+ if isinstance(value, Mapping):
+ return {key: _normalize_config_paths(item) for key, item in value.items()}
+ if isinstance(value, list):
+ return [_normalize_config_paths(item) for item in value]
+ if isinstance(value, tuple):
+ return tuple(_normalize_config_paths(item) for item in value)
+ return value
+
+
+def is_sdxl_unet_config(config: Mapping[str, Any], class_name: str = "") -> bool:
+ """Return whether a Diffusers config matches the supported SDXL base 1.0 UNet fingerprint."""
+
+ resolved_class = str(config.get("_class_name", class_name)).lower()
+ if resolved_class != "unet2dconditionmodel":
+ return False
+ if any(config.get(name) != expected for name, expected in _SDXL_BASE_SCALAR_FIELDS.items()):
+ return False
+ return all(tuple(config.get(name, ())) == expected for name, expected in _SDXL_BASE_SEQUENCE_FIELDS.items())
+
+
+def _effective_weight(source: SourceLinearRecord, device: torch.device) -> torch.Tensor:
+ out_features, in_features = source.residual_weight.shape
+ rank = source.lora_down.shape[0]
+ if (
+ source.residual_weight.ndim != 2
+ or source.lora_down.shape != (rank, in_features)
+ or source.lora_up.shape != (out_features, rank)
+ or source.smooth.shape != (in_features,)
+ ):
+ raise ValueError(f"{source.name} source dimensions are inconsistent")
+ residual = source.residual_weight.detach().to(device=device, dtype=torch.float32)
+ up = source.lora_up.detach().to(device=device, dtype=torch.float32)
+ down = source.lora_down.detach().to(device=device, dtype=torch.float32)
+ smooth = source.smooth.detach().to(device=device, dtype=torch.float32)
+ return (residual + up @ down) * smooth.reshape(1, -1)
+
+
+def _has_shared_input_decomposition(sources: tuple[SourceLinearRecord, ...]) -> bool:
+ first = sources[0]
+ return all(
+ source.scheme == first.scheme
+ and torch.equal(source.lora_down, first.lora_down)
+ and torch.equal(source.smooth, first.smooth)
+ and torch.equal(source.smooth_orig, first.smooth_orig)
+ for source in sources[1:]
+ )
+
+
+def _fused_bias(prefix: str, sources: tuple[SourceLinearRecord, ...], device: torch.device | None = None):
+ biases = [source.bias for source in sources]
+ if any(bias is None for bias in biases) and not all(bias is None for bias in biases):
+ raise ValueError(f"{prefix} fused sources must either all have bias or all omit bias")
+ if biases[0] is None:
+ return None
+ values = biases if device is None else [bias.to(device) for bias in biases]
+ return torch.cat(values).detach().cpu().contiguous() if device is None else torch.cat(values)
+
+
+@dataclass
+class SDXLSVDQuantNunchakuAdapter:
+ """Map Diffusers SDXL UNet projections to the Nunchaku SDXL tensor schema."""
+
+ config: Mapping[str, Any] | None = None
+ decomposition_device: str | torch.device = "cpu"
+ require_complete_model: bool = True
+
+ def __post_init__(self) -> None:
+ try:
+ self.decomposition_device = torch.device(self.decomposition_device)
+ except (RuntimeError, TypeError) as exc:
+ raise ValueError(f"invalid SDXL decomposition_device {self.decomposition_device!r}") from exc
+ if self.decomposition_device.type not in ("cpu", "cuda"):
+ raise ValueError("SDXL decomposition_device must be CPU or CUDA")
+ if self.decomposition_device.type == "cuda":
+ if not torch.cuda.is_available():
+ raise ValueError("SDXL decomposition_device requests CUDA, but CUDA is not available")
+ device_count = torch.cuda.device_count()
+ index = self.decomposition_device.index
+ if index is not None and (index < 0 or index >= device_count):
+ raise ValueError(
+ f"SDXL decomposition_device index {index} is invalid for CUDA device_count={device_count}"
+ )
+ if self.config is not None:
+ self.config = _config_dict(self.config)
+ if not isinstance(self.require_complete_model, bool):
+ raise ValueError("require_complete_model must be a bool")
+
+ def _resolved_config(self, model: torch.nn.Module) -> dict[str, Any]:
+ value = self.config if self.config is not None else getattr(model, "config", None)
+ if value is None:
+ raise ValueError("SDXL export requires explicit config or model.config")
+ config = _normalize_config_paths(_config_dict(value))
+ if not is_sdxl_unet_config(config, type(model).__name__):
+ raise ValueError("SDXL adapter requires an SDXL UNet2DConditionModel config")
+ try:
+ json.dumps(config)
+ except (TypeError, ValueError) as exc:
+ raise ValueError("SDXL config must be JSON serializable") from exc
+ return config
+
+ def quant_block_list(self, model: torch.nn.Module) -> list[list[str]]:
+ self._resolved_config(model)
+ block_names = [
+ [name]
+ for name, module in model.named_modules()
+ if name and type(module).__name__ == "BasicTransformerBlock"
+ ]
+ if not block_names:
+ raise ValueError("SDXL adapter found no BasicTransformerBlock modules")
+ return block_names
+
+ def metadata(self, model: torch.nn.Module, rank: int) -> Mapping[str, str]:
+ return {
+ "model_class": "NunchakuSDXLUNet2DConditionModel",
+ "config": json.dumps(self._resolved_config(model), sort_keys=True),
+ "format": "pt",
+ "comfy_config": "{}",
+ }
+
+ @staticmethod
+ def _direct(source: SourceLinearRecord) -> SVDQuantExportRecord:
+ return SVDQuantExportRecord(
+ prefix=source.name,
+ residual_weight=source.residual_weight,
+ lora_down=source.lora_down,
+ lora_up=source.lora_up,
+ smooth=source.smooth,
+ smooth_orig=source.smooth_orig,
+ bias=source.bias,
+ scheme=source.scheme,
+ sources=(source,),
+ omitted_suffixes=frozenset({"bias"}) if source.bias is None else frozenset(),
+ )
+
+ def _fuse_qkv(self, prefix: str, sources: tuple[SourceLinearRecord, ...], rank: int) -> SVDQuantExportRecord:
+ if _has_shared_input_decomposition(sources):
+ first = sources[0]
+ return SVDQuantExportRecord(
+ prefix=prefix,
+ residual_weight=torch.cat([source.residual_weight for source in sources]).detach().cpu().contiguous(),
+ lora_down=first.lora_down.detach().cpu().contiguous(),
+ lora_up=torch.cat([source.lora_up for source in sources]).detach().cpu().contiguous(),
+ smooth=first.smooth.detach().cpu().contiguous(),
+ smooth_orig=first.smooth_orig.detach().cpu().contiguous(),
+ bias=_fused_bias(prefix, sources),
+ scheme=first.scheme,
+ sources=sources,
+ omitted_suffixes=frozenset({"bias"}) if first.bias is None else frozenset(),
+ )
+ try:
+ effective = [_effective_weight(source, self.decomposition_device) for source in sources]
+ input_dims = {weight.shape[1] for weight in effective}
+ if len(input_dims) != 1:
+ raise ValueError(f"{prefix} fused sources have incompatible input dimensions {sorted(input_dims)}")
+ weight = torch.cat(effective, dim=0)
+ if rank > min(weight.shape):
+ raise ValueError(f"{prefix} configured rank={rank} exceeds fused dimensions {tuple(weight.shape)}")
+ if not bool(torch.isfinite(weight).all()):
+ raise ValueError(f"{prefix} effective weight contains non-finite values")
+ u, singular_values, vh = torch.linalg.svd(weight, full_matrices=False)
+ up = u[:, :rank] * singular_values[:rank]
+ down = vh[:rank]
+ residual = weight - up @ down
+ template = sources[0]
+ return SVDQuantExportRecord(
+ prefix=prefix,
+ residual_weight=residual.to(dtype=template.residual_weight.dtype).cpu().contiguous(),
+ lora_down=down.to(dtype=template.lora_down.dtype).cpu().contiguous(),
+ lora_up=up.to(dtype=template.lora_up.dtype).cpu().contiguous(),
+ smooth=torch.ones(weight.shape[1], dtype=template.smooth.dtype).cpu(),
+ smooth_orig=torch.ones(weight.shape[1], dtype=template.smooth_orig.dtype).cpu(),
+ bias=(
+ _fused_bias(prefix, sources, self.decomposition_device).cpu().contiguous()
+ if sources[0].bias is not None
+ else None
+ ),
+ scheme=template.scheme,
+ sources=sources,
+ omitted_suffixes=frozenset({"bias"}) if template.bias is None else frozenset(),
+ )
+ finally:
+ if self.decomposition_device.type == "cuda":
+ torch.cuda.empty_cache()
+
+ @staticmethod
+ def _expected_blocks(model: torch.nn.Module) -> set[str]:
+ return {name for name, module in model.named_modules() if type(module).__name__ == "BasicTransformerBlock"}
+
+ def map_modules(
+ self, model: torch.nn.Module, records: Iterable[SourceLinearRecord]
+ ) -> Iterable[SVDQuantExportRecord]:
+ self._resolved_config(model)
+ by_block: dict[str, dict[str, SourceLinearRecord]] = {}
+ for record in records:
+ match = _BLOCK_RE.match(record.name)
+ if match is None:
+ raise ValueError(f"unrecognized SDXL SVDQuant source {record.name!r}")
+ block_prefix, local_name = match.groups()
+ local = by_block.setdefault(block_prefix, {})
+ if local_name in local:
+ raise ValueError(f"duplicate SDXL source {record.name!r}")
+ local[local_name] = record
+
+ required = set(SDXL_SVDQUANT_TARGET_MODULES)
+ if self.require_complete_model:
+ expected_blocks = self._expected_blocks(model)
+ if set(by_block) != expected_blocks:
+ raise ValueError(
+ f"complete SDXL block coverage mismatch: expected {sorted(expected_blocks)}, got {sorted(by_block)}"
+ )
+ output: list[SVDQuantExportRecord] = []
+ for block_prefix, local in sorted(by_block.items()):
+ names = set(local)
+ if self.require_complete_model and names != required:
+ raise ValueError(
+ f"complete SDXL {block_prefix} coverage: missing={sorted(required - names)}, "
+ f"extras={sorted(names - required)}"
+ )
+ present_qkv = tuple(local[name] for name in _QKV_SOURCES if name in local)
+ if present_qkv and len(present_qkv) != len(_QKV_SOURCES):
+ missing = [name for name in _QKV_SOURCES if name not in local]
+ raise ValueError(f"{block_prefix}.attn1.to_qkv missing fused sources {missing}")
+ if present_qkv:
+ rank = present_qkv[0].lora_down.shape[0]
+ output.append(self._fuse_qkv(f"{block_prefix}.attn1.to_qkv", present_qkv, rank))
+ output.extend(self._direct(local[name]) for name in _DIRECT_SOURCES if name in local)
+ return output
+
+ def validate_records(
+ self, sources: tuple[SourceLinearRecord, ...], records: tuple[SVDQuantExportRecord, ...]
+ ) -> None:
+ ranks = {source.lora_down.shape[0] for source in sources}
+ if len(ranks) != 1:
+ raise ValueError(f"SDXL source ranks must agree, got {sorted(ranks)}")
+ prefixes = [record.prefix for record in records]
+ if len(prefixes) != len(set(prefixes)):
+ raise ValueError("SDXL adapter produced duplicate logical record prefixes")
+
+ def extra_tensors(self, model: torch.nn.Module) -> Mapping[str, torch.Tensor]:
+ """Copy every runtime tensor not replaced by an SVDQW4A4Linear."""
+
+ from auto_round.algorithms.transforms.svdquant.wrapper import SVDQuantLinear
+
+ quantized_prefixes = tuple(
+ f"{name}." for name, module in model.named_modules() if name and isinstance(module, SVDQuantLinear)
+ )
+ tensors: dict[str, torch.Tensor] = {}
+ for name, tensor in model.state_dict().items():
+ if name.startswith(quantized_prefixes):
+ continue
+ runtime_name = name.replace(".orig_layer.", ".")
+ if runtime_name in tensors:
+ raise ValueError(
+ f"SDXL passthrough tensor name collision after unwrapping {name!r} to {runtime_name!r}"
+ )
+ value = tensor.detach()
+ if value.is_floating_point():
+ value = value.to(torch.bfloat16)
+ tensors[runtime_name] = value.cpu().contiguous()
+ self._passthrough_keys = frozenset(tensors)
+ return tensors
+
+ def validate(self, tensors: Mapping[str, torch.Tensor], metadata: Mapping[str, str]) -> None:
+ if metadata.get("model_class") != "NunchakuSDXLUNet2DConditionModel":
+ raise ValueError("SDXL metadata has incorrect model_class")
+ if metadata.get("format") != "pt" or metadata.get("comfy_config") != "{}":
+ raise ValueError("SDXL metadata requires format='pt' and empty comfy_config")
+ try:
+ config = json.loads(metadata["config"])
+ except (KeyError, json.JSONDecodeError) as exc:
+ raise ValueError("SDXL metadata config must be a JSON object") from exc
+ if not isinstance(config, dict) or not is_sdxl_unet_config(config):
+ raise ValueError("SDXL metadata config must describe an SDXL UNet")
+
+ passthrough_keys = getattr(self, "_passthrough_keys", frozenset())
+ missing_passthrough = passthrough_keys - tensors.keys()
+ if missing_passthrough:
+ raise ValueError(f"SDXL artifact is missing passthrough tensors: {sorted(missing_passthrough)[:5]}")
+ internal_markers = (".residual_linear.", ".lora_down.", ".lora_up.")
+ for key, tensor in tensors.items():
+ if any(marker in key for marker in internal_markers):
+ raise ValueError(f"SDXL artifact exposes wrapper-internal tensor {key!r}")
+ if tensor.device.type != "cpu" or not tensor.is_contiguous():
+ raise ValueError(f"SDXL tensor {key!r} must be contiguous on CPU")
+ if tensor.is_floating_point() and not bool(torch.isfinite(tensor).all()):
+ raise ValueError(f"SDXL tensor {key!r} must be finite")
+ if key in passthrough_keys:
+ continue
+ if key.endswith(".qweight"):
+ if tensor.dtype != torch.int8 or tensor.ndim != 2:
+ raise ValueError(f"SDXL qweight {key!r} must be 2D int8")
+ elif key.endswith(".wscales"):
+ if tensor.dtype != torch.uint8 or tensor.ndim != 2:
+ raise ValueError(f"SDXL wscales {key!r} must be 2D uint8")
+ elif key.endswith((".lora_down", ".lora_up")):
+ if tensor.dtype != torch.bfloat16 or tensor.ndim != 2:
+ raise ValueError(f"SDXL low-rank tensor {key!r} must be 2D BF16")
+ elif key.endswith((".smooth", ".smooth_orig", ".bias")):
+ if tensor.dtype != torch.bfloat16 or tensor.ndim != 1:
+ raise ValueError(f"SDXL vector tensor {key!r} must be 1D BF16")
+ else:
+ raise ValueError(f"SDXL artifact contains unknown packed tensor {key!r}")
+
+
+__all__ = ["SDXL_SVDQUANT_TARGET_MODULES", "SDXLSVDQuantNunchakuAdapter", "is_sdxl_unet_config"]
diff --git a/auto_round/export/svdquant_nunchaku.py b/auto_round/export/svdquant_nunchaku.py
index e493e9ad8..7dfa31376 100644
--- a/auto_round/export/svdquant_nunchaku.py
+++ b/auto_round/export/svdquant_nunchaku.py
@@ -85,6 +85,7 @@ class SVDQuantExportRecord:
scheme: SVDQuantLinearScheme
sources: tuple[SourceLinearRecord, ...]
key_mapping: Mapping[str, str] = field(default_factory=dict)
+ omitted_suffixes: frozenset[str] = field(default_factory=frozenset)
class SVDQuantModelAdapter(Protocol):
@@ -366,6 +367,9 @@ def _validate_export_record(record: SVDQuantExportRecord, config: SVDQuantExport
f"{record.prefix} export group_size={config.group_size} disagrees with "
f"selected group_size={selected_scheme[2]}"
)
+ unsupported_omissions = record.omitted_suffixes - {"bias"}
+ if unsupported_omissions:
+ raise ValueError(f"{record.prefix} cannot omit required tensor suffixes {sorted(unsupported_omissions)}")
return rank
@@ -478,6 +482,8 @@ def _serialize_export_records(
if config.debug_unpacked:
serialized["residual.weight"] = record.residual_weight
for suffix, tensor in serialized.items():
+ if suffix in record.omitted_suffixes:
+ continue
key = _export_key(record, suffix)
if key in tensors:
raise ValueError(f"model adapter produced duplicate tensor key {key!r}")
diff --git a/docs/svdquant_details.md b/docs/svdquant_details.md
index 6f709daaf..f1cb6da05 100644
--- a/docs/svdquant_details.md
+++ b/docs/svdquant_details.md
@@ -11,11 +11,12 @@ Linear(x) ~= QuantizedLinear(x, Q(R)) + Linear(Linear(x, V), U)
This feature follows the decomposition proposed in [SVDQuant: Absorbing
Outliers by Low-Rank Components for 4-Bit Diffusion
Models](https://arxiv.org/abs/2411.05007). AutoRound can combine the transform
-with RTN or SignRound and export a Nunchaku-loadable MXFP4 FLUX pipeline.
+with RTN or SignRound and export Nunchaku-loadable MXFP4 FLUX and SDXL
+pipelines.
-> The end-to-end export path has been validated with FLUX.1-dev only. Other
-> models using compatible Diffusers FLUX block classes are experimental and
-> produce a warning.
+> The architecture adapters are strict: FLUX is validated for FLUX.1-dev and
+> the SDXL adapter targets `stabilityai/stable-diffusion-xl-base-1.0`. Stable
+> Diffusion 1.x UNets are not handled by the SDXL adapter.
## Quick start
@@ -45,6 +46,37 @@ CUDA_VISIBLE_DEVICES=0 auto-round-rtn \
When `svdquant` is selected and `--format` is omitted, the CLI also defaults to
`svdquant_nunchaku`.
+### SDXL base 1.0 RTN workflow
+
+SDXL uses a UNet rather than a Diffusers transformer component. Auto detection
+selects the SDXL adapter from the UNet config; it can also be selected
+explicitly:
+
+```bash
+CUDA_VISIBLE_DEVICES=0 auto-round-rtn \
+ --model /path/to/stable-diffusion-xl-base-1.0 \
+ --model_dtype bf16 \
+ --scheme MXFP4 \
+ --algorithm svdquant \
+ --svdquant-model-adapter sdxl \
+ --format svdquant_nunchaku \
+ --device 0 \
+ --output_dir ./sdxl-base-1.0-mxfp4-svdquant-rtn
+```
+
+Only the projections replaced by Nunchaku are quantized. SDXL self-attention
+Q/K/V projections share one low-rank down factor and are exported as fused
+`to_qkv`. Cross-attention K/V, convolutions, normalization layers, and the
+remaining UNet state stay in BF16.
+
+Quality results for SDXL base 1.0:
+
+| Format | CLIP ↑ | CLIP-IQA ↑ | ImageReward ↑ |
+| --- | ---: | ---: | ---: |
+| BF16 | 26.8583 | 0.929027 | 0.812544 |
+| MXFP4 | 26.9006 | 0.895704 | 0.697131 |
+| MXFP4 (SVDQuant) | 26.8088 | 0.927907 | 0.7967 |
+
### Default SignRound workflow
Use SignRound as the terminal quantizer by selecting `auto_round`:
@@ -109,8 +141,9 @@ Original linear weights
-> Nunchaku export
```
-FLUX Q/K/V projections are grouped so they share one low-rank down factor and
-retain separate up factors, matching the runtime layout.
+FLUX and SDXL self-attention Q/K/V projections are grouped so they share one
+low-rank down factor and retain separate up factors, matching their runtime
+layouts.
## Residual iteration
@@ -233,9 +266,39 @@ image = pipe(
image.save("flux-svdquant-mxfp4.png")
```
+For SDXL, load the exported Nunchaku UNet and inject it into the saved
+Diffusers pipeline:
+
+```python
+import torch
+from diffusers import StableDiffusionXLPipeline
+from nunchaku.models.unets.unet_sdxl import NunchakuSDXLUNet2DConditionModel
+
+model_dir = "./sdxl-base-1.0-mxfp4-svdquant-rtn"
+unet = NunchakuSDXLUNet2DConditionModel.from_pretrained(
+ f"{model_dir}/unet/diffusion_pytorch_model.safetensors",
+ torch_dtype=torch.bfloat16,
+ device="cuda:0",
+)
+pipe = StableDiffusionXLPipeline.from_pretrained(
+ model_dir,
+ unet=unet,
+ torch_dtype=torch.bfloat16,
+ local_files_only=True,
+).to("cuda:0")
+
+image = pipe(
+ "A cinematic photograph of a red panda in a bamboo forest",
+ num_inference_steps=20,
+ guidance_scale=5.0,
+ generator=torch.Generator(device="cuda").manual_seed(12345),
+).images[0]
+image.save("sdxl-svdquant-mxfp4.png")
+```
+
## Limitations
-- Runtime-loadable export is currently validated only for FLUX.1-dev.
+- The SDXL adapter supports SDXL base 1.0, not Stable Diffusion 1.x UNets.
- SVDQuant requires `nblocks=1`.
- Smooth search replays every retained call for every Alpha/Beta candidate.
- More residual iterations repeat decomposition and QDQ work.
diff --git a/docs/svdquant_details_CN.md b/docs/svdquant_details_CN.md
index bce111140..ffd4c51e9 100644
--- a/docs/svdquant_details_CN.md
+++ b/docs/svdquant_details_CN.md
@@ -10,10 +10,11 @@ Linear(x) ~= QuantizedLinear(x, Q(R)) + Linear(Linear(x, V), U)
该功能采用 [SVDQuant: Absorbing Outliers by Low-Rank Components for 4-Bit
Diffusion Models](https://arxiv.org/abs/2411.05007) 提出的分解方法。
AutoRound 可以将该变换与 RTN 或 SignRound 组合,并导出 Nunchaku 可直接加载的
-MXFP4 FLUX pipeline。
+MXFP4 FLUX 和 SDXL pipeline。
-> 目前端到端导出流程仅在 FLUX.1-dev 上完成验证。其他使用兼容 Diffusers
-> FLUX block 类的模型仍属于实验性支持,运行时会输出警告。
+> 架构 adapter 使用严格匹配:FLUX 已在 FLUX.1-dev 上验证;SDXL adapter 面向
+> `stabilityai/stable-diffusion-xl-base-1.0`。Stable Diffusion 1.x UNet 不会使用
+> SDXL adapter。
## 快速开始
@@ -42,6 +43,35 @@ CUDA_VISIBLE_DEVICES=0 auto-round-rtn \
选择 `svdquant` 且未指定 `--format` 时,CLI 也会默认使用
`svdquant_nunchaku`。
+### SDXL base 1.0 RTN 流程
+
+SDXL 使用 UNet,而不是 Diffusers transformer component。AutoRound 可以根据 UNet
+config 自动选择 SDXL adapter,也可以显式指定:
+
+```bash
+CUDA_VISIBLE_DEVICES=0 auto-round-rtn \
+ --model /path/to/stable-diffusion-xl-base-1.0 \
+ --model_dtype bf16 \
+ --scheme MXFP4 \
+ --algorithm svdquant \
+ --svdquant-model-adapter sdxl \
+ --format svdquant_nunchaku \
+ --device 0 \
+ --output_dir ./sdxl-base-1.0-mxfp4-svdquant-rtn
+```
+
+只量化 Nunchaku runtime 会替换的 projection。SDXL self-attention Q/K/V 共享一个
+低秩 down factor,并融合导出为 `to_qkv`。Cross-attention K/V、卷积、normalization
+层及其余 UNet state 保持 BF16。
+
+SDXL base 1.0 的质量结果如下:
+
+| 格式 | CLIP ↑ | CLIP-IQA ↑ | ImageReward ↑ |
+| --- | ---: | ---: | ---: |
+| BF16 | 26.8583 | 0.929027 | 0.812544 |
+| MXFP4 | 26.9006 | 0.895704 | 0.697131 |
+| MXFP4(SVDQuant) | 26.8088 | 0.927907 | 0.7967 |
+
### 默认 SignRound 流程
选择 `auto_round` 即可使用 SignRound 作为最终量化算法:
@@ -105,8 +135,8 @@ SVDQuant 是结构变换预处理器,后面必须接一个最终量化算法
-> Nunchaku 导出
```
-FLUX 的 Q/K/V projection 会作为一组处理:共享一个低秩 down factor,并保留各自的
-up factor,以匹配运行时布局。
+FLUX 和 SDXL self-attention 的 Q/K/V projection 会作为一组处理:共享一个低秩
+down factor,并保留各自的 up factor,以匹配对应的运行时布局。
## Residual iteration
@@ -226,9 +256,38 @@ image = pipe(
image.save("flux-svdquant-mxfp4.png")
```
+对于 SDXL,需要加载导出的 Nunchaku UNet,再注入保存后的 Diffusers pipeline:
+
+```python
+import torch
+from diffusers import StableDiffusionXLPipeline
+from nunchaku.models.unets.unet_sdxl import NunchakuSDXLUNet2DConditionModel
+
+model_dir = "./sdxl-base-1.0-mxfp4-svdquant-rtn"
+unet = NunchakuSDXLUNet2DConditionModel.from_pretrained(
+ f"{model_dir}/unet/diffusion_pytorch_model.safetensors",
+ torch_dtype=torch.bfloat16,
+ device="cuda:0",
+)
+pipe = StableDiffusionXLPipeline.from_pretrained(
+ model_dir,
+ unet=unet,
+ torch_dtype=torch.bfloat16,
+ local_files_only=True,
+).to("cuda:0")
+
+image = pipe(
+ "A cinematic photograph of a red panda in a bamboo forest",
+ num_inference_steps=20,
+ guidance_scale=5.0,
+ generator=torch.Generator(device="cuda").manual_seed(12345),
+).images[0]
+image.save("sdxl-svdquant-mxfp4.png")
+```
+
## 当前限制
-- Runtime-loadable export 目前只在 FLUX.1-dev 上完成验证。
+- SDXL adapter 支持 SDXL base 1.0,不支持 Stable Diffusion 1.x UNet。
- SVDQuant 要求 `nblocks=1`。
- Smooth 搜索会针对每组 Alpha/Beta 候选重放所有保留 calls。
- 增加 residual iterations 会重复执行分解和 QDQ。
diff --git a/test/e2e/test_cuda/test_sdxl_svdquant_nunchaku.py b/test/e2e/test_cuda/test_sdxl_svdquant_nunchaku.py
new file mode 100644
index 000000000..0d5aa2bfd
--- /dev/null
+++ b/test/e2e/test_cuda/test_sdxl_svdquant_nunchaku.py
@@ -0,0 +1,125 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""End-to-end SDXL Base 1.0 SVDQuant MXFP4 export and Nunchaku inference test."""
+
+from __future__ import annotations
+
+import json
+
+import numpy as np
+import pytest
+import torch
+from safetensors import safe_open
+
+pytestmark = [
+ pytest.mark.e2e,
+ pytest.mark.skipif(not torch.cuda.is_available(), reason="SDXL Nunchaku inference requires CUDA"),
+]
+
+
+def test_sdxl_base_svdquant_mxfp4_loads_and_generates(tmp_path):
+ """Quantize the real SDXL Base 1.0 UNet, load it with Nunchaku, and generate one image."""
+ pytest.importorskip("diffusers")
+ pytest.importorskip("nunchaku")
+ from test.helpers import get_model_path
+
+ from diffusers import StableDiffusionXLPipeline
+ from nunchaku.models.unets.unet_sdxl import NunchakuSDXLUNet2DConditionModel
+
+ from auto_round import AutoRound
+ from auto_round.algorithms.quantization.rtn.config import RTNConfig
+ from auto_round.algorithms.transforms.svdquant.config import SVDQuantConfig
+
+ model_path = get_model_path("stabilityai/stable-diffusion-xl-base-1.0")
+ output_dir = tmp_path / "sdxl-base-svdquant-mxfp4"
+ prompt = "A cinematic photograph of a red panda in a bamboo forest"
+ seed = 12345
+ num_inference_steps = 20
+ guidance_scale = 5.0
+ pipe = StableDiffusionXLPipeline.from_pretrained(
+ model_path,
+ torch_dtype=torch.bfloat16,
+ use_safetensors=True,
+ variant="fp16",
+ ).to("cuda:0")
+ reference_image = pipe(
+ prompt,
+ num_inference_steps=num_inference_steps,
+ guidance_scale=guidance_scale,
+ generator=torch.Generator(device="cuda:0").manual_seed(seed),
+ height=512,
+ width=512,
+ ).images[0]
+ reference_pixels = np.asarray(reference_image, dtype=np.float32) / 255.0
+ compressor = AutoRound(
+ model=pipe,
+ tokenizer=None,
+ scheme="MXFP4",
+ alg_configs=[
+ SVDQuantConfig(rank=32, smooth_enabled=False, model_adapter="sdxl"),
+ RTNConfig(disable_opt_rtn=True),
+ ],
+ device_map=0,
+ low_cpu_mem_usage=True,
+ num_inference_steps=1,
+ )
+ compressor.quantize_and_save(output_dir, format="svdquant_nunchaku")
+ del compressor, pipe
+
+ weight_path = output_dir / "unet" / "diffusion_pytorch_model.safetensors"
+ assert weight_path.is_file()
+ with safe_open(weight_path, framework="pt", device="cpu") as handle:
+ metadata = handle.metadata()
+ quantization_config = json.loads(metadata["quantization_config"])
+ assert metadata["model_class"] == "NunchakuSDXLUNet2DConditionModel"
+ assert quantization_config["rank"] == 32
+ assert quantization_config["weight"]["dtype"] == "fp4_e2m1_all"
+ assert quantization_config["weight"]["group_size"] == 32
+ assert quantization_config["activation"]["dtype"] == "fp4_e2m1_all"
+ assert quantization_config["activation"]["group_size"] == 32
+
+ unet = NunchakuSDXLUNet2DConditionModel.from_pretrained(
+ weight_path,
+ torch_dtype=torch.bfloat16,
+ device="cuda:0",
+ )
+ quantized_pipe = StableDiffusionXLPipeline.from_pretrained(
+ output_dir,
+ unet=unet,
+ torch_dtype=torch.bfloat16,
+ local_files_only=True,
+ ).to("cuda:0")
+ try:
+ image = quantized_pipe(
+ prompt,
+ num_inference_steps=num_inference_steps,
+ guidance_scale=guidance_scale,
+ generator=torch.Generator(device="cuda:0").manual_seed(seed),
+ height=512,
+ width=512,
+ ).images[0]
+ pixels = np.asarray(image, dtype=np.float32) / 255.0
+ assert pixels.shape == (512, 512, 3)
+ assert np.isfinite(pixels).all()
+ mean_absolute_error = np.abs(reference_pixels - pixels).mean()
+ reference_centered = reference_pixels - reference_pixels.mean()
+ pixels_centered = pixels - pixels.mean()
+ centered_cosine = np.sum(reference_centered * pixels_centered) / np.sqrt(
+ np.sum(reference_centered**2) * np.sum(pixels_centered**2)
+ )
+ assert mean_absolute_error < 0.15
+ assert centered_cosine > 0.70
+ finally:
+ del quantized_pipe, unet
+ torch.cuda.empty_cache()
diff --git a/test/unit/common/algorithms/test_svdquant.py b/test/unit/common/algorithms/test_svdquant.py
index e3a42e9c2..876bb36a5 100644
--- a/test/unit/common/algorithms/test_svdquant.py
+++ b/test/unit/common/algorithms/test_svdquant.py
@@ -49,6 +49,17 @@ class FluxTransformerBlock(torch.nn.Module):
self.norm1.linear = torch.nn.Linear(32, 16)
+class BasicTransformerBlock(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.attn1 = torch.nn.Module()
+ self.attn1.to_q = torch.nn.Linear(32, 16)
+ self.attn1.to_k = torch.nn.Linear(32, 16)
+ self.attn1.to_v = torch.nn.Linear(32, 16)
+ self.attn2 = torch.nn.Module()
+ self.attn2.to_k = torch.nn.Linear(32, 16)
+
+
def _prepare_model():
model = DummyModel()
for name, module in model.named_modules():
@@ -185,3 +196,65 @@ def test_flux_targets_resolve_when_model_is_attached_after_prepare_run():
assert isinstance(model.blocks[0].attn.to_q, SVDQuantLinear)
assert isinstance(model.blocks[0].norm1.linear, torch.nn.Linear)
+
+
+def test_sdxl_targets_resolve_when_model_is_attached_after_prepare_run():
+ model = torch.nn.Module()
+ model.config = {
+ "_class_name": "UNet2DConditionModel",
+ "addition_embed_type": "text_time",
+ "attention_head_dim": [5, 10, 20],
+ "block_out_channels": [320, 640, 1280],
+ "cross_attention_dim": 2048,
+ "down_block_types": ["DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D"],
+ "in_channels": 4,
+ "layers_per_block": 2,
+ "out_channels": 4,
+ "projection_class_embeddings_input_dim": 2816,
+ "sample_size": 128,
+ "transformer_layers_per_block": [1, 2, 10],
+ "up_block_types": ["CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D"],
+ "use_linear_projection": True,
+ }
+ model.blocks = torch.nn.ModuleList([BasicTransformerBlock()])
+ for name, module in model.named_modules():
+ module.global_name = name
+ if isinstance(module, torch.nn.Linear):
+ for key, value in {
+ "bits": 4,
+ "group_size": 32,
+ "sym": True,
+ "data_type": "mx_fp4e2m1",
+ "act_bits": 4,
+ "act_group_size": 32,
+ "act_sym": True,
+ "act_data_type": "mx_fp4e2m1",
+ "act_dynamic": True,
+ }.items():
+ setattr(module, key, value)
+
+ model_context = SimpleNamespace(model=None)
+ transform = AlgorithmComposer([SVDQuantConfig(rank=2), RTNConfig(disable_opt_rtn=True)]).preprocessors[0]
+ transform.bind(
+ SimpleNamespace(
+ model_context=model_context,
+ compress_context=None,
+ calibration_context=None,
+ scheme_context=PRESET_SCHEMES["MXFP4"],
+ scale_dtype=None,
+ nblocks=1,
+ quant_block_list=[["blocks.0"]],
+ )
+ )
+ transform.prepare_run()
+ model_context.model = model
+ ctx = BlockContext(model=model, block_names=["blocks.0"], block_name="blocks.0", block_index=0)
+
+ transform.pre_quantize_block(ctx)
+
+ assert isinstance(model.blocks[0].attn1.to_q, SVDQuantLinear)
+ assert isinstance(model.blocks[0].attn1.to_k, SVDQuantLinear)
+ assert isinstance(model.blocks[0].attn1.to_v, SVDQuantLinear)
+ torch.testing.assert_close(model.blocks[0].attn1.to_q.lora_down.weight, model.blocks[0].attn1.to_k.lora_down.weight)
+ torch.testing.assert_close(model.blocks[0].attn1.to_q.lora_down.weight, model.blocks[0].attn1.to_v.lora_down.weight)
+ assert isinstance(model.blocks[0].attn2.to_k, torch.nn.Linear)
diff --git a/test/unit/common/algorithms/test_svdquant_smooth_adapters.py b/test/unit/common/algorithms/test_svdquant_smooth_adapters.py
index 6e798a274..35abb115b 100644
--- a/test/unit/common/algorithms/test_svdquant_smooth_adapters.py
+++ b/test/unit/common/algorithms/test_svdquant_smooth_adapters.py
@@ -52,6 +52,14 @@ class FluxTransformerBlock(torch.nn.Module):
self.ff_context = FeedForward(width)
+class BasicTransformerBlock(torch.nn.Module):
+ def __init__(self, width=4):
+ super().__init__()
+ self.attn1 = Attention(width)
+ self.attn2 = Attention(width)
+ self.ff = FeedForward(width)
+
+
def test_flux_adapter_discovers_fused_projection_groups_without_duplicates():
block = FluxTransformerBlock()
block.global_name = "transformer_blocks.7"
@@ -72,3 +80,29 @@ def test_flux_adapter_discovers_fused_projection_groups_without_duplicates():
assert len(by_key["transformer_blocks.7.attn.qkv"].projections) == 3
assert len(by_key["transformer_blocks.7.attn.add_qkv"].projections) == 3
assert len({id(projection) for group in groups for projection in group.projections}) == 12
+
+
+def test_sdxl_adapter_groups_self_attention_qkv_and_keeps_other_targets_direct():
+ block = BasicTransformerBlock()
+ block.global_name = "down_blocks.1.attentions.0.transformer_blocks.0"
+ targets = {
+ "attn1.to_q",
+ "attn1.to_k",
+ "attn1.to_v",
+ "attn1.to_out.0",
+ "attn2.to_q",
+ "attn2.to_out.0",
+ "ff.net.0.proj",
+ "ff.net.2",
+ }
+
+ groups = discover_svdquant_groups(
+ block, lambda name, module: isinstance(module, torch.nn.Linear) and name in targets
+ )
+ by_key = {group.key: group for group in groups}
+
+ qkv_key = "down_blocks.1.attentions.0.transformer_blocks.0.attn1.qkv"
+ assert qkv_key in by_key
+ assert len(by_key[qkv_key].projections) == 3
+ assert len(groups) == 6
+ assert len({id(projection) for group in groups for projection in group.projections}) == 8
diff --git a/test/unit/common/export/test_svdquant_nunchaku_format.py b/test/unit/common/export/test_svdquant_nunchaku_format.py
index f84663744..b66aec6d4 100644
--- a/test/unit/common/export/test_svdquant_nunchaku_format.py
+++ b/test/unit/common/export/test_svdquant_nunchaku_format.py
@@ -32,6 +32,83 @@ def _mxfp4_compressor(**updates):
return SimpleNamespace(**values)
+def _sdxl_base_config():
+ return {
+ "_class_name": "UNet2DConditionModel",
+ "addition_embed_type": "text_time",
+ "attention_head_dim": [5, 10, 20],
+ "block_out_channels": [320, 640, 1280],
+ "cross_attention_dim": 2048,
+ "down_block_types": ["DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D"],
+ "in_channels": 4,
+ "layers_per_block": 2,
+ "out_channels": 4,
+ "projection_class_embeddings_input_dim": 2816,
+ "sample_size": 128,
+ "transformer_layers_per_block": [1, 2, 10],
+ "up_block_types": ["CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D"],
+ "use_linear_projection": True,
+ }
+
+
+def test_format_resolves_nested_sdxl_quant_blocks():
+ class BasicTransformerBlock(torch.nn.Module):
+ pass
+
+ class SDXLModel(torch.nn.Module):
+ config = _sdxl_base_config()
+
+ def __init__(self):
+ super().__init__()
+ attention = torch.nn.Module()
+ attention.transformer_blocks = torch.nn.ModuleList([BasicTransformerBlock(), BasicTransformerBlock()])
+ down_block = torch.nn.Module()
+ down_block.attentions = torch.nn.ModuleList([attention])
+ self.down_blocks = torch.nn.ModuleList([down_block])
+
+ scheme = PRESET_SCHEMES["MXFP4"].copy()
+ output_format = get_formats("svdquant_nunchaku", _mxfp4_compressor())[0]
+ ctx = SimpleNamespace(model=SDXLModel(), layer_config=None, quant_block_list=None)
+
+ _, _, _, quant_block_list = output_format.check_and_reset_format(scheme, ctx)
+
+ assert quant_block_list == [
+ ["down_blocks.0.attentions.0.transformer_blocks.0"],
+ ["down_blocks.0.attentions.0.transformer_blocks.1"],
+ ]
+
+
[email protected]("quant_block_list", [[["custom.block"]], []], ids=["custom", "empty"])
+def test_format_preserves_explicit_quant_blocks_without_adapter_resolution(monkeypatch, quant_block_list):
+ import auto_round.export.svdquant_adapters as svdquant_adapters
+
+ monkeypatch.setattr(
+ svdquant_adapters,
+ "resolve_svdquant_model_adapter",
+ lambda *args, **kwargs: pytest.fail("explicit quant blocks must skip adapter resolution"),
+ )
+ scheme = PRESET_SCHEMES["MXFP4"].copy()
+ output_format = get_formats("svdquant_nunchaku", _mxfp4_compressor())[0]
+ ctx = SimpleNamespace(model=torch.nn.Module(), layer_config=None, quant_block_list=quant_block_list)
+
+ _, _, _, resolved_quant_block_list = output_format.check_and_reset_format(scheme, ctx)
+
+ assert resolved_quant_block_list is quant_block_list
+
+
+def test_format_leaves_flux_quant_blocks_unresolved():
+ class FluxModel(torch.nn.Module):
+ config = {"_class_name": "FluxTransformer2DModel", "num_layers": 0, "num_single_layers": 0}
+
+ scheme = PRESET_SCHEMES["MXFP4"].copy()
+ output_format = get_formats("svdquant_nunchaku", _mxfp4_compressor())[0]
+ ctx = SimpleNamespace(model=FluxModel(), layer_config=None, quant_block_list=None)
+
+ _, _, _, quant_block_list = output_format.check_and_reset_format(scheme, ctx)
+
+ assert quant_block_list is None
+
+
def test_svdquant_nunchaku_rejects_incompatible_scheme():
scheme = PRESET_SCHEMES["MXFP4"].copy()
scheme.group_size = 64
@@ -120,6 +197,48 @@ def test_format_uses_flux_adapter_and_diffusers_weight_name(monkeypatch, tmp_pat
assert (tmp_path / "config.json").is_file()
+def test_format_auto_resolves_sdxl_adapter(monkeypatch, tmp_path):
+ import auto_round.export.svdquant_nunchaku as exporter
+ from auto_round.export.svdquant_adapters.sdxl import SDXLSVDQuantNunchakuAdapter
+
+ output_format = get_formats("svdquant_nunchaku", _mxfp4_compressor())[0]
+
+ class SDXLModel(torch.nn.Module):
+ config = {
+ "_class_name": "UNet2DConditionModel",
+ "addition_embed_type": "text_time",
+ "attention_head_dim": [5, 10, 20],
+ "block_out_channels": [320, 640, 1280],
+ "cross_attention_dim": 2048,
+ "down_block_types": ["DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D"],
+ "in_channels": 4,
+ "layers_per_block": 2,
+ "out_channels": 4,
+ "projection_class_embeddings_input_dim": 2816,
+ "sample_size": 128,
+ "transformer_layers_per_block": [1, 2, 10],
+ "up_block_types": ["CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D"],
+ "use_linear_projection": True,
+ }
+
+ def save_config(self, output_dir):
+ Path(output_dir).mkdir(parents=True, exist_ok=True)
+ Path(output_dir, "config.json").write_text(json.dumps(self.config), encoding="utf-8")
+
+ model = SDXLModel()
+ captured = {}
+
+ def fake_export(export_model, output_path, *, config, residual_provider, adapter):
+ captured.update(output_path=output_path, adapter=adapter)
+
+ monkeypatch.setattr(exporter, "save_svdquant_nunchaku_safetensors", fake_export)
+
+ output_format.save_quantized(tmp_path, model=model, model_adapter="auto", device="cpu")
+
+ assert captured["output_path"] == str(tmp_path / "diffusion_pytorch_model.safetensors")
+ assert isinstance(captured["adapter"], SDXLSVDQuantNunchakuAdapter)
+
+
def test_diffusion_save_exports_self_contained_nunchaku_pipeline(tmp_path):
from auto_round.compressors.diffusion_mixin import DiffusionMixin
@@ -182,3 +301,67 @@ def test_diffusion_save_exports_self_contained_nunchaku_pipeline(tmp_path):
model_index = json.loads((tmp_path / "model_index.json").read_text(encoding="utf-8"))
assert model_index["transformer"] == ["diffusers", "FluxTransformer2DModel"]
assert model_index["vae"] == ["diffusers", "AutoencoderKL"]
+
+
+def test_diffusion_save_exports_sdxl_nunchaku_unet_component(tmp_path):
+ from auto_round.compressors.diffusion_mixin import DiffusionMixin
+
+ class QuantizedUNet(torch.nn.Module):
+ def save_config(self, output_dir):
+ output_dir = Path(output_dir)
+ output_dir.mkdir(parents=True, exist_ok=True)
+ (output_dir / "config.json").write_text('{"_class_name": "UNet2DConditionModel"}', encoding="utf-8")
+
+ class Bf16Component:
+ def save_pretrained(self, output_dir):
+ output_dir = Path(output_dir)
+ output_dir.mkdir(parents=True, exist_ok=True)
+ (output_dir / "config.json").write_text("{}", encoding="utf-8")
+ (output_dir / "diffusion_pytorch_model.safetensors").touch()
+
+ unet = QuantizedUNet()
+ vae = Bf16Component()
+
+ class Pipeline:
+ def __init__(self):
+ self.unet = unet
+ self.vae = vae
+ self.components = {"unet": unet, "vae": vae}
+
+ def save_config(self, output_dir):
+ Path(output_dir, "model_index.json").write_text(
+ json.dumps(
+ {
+ "_class_name": "StableDiffusionXLPipeline",
+ "unet": ["diffusers", "UNet2DConditionModel"],
+ "vae": ["diffusers", "AutoencoderKL"],
+ }
+ ),
+ encoding="utf-8",
+ )
+
+ class ExportParent:
+ def save_quantized(self, output_dir, **kwargs):
+ Path(output_dir).mkdir(parents=True, exist_ok=True)
+ save_file(
+ {"probe": torch.zeros(1)},
+ f"{output_dir}/diffusion_pytorch_model.safetensors",
+ metadata={"model_class": "NunchakuSDXLUNet2DConditionModel"},
+ )
+ return self.model_context.model
+
+ class Compressor(DiffusionMixin, ExportParent):
+ pass
+
+ compressor = Compressor.__new__(Compressor)
+ compressor.formats = [SimpleNamespace(format_name="svdquant_nunchaku")]
+ compressor.model_context = SimpleNamespace(pipe=Pipeline(), model=unet)
+ compressor.compress_context = SimpleNamespace(is_immediate_saving=False)
+
+ compressor.save_quantized(tmp_path)
+
+ assert (tmp_path / "unet" / "diffusion_pytorch_model.safetensors").is_file()
+ assert (tmp_path / "vae" / "diffusion_pytorch_model.safetensors").is_file()
+ model_index = json.loads((tmp_path / "model_index.json").read_text(encoding="utf-8"))
+ assert model_index["unet"] == ["diffusers", "UNet2DConditionModel"]
+ assert model_index["vae"] == ["diffusers", "AutoencoderKL"]
diff --git a/test/unit/common/export/test_svdquant_sdxl_adapter.py b/test/unit/common/export/test_svdquant_sdxl_adapter.py
new file mode 100644
index 000000000..f968079c9
--- /dev/null
+++ b/test/unit/common/export/test_svdquant_sdxl_adapter.py
@@ -0,0 +1,299 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+
+import pytest
+import torch
+
+from auto_round.export.svdquant_adapters import (
+ SDXL_SVDQUANT_TARGET_MODULES,
+ detect_svdquant_model_adapter,
+)
+from auto_round.export.svdquant_adapters.sdxl import SDXLSVDQuantNunchakuAdapter
+from auto_round.export.svdquant_nunchaku import (
+ SourceLinearRecord,
+ SVDQuantExportConfig,
+ SVDQuantLinearScheme,
+ collect_svdquant_tensors,
+ save_svdquant_nunchaku_safetensors,
+)
+
+SCHEME = SVDQuantLinearScheme("mx_fp4", 4, 32, True, "mx_fp4", 4, 32, True, True)
+
+
+class ConfiguredModel(torch.nn.Module):
+ def __init__(self, config):
+ super().__init__()
+ self.config = config
+
+
+class BasicTransformerBlock(torch.nn.Module):
+ pass
+
+
+class RuntimeLinearWrapper(torch.nn.Module):
+ def __init__(self, orig_layer):
+ super().__init__()
+ self.orig_layer = orig_layer
+
+
+def _install(root, path, module):
+ current = root
+ parts = path.split(".")
+ for part in parts[:-1]:
+ if not hasattr(current, part):
+ current.add_module(part, torch.nn.Module())
+ current = getattr(current, part)
+ current.add_module(parts[-1], module)
+
+
+def _sdxl_config():
+ return {
+ "_class_name": "UNet2DConditionModel",
+ "addition_embed_type": "text_time",
+ "attention_head_dim": [5, 10, 20],
+ "block_out_channels": [320, 640, 1280],
+ "cross_attention_dim": 2048,
+ "down_block_types": ["DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D"],
+ "in_channels": 4,
+ "layers_per_block": 2,
+ "out_channels": 4,
+ "projection_class_embeddings_input_dim": 2816,
+ "sample_size": 128,
+ "transformer_layers_per_block": [1, 2, 10],
+ "up_block_types": ["CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D"],
+ "use_linear_projection": True,
+ }
+
+
+def _source(name, out_features=8, in_features=8, rank=2, seed=0, bias=True):
+ generator = torch.Generator().manual_seed(seed)
+ return SourceLinearRecord(
+ name=name,
+ residual_weight=torch.randn(out_features, in_features, generator=generator),
+ lora_down=torch.randn(rank, in_features, generator=generator),
+ lora_up=torch.randn(out_features, rank, generator=generator),
+ smooth=torch.linspace(0.5, 1.5, in_features),
+ smooth_orig=torch.linspace(0.75, 1.75, in_features),
+ bias=torch.randn(out_features, generator=generator) if bias else None,
+ scheme=SCHEME,
+ )
+
+
+def _effective(source):
+ return (source.residual_weight + source.lora_up @ source.lora_down) * source.smooth
+
+
+def _wrapped_linear(in_features=32, out_features=8, rank=2, bias=True):
+ from auto_round.algorithms.transforms.svdquant.wrapper import SVDQuantLinear
+
+ residual = torch.nn.Linear(in_features, out_features, bias=bias)
+ residual.data_type, residual.bits, residual.group_size, residual.sym = "mx_fp4", 4, 32, True
+ residual.act_data_type, residual.act_bits, residual.act_group_size = "mx_fp4", 4, 32
+ residual.act_sym, residual.act_dynamic = True, True
+ return SVDQuantLinear(
+ residual,
+ torch.nn.Linear(in_features, rank, bias=False),
+ torch.nn.Linear(rank, out_features, bias=False),
+ torch.linspace(0.5, 1.5, in_features),
+ )
+
+
+def test_detects_sdxl_unet_from_runtime_relevant_config():
+ model = ConfiguredModel(_sdxl_config())
+
+ assert detect_svdquant_model_adapter(model) == "sdxl"
+
+
+def test_does_not_detect_custom_sdxl_like_unet_as_base_1_0():
+ config = _sdxl_config()
+ config["transformer_layers_per_block"] = [1, 2, 4]
+
+ assert detect_svdquant_model_adapter(ConfiguredModel(config)) == "identity"
+
+
+def test_does_not_treat_stable_diffusion_v1_unet_as_sdxl():
+ model = ConfiguredModel(
+ {
+ "_class_name": "UNet2DConditionModel",
+ "cross_attention_dim": 768,
+ }
+ )
+
+ assert detect_svdquant_model_adapter(model) == "identity"
+
+
+def test_sdxl_quant_block_list_returns_nested_basic_transformer_blocks_in_named_module_order():
+ model = ConfiguredModel(_sdxl_config())
+ first = "down_blocks.1.attentions.0.transformer_blocks.0"
+ second = "mid_block.attentions.0.transformer_blocks.0"
+ _install(model, first, BasicTransformerBlock())
+ _install(model, second, BasicTransformerBlock())
+
+ assert SDXLSVDQuantNunchakuAdapter().quant_block_list(model) == [[first], [second]]
+
+
+def test_sdxl_quant_block_list_rejects_unsupported_config():
+ config = _sdxl_config()
+ config["sample_size"] = 64
+ model = ConfiguredModel(config)
+ _install(model, "mid_block.attentions.0.transformer_blocks.0", BasicTransformerBlock())
+
+ with pytest.raises(ValueError, match="requires an SDXL UNet2DConditionModel config"):
+ SDXLSVDQuantNunchakuAdapter().quant_block_list(model)
+
+
+def test_sdxl_quant_block_list_rejects_supported_model_without_basic_transformer_blocks():
+ model = ConfiguredModel(_sdxl_config())
+
+ with pytest.raises(ValueError, match="no BasicTransformerBlock"):
+ SDXLSVDQuantNunchakuAdapter().quant_block_list(model)
+
+
+def test_sdxl_target_allowlist_matches_nunchaku_patched_linears():
+ assert set(SDXL_SVDQUANT_TARGET_MODULES) == {
+ "attn1.to_q",
+ "attn1.to_k",
+ "attn1.to_v",
+ "attn1.to_out.0",
+ "attn2.to_q",
+ "attn2.to_out.0",
+ "ff.net.0.proj",
+ "ff.net.2",
+ }
+
+
+def test_sdxl_maps_direct_linears_and_fuses_self_attention_qkv():
+ prefix = "down_blocks.1.attentions.0.transformer_blocks.0"
+ qkv = tuple(_source(f"{prefix}.attn1.to_{name}", seed=index + 1) for index, name in enumerate("qkv"))
+ direct = _source(f"{prefix}.attn2.to_q", seed=9)
+ adapter = SDXLSVDQuantNunchakuAdapter(require_complete_model=False)
+
+ records = tuple(adapter.map_modules(ConfiguredModel(_sdxl_config()), (*qkv, direct)))
+
+ assert [record.prefix for record in records] == [f"{prefix}.attn1.to_qkv", f"{prefix}.attn2.to_q"]
+ fused = records[0]
+ assert fused.sources == qkv
+ assert fused.lora_down.shape[0] == 2
+ expected = torch.cat([_effective(source) for source in qkv])
+ actual = (fused.residual_weight + fused.lora_up @ fused.lora_down) * fused.smooth
+ torch.testing.assert_close(actual, expected, atol=2e-5, rtol=2e-5)
+ torch.testing.assert_close(fused.bias, torch.cat([source.bias for source in qkv]))
+ assert records[1].sources == (direct,)
+
+
+def test_sdxl_preserves_shared_qkv_decomposition_without_recomposing():
+ prefix = "mid_block.attentions.0.transformer_blocks.0"
+ shared_down = torch.randn(2, 8)
+ shared_smooth = torch.linspace(0.5, 1.5, 8)
+ shared_smooth_orig = torch.linspace(0.75, 1.75, 8)
+ sources = tuple(_source(f"{prefix}.attn1.to_{name}", seed=index + 11) for index, name in enumerate("qkv"))
+ sources = tuple(
+ SourceLinearRecord(
+ name=source.name,
+ residual_weight=source.residual_weight,
+ lora_down=shared_down.clone(),
+ lora_up=source.lora_up,
+ smooth=shared_smooth.clone(),
+ smooth_orig=shared_smooth_orig.clone(),
+ bias=source.bias,
+ scheme=source.scheme,
+ )
+ for source in sources
+ )
+
+ (record,) = tuple(
+ SDXLSVDQuantNunchakuAdapter(require_complete_model=False).map_modules(ConfiguredModel(_sdxl_config()), sources)
+ )
+
+ torch.testing.assert_close(record.residual_weight, torch.cat([source.residual_weight for source in sources]))
+ torch.testing.assert_close(record.lora_down, shared_down)
+ torch.testing.assert_close(record.lora_up, torch.cat([source.lora_up for source in sources]))
+ torch.testing.assert_close(record.smooth, shared_smooth)
+ torch.testing.assert_close(record.smooth_orig, shared_smooth_orig)
+
+
+def test_sdxl_metadata_names_runtime_model_and_serializes_config():
+ model = ConfiguredModel(_sdxl_config())
+
+ metadata = SDXLSVDQuantNunchakuAdapter(require_complete_model=False).metadata(model, rank=32)
+
+ assert metadata["model_class"] == "NunchakuSDXLUNet2DConditionModel"
+ assert metadata["format"] == "pt"
+ assert metadata["comfy_config"] == "{}"
+ assert json.loads(metadata["config"]) == _sdxl_config()
+
+
+def test_sdxl_biasless_runtime_records_omit_zero_bias_tensor():
+ prefix = "mid_block.attentions.0.transformer_blocks.0"
+ source = _source(f"{prefix}.attn2.to_q", bias=False)
+ adapter = SDXLSVDQuantNunchakuAdapter(require_complete_model=False)
+
+ (record,) = tuple(adapter.map_modules(ConfiguredModel(_sdxl_config()), (source,)))
+
+ assert record.omitted_suffixes == frozenset({"bias"})
+
+
+def test_sdxl_biasless_runtime_serialization_has_no_unexpected_bias_key():
+ prefix = "mid_block.attentions.0.transformer_blocks.0"
+ model = ConfiguredModel(_sdxl_config())
+ _install(model, f"{prefix}.attn2.to_q", _wrapped_linear(bias=False))
+
+ tensors = collect_svdquant_tensors(
+ model,
+ adapter=SDXLSVDQuantNunchakuAdapter(require_complete_model=False),
+ config=SVDQuantExportConfig(runtime_loadable=True),
+ )
+
+ assert f"{prefix}.attn2.to_q.qweight" in tensors
+ assert f"{prefix}.attn2.to_q.bias" not in tensors
+
+
+def test_complete_sdxl_mapping_rejects_missing_runtime_projection():
+ prefix = "down_blocks.1.attentions.0.transformer_blocks.0"
+ sources = tuple(_source(f"{prefix}.attn1.to_{name}", seed=index + 1) for index, name in enumerate("qkv"))
+ model = ConfiguredModel(_sdxl_config())
+ _install(model, prefix, BasicTransformerBlock())
+
+ with pytest.raises(ValueError, match="coverage.*missing"):
+ tuple(SDXLSVDQuantNunchakuAdapter(require_complete_model=True).map_modules(model, sources))
+
+
+def test_sdxl_export_preserves_float_unet_state_without_wrapper_internal_keys(tmp_path):
+ from safetensors import safe_open
+
+ prefix = "down_blocks.1.attentions.0.transformer_blocks.0"
+ model = ConfiguredModel(_sdxl_config())
+ _install(model, f"{prefix}.attn2.to_q", _wrapped_linear())
+ _install(model, f"{prefix}.attn2.to_k", RuntimeLinearWrapper(torch.nn.Linear(32, 8)))
+ _install(model, "conv_in", torch.nn.Conv2d(4, 8, kernel_size=3, padding=1))
+ adapter = SDXLSVDQuantNunchakuAdapter(require_complete_model=False)
+ config = SVDQuantExportConfig(runtime_loadable=True)
+
+ tensors = collect_svdquant_tensors(model, adapter=adapter, config=config)
+ path = tmp_path / "sdxl.safetensors"
+ save_svdquant_nunchaku_safetensors(model, str(path), adapter=adapter, config=config)
+
+ assert f"{prefix}.attn2.to_k.weight" in tensors
+ assert f"{prefix}.attn2.to_k.bias" in tensors
+ assert not any(".orig_layer." in key for key in tensors)
+ assert "conv_in.weight" in tensors
+ assert "conv_in.bias" in tensors
+ assert not any(".residual_linear." in key or ".lora_down." in key or ".lora_up." in key for key in tensors)
+ assert all(tensor.device.type == "cpu" and tensor.is_contiguous() for tensor in tensors.values())
+ with safe_open(path, framework="pt") as handle:
+ assert set(handle.keys()) == set(tensors)
+ assert handle.metadata()["model_class"] == "NunchakuSDXLUNet2DConditionModel"
+ assert handle.get_tensor("conv_in.weight").dtype == torch.bfloat16
diff --git a/test/unit/common/utils/test_cli_usage.py b/test/unit/common/utils/test_cli_usage.py
index 03a9ff39c..3803c0dc4 100644
--- a/test/unit/common/utils/test_cli_usage.py
+++ b/test/unit/common/utils/test_cli_usage.py
@@ -339,7 +339,8 @@ def test_legacy_disable_flags_map_to_enable_bools():
assert args.enable_quanted_input is False
-def test_svdquant_cli_builds_hyphenated_options_before_rtn():
[email protected]("model_adapter", ["flux", "sdxl"])
+def test_svdquant_cli_builds_hyphenated_options_before_rtn(model_adapter):
from auto_round.algorithms.transforms.svdquant.config import SVDQuantConfig
from auto_round.cli.algorithms import AlgorithmHandler
from auto_round.cli.parser import build_quantize_parser
@@ -368,7 +369,7 @@ def test_svdquant_cli_builds_hyphenated_options_before_rtn():
"--svdquant-exclude-modules",
"proj_out",
"--svdquant-model-adapter",
- "flux",
+ model_adapter,
"--disable_opt_rtn",
]
)
@@ -385,7 +386,7 @@ def test_svdquant_cli_builds_hyphenated_options_before_rtn():
assert configs[0].low_rank_dtype == "fp32"
assert configs[0].target_modules == ["attn", "ff"]
assert configs[0].exclude_modules == ["proj_out"]
- assert configs[0].model_adapter == "flux"
+ assert configs[0].model_adapter == model_adapter
assert configs[1].__class__.__name__ == "RTNConfig"