|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 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 | +import os |
| 17 | +import sys |
| 18 | +import tempfile |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import onnx |
| 22 | +import pytest |
| 23 | + |
| 24 | +from _test_utils.import_helper import skip_if_no_tensorrt, skip_if_no_trtexec |
| 25 | +import .models as _test_models |
| 26 | + |
| 27 | +from modelopt.onnx.quantization.autotune.workflows import ( |
| 28 | + init_benchmark_instance, |
| 29 | + region_pattern_autotuning_workflow, |
| 30 | +) |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def simple_conv_model(): |
| 34 | + """Simple ONNX model: Input -> Conv -> Relu -> Output. Created via models.py.""" |
| 35 | + return _test_models._create_simple_conv_onnx_model() |
| 36 | + |
| 37 | +@pytest.mark.parametrize("use_trtexec", [True, False]) |
| 38 | +def test_export_quantized_model(use_trtexec, simple_conv_model): |
| 39 | + """Test exporting quantized model with Q/DQ.""" |
| 40 | + if use_trtexec: |
| 41 | + skip_if_no_trtexec() |
| 42 | + else: |
| 43 | + skip_if_no_tensorrt() |
| 44 | + |
| 45 | + with tempfile.NamedTemporaryFile(suffix=".onnx", delete=False) as f: |
| 46 | + baseline_model_path = f.name |
| 47 | + |
| 48 | + # Save baseline model |
| 49 | + onnx.save(simple_conv_model, baseline_model_path) |
| 50 | + |
| 51 | + output_dir = baseline_model_path.strip(".onnx") |
| 52 | + output_path = output_dir + ".quant.onnx" |
| 53 | + |
| 54 | + try: |
| 55 | + init_benchmark_instance(use_trtexec=use_trtexec) |
| 56 | + autotuner = region_pattern_autotuning_workflow(baseline_model_path, Path(output_dir)) |
| 57 | + |
| 58 | + # Export model with Q/DQ insertion |
| 59 | + autotuner.export_onnx(output_path, insert_qdq=True) |
| 60 | + |
| 61 | + # Verify file was created |
| 62 | + assert os.path.exists(output_path) |
| 63 | + |
| 64 | + # Verify it's a valid ONNX model |
| 65 | + exported_model = onnx.load(output_path) |
| 66 | + assert exported_model is not None |
| 67 | + |
| 68 | + # Verify that it contains Q/DQ nodes |
| 69 | + qdq_nodes = [ |
| 70 | + n |
| 71 | + for n in exported_model.graph.node |
| 72 | + if n.op_type in ["QuantizeLinear", "DequantizeLinear"] |
| 73 | + ] |
| 74 | + assert qdq_nodes, "Q/DQ nodes not found in quantized model" |
| 75 | + |
| 76 | + print("✓ QDQAutotuner export quantized model") |
| 77 | + finally: |
| 78 | + if os.path.exists(output_path): |
| 79 | + os.unlink(output_path) |
| 80 | + |
0 commit comments