Files
Model-Optimizer/tests/_test_utils/torch/diffusers_models.py
T
jingyu-ml 668b8a19e8 [1/3] Diffusion ckpt export for NVFP4 & FP8 (#781)
## What does this PR do?

**Type of change:** New feature <!-- Use one of the following: Bug fix,
new feature, new example, new tests, documentation. -->

**Overview:** 

This PR adds support for exporting quantized diffusers models (DiT,
Flux, SD3, UNet, etc.) to HuggingFace checkpoint format, enabling
deployment to inference frameworks like SGLang, vLLM, and TensorRT-LLM.

**Changes**

New file: `diffusers_utils.py`
- Dummy input generation for various diffusion models
- Pipeline component extraction helpers
- QKV projection detection and grouping
- `hide_quantizers_from_state_dict()` context manager for clean saves

Refactored: `unified_export_hf.py`
- New `_fuse_qkv_linears_diffusion()` for QKV amax fusion
- `_export_diffusers_checkpoint()` to export full pipelines (models +
tokenizers + schedulers etc.)

Plans

- [x] [1/3] Add the basic functionalities to support limited image
models with NVFP4 + FP8, with some refactoring on the previous LLM code
and the diffusers example. PIC: @jingyu-ml
- [ ] [2/3] Add support to more video gen modelsPIC: @jingyu-ml 
- [ ] [3/3] Add test cases, refactor on the doc, and all related README.
PIC: @jingyu-ml

## Usage
<!-- You can potentially add a usage example below. -->
```
mtq.quantize(pipe, quant_config, forward_call)
export_hf_checkpoint(pipe, export_dir=hf_ckpt_dir)
```

## Testing
<!-- Mention how have you tested your change if applicable. -->

## Before your PR is "*Ready for review*"
<!-- If you haven't finished some of the above items you can still open
`Draft` PR. -->

- **Make sure you read and follow [Contributor
guidelines](https://github.com/NVIDIA/Model-Optimizer/blob/main/CONTRIBUTING.md)**
and your commits are signed.
- **Is this change backward compatible?**: Yes <!--- If No, explain why.
-->
- **Did you write any new necessary tests?**:No
- **Did you add or update any necessary documentation?**:No
- **Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?**:No
<!--- Only for new features, API changes, critical bug fixes or bw
breaking changes. -->

## Additional Information
<!-- E.g. related issue. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## New Features
* Added HuggingFace checkpoint export support for quantized diffusion
models with configurable output directory
* Introduced new `--hf-ckpt-dir` CLI argument for specifying checkpoint
export destination
* Extended export functionality to support selective component exports
from diffusion pipelines
* Enhanced quantized model export with improved component handling and
multi-stage checkpoint generation

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jingyu Xin <jingyux@nvidia.com>
2026-01-21 23:11:09 +00:00

134 lines
4.3 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2023-2025 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.
from pathlib import Path
import pytest
import torch
pytest.importorskip("diffusers")
from diffusers import UNet2DConditionModel
try:
from diffusers.models.transformers import DiTTransformer2DModel, FluxTransformer2DModel
except Exception: # pragma: no cover - optional diffusers models
DiTTransformer2DModel = None
FluxTransformer2DModel = None
import modelopt.torch.opt as mto
def get_tiny_unet(**config_kwargs) -> UNet2DConditionModel:
"""Create a tiny UNet2DConditionModel for testing."""
kwargs = {
"sample_size": 8,
"in_channels": 2,
"out_channels": 2,
"down_block_types": ("DownBlock2D",),
"up_block_types": ("UpBlock2D",),
"block_out_channels": (2,),
"layers_per_block": 1,
"cross_attention_dim": 2,
"attention_head_dim": 1,
"norm_num_groups": 1,
"mid_block_type": None,
}
kwargs.update(**config_kwargs)
tiny_unet = UNet2DConditionModel(**kwargs)
return tiny_unet
def get_tiny_dit(**config_kwargs):
"""Create a tiny DiTTransformer2DModel for testing."""
if DiTTransformer2DModel is None:
pytest.skip("DiTTransformer2DModel is not available in this diffusers version.")
kwargs = {
"num_attention_heads": 2,
"attention_head_dim": 8,
"in_channels": 2,
"out_channels": 2,
"num_layers": 1,
"norm_num_groups": 1,
"sample_size": 8,
"patch_size": 2,
"num_embeds_ada_norm": 10,
}
kwargs.update(**config_kwargs)
return DiTTransformer2DModel(**kwargs)
def get_tiny_flux(**config_kwargs):
"""Create a tiny FluxTransformer2DModel for testing."""
if FluxTransformer2DModel is None:
pytest.skip("FluxTransformer2DModel is not available in this diffusers version.")
kwargs = {
"patch_size": 1,
"in_channels": 4,
"out_channels": 4,
"num_layers": 1,
"num_single_layers": 1,
"attention_head_dim": 8,
"num_attention_heads": 2,
"joint_attention_dim": 8,
"pooled_projection_dim": 8,
"guidance_embeds": False,
"axes_dims_rope": (2, 2, 4),
}
kwargs.update(**config_kwargs)
return FluxTransformer2DModel(**kwargs)
def create_tiny_unet_dir(tmp_path: Path, **config_kwargs) -> Path:
"""Create and save a tiny UNet model to a directory."""
tiny_unet = get_tiny_unet(**config_kwargs)
tiny_unet.save_pretrained(tmp_path / "tiny_unet")
return tmp_path / "tiny_unet"
def get_unet_dummy_inputs(model: UNet2DConditionModel, batch_size: int = 1):
"""Generate dummy inputs for testing UNet models."""
latents = torch.randn(
batch_size, model.config.in_channels, model.config.sample_size, model.config.sample_size
)
timestep = torch.tensor([0])
encoder_hidden_states = torch.randn(batch_size, 1, model.config.cross_attention_dim)
return {"sample": latents, "timestep": timestep, "encoder_hidden_states": encoder_hidden_states}
def df_output_tester(model_ref, model_test):
"""Test if two diffusers models produce the same output."""
inputs = get_unet_dummy_inputs(model_ref)
model_ref.eval()
model_test.eval()
with torch.no_grad():
output_ref = model_ref(**inputs).sample
output_test = model_test(**inputs).sample
assert torch.allclose(output_ref, output_test)
def df_modelopt_state_and_output_tester(model_ref, model_test):
"""Test if two diffusers models have the same modelopt state and outputs."""
model_ref_state = mto.modelopt_state(model_ref)
model_test_state = mto.modelopt_state(model_test)
assert model_ref_state == model_test_state
df_output_tester(model_ref, model_test)