-
Notifications
You must be signed in to change notification settings - Fork 368
Updated the diffusion config issue and more test cases #937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4ba3ac1
aba42e7
4a07d88
36f984b
71ea192
17d5dcf
724d5a2
9a692e0
699bdc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,130 @@ | ||||||
| # 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. | ||||||
|
|
||||||
| from pathlib import Path | ||||||
| from typing import NamedTuple | ||||||
|
|
||||||
| import pytest | ||||||
| from _test_utils.examples.models import FLUX_SCHNELL_PATH, SDXL_1_0_PATH | ||||||
| from _test_utils.examples.run_command import run_example_command | ||||||
| from _test_utils.torch.misc import minimum_sm | ||||||
|
|
||||||
|
|
||||||
| class DiffuserHfExportModel(NamedTuple): | ||||||
| name: str | ||||||
| path: str | ||||||
| dtype: str | ||||||
| format_type: str | ||||||
| quant_algo: str | ||||||
| collect_method: str | ||||||
| model_dtype: str = "Half" | ||||||
|
|
||||||
| def quantize_and_export_hf(self, tmp_path: Path) -> Path: | ||||||
| hf_ckpt_dir = tmp_path / f"{self.name}_{self.format_type}_hf_ckpt" | ||||||
| cmd_args = [ | ||||||
| "python", | ||||||
| "quantize.py", | ||||||
| "--model", | ||||||
| self.name, | ||||||
| "--override-model-path", | ||||||
| self.path, | ||||||
| "--calib-size", | ||||||
| "8", | ||||||
| "--batch-size", | ||||||
| "2", | ||||||
| "--n-steps", | ||||||
| "20", | ||||||
| "--percentile", | ||||||
| "1.0", | ||||||
| "--alpha", | ||||||
| "0.8", | ||||||
| "--format", | ||||||
| self.format_type, | ||||||
| "--quant-algo", | ||||||
| self.quant_algo, | ||||||
| "--collect-method", | ||||||
| self.collect_method, | ||||||
| "--model-dtype", | ||||||
| self.model_dtype, | ||||||
| "--trt-high-precision-dtype", | ||||||
| self.dtype, | ||||||
| "--hf-ckpt-dir", | ||||||
| str(hf_ckpt_dir), | ||||||
| ] | ||||||
| run_example_command(cmd_args, "diffusers/quantization") | ||||||
| return hf_ckpt_dir | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.parametrize( | ||||||
| "model", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we cover Wan2.2 and LTX-2?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wan2.2 and LTX-2 is too big for CI, lets see if we have other options
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To test in CI, we would need to define custom tiny versions of these models like we do for llms. Or if there are tiny test versions available on HF we can use them |
||||||
| [ | ||||||
| DiffuserHfExportModel( | ||||||
| name="sdxl-1.0", | ||||||
| path=SDXL_1_0_PATH, | ||||||
| dtype="Half", | ||||||
| format_type="int8", | ||||||
| quant_algo="smoothquant", | ||||||
| collect_method="min-mean", | ||||||
| ), | ||||||
| DiffuserHfExportModel( | ||||||
| name="flux-schnell", | ||||||
| path=FLUX_SCHNELL_PATH, | ||||||
| dtype="BFloat16", | ||||||
| format_type="int8", | ||||||
| quant_algo="smoothquant", | ||||||
| collect_method="min-mean", | ||||||
| model_dtype="BFloat16", | ||||||
| ), | ||||||
| pytest.param( | ||||||
| DiffuserHfExportModel( | ||||||
| name="sdxl-1.0", | ||||||
| path=SDXL_1_0_PATH, | ||||||
| dtype="Half", | ||||||
| format_type="fp8", | ||||||
| quant_algo="max", | ||||||
| collect_method="default", | ||||||
| ), | ||||||
| marks=minimum_sm(89), | ||||||
| ), | ||||||
| pytest.param( | ||||||
| DiffuserHfExportModel( | ||||||
| name="flux-schnell", | ||||||
| path=FLUX_SCHNELL_PATH, | ||||||
| dtype="BFloat16", | ||||||
| format_type="fp4", | ||||||
| quant_algo="max", | ||||||
| collect_method="default", | ||||||
| model_dtype="BFloat16", | ||||||
| ), | ||||||
| marks=minimum_sm(89), | ||||||
|
||||||
| marks=minimum_sm(89), | |
| marks=minimum_sm(100), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jingyu-ml PTAL at this copilot comment
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # 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 json | ||
|
|
||
| import pytest | ||
| from _test_utils.torch.diffusers_models import get_tiny_dit, get_tiny_flux, get_tiny_unet | ||
|
|
||
| pytest.importorskip("diffusers") | ||
|
|
||
| import modelopt.torch.quantization as mtq | ||
| from modelopt.torch.export.diffusers_utils import generate_diffusion_dummy_inputs | ||
| from modelopt.torch.export.unified_export_hf import export_hf_checkpoint | ||
|
|
||
|
|
||
| def _load_config(config_path): | ||
| with open(config_path) as file: | ||
| return json.load(file) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_factory", [get_tiny_unet, get_tiny_dit, get_tiny_flux]) | ||
| @pytest.mark.parametrize( | ||
| ("config_id", "quant_cfg"), | ||
| [ | ||
| ("int8", mtq.INT8_DEFAULT_CFG), | ||
| ("int8_smoothquant", mtq.INT8_SMOOTHQUANT_CFG), | ||
| ("fp8", mtq.FP8_DEFAULT_CFG), | ||
| ], | ||
| ) | ||
| def test_export_diffusers_real_quantized(tmp_path, model_factory, config_id, quant_cfg): | ||
| model = model_factory() | ||
| export_dir = tmp_path / f"export_{type(model).__name__}_{config_id}_real_quant" | ||
|
|
||
| def _calib_fn(m): | ||
| param = next(m.parameters()) | ||
| dummy_inputs = generate_diffusion_dummy_inputs(m, param.device, param.dtype) | ||
| assert dummy_inputs is not None | ||
| m(**dummy_inputs) | ||
|
|
||
| mtq.quantize(model, quant_cfg, forward_loop=_calib_fn) | ||
|
|
||
| export_hf_checkpoint(model, export_dir=export_dir) | ||
|
|
||
| config_path = export_dir / "config.json" | ||
| assert config_path.exists() | ||
|
|
||
| config_data = _load_config(config_path) | ||
| assert "quantization_config" in config_data | ||
|
|
||
|
|
||
| def test_export_diffusers_real_quantized_fp4(tmp_path): | ||
| """FP4 export test using get_tiny_dit (the only tiny model with FP4-compatible weight shapes).""" | ||
| model = get_tiny_dit() | ||
| export_dir = tmp_path / "export_DiTTransformer2DModel_fp4_real_quant" | ||
|
|
||
| def _calib_fn(m): | ||
| param = next(m.parameters()) | ||
| dummy_inputs = generate_diffusion_dummy_inputs(m, param.device, param.dtype) | ||
| assert dummy_inputs is not None | ||
| m(**dummy_inputs) | ||
|
|
||
| mtq.quantize(model, mtq.NVFP4_DEFAULT_CFG, forward_loop=_calib_fn) | ||
|
|
||
| export_hf_checkpoint(model, export_dir=export_dir) | ||
|
|
||
| config_path = export_dir / "config.json" | ||
| assert config_path.exists() | ||
|
|
||
| config_data = _load_config(config_path) | ||
| assert "quantization_config" in config_data |
Uh oh!
There was an error while loading. Please reload this page.