From 32b71bb237dc15f22c36d00b836f3a6b1799756d Mon Sep 17 00:00:00 2001 From: adrianlizarraga Date: Tue, 30 Jan 2024 11:37:51 -0800 Subject: [PATCH 1/3] Ensure QDQ models have the MS domain opset if using contrib q/dq ops --- onnxruntime/python/tools/quantization/qdq_quantizer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/onnxruntime/python/tools/quantization/qdq_quantizer.py b/onnxruntime/python/tools/quantization/qdq_quantizer.py index b0153aed766ad..123cfe913d6e2 100644 --- a/onnxruntime/python/tools/quantization/qdq_quantizer.py +++ b/onnxruntime/python/tools/quantization/qdq_quantizer.py @@ -270,6 +270,8 @@ def quantize_model(self): self.model.model.producer_name = __producer__ self.model.model.producer_version = __version__ + if self.qdq_op_domain == ms_domain: + self.model.set_opset_import(ms_domain, 1) return self.model.model From 2f17f18866960479f347126ee26df40e725b6bc9 Mon Sep 17 00:00:00 2001 From: adrianlizarraga Date: Tue, 30 Jan 2024 11:59:22 -0800 Subject: [PATCH 2/3] Add Test --- onnxruntime/test/python/quantization/test_qdq.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/onnxruntime/test/python/quantization/test_qdq.py b/onnxruntime/test/python/quantization/test_qdq.py index 4de797400836f..8504f0f90fa89 100644 --- a/onnxruntime/test/python/quantization/test_qdq.py +++ b/onnxruntime/test/python/quantization/test_qdq.py @@ -601,6 +601,13 @@ def verify_qdq(self, per_channel, activation_type, weight_type, extra_options=No ) check_model_correctness(self, model_fp32_path, model_qdq_path, data_reader.get_next()) + # If the model uses Q/DQ ops with "com.microsoft" domain (e.g., for int16 support), + # then ensure the model has the appropriate opset import. + if extra_options and extra_options.get("UseQDQContribOps", False): + qdq_model = onnx.load_model(model_qdq_path) + ms_opset = next((opset for opset in qdq_model.opset_import if opset.domain == "com.microsoft"), None) + self.assertTrue(ms_opset is not None) + def verify_qop(self, per_channel, is_quant_type_int8): np.random.seed(1) model_fp32_path = str(Path(self._tmp_model_dir.name) / f"conv_relu_fp32.{per_channel}.onnx") From df5bc55410b7cb9de1d98fd17a8c4bbb92f5a0e1 Mon Sep 17 00:00:00 2001 From: adrianlizarraga Date: Tue, 30 Jan 2024 12:51:57 -0800 Subject: [PATCH 3/3] Use assertIsNot() instead of assertTrue(a is not None) --- onnxruntime/test/python/quantization/test_qdq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/test/python/quantization/test_qdq.py b/onnxruntime/test/python/quantization/test_qdq.py index 8504f0f90fa89..223f405e8947a 100644 --- a/onnxruntime/test/python/quantization/test_qdq.py +++ b/onnxruntime/test/python/quantization/test_qdq.py @@ -606,7 +606,7 @@ def verify_qdq(self, per_channel, activation_type, weight_type, extra_options=No if extra_options and extra_options.get("UseQDQContribOps", False): qdq_model = onnx.load_model(model_qdq_path) ms_opset = next((opset for opset in qdq_model.opset_import if opset.domain == "com.microsoft"), None) - self.assertTrue(ms_opset is not None) + self.assertIsNot(ms_opset, None) def verify_qop(self, per_channel, is_quant_type_int8): np.random.seed(1)