Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ONNX] Added QLinearWhere from com.microsoft domain (#29544)

Vatsala Shanubhagf5ee14f81688
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.md
index 736ae3c485c..7031a2dc0c6 100644
--- a/src/frontends/onnx/docs/supported_ops.md
+++ b/src/frontends/onnx/docs/supported_ops.md
@@ -281,7 +281,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)
|com.microsoft |QLinearReduceMean |1 |1 | |
|com.microsoft |QLinearSigmoid |1 |1 | |
|com.microsoft |QLinearSoftmax |1 |1 | |
-|com.microsoft |QLinearWhere | |1 | |
+|com.microsoft |QLinearWhere |1 |1 | |
|com.microsoft |QMoE | |1 | |
|com.microsoft |QOrderedAttention | |1 | |
|com.microsoft |QOrderedGelu | |1 | |
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_where.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_where.cpp
new file mode 100644
index 00000000000..76708187ba2
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_where.cpp
@@ -0,0 +1,60 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+
+#include "core/operator_set.hpp"
+#include "exceptions.hpp"
+#include "openvino/frontend/exception.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/select.hpp"
+#include "openvino/op/subtract.hpp"
+#include "utils/common.hpp"
+
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace onnx {
+namespace com_microsoft {
+namespace opset_1 {
+
+ov::OutputVector qlinear_where(const ov::frontend::onnx::Node& node) {
+ common::default_op_checks(node, 9);
+
+ auto condition = node.get_ov_inputs().at(0);
+ auto x = node.get_ov_inputs().at(1);
+ auto x_scale = node.get_ov_inputs().at(2);
+ auto x_zero_point = node.get_ov_inputs().at(3);
+ auto y = node.get_ov_inputs().at(4);
+ auto y_scale = node.get_ov_inputs().at(5);
+ auto y_zero_point = node.get_ov_inputs().at(6);
+ auto z_scale = node.get_ov_inputs().at(7);
+ auto z_zero_point = node.get_ov_inputs().at(8);
+
+ auto x_minus_zero_point = std::make_shared<v1::Subtract>(x, x_zero_point);
+ auto y_minus_zero_point = std::make_shared<v1::Subtract>(y, y_zero_point);
+
+ auto x_minus_zero_point_float = std::make_shared<v0::Convert>(x_minus_zero_point, x_scale.get_element_type());
+ auto y_minus_zero_point_float = std::make_shared<v0::Convert>(y_minus_zero_point, y_scale.get_element_type());
+
+ auto x_dequant = std::make_shared<v1::Multiply>(x_scale, x_minus_zero_point_float);
+ auto y_dequant = std::make_shared<v1::Multiply>(y_scale, y_minus_zero_point_float);
+
+ auto selected = std::make_shared<v1::Select>(condition, x_dequant, y_dequant);
+
+ auto scaled_result = std::make_shared<v1::Divide>(selected, z_scale);
+ auto quantise_result = std::make_shared<v0::Convert>(scaled_result, x.get_element_type());
+ auto final_output = std::make_shared<v1::Add>(quantise_result, z_zero_point);
+
+ return {final_output};
+}
+
+ONNX_OP("QLinearWhere", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_where, MICROSOFT_DOMAIN);
+
+} // namespace opset_1
+} // namespace com_microsoft
+} // namespace onnx
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/onnx/tests/models/com.microsoft/qlinear_where_i8.prototxt b/src/frontends/onnx/tests/models/com.microsoft/qlinear_where_i8.prototxt
new file mode 100644
index 00000000000..2e171422297
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/qlinear_where_i8.prototxt
@@ -0,0 +1,150 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+producer_version: ""
+model_version: 0
+graph {
+ name: "test_qlinear_where"
+
+ node {
+ input: "condition"
+ input: "X"
+ input: "X_scale"
+ input: "X_zero_point"
+ input: "Y"
+ input: "Y_scale"
+ input: "Y_zero_point"
+ input: "Z_scale"
+ input: "Z_zero_point"
+ output: "Z"
+ op_type: "QLinearWhere"
+ domain: "com.microsoft"
+ }
+
+ input {
+ name: "condition"
+ type {
+ tensor_type {
+ elem_type: 9
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "X_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "X_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Y_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Y_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Z_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Z_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ output {
+ name: "Z"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+}
+
+opset_import {
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/com.microsoft/qlinear_where_u8.prototxt b/src/frontends/onnx/tests/models/com.microsoft/qlinear_where_u8.prototxt
new file mode 100644
index 00000000000..49775015e76
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/qlinear_where_u8.prototxt
@@ -0,0 +1,154 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+producer_version: ""
+model_version: 0
+graph {
+ name: "test_qlinear_where_uint8_large"
+
+ node {
+ input: "condition"
+ input: "X"
+ input: "X_scale"
+ input: "X_zero_point"
+ input: "Y"
+ input: "Y_scale"
+ input: "Y_zero_point"
+ input: "Z_scale"
+ input: "Z_zero_point"
+ output: "Z"
+ op_type: "QLinearWhere"
+ domain: "com.microsoft"
+ }
+
+ input {
+ name: "condition"
+ type {
+ tensor_type {
+ elem_type: 9
+ shape {
+ dim { dim_value: 4 }
+ dim { dim_value: 5 }
+ dim { dim_value: 6 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim { dim_value: 4 }
+ dim { dim_value: 5 }
+ dim { dim_value: 6 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "X_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "X_zero_point"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim { dim_value: 4 }
+ dim { dim_value: 5 }
+ dim { dim_value: 6 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Y_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Y_zero_point"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Z_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "Z_zero_point"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+
+ output {
+ name: "Z"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim { dim_value: 4 }
+ dim { dim_value: 5 }
+ dim { dim_value: 6 }
+ }
+ }
+ }
+ }
+}
+
+opset_import {
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
index bc3f9c33671..22a8cd03feb 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -2279,3 +2279,80 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinearsoftmax_opset12) {
test_case.add_expected_output<int8_t>(Shape{4, 5}, expected_output);
test_case.run();
}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_where_i8) {
+ const auto model = convert_model("com.microsoft/qlinear_where_i8.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ const std::vector<bool> condition = {true, false, true, false, true, false};
+ const std::vector<int8_t> X = {10, 20, 30, 40, 50, 60};
+ const std::vector<int8_t> Y = {100, 90, 80, 70, 60, 50};
+ const std::vector<float> x_scale = {0.1f};
+ const std::vector<int8_t> x_zero_point = {0};
+ const std::vector<float> y_scale = {0.2f};
+ const std::vector<int8_t> y_zero_point = {5};
+ const std::vector<float> z_scale = {0.15f};
+ const std::vector<int8_t> z_zero_point = {3};
+
+ const std::vector<int8_t> expected_output = {9, 116, 23, 89, 36, 63};
+
+ test_case.add_input<bool>(Shape{2, 3}, condition);
+ test_case.add_input<int8_t>(Shape{2, 3}, X);
+ test_case.add_input<float>(Shape{1}, x_scale);
+ test_case.add_input<int8_t>(Shape{1}, x_zero_point);
+ test_case.add_input<int8_t>(Shape{2, 3}, Y);
+ test_case.add_input<float>(Shape{1}, y_scale);
+ test_case.add_input<int8_t>(Shape{1}, y_zero_point);
+ test_case.add_input<float>(Shape{1}, z_scale);
+ test_case.add_input<int8_t>(Shape{1}, z_zero_point);
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_where_u8) {
+ const auto model = convert_model("com.microsoft/qlinear_where_u8.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ const std::vector<uint8_t> condition = {1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
+ 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1,
+ 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
+ 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0,
+ 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1};
+ const std::vector<uint8_t> X = {
+ 10, 20, 30, 40, 50, 60, 15, 25, 35, 45, 55, 65, 12, 22, 32, 42, 52, 62, 18, 28, 38, 48, 58, 68,
+ 70, 80, 90, 100, 110, 120, 75, 85, 95, 105, 115, 100, 72, 82, 92, 102, 112, 108, 78, 88, 98, 108, 118, 115,
+ 20, 30, 40, 50, 60, 70, 25, 35, 45, 55, 65, 75, 22, 32, 42, 52, 62, 72, 28, 38, 48, 58, 68, 78,
+ 80, 90, 100, 110, 120, 110, 85, 95, 105, 115, 100, 90, 82, 92, 102, 112, 108, 98, 88, 98, 108, 118, 115, 105,
+ 30, 40, 50, 60, 70, 80, 35, 45, 55, 65, 75, 85, 32, 42, 52, 62, 72, 82, 38, 48, 58, 68, 78, 88};
+
+ const std::vector<uint8_t> Y = {
+ 50, 40, 30, 20, 10, 5, 60, 55, 45, 35, 25, 15, 65, 55, 45, 35, 25, 15, 75, 65, 55, 45, 35, 25,
+ 100, 90, 80, 70, 60, 50, 110, 100, 90, 80, 70, 60, 120, 110, 100, 90, 80, 70, 105, 95, 85, 75, 65, 55,
+ 40, 30, 20, 10, 5, 3, 50, 40, 30, 20, 10, 8, 55, 45, 35, 25, 15, 12, 65, 55, 45, 35, 25, 22,
+ 90, 80, 70, 60, 50, 40, 100, 90, 80, 70, 60, 50, 110, 100, 90, 80, 70, 60, 95, 85, 75, 65, 55, 45,
+ 30, 20, 10, 5, 3, 2, 40, 30, 20, 10, 8, 6, 45, 35, 25, 15, 12, 10, 55, 45, 35, 25, 22, 20};
+
+ const std::vector<float> X_scale{0.1f};
+ const std::vector<uint8_t> X_zero_point{0};
+ const std::vector<float> Y_scale{0.1f};
+ const std::vector<uint8_t> Y_zero_point{0};
+ const std::vector<float> Z_scale{0.1f};
+ const std::vector<uint8_t> Z_zero_point{0};
+
+ std::vector<uint8_t> expected_output(X.size());
+ for (size_t i = 0; i < X.size(); i++) {
+ expected_output[i] = condition[i] ? X[i] : Y[i];
+ }
+
+ test_case.add_input<uint8_t>(Shape{4, 5, 6}, condition);
+ test_case.add_input<uint8_t>(Shape{4, 5, 6}, X);
+ test_case.add_input<float>(Shape{1}, X_scale);
+ test_case.add_input<uint8_t>(Shape{1}, X_zero_point);
+ test_case.add_input<uint8_t>(Shape{4, 5, 6}, Y);
+ test_case.add_input<float>(Shape{1}, Y_scale);
+ test_case.add_input<uint8_t>(Shape{1}, Y_zero_point);
+ test_case.add_input<float>(Shape{1}, Z_scale);
+ test_case.add_input<uint8_t>(Shape{1}, Z_zero_point);
+
+ test_case.add_expected_output<uint8_t>(Shape{4, 5, 6}, expected_output);
+ test_case.run();
+}