mirror of
https://github.com/NVIDIA/Model-Optimizer.git
synced 2026-10-13 04:49:49 +08:00
### What does this PR do? Type of change: New feature Adds **Qwen-Image** (`Qwen/Qwen-Image`, `QwenImageTransformer2DModel`) to the diffusers quantization example and exports HuggingFace checkpoints in three precisions — **FP8**, **NVFP4**, and **NVFP4 + SVDQuant** — through the unified HF export. - Registers `--model qwen-image` (lazy diffusers import; no `trust_remote_code`). - Transformer-block-range recipe: quantizes only the linears under `transformer_blocks`, keeping the **first 2 / last 2** blocks (and everything outside `transformer_blocks`) in original precision. Applied **before** calibration so SVDQuant never mutates the excluded blocks. Expressed with the top-level `enable` `QuantizerCfgEntry` field (disable-all → re-enable `transformer_blocks` → disable first/last-N). - SVDQuant export (AWQ-style): promotes quantizer-owned tensors to clean module-level safetensors keys at export time — `weight_quantizer.svdquant_lora_a/b → <module>.svdquant_lora_a/b` and `input_quantizer._pre_quant_scale → <module>.pre_quant_scale` — with a documented `NVFP4_SVD` `quantization_config` (`group_size`, `has_zero_point: false`, `pre_quant_scale: true`, `lora_rank`). **Core SVDQuant quantization code (`modelopt/torch/quantization`) is unchanged.** - Shared export-path change — **intentionally global** (applies to all diffusers exports — SDXL / Flux / Wan, not just Qwen; the full export suite was verified green on GB200): `hide_quantizers_from_state_dict` now strips quantizer state from *all* modules (not just quant-linears) so calibrated norm-layer input quantizers no longer leak `input_quantizer._amax`. (An earlier `max_shard_size` workaround was dropped after merging `main`: #1794 makes the ComfyUI layerwise-metadata post-processing a no-op unless explicitly opted in, so a default sharded export no longer hits the unsupported-sharded path.) ### Usage ```bash python examples/diffusers/quantization/quantize.py \ --model qwen-image --override-model-path <Qwen-Image> --model-dtype BFloat16 \ --format fp4 --quant-algo svdquant --lowrank 32 \ --calib-size 64 --n-steps 20 \ --hf-ckpt-dir <out> # FP8: --format fp8 --quant-algo max # NVFP4: --format fp4 --quant-algo max ``` ### Testing - Focused unit + example tests pass on GB200 (sm_100): block-range recipe, `NVFP4_SVD` config schema, SVDQuant forward/fold (LoRA stays on `weight_quantizer`), Qwen dummy-input / strict-QKV-fusion / promotion, pipeline loading, and the diffusers HF-export test for Qwen FP8 / NVFP4 / SVDQuant. - Full `tests/examples/diffusers/test_export_diffusers_hf_ckpt.py` is green (SDXL, Flux, Qwen, Wan2.2) — confirms the shared export changes do not regress other models. - End-to-end on the real `Qwen/Qwen-Image` (~20B): all three formats export valid HF checkpoints — only `transformer_blocks` 2..57 quantized, nothing outside, no quantizer/`_amax` leak, correct `weight_scale`(`_2`)/`input_scale`, promoted SVDQuant keys (rank-consistent shapes), and the expected `quantization_config`. ### Before your PR is "*Ready for review*" - Is this change backward compatible?: ✅ <!-- live-model LoRA storage unchanged; existing exports unaffected --> - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: N/A - Did you write any new necessary tests?: ✅ - Did you update Changelog?: ❌ <!-- new-example feature; add a CHANGELOG.rst entry if required --> - Did you get Claude approval on this PR?: ❌ <!-- run /claude review --> ### Additional Information All changes are confined to the diffusers example (`examples/diffusers/quantization`) plus the shared export path (`modelopt/torch/export`); the core quantization library is untouched. ### Follow-up (next step): fused-QKV SVDQuant for sglang / Nunchaku This export keeps attention `q/k/v` (and `add_q/k/v_proj`) as **separate** projections — the diffusers-native layout. That matches sglang's bf16 / FP8 / plain-NVFP4 paths (which also keep QKV separate) and ModelOpt/TRT-LLM consumers, so those load 1:1. sglang's **NVFP4-SVDQuant (Nunchaku)** path, however, builds a **fused** `to_qkv` with a *single* fused rank-r LoRA in Nunchaku-native format (`proj_down`/`proj_up`, `smooth_factor`, `wscales`/`wtscale`). Our per-projection tensors (`svdquant_lora_a/b` + `pre_quant_scale`; three independent rank-r decompositions) are not directly loadable there — and cannot be fused at load time, because the fp16 weight residual needed to derive a single fused rank-r is not preserved after export. **Planned next step:** an opt-in fused-QKV SVDQuant export mode that fuses q/k/v **before** SVDQuant calibration (yielding one rank-r over the fused weight) and emits a Nunchaku-compatible layout, enabling lower-latency fused-QKV inference in sglang. Tracked as a separate follow-up. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added Qwen-Image (`QWEN_IMAGE`) model quantization and Diffusers export support * Added NVFP4_SVD (SVDQuant) export configuration support * Added transformer block-range quantization recipes (exclude first/last blocks) * **Bug Fixes** * Improved missing-pipeline error messaging for Qwen-Image * Prevented quantizer-related tensor/buffer leakage by promoting and cleaning quantizer outputs during export * **Tests** * Added Qwen-Image HF checkpoint export tests and offline fixtures * Added unit coverage for SVDQuant promotion/clean state-dict keys <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Jingyu Xin <jingyux@nvidia.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
196 lines
7.3 KiB
Python
196 lines
7.3 KiB
Python
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
# 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 os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import torch
|
|
import torch.nn.functional as F
|
|
from datasets import load_dataset
|
|
from diffusers.models.attention_processor import Attention, AttnProcessor
|
|
from diffusers.models.lora import LoRACompatibleConv, LoRACompatibleLinear
|
|
from diffusers.utils import load_image
|
|
|
|
import modelopt.torch.quantization as mtq
|
|
from modelopt.torch.quantization.plugins.diffusion.diffusers import AttentionModuleMixin
|
|
|
|
USE_PEFT = True
|
|
try:
|
|
from peft.tuners.lora.layer import Conv2d as PEFTLoRAConv2d
|
|
from peft.tuners.lora.layer import Linear as PEFTLoRALinear
|
|
except ModuleNotFoundError:
|
|
USE_PEFT = False
|
|
|
|
|
|
# Model-specific filter functions for quantization
|
|
def filter_func_default(name: str) -> bool:
|
|
"""Default filter function for general models."""
|
|
pattern = re.compile(
|
|
r".*(time_emb_proj|time_embedding|conv_in|conv_out|conv_shortcut|add_embedding|pos_embed|time_text_embed|context_embedder|norm_out|x_embedder).*"
|
|
)
|
|
return pattern.match(name) is not None
|
|
|
|
|
|
def check_conv_and_mha(backbone, if_fp4, quantize_mha):
|
|
for name, module in backbone.named_modules():
|
|
if isinstance(module, (torch.nn.Conv1d, torch.nn.Conv2d)) and if_fp4:
|
|
module.weight_quantizer.disable()
|
|
module.input_quantizer.disable()
|
|
|
|
print(f"Disabled NVFP4 Conv layer quantization for layer {name}")
|
|
|
|
elif isinstance(module, (Attention, AttentionModuleMixin)):
|
|
head_size = int(module.inner_dim / module.heads)
|
|
if not quantize_mha or head_size % 16 != 0:
|
|
for attr in (
|
|
"q_bmm_quantizer",
|
|
"k_bmm_quantizer",
|
|
"v_bmm_quantizer",
|
|
"softmax_quantizer",
|
|
"bmm2_output_quantizer",
|
|
):
|
|
if hasattr(module, attr):
|
|
getattr(module, attr).disable()
|
|
setattr(module, "_disable_fp8_mha", True)
|
|
|
|
print(f"Disabled Attention layer quantization for layer {name}")
|
|
else:
|
|
setattr(module, "_disable_fp8_mha", False)
|
|
|
|
|
|
def filter_func_ltx_video(name: str) -> bool:
|
|
"""Filter function specifically for LTX-Video models."""
|
|
pattern = re.compile(
|
|
r".*(proj_in|time_embed|caption_projection|proj_out|patchify_proj|adaln_single|transformer_blocks\.(0|1|2|45|46|47)\.).*"
|
|
)
|
|
return pattern.match(name) is not None
|
|
|
|
|
|
def filter_func_flux_dev(name: str) -> bool:
|
|
"""Filter function specifically for Flux-dev models."""
|
|
pattern = re.compile(
|
|
r"(proj_out.*|.*(time_text_embed|context_embedder|x_embedder|norm_out|time_guidance_embed|stream_modulation).*)"
|
|
)
|
|
return pattern.match(name) is not None
|
|
|
|
|
|
def filter_func_ltx2_vae(name: str) -> bool:
|
|
"""Filter for LTX-2 VAE: keeps only conv1/conv2 in up_blocks resnets."""
|
|
keep = re.compile(r".*up_blocks\.\d+\.resnets\.\d+\.conv[12](?:\.|$)")
|
|
return not keep.match(name)
|
|
|
|
|
|
def filter_func_wan_vae(name: str) -> bool:
|
|
"""Filter for Wan 2.2 VAE: keeps only conv1/conv2 in resnet blocks."""
|
|
keep = re.compile(
|
|
r".*(down_blocks\.\d+\.(?:resnets\.\d+\.)?conv[12]"
|
|
r"|mid_block\.resnets\.\d+\.conv[12]"
|
|
r"|up_blocks\.\d+\.resnets\.\d+\.conv[12])(?:\.|$)"
|
|
)
|
|
return not keep.match(name)
|
|
|
|
|
|
def filter_func_wan_video(name: str) -> bool:
|
|
"""Filter function specifically for WAN-Video models."""
|
|
pattern = re.compile(
|
|
r".*(patch_embedding|condition_embedder|proj_out|blocks\.(0|1|2|37|38|39)\.).*"
|
|
)
|
|
return pattern.match(name) is not None
|
|
|
|
|
|
# Qwen-Image's transformer has 60 ``transformer_blocks``. The recipe quantizes
|
|
# only those blocks while keeping the first two and last two -- and everything
|
|
# outside ``transformer_blocks`` -- in original precision. The model-agnostic,
|
|
# config-driven form of this recipe (deriving the block count from the model)
|
|
# lives in quantize.py; this name-only filter covers the plain FP8/NVFP4 path
|
|
# for the full 60-block Qwen-Image transformer.
|
|
QWEN_IMAGE_NUM_TRANSFORMER_BLOCKS = 60
|
|
_QWEN_IMAGE_BLOCK_RE = re.compile(r"(?:^|\.)transformer_blocks\.(\d+)(?:\.|$)")
|
|
|
|
|
|
def filter_func_qwen_image(name: str) -> bool:
|
|
"""Filter function specifically for Qwen-Image models.
|
|
|
|
Returns ``True`` for modules to keep in original precision (quantization
|
|
disabled): everything outside ``transformer_blocks``, plus the first two and
|
|
last two transformer blocks.
|
|
"""
|
|
match = _QWEN_IMAGE_BLOCK_RE.search(name)
|
|
if match is None:
|
|
return True
|
|
block_idx = int(match.group(1))
|
|
return block_idx < 2 or block_idx >= QWEN_IMAGE_NUM_TRANSFORMER_BLOCKS - 2
|
|
|
|
|
|
def load_calib_prompts(
|
|
batch_size,
|
|
calib_data_path: str | Path = "Gustavosta/Stable-Diffusion-Prompts",
|
|
split="train",
|
|
column="Prompt",
|
|
) -> list[list[str]]:
|
|
prompt_list: list[str] = []
|
|
if isinstance(calib_data_path, Path):
|
|
with open(calib_data_path) as f:
|
|
prompt_list = f.readlines()
|
|
else:
|
|
dataset = load_dataset(calib_data_path)
|
|
prompt_list = list(dataset[split][column])
|
|
return [prompt_list[i : i + batch_size] for i in range(0, len(prompt_list), batch_size)]
|
|
|
|
|
|
def load_calib_images(folder_path):
|
|
images = []
|
|
for filename in os.listdir(folder_path):
|
|
img_path = os.path.join(folder_path, filename)
|
|
if os.path.isfile(img_path):
|
|
image = load_image(img_path)
|
|
if image is not None:
|
|
images.append(image)
|
|
return images
|
|
|
|
|
|
def set_fmha(unet):
|
|
for name, module in unet.named_modules():
|
|
if isinstance(module, Attention):
|
|
module.set_processor(AttnProcessor())
|
|
|
|
|
|
def check_lora(unet):
|
|
for name, module in unet.named_modules():
|
|
if isinstance(module, (LoRACompatibleConv, LoRACompatibleLinear)):
|
|
assert module.lora_layer is None, (
|
|
f"To quantize {name}, LoRA layer should be fused/merged. Please"
|
|
" fuse the LoRA layer before quantization."
|
|
)
|
|
elif USE_PEFT and isinstance(module, (PEFTLoRAConv2d, PEFTLoRALinear)):
|
|
assert module.merged, (
|
|
f"To quantize {name}, LoRA layer should be fused/merged. Please"
|
|
" fuse the LoRA layer before quantization."
|
|
)
|
|
|
|
|
|
def fp8_mha_disable(backbone, quantized_mha_output: bool = True):
|
|
def mha_filter_func(name):
|
|
pattern = re.compile(
|
|
r".*(q_bmm_quantizer|k_bmm_quantizer|v_bmm_quantizer|softmax_quantizer).*"
|
|
if quantized_mha_output
|
|
else r".*(q_bmm_quantizer|k_bmm_quantizer|v_bmm_quantizer|softmax_quantizer|bmm2_output_quantizer).*"
|
|
)
|
|
return pattern.match(name) is not None
|
|
|
|
if hasattr(F, "scaled_dot_product_attention"):
|
|
mtq.disable_quantizer(backbone, mha_filter_func)
|