Files
Model-Optimizer/tests/gpu/torch/quantization/test_quantize_cuda.py
T
realAsma ac30686c82 Track global_amax for weight FP4 MSE sweep; Refactor to NVFP4StaticQantizer, NVFP4MSECalibrator (#849)
## What does this PR do?

**Type of change:** ? <!-- Use one of the following: Bug fix, new
feature, new example, new tests, documentation. -->

**Overview:** ?

## 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?**: Yes/No <!--- If No, explain
why. -->
- **Did you write any new necessary tests?**: Yes/No
- **Did you add or update any necessary documentation?**: Yes/No
- **Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?**:
Yes/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

* **New Features**
* Added NVFP4StaticQuantizer for improved 4-bit quantization with
enhanced precision control
* Introduced NVFP4MSECalibrator with flexible candidate generation for
calibration optimization

* **Improvements**
* Optimized GPU kernels for Hopper+ graphics cards with better
performance
  * Extended Triton support to broader GPU compatibility
* Enhanced backward compatibility for restoring previously quantized
models

* **Tests**
* Added comprehensive test coverage for new quantizers and calibration
methods

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: realAsma <akuriparambi@nvidia.com>
2026-02-06 19:47:36 +00:00

143 lines
4.5 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.
"""High-level tests for quantization."""
import pytest
from _test_utils.torch.quantization.models import SimpleConv, SimpleConvLinear, SimpleLinear
from _test_utils.torch.quantization.quantize_common import (
FP4_SVDQUANT_CFG,
INT4_AWQ_CLIP_CFG,
INT4_AWQ_FULL_CFG,
quantize_model_and_forward,
save_restore_test,
)
import modelopt.torch.quantization as mtq
from modelopt.torch.quantization.extensions import get_cuda_ext_mx
NVFP4_WEIGHT_ACT_MSE_CFG = {
"quant_cfg": {
"*weight_quantizer": {
"num_bits": (2, 1),
"block_sizes": {-1: 16, "type": "static", "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,
},
},
"algorithm": {
"method": "mse",
"step_size": 0.25,
"start_multiplier": 0.25,
"stop_multiplier": 2.0,
},
}
NVFP4_WEIGHT_MSE_FP8_SWEEP_CFG = {
"quant_cfg": {
"*weight_quantizer": {
"num_bits": (2, 1),
"block_sizes": {-1: 16, "type": "static", "scale_bits": (4, 3)},
"axis": None,
"enable": True,
},
"*input_quantizer": {
"enable": False,
},
},
"algorithm": {
"method": "mse",
"fp8_scale_sweep": True,
},
}
@pytest.mark.parametrize("model_cls", [SimpleLinear, SimpleConv, SimpleConvLinear])
@pytest.mark.parametrize(
"config",
[
mtq.INT8_DEFAULT_CFG,
mtq.FP8_DEFAULT_CFG,
mtq.W4A8_AWQ_BETA_CFG,
mtq.INT8_SMOOTHQUANT_CFG,
mtq.INT4_BLOCKWISE_WEIGHT_ONLY_CFG,
mtq.INT4_AWQ_CFG,
INT4_AWQ_CLIP_CFG,
INT4_AWQ_FULL_CFG,
mtq.NVFP4_DEFAULT_CFG,
FP4_SVDQUANT_CFG,
mtq.NVFP4_AWQ_LITE_CFG,
mtq.NVFP4_AWQ_CLIP_CFG,
mtq.NVFP4_AWQ_FULL_CFG,
mtq.MXFP8_DEFAULT_CFG,
mtq.MXFP6_DEFAULT_CFG,
mtq.MXFP4_DEFAULT_CFG,
mtq.MXINT8_DEFAULT_CFG,
mtq.NVFP4_KV_ROTATE_CFG,
mtq.FP8_2D_BLOCKWISE_WEIGHT_ONLY_CFG,
NVFP4_WEIGHT_ACT_MSE_CFG,
NVFP4_WEIGHT_MSE_FP8_SWEEP_CFG,
],
)
def test_quantize(model_cls, config):
"""Test quantize function can run without problems."""
if config in [
mtq.NVFP4_DEFAULT_CFG,
FP4_SVDQUANT_CFG,
mtq.NVFP4_AWQ_LITE_CFG,
mtq.NVFP4_AWQ_CLIP_CFG,
mtq.NVFP4_AWQ_FULL_CFG,
mtq.MXFP8_DEFAULT_CFG,
mtq.MXFP6_DEFAULT_CFG,
mtq.MXFP4_DEFAULT_CFG,
mtq.MXINT8_DEFAULT_CFG,
mtq.NVFP4_KV_ROTATE_CFG,
mtq.FP8_2D_BLOCKWISE_WEIGHT_ONLY_CFG,
NVFP4_WEIGHT_ACT_MSE_CFG,
NVFP4_WEIGHT_MSE_FP8_SWEEP_CFG,
]:
if get_cuda_ext_mx() is None:
pytest.skip("cuda_ext_mx is not available")
if model_cls in [SimpleConv, SimpleConvLinear]:
pytest.skip("Conv weight quantization will fail as the kernel_size < FP4 blocksize")
if config == mtq.FP8_2D_BLOCKWISE_WEIGHT_ONLY_CFG:
# reduce block sizes for simple testing models
config["quant_cfg"]["*weight_quantizer"]["block_sizes"] = {-1: 8, -2: 8}
model = model_cls().cuda()
calib_data = [model.get_input().cuda() for _ in range(8)]
quantize_model_and_forward(model, config, calib_data)
@pytest.mark.parametrize(
("model_cls", "quant_config"),
[
(SimpleLinear, mtq.INT8_SMOOTHQUANT_CFG),
(SimpleLinear, mtq.W4A8_AWQ_BETA_CFG),
(SimpleConvLinear, mtq.INT8_DEFAULT_CFG),
(SimpleLinear, NVFP4_WEIGHT_MSE_FP8_SWEEP_CFG),
(SimpleLinear, NVFP4_WEIGHT_ACT_MSE_CFG),
],
)
def test_save_restore(model_cls, quant_config):
test_cpu_restore = quant_config == mtq.INT8_SMOOTHQUANT_CFG
save_restore_test(model_cls, "cuda", quant_config, test_cpu_restore=test_cpu_restore)