Files
Model-Optimizer/tests/examples/diffusers/conftest.py
T
jingyu-mlandClaude Opus 4.8 aec72ffa68 Add DMD2 distillation for Qwen-Image (fastgen) (#1326)
### What does this PR do?

**Type of change:** New example + new `modelopt.torch.fastgen` library
module.

Adds **DMD2 (Distribution Matching Distillation) for Qwen-Image** —
distilling the base model into a few-step (1–4) generator. Includes the
framework-agnostic `modelopt.torch.fastgen` loss library (DMD pipeline,
EMA, optional GAN discriminator) and a NeMo AutoModel–based training
example with a mock-data smoke config, a real-data config, and inference
/ export scripts.

**Noted**: the example script will be migrated to AutoModel repo

### Usage

```bash
# Mock-data wiring smoke — runs end-to-end with no dataset to prepare
torchrun --nproc-per-node=8 \
    examples/diffusers/fastgen/dmd2_finetune.py \
    --config examples/diffusers/fastgen/configs/dmd2_qwen_image_smoke.yaml
```

See `examples/diffusers/fastgen/README.md` for real-data training and
inference.

### Testing

Unit tests under `tests/unit/torch/fastgen/`; `pre-commit` /
code-quality clean.

### Before your PR is "*Ready for review*"

- Backward compatible?: ✅ (new, additive module)
- Followed `CONTRIBUTING.md` for any copied code / new deps: ✅
- New tests added?: ✅
- Updated Changelog?: N/A


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

* **New Features**
* Adds a FastGen-based distillation framework (DMD2) with
student/fake-score training, EMA support, GAN discriminator branch,
inference pipeline, and export utilities.
* Qwen-Image integration with latent packing and feature-capture for
plugin-enabled pipelines.

* **Documentation**
* New README, example configs, and runnable example scripts for
Qwen-Image distillation and inference.

* **Tests**
* Comprehensive unit tests covering math parity, gradient routing,
plugins, hooks, EMA, and recipe setup.
<!-- 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 (1M context) <noreply@anthropic.com>
2026-06-08 16:03:35 -07:00

49 lines
1.9 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 pytest
@pytest.fixture(scope="session")
def tiny_wan22_path(tmp_path_factory):
"""Create a tiny Wan 2.2 (14B-style) pipeline and return its path.
Built once per session and shared across all tests that need it.
"""
try:
from _test_utils.torch.diffusers_models import create_tiny_wan22_pipeline_dir
except ImportError:
pytest.skip("Wan 2.2 diffusers models not available (requires diffusers with WanPipeline)")
tmp_path = tmp_path_factory.mktemp("wan22")
return str(create_tiny_wan22_pipeline_dir(tmp_path))
@pytest.fixture(scope="session")
def tiny_qwen_image_path(tmp_path_factory):
"""Create a tiny Qwen-Image pipeline and return its path (built once per session).
SKETCH fixture for the recipe-level DMD2 e2e (``test_fastgen_recipe_e2e.py``).
See ``create_tiny_qwen_image_pipeline_dir`` for caveats — notably the tiny
Qwen2.5-VL text encoder, which needs in-container validation.
"""
try:
from _test_utils.torch.diffusers_models import create_tiny_qwen_image_pipeline_dir
except ImportError:
pytest.skip("Qwen-Image diffusers models not available")
tmp_path = tmp_path_factory.mktemp("qwen_image")
return str(create_tiny_qwen_image_pipeline_dir(tmp_path))