Files
Model-Optimizer/tests/examples/diffusers/test_diffusers.py
T
Keval MorabiaandClaude Opus 4.8 0081473861 Speed up slow unit/gpu/example tests (#1616)
### What does this PR do?

Type of change: test infrastructure / test speedups + CI stabilization

Make the test suite faster, `tests/unit` hermetic, and the CI lanes
stable, without losing coverage. Most changes are mechanical test/infra
edits; the buckets below cover the diff broadly.

**Unit tests — hermetic (no HF Hub):** toy local datasets/configs + the
local tiny tokenizer (with a checked-in chat template) replace Hub
assets; `tests/unit/conftest.py` enforces offline mode. Genuinely-HF
tests moved to `tests/gpu*` (e.g. the new
`tests/gpu/torch/utils/test_dataset_utils.py`). `CONTRIBUTING.md`
documents the hermetic-unit-test expectation.

**Unit-test speedups (no coverage loss):** speculative (disable CPU
torch.compile), calibrator (fewer histogram bins), ONNX conv/dynamo
(smaller shapes + representative subset), Ruler/sparse-attention (local
tokenizer), data-parallel autoquant (world size 4→2). Shared
`tiny_tokenizer` fixture. The distributed test helper now uses a private
`spawn` context instead of mutating the global start method (avoids
cross-test contamination).

**Rarely-used autonas/fastnas tests:** heavy parametrize cases marked
`@pytest.mark.manual`, one representative kept per test (fastnas
preferred); lighter sibling tests still cover core behavior. The legacy
FSDP1 NAS distributed test is also dropped: FastNAS/AutoNAS aren't used
with either FSDP1 or FSDP2, and FSDP1 is superseded by the newer FSDP2
API — so we keep a single FSDP2 case as a sanity check and drop FSDP1,
leaving the suite leaner.

**gpu_megatron:** deduplicate distributed worker pools by world_size
within a module (saves a redundant pool spin-up in multi-pool files;
module-scoped, no cross-module reuse).

**Example tests:** reduce per-test work via args that default to current
behavior (tests pass the fast values) — torch_onnx TRT optimization
level, diffusers calibration/inference steps, eagle `sample_size`,
megatron_bridge iters/calib, llm_sparsity data slice, export
safetensors-structure `calib_size`. Also enable the recently added
`gpt-oss` example tests in CI.

**Per-test timeouts:** `pytest-timeout` with a default per-directory
timeout (60s unit / 300s gpu+example) enforced in `tests/conftest.py`
(`timeout_func_only` in `pyproject.toml`), so a new test cannot silently
exceed the budget — an unmapped test dir crashes collection. A few
inherently slow tests carry explicit higher per-test overrides
(CUDA-compile, autotune, dflash).

**CUDA kernel pre-compilation:** a dedicated `tests/gpu/_extensions`
test JIT-builds the conv3d implicit-GEMM kernel up front (collected
before the functional tests in the same process) so the one-time build
cost no longer lands on — and time out — the first functional test that
uses it. Mirrored into the `llm_ptq`/`vlm_ptq` example lanes.

**Test relocation & optional-dependency guards:** vLLM sparsity plugin
test moved to `tests/gpu_vllm` (drops the in-test `importorskip`);
diffusers-dependent unit test guarded with `importorskip("diffusers")`
for partial-install lanes; `gpt_oss` example test dir renamed to
`gpt-oss` to match the CI matrix.

**Diffusers test models:** shared model-path constants in
`tests/_test_utils/examples/models.py` consolidated/renamed and point at
tiny `hf-internal-testing` test pipes (SDXL/SD3/FLUX) so
cachify/quantize/export tests run on toy weights; `local_id`s
normalized.

**Shared dataset utils:** `examples/llm_sparsity/.../hf_pts.py` now uses
`get_dataset_dataloader` (drops the bespoke cnn_dailymail-only
`get_calib_dataloader`; supports any registered/HF/JSONL dataset,
includes attention_mask); `data_prep.py` gains `--max_samples`.

**CI workflows:** container image bumps (pytorch 26.04→26.05, TRT-LLM
rc16→rc17) and tightened lane timeouts (unit 30→15 min, gpu lanes
trimmed, onnx example lane 45 min).

**Imports at top of file:** in-function imports across the test suite
are moved to module top per the coding guideline, conservatively —
optional deps stay guarded (in-function or behind a module-level
`importorskip`) in `tests/unit` since the partial-install lane runs
without them, and build/hardware-availability imports (apex, triton,
megatron/transformer_engine, tensorrt_llm) plus `_test_utils` lazy
guards are left in place.

**Kernel warning filters:** the repeated `filterwarnings` blanket-ignore
in six `tests/gpu/torch/kernels/**` modules is consolidated into a
scoped hook in `tests/gpu/torch/kernels/conftest.py` (kernel tests only
— the rest of the suite keeps surfacing warnings).

**Eagle example speedups:** `torch.compile` (eagle recipe default) added
~2 min to every eagle training test; it's now disabled in the eagle
example tests except one smoke (`test_llama_eagle3[1-False]`), and the
downstream resume / AR-validate / export tests point at the compile-free
checkpoint. Measured: `test_ar_validate` 139s→17s, offline training
142s→22s, streaming 140s→23s — the compile path is still smoke-tested
once.

**Example lanes install editable (`-e`):** so example scripts launched
as subprocesses resolve `modelopt` to the same source path as the test
process and reuse the pre-compiled CUDA-extension cache instead of
recompiling (~2 min/test); verified in the TRT-LLM container.

**Tiny test tokenizer:** `get_tiny_tokenizer` defaults to left padding
(what decoder-LM calibration expects) and ships a terse
generation-tagged chat template — replacing a verbose ChatML one that
inflated tokenized length on the 128-vocab tokenizer and broke the
offline-PTQ example tests' `max-seq-len` filter.

**Restored Hub-download coverage:** the live (ungated) HF dataset
round-trips exercising `get_dataset_samples`' download branch now live
in `tests/gpu/torch/utils/test_dataset_utils.py` (they had been dropped
from the hermetic unit file without a counterpart).

Individual file changes not explicitly called out above fall under this
general test/CI cleanup.

### Testing

Unit + the touched gpu_megatron files validated locally; example/GPU
lanes validated in CI.

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

- Is this change backward compatible?: ✅ (tests + example CLI args
default to prior behavior)
- If you copied code from any other sources or added a new PIP
dependency: N/A
- Did you write any new necessary tests?: N/A (optimizes/relocates
existing tests)
- Did you update Changelog?: N/A
- Did you get Claude approval on this PR?: ❌ (pending)

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

## Release Notes

* **Chores**
* Updated container image versions for PyTorch (26.04→26.05),
TensorRT-LLM (1.3.0rc16→1.3.0rc17), and ONNX/TensorRT (26.04→26.05).

* **Tests**
* Enhanced test isolation: unit tests now run hermetically without
HuggingFace Hub access.
* Optimized test runtime via smaller model/dataset parameters and
parallel test caching.
* Added CUDA extension availability tests and extended dataset utility
coverage.

* **Documentation**
* Updated testing guidelines in `CONTRIBUTING.md` to emphasize offline
test design.

* **Chores**
* Added pytest timeout configuration and improved CI/CD workflow
efficiency with editable installs.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 17:13:10 +00:00

307 lines
9.0 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
from typing import NamedTuple
import pytest
from _test_utils.examples.models import FLUX_SCHNELL_PATH, SD3_PATH, SDXL_PATH
from _test_utils.examples.run_command import run_example_command
from _test_utils.torch.misc import minimum_sm
class DiffuserModel(NamedTuple):
dtype: str
name: str
path: str
format_type: str
quant_algo: str
collect_method: str
def _run_cmd(self, script: str, *args: str) -> None:
cmd_args = [
"python",
script,
"--model",
self.name,
"--override-model-path",
self.path,
]
cmd_args.extend(args)
run_example_command(cmd_args, "diffusers/quantization")
def _format_args(self) -> list[str]:
return [
"--calib-size",
"4",
"--percentile",
"1.0",
"--alpha",
"0.8",
"--n-steps",
"2",
"--batch-size",
"2",
"--format",
self.format_type,
"--collect-method",
self.collect_method,
"--quant-algo",
self.quant_algo,
]
def quantize(self, tmp_path: Path) -> None:
self._run_cmd(
"quantize.py",
*self._format_args(),
"--trt-high-precision-dtype",
self.dtype,
"--quantized-torch-ckpt-save-path",
str(tmp_path / f"{self.name}_{self.format_type}.pt"),
"--onnx-dir",
str(tmp_path / f"{self.name}_{self.format_type}_onnx"),
)
def restore(self, tmp_path: Path) -> None:
self._run_cmd(
"quantize.py",
*self._format_args(),
"--trt-high-precision-dtype",
self.dtype,
"--restore-from",
str(tmp_path / f"{self.name}_{self.format_type}.pt"),
"--onnx-dir",
str(tmp_path / f"{self.name}_{self.format_type}_onnx"),
)
def inference(self, tmp_path: Path) -> None:
self._run_cmd(
"diffusion_trt.py",
"--onnx-load-path",
str(tmp_path / f"{self.name}_{self.format_type}_onnx/model.onnx"),
"--dq-only",
"--torch-autocast",
"--num-inference-steps",
"2",
)
@pytest.mark.parametrize(
"model",
[
DiffuserModel(
name="flux-schnell",
path=FLUX_SCHNELL_PATH,
dtype="BFloat16",
format_type="int8",
quant_algo="smoothquant",
collect_method="min-mean",
),
DiffuserModel(
name="sd3-medium",
path=SD3_PATH,
dtype="Half",
format_type="int8",
quant_algo="smoothquant",
collect_method="min-mean",
),
pytest.param(
DiffuserModel(
name="sdxl-1.0",
path=SDXL_PATH,
dtype="Half",
format_type="fp8",
quant_algo="max",
collect_method="default",
),
marks=minimum_sm(89),
),
DiffuserModel(
name="sdxl-1.0",
path=SDXL_PATH,
dtype="Half",
format_type="int8",
quant_algo="smoothquant",
collect_method="min-mean",
),
],
ids=[
"flux_schnell_bf16_int8_smoothquant_3.0_min_mean",
"sd3_medium_fp16_int8_smoothquant_3.0_min_mean",
"sdxl_1.0_fp16_fp8_max_3.0_default",
"sdxl_1.0_fp16_int8_smoothquant_3.0_min_mean",
],
)
def test_diffusers_quantization(
model: DiffuserModel,
tmp_path: Path,
) -> None:
model.quantize(tmp_path)
model.restore(tmp_path)
model.inference(tmp_path)
class Wan22Model(NamedTuple):
model: str
backbone: str | None
format_type: str
quant_algo: str
collect_method: str
def _ckpt_path(self, tmp_path: Path) -> str:
stem = self.model.replace("wan2.2-t2v-", "")
parts = [stem, *([self.backbone] if self.backbone else []), self.format_type]
return str(tmp_path / f"wan22_{'_'.join(parts)}.pt")
def _common_args(self, tiny_wan22_path: str) -> list[str]:
cmd_args = [
"python",
"quantize.py",
"--model",
self.model,
"--override-model-path",
tiny_wan22_path,
"--format",
self.format_type,
"--quant-algo",
self.quant_algo,
"--collect-method",
self.collect_method,
"--model-dtype",
"BFloat16",
"--trt-high-precision-dtype",
"BFloat16",
"--calib-size",
"2",
"--batch-size",
"1",
"--n-steps",
"2",
# Tiny video dims — override MODEL_DEFAULTS for fast CI.
"--extra-param",
"height=16",
"--extra-param",
"width=16",
"--extra-param",
"num_frames=5",
]
if self.backbone is not None:
cmd_args.extend(["--backbone", self.backbone])
return cmd_args
def quantize(self, tiny_wan22_path: str, tmp_path: Path) -> None:
run_example_command(
[
*self._common_args(tiny_wan22_path),
"--quantized-torch-ckpt-save-path",
self._ckpt_path(tmp_path),
],
"diffusers/quantization",
)
def restore(self, tiny_wan22_path: str, tmp_path: Path) -> None:
run_example_command(
[*self._common_args(tiny_wan22_path), "--restore-from", self._ckpt_path(tmp_path)],
"diffusers/quantization",
)
# The VAE (``AutoencoderKLWan``) is shared between Wan 2.2 14B and 5B, so the
# Conv3D NVFP4 implicit-GEMM dispatch exercises the same kernel either way; we
# parametrize both ``--model`` values to also cover the ``quantize.py`` dispatch
# for each.
@pytest.mark.parametrize(
"wan_model",
[
Wan22Model("wan2.2-t2v-14b", None, "int8", "smoothquant", "min-mean"),
pytest.param(
Wan22Model("wan2.2-t2v-14b", None, "fp8", "max", "default"),
marks=minimum_sm(89),
),
pytest.param(
Wan22Model("wan2.2-t2v-14b", None, "fp4", "max", "default"),
marks=minimum_sm(89),
),
pytest.param(
Wan22Model("wan2.2-t2v-14b", "vae", "fp8", "max", "default"),
marks=minimum_sm(89),
),
pytest.param(
Wan22Model("wan2.2-t2v-14b", "vae", "fp4", "max", "default"),
marks=minimum_sm(89),
),
pytest.param(
Wan22Model("wan2.2-t2v-5b", "vae", "fp8", "max", "default"),
marks=minimum_sm(89),
),
pytest.param(
Wan22Model("wan2.2-t2v-5b", "vae", "fp4", "max", "default"),
marks=minimum_sm(89),
),
],
ids=[
"wan22_14b_transformer_int8_smoothquant",
"wan22_14b_transformer_fp8_max",
"wan22_14b_transformer_fp4_max",
"wan22_14b_vae_fp8_max",
"wan22_14b_vae_fp4_max",
"wan22_5b_vae_fp8_max",
"wan22_5b_vae_fp4_max",
],
)
def test_wan22_quantization(wan_model: Wan22Model, tiny_wan22_path: str, tmp_path: Path) -> None:
wan_model.quantize(tiny_wan22_path, tmp_path)
wan_model.restore(tiny_wan22_path, tmp_path)
@pytest.mark.parametrize(
("model_name", "model_path", "torch_compile"),
[
("flux-schnell", FLUX_SCHNELL_PATH, False),
("flux-schnell", FLUX_SCHNELL_PATH, True),
("sd3-medium", SD3_PATH, False),
("sd3-medium", SD3_PATH, True),
("sdxl-1.0", SDXL_PATH, False),
("sdxl-1.0", SDXL_PATH, True),
],
ids=[
"flux_schnell_torch",
"flux_schnell_torch_compile",
"sd3_medium_torch",
"sd3_medium_torch_compile",
"sdxl_1.0_torch",
"sdxl_1.0_torch_compile",
],
)
def test_diffusion_trt_torch(
model_name: str,
model_path: str,
torch_compile: bool,
) -> None:
cmd_args = [
"python",
"diffusion_trt.py",
"--model",
model_name,
"--override-model-path",
model_path,
"--torch",
"--num-inference-steps",
"2",
]
if torch_compile:
cmd_args.append("--torch-compile")
run_example_command(cmd_args, "diffusers/quantization")