|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | +from typing import NamedTuple |
| 18 | + |
| 19 | +import pytest |
| 20 | +from _test_utils.examples.models import FLUX_SCHNELL_PATH, SDXL_1_0_PATH |
| 21 | +from _test_utils.examples.run_command import run_example_command |
| 22 | +from _test_utils.torch.misc import minimum_sm |
| 23 | + |
| 24 | + |
| 25 | +class DiffuserHfExportModel(NamedTuple): |
| 26 | + name: str |
| 27 | + path: str |
| 28 | + dtype: str |
| 29 | + format_type: str |
| 30 | + quant_algo: str |
| 31 | + collect_method: str |
| 32 | + |
| 33 | + def quantize_and_export_hf(self, tmp_path: Path) -> Path: |
| 34 | + hf_ckpt_dir = tmp_path / f"{self.name}_{self.format_type}_hf_ckpt" |
| 35 | + cmd_args = [ |
| 36 | + "python", |
| 37 | + "quantize.py", |
| 38 | + "--model", |
| 39 | + self.name, |
| 40 | + "--override-model-path", |
| 41 | + self.path, |
| 42 | + "--calib-size", |
| 43 | + "8", |
| 44 | + "--batch-size", |
| 45 | + "2", |
| 46 | + "--n-steps", |
| 47 | + "20", |
| 48 | + "--percentile", |
| 49 | + "1.0", |
| 50 | + "--alpha", |
| 51 | + "0.8", |
| 52 | + "--format", |
| 53 | + self.format_type, |
| 54 | + "--quant-algo", |
| 55 | + self.quant_algo, |
| 56 | + "--collect-method", |
| 57 | + self.collect_method, |
| 58 | + "--trt-high-precision-dtype", |
| 59 | + self.dtype, |
| 60 | + "--hf-ckpt-dir", |
| 61 | + str(hf_ckpt_dir), |
| 62 | + ] |
| 63 | + run_example_command(cmd_args, "diffusers/quantization") |
| 64 | + return hf_ckpt_dir |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "model", |
| 69 | + [ |
| 70 | + DiffuserHfExportModel( |
| 71 | + name="sdxl-1.0", |
| 72 | + path=SDXL_1_0_PATH, |
| 73 | + dtype="Half", |
| 74 | + format_type="int8", |
| 75 | + quant_algo="smoothquant", |
| 76 | + collect_method="min-mean", |
| 77 | + ), |
| 78 | + DiffuserHfExportModel( |
| 79 | + name="flux-schnell", |
| 80 | + path=FLUX_SCHNELL_PATH, |
| 81 | + dtype="BFloat16", |
| 82 | + format_type="int8", |
| 83 | + quant_algo="smoothquant", |
| 84 | + collect_method="min-mean", |
| 85 | + ), |
| 86 | + pytest.param( |
| 87 | + DiffuserHfExportModel( |
| 88 | + name="sdxl-1.0", |
| 89 | + path=SDXL_1_0_PATH, |
| 90 | + dtype="Half", |
| 91 | + format_type="fp8", |
| 92 | + quant_algo="max", |
| 93 | + collect_method="default", |
| 94 | + ), |
| 95 | + marks=minimum_sm(89), |
| 96 | + ), |
| 97 | + pytest.param( |
| 98 | + DiffuserHfExportModel( |
| 99 | + name="flux-schnell", |
| 100 | + path=FLUX_SCHNELL_PATH, |
| 101 | + dtype="BFloat16", |
| 102 | + format_type="fp4", |
| 103 | + quant_algo="max", |
| 104 | + collect_method="default", |
| 105 | + ), |
| 106 | + marks=minimum_sm(89), |
| 107 | + ), |
| 108 | + ], |
| 109 | + ids=[ |
| 110 | + "sdxl_1.0_int8_smoothquant_min_mean", |
| 111 | + "flux_schnell_int8_smoothquant_min_mean", |
| 112 | + "sdxl_1.0_fp8_max_default", |
| 113 | + "flux_schnell_fp4_max_default", |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_diffusers_hf_ckpt_export(model: DiffuserHfExportModel, tmp_path: Path) -> None: |
| 117 | + hf_ckpt_dir = model.quantize_and_export_hf(tmp_path) |
| 118 | + |
| 119 | + assert hf_ckpt_dir.exists(), f"HF checkpoint directory was not created: {hf_ckpt_dir}" |
| 120 | + |
| 121 | + config_files = list(hf_ckpt_dir.rglob("config.json")) |
| 122 | + assert len(config_files) > 0, f"No config.json found in {hf_ckpt_dir}" |
| 123 | + |
| 124 | + weight_files = list(hf_ckpt_dir.rglob("*.safetensors")) + list(hf_ckpt_dir.rglob("*.bin")) |
| 125 | + assert len(weight_files) > 0, f"No weight files (.safetensors or .bin) found in {hf_ckpt_dir}" |
0 commit comments