Skip to content

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

[ONNX] Added QLinear add and mul from com.microsoft domain (#28288)

Vatsala Shanubhagd5b2966e532b
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_ops.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_ops.cpp
new file mode 100644
index 00000000000..92908bef251
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_ops.cpp
@@ -0,0 +1,106 @@
+// 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/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/reshape.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 {
+
+template <typename BinaryOp>
+ov::OutputVector qlinear_op(const ov::frontend::onnx::Node& node, BinaryOp binary_op) {
+ common::default_op_checks(node, 7);
+
+ const auto inputs = node.get_ov_inputs();
+ auto A = inputs[0];
+ auto A_scale = inputs[1];
+ auto A_zero_point =
+ (inputs[2].get_shape().empty()) ? v0::Constant::create(A.get_element_type(), {}, {0}) : inputs[2];
+
+ auto B = inputs[3];
+ auto B_scale = inputs[4];
+ auto B_zero_point =
+ (inputs[5].get_shape().empty()) ? v0::Constant::create(A.get_element_type(), {}, {0}) : inputs[5];
+
+ auto C_scale = inputs[6];
+
+ auto C_zero_point = inputs.size() > 7 ? inputs[7] : v0::Constant::create(C_scale.get_element_type(), {}, {0});
+
+ CHECK_VALID_NODE(
+ node,
+ (A.get_element_type() == element::i8 || A.get_element_type() == element::u8) &&
+ (B.get_element_type() == element::i8 || B.get_element_type() == element::u8) &&
+ (A_zero_point.get_element_type() == element::i8 || A_zero_point.get_element_type() == element::u8) &&
+ (B_zero_point.get_element_type() == element::i8 || B_zero_point.get_element_type() == element::u8),
+ "All inputs (A, B, A_zero_point, B_zero_point) must be either int8 or uint8. Got A: ",
+ A.get_element_type(),
+ ", B: ",
+ B.get_element_type(),
+ ", A_zero_point: ",
+ A_zero_point.get_element_type(),
+ ", B_zero_point: ",
+ B_zero_point.get_element_type());
+
+ auto A_minus_zero_point = std::make_shared<v1::Subtract>(A, A_zero_point);
+ auto B_minus_zero_point = std::make_shared<v1::Subtract>(B, B_zero_point);
+
+ auto A_minus_zero_point_float = std::make_shared<v0::Convert>(A_minus_zero_point, A_scale.get_element_type());
+ auto B_minus_zero_point_float = std::make_shared<v0::Convert>(B_minus_zero_point, B_scale.get_element_type());
+
+ auto A_scaled = std::make_shared<v1::Multiply>(A_scale, A_minus_zero_point_float);
+ auto B_scaled = std::make_shared<v1::Multiply>(B_scale, B_minus_zero_point_float);
+
+ auto result_scaled = binary_op(A_scaled, B_scaled);
+
+ auto result_divided = std::make_shared<v1::Divide>(result_scaled, C_scale);
+
+ auto C_zero_point_float = std::make_shared<v0::Convert>(C_zero_point, C_scale.get_element_type());
+
+ auto C_float = std::make_shared<v1::Add>(result_divided, C_zero_point_float);
+ auto C = std::make_shared<v0::Convert>(C_float, A.get_element_type());
+
+ return ov::OutputVector{C};
+}
+
+ov::OutputVector qlinear_add(const ov::frontend::onnx::Node& node) {
+ // Original documentation:
+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftqlinearadd
+ // C = (A_scale * (A - A_zero_point) + B_scale * (B - B_zero_point))/C_scale + C_zero_point
+ return qlinear_op(node, [](auto a, auto b) {
+ return std::make_shared<v1::Add>(a, b);
+ });
+}
+
+ov::OutputVector qlinear_mul(const ov::frontend::onnx::Node& node) {
+ // Original documentation:
+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftqlinearmul
+ // C = (A_scale * (A - A_zero_point) * B_scale * (B - B_zero_point))/C_scale + C_zero_point
+ return qlinear_op(node, [](auto a, auto b) {
+ return std::make_shared<v1::Multiply>(a, b);
+ });
+}
+
+namespace {
+ONNX_OP("QLinearAdd", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_add, MICROSOFT_DOMAIN);
+}
+ONNX_OP("QLinearMul", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_mul, MICROSOFT_DOMAIN);
+} // namespace opset_1
+} // namespace com_microsoft
+} // namespace onnx
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/onnx/tests/models/com.microsoft/q_linear_add.prototxt b/src/frontends/onnx/tests/models/com.microsoft/q_linear_add.prototxt
new file mode 100644
index 00000000000..606b09ca12d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/q_linear_add.prototxt
@@ -0,0 +1,159 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+producer_version: ""
+model_version: 0
+graph {
+ name: "test_qlinear_add"
+ node {
+ input: "A"
+ input: "A_scale"
+ input: "A_zero_point"
+ input: "B"
+ input: "B_scale"
+ input: "B_zero_point"
+ input: "C_scale"
+ input: "C_zero_point"
+ output: "C"
+ op_type: "QLinearAdd"
+ domain: "com.microsoft"
+ }
+
+ input {
+ name: "A"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "A_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "A_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "B"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "B_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "B_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "C_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "C_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3 #
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ output {
+ name: "C"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+
+opset_import {
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/com.microsoft/q_linear_mul.prototxt b/src/frontends/onnx/tests/models/com.microsoft/q_linear_mul.prototxt
new file mode 100644
index 00000000000..4b12e765c9d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/q_linear_mul.prototxt
@@ -0,0 +1,159 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+producer_version: ""
+model_version: 0
+graph {
+ name: "test_qlinear_mul"
+ node {
+ input: "A"
+ input: "A_scale"
+ input: "A_zero_point"
+ input: "B"
+ input: "B_scale"
+ input: "B_zero_point"
+ input: "C_scale"
+ input: "C_zero_point"
+ output: "C"
+ op_type: "QLinearMul"
+ domain: "com.microsoft"
+ }
+
+ input {
+ name: "A"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "A_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "A_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "B"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "B_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "B_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "C_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ input {
+ name: "C_zero_point"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+
+ output {
+ name: "C"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+
+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 eef8758599b..1b07401cce0 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -1553,3 +1553,63 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmul_integer_to_float) {
test_case.run();
}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_add) {
+ const auto model = convert_model("com.microsoft/q_linear_add.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ const std::vector<int8_t> data_A{10, 20, 30, 40};
+ const std::vector<int8_t> data_B{1, 2, 3, 4};
+
+ const std::vector<float> a_scale{0.1f};
+ const std::vector<float> b_scale{0.2f};
+ const std::vector<float> c_scale{0.3f};
+
+ const std::vector<int8_t> a_zero_point{5};
+ const std::vector<int8_t> b_zero_point{3};
+ const std::vector<int8_t> c_zero_point{2};
+
+ const std::vector<int8_t> expected_output{2, 6, 10, 14};
+
+ test_case.add_input<int8_t>(Shape{2, 2}, data_A);
+ test_case.add_input<float>(Shape{1}, a_scale);
+ test_case.add_input<int8_t>(Shape{1}, a_zero_point);
+ test_case.add_input<int8_t>(Shape{2, 2}, data_B);
+ test_case.add_input<float>(Shape{1}, b_scale);
+ test_case.add_input<int8_t>(Shape{1}, b_zero_point);
+ test_case.add_input<float>(Shape{1}, c_scale);
+ test_case.add_input<int8_t>(Shape{1}, c_zero_point);
+
+ test_case.add_expected_output<int8_t>(Shape{2, 2}, expected_output);
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_mul) {
+ const auto model = convert_model("com.microsoft/q_linear_mul.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ const std::vector<int8_t> data_A{10, 20, 30, 40};
+ const std::vector<int8_t> data_B{2, 3, 4, 5};
+
+ const std::vector<float> a_scale{0.1f};
+ const std::vector<float> b_scale{0.2f};
+ const std::vector<float> c_scale{0.3f};
+
+ const std::vector<int8_t> a_zero_point{5};
+ const std::vector<int8_t> b_zero_point{3};
+ const std::vector<int8_t> c_zero_point{2};
+
+ const std::vector<int8_t> expected_output{1, 2, 3, 6};
+
+ test_case.add_input<int8_t>(Shape{2, 2}, data_A);
+ test_case.add_input<float>(Shape{1}, a_scale);
+ test_case.add_input<int8_t>(Shape{1}, a_zero_point);
+ test_case.add_input<int8_t>(Shape{2, 2}, data_B);
+ test_case.add_input<float>(Shape{1}, b_scale);
+ test_case.add_input<int8_t>(Shape{1}, b_zero_point);
+ test_case.add_input<float>(Shape{1}, c_scale);
+ test_case.add_input<int8_t>(Shape{1}, c_zero_point);
+
+ test_case.add_expected_output<int8_t>(Shape{2, 2}, expected_output);
+ test_case.run();
+}