mirror of
https://github.com/NVIDIA/Model-Optimizer.git
synced 2026-11-08 09:03:20 +08:00
## What does this PR do?
**Type of change:** new tests, Bug fix <!-- Use one of the following:
Bug fix, new feature, new example, new tests, documentation. -->
**Overview:**
- **Fixed the INT8 config issue**
- **Add HF checkpoint export test coverage**
1. The `--hf-ckpt-dir` export path had zero test coverage. This MR adds
tests at two levels:
2. Unit tests (tests/unit/torch/export/test_export_diffusers.py):
- Extended test_export_diffusers_real_quantized to parametrize over
INT8, INT8 SmoothQuant, FP8, and FP4 configs
- (previously only FP8). This gives 3 models x 4 configs = 12 test
cases.
3. GPU integration tests
(tests/gpu/torch/export/test_export_diffusers_hf_ckpt.py)
- New file testing the full quantize.py --hf-ckpt-dir pipeline via
subprocess with 4 combos:
- SDXL INT8 smoothquant min-mean (the exact scenario that triggered the
bug)
- Flux INT8 smoothquant min-mean
- SDXL FP8
- Flux FP4
## Usage
<!-- You can potentially add a usage example below. -->
```python
# Add a code snippet demonstrating how to use this
```
## 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?**:No <!--- If No, explain why.
-->
- **Did you write any new necessary tests?**: Yes
- **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
## Release Notes
* **Tests**
* Added test coverage for exporting Diffusers models with Hugging Face
checkpoints across multiple quantization formats (INT8, FP8, FP4)
* Extended quantization export testing to validate multiple
configuration scenarios
* **Chores**
* Refined INT8 quantization configuration with improved calibrator
support for convolution layers
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Signed-off-by: Jingyu Xin <jingyux@nvidia.com>
145 lines
4.7 KiB
Python
145 lines
4.7 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 torch.nn as nn
|
|
from calib.plugin_calib import PercentileCalibrator
|
|
|
|
FP8_DEFAULT_CONFIG = {
|
|
"quant_cfg": {
|
|
"*weight_quantizer": {"num_bits": (4, 3), "axis": None},
|
|
"*input_quantizer": {"num_bits": (4, 3), "axis": None},
|
|
"*output_quantizer": {"enable": False},
|
|
"*softmax_quantizer": {
|
|
"num_bits": (4, 3),
|
|
"axis": None,
|
|
},
|
|
"default": {"enable": False},
|
|
},
|
|
"algorithm": "max",
|
|
}
|
|
|
|
INT8_DEFAULT_CONFIG = {
|
|
"quant_cfg": {
|
|
"*weight_quantizer": {"num_bits": 8, "axis": 0},
|
|
"*input_quantizer": {"num_bits": 8, "axis": None},
|
|
"*output_quantizer": {"enable": False},
|
|
"default": {"enable": False},
|
|
},
|
|
"algorithm": "max",
|
|
}
|
|
|
|
NVFP4_DEFAULT_CONFIG = {
|
|
"quant_cfg": {
|
|
"*weight_quantizer": {
|
|
"num_bits": (2, 1),
|
|
"block_sizes": {-1: 16, "type": "dynamic", "scale_bits": (4, 3)},
|
|
"axis": None,
|
|
"enable": True,
|
|
},
|
|
"*input_quantizer": {
|
|
"num_bits": (2, 1),
|
|
"block_sizes": {-1: 16, "type": "dynamic", "scale_bits": (4, 3)},
|
|
"axis": None,
|
|
"enable": True,
|
|
},
|
|
"*output_quantizer": {"enable": False},
|
|
"*softmax_quantizer": {
|
|
"num_bits": (4, 3),
|
|
"axis": None,
|
|
},
|
|
"default": {"enable": False},
|
|
},
|
|
"algorithm": "max",
|
|
}
|
|
|
|
NVFP4_FP8_MHA_CONFIG = {
|
|
"quant_cfg": {
|
|
"**weight_quantizer": {
|
|
"num_bits": (2, 1),
|
|
"block_sizes": {-1: 16, "type": "dynamic", "scale_bits": (4, 3)},
|
|
"axis": None,
|
|
"enable": True,
|
|
},
|
|
"**input_quantizer": {
|
|
"num_bits": (2, 1),
|
|
"block_sizes": {-1: 16, "type": "dynamic", "scale_bits": (4, 3)},
|
|
"axis": None,
|
|
"enable": True,
|
|
},
|
|
"*output_quantizer": {"enable": False},
|
|
"*[qkv]_bmm_quantizer": {
|
|
"num_bits": (4, 3),
|
|
"axis": None,
|
|
},
|
|
"*softmax_quantizer": {
|
|
"num_bits": (4, 3),
|
|
"axis": None,
|
|
},
|
|
"*bmm2_output_quantizer": {
|
|
"num_bits": (4, 3),
|
|
"axis": None,
|
|
},
|
|
"default": {"enable": False},
|
|
},
|
|
"algorithm": {"method": "svdquant", "lowrank": 32},
|
|
}
|
|
|
|
|
|
def set_quant_config_attr(quant_config, trt_high_precision_dtype, quant_algo, **kwargs):
|
|
algo_cfg = {"method": quant_algo}
|
|
|
|
if quant_algo == "smoothquant" and "alpha" in kwargs:
|
|
algo_cfg["alpha"] = kwargs["alpha"]
|
|
elif quant_algo == "svdquant" and "lowrank" in kwargs:
|
|
algo_cfg["lowrank"] = kwargs["lowrank"]
|
|
quant_config["algorithm"] = algo_cfg
|
|
|
|
for p in quant_config["quant_cfg"].values():
|
|
if "num_bits" in p and "trt_high_precision_dtype" not in p:
|
|
p["trt_high_precision_dtype"] = trt_high_precision_dtype
|
|
|
|
|
|
def reset_set_int8_config(quant_config, percentile, n_steps, collect_method, backbone):
|
|
"""Add PercentileCalibrator to Conv2d input quantizers.
|
|
|
|
Linear layers are left unchanged — their axis settings come from the base
|
|
quant_config (e.g. INT8_SMOOTHQUANT_CFG or INT8_DEFAULT_CONFIG).
|
|
|
|
Args:
|
|
quant_config: The quantization configuration dictionary
|
|
percentile: Percentile value for calibration
|
|
n_steps: Number of calibration steps
|
|
collect_method: Method for collecting calibration statistics
|
|
backbone: The model backbone to analyze layer types
|
|
"""
|
|
for name, module in backbone.named_modules():
|
|
if isinstance(module, nn.Conv2d):
|
|
aq_name = f"*{name}*input_quantizer*"
|
|
quant_config["quant_cfg"][aq_name] = {
|
|
"num_bits": 8,
|
|
"axis": None,
|
|
"calibrator": (
|
|
PercentileCalibrator,
|
|
(),
|
|
{
|
|
"num_bits": 8,
|
|
"axis": None,
|
|
"percentile": percentile,
|
|
"total_step": n_steps,
|
|
"collect_method": collect_method,
|
|
},
|
|
),
|
|
}
|