OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[ONNX] Added QLinear Sigmoid and LeakyRelu from com.microsoft domain (#28287)
Vatsala Shanubhag4e6617cdde31
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_activation.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_activation.cppnew file mode 100644index 00000000000..3702d80c79e--- /dev/null+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_activation.cpp@@ -0,0 +1,92 @@+// 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/maximum.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/sigmoid.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 ActivationType>+ov::OutputVector qlinear_activation(const ov::frontend::onnx::Node& node, const ActivationType& activation_fn) {+ common::default_op_checks(node, 5);++ const auto inputs = node.get_ov_inputs();+ auto input_tensor = inputs[0];+ auto input_scale = inputs[1];+ auto input_zero_point =+ (inputs[2].get_shape().empty()) ? v0::Constant::create(input_tensor.get_element_type(), {}, {0}) : inputs[2];+ auto output_scale = inputs[3];+ auto output_zero_point =+ (inputs.size() > 4) ? inputs[4] : v0::Constant::create(input_tensor.get_element_type(), {}, {0});++ CHECK_VALID_NODE(node,+ (input_tensor.get_element_type() == element::i8 || input_tensor.get_element_type() == element::u8),+ "Input tensor must be either int8 or uint8. Got: ",+ input_tensor.get_element_type());++ auto input_subtracted = std::make_shared<v1::Subtract>(input_tensor, input_zero_point);+ auto input_dequantized =+ std::make_shared<v1::Multiply>(std::make_shared<v0::Convert>(input_subtracted, input_scale.get_element_type()),+ input_scale);++ auto activation_result = activation_fn(input_dequantized);++ auto scaled_result_float = std::make_shared<v1::Divide>(activation_result, output_scale);+ auto quantized_result =+ std::make_shared<v1::Add>(std::make_shared<v0::Convert>(scaled_result_float, input_tensor.get_element_type()),+ output_zero_point);++ return ov::OutputVector{quantized_result};+}++ov::OutputVector qlinear_sigmoid(const ov::frontend::onnx::Node& node) {+ // Original documentation:+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftqlinearsigmoid+ // f(x) = quantize(Sigmoid(dequantize(x)))++ return qlinear_activation(node, [](const std::shared_ptr<ov::Node>& input_dequantized) {+ return std::make_shared<v0::Sigmoid>(input_dequantized);+ });+}++ov::OutputVector qlinear_leaky_relu(const ov::frontend::onnx::Node& node) {+ // Original documentation:+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftqlinearleakyrelu+ // f(x) = quantize(alpha * dequantize(x)) for x < 0,+ // quantize(dequantize(x)) for x >= 0++ return qlinear_activation(node, [&](const std::shared_ptr<ov::Node>& input_dequantized) {+ auto alpha =+ v0::Constant::create(input_dequantized->get_element_type(), {}, {node.get_attribute_value<float>("alpha")});+ return std::make_shared<v1::Maximum>(input_dequantized,+ std::make_shared<v1::Multiply>(input_dequantized, alpha));+ });+}++namespace {+ONNX_OP("QLinearSigmoid", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_sigmoid, MICROSOFT_DOMAIN);+}+ONNX_OP("QLinearLeakyRelu", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_leaky_relu, MICROSOFT_DOMAIN);++} // namespace opset_1+} // namespace com_microsoft+} // namespace onnx+} // namespace frontend+} // namespace ovdiff --git a/src/frontends/onnx/tests/models/com.microsoft/q_linear_leaky_relu.prototxt b/src/frontends/onnx/tests/models/com.microsoft/q_linear_leaky_relu.prototxtnew file mode 100644index 00000000000..9ae6e1388eb--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/q_linear_leaky_relu.prototxt@@ -0,0 +1,101 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+producer_version: ""+model_version: 0+graph {+ name: "test_qlinear_leakyrelu"++ node {+ input: "X"+ input: "X_scale"+ input: "X_zero_point"+ input: "Y_scale"+ input: "Y_zero_point"+ output: "Y"+ op_type: "QLinearLeakyRelu"+ attribute {+ name: "alpha"+ f: 0.1+ type: FLOAT+ }+ domain: "com.microsoft"+ }++ 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 # float+ shape {+ dim { dim_value: 1 }+ }+ }+ }+ }++ input {+ name: "X_zero_point"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim { dim_value: 1 }+ }+ }+ }+ }++ 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 }+ }+ }+ }+ }++ output {+ name: "Y"+ 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/q_linear_sigmoid.prototxt b/src/frontends/onnx/tests/models/com.microsoft/q_linear_sigmoid.prototxtnew file mode 100644index 00000000000..17f7b7872bd--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/q_linear_sigmoid.prototxt@@ -0,0 +1,112 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+producer_version: ""+model_version: 0+graph {+ name: "test_qlinear_sigmoid"++ node {+ input: "X"+ input: "X_scale"+ input: "X_zero_point"+ input: "Y_scale"+ input: "Y_zero_point"+ output: "Y"+ op_type: "QLinearSigmoid"+ domain: "com.microsoft"+ }++ 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_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+ }+ }+ }+ }+ }++ output {+ name: "Y"+ 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/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cppindex 1b07401cce0..246315a9e03 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -1554,6 +1554,52 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmul_integer_to_float) {test_case.run();}+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinearsigmoid) {+ const auto model = convert_model("com.microsoft/q_linear_sigmoid.onnx");+ auto test_case = ov::test::TestCase(model, s_device);++ const std::vector<int8_t> data_X{-50, -25, 0, 25, 50, 75};++ 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{0};++ const std::vector<int8_t> expected_output{0, 0, 2, 4, 4, 4};++ test_case.add_input<int8_t>(Shape{2, 3}, data_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<float>(Shape{1}, y_scale);+ test_case.add_input<int8_t>(Shape{1}, y_zero_point);++ test_case.add_expected_output<int8_t>(Shape{2, 3}, expected_output);+ test_case.run();+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinearleakyrelu) {+ const auto model = convert_model("com.microsoft/q_linear_leaky_relu.onnx");+ auto test_case = ov::test::TestCase(model, s_device);++ const std::vector<int8_t> data_X{-50, -25, 0, 25, 50, 75};++ 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{0};++ const std::vector<int8_t> expected_output{-2, -1, 0, 12, 25, 37};++ test_case.add_input<int8_t>(Shape{2, 3}, data_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<float>(Shape{1}, y_scale);+ test_case.add_input<int8_t>(Shape{1}, y_zero_point);++ test_case.add_expected_output<int8_t>(Shape{2, 3}, expected_output);+ 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);