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 QLinearAveragePool from com.microsoft domain (#28799)
Vatsala Shanubhage58f38fac970
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.cppindex 3702d80c79e..850c24fab2c 100644--- a/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_activation.cpp+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/qlinear_activation.cpp@@ -6,6 +6,7 @@#include "exceptions.hpp"#include "openvino/frontend/exception.hpp"#include "openvino/op/add.hpp"+#include "openvino/op/avg_pool.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/divide.hpp"@@ -57,21 +58,12 @@ ov::OutputVector qlinear_activation(const ov::frontend::onnx::Node& node, const}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")});@@ -80,10 +72,44 @@ ov::OutputVector qlinear_leaky_relu(const ov::frontend::onnx::Node& node) {});}-namespace {-ONNX_OP("QLinearSigmoid", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_sigmoid, MICROSOFT_DOMAIN);+ov::OutputVector qlinear_avg_pool(const ov::frontend::onnx::Node& node) {+ return qlinear_activation(node, [&](const std::shared_ptr<ov::Node>& input_dequantized) {+ const auto kernel_shape = node.get_attribute_value<std::vector<int64_t>>("kernel_shape");+ auto strides = node.get_attribute_value<std::vector<int64_t>>("strides", std::vector<int64_t>{1});+ auto pads = node.get_attribute_value<std::vector<int64_t>>("pads", std::vector<int64_t>{0});+ const auto ceil_mode = node.get_attribute_value<int64_t>("ceil_mode", 0);+ const auto count_include_pad = node.get_attribute_value<int64_t>("count_include_pad", 0);+ const auto auto_pad = node.get_attribute_value<std::string>("auto_pad", "NOTSET");++ const auto input_rank = input_dequantized->get_shape().size();+ const size_t num_spatial_dims = input_rank - 2;++ pads.resize(num_spatial_dims * 2, pads.size() == 1 ? pads[0] : 0);+ strides.resize(num_spatial_dims, strides.size() == 1 ? strides[0] : 1);++ auto avg_pool = std::make_shared<v1::AvgPool>(input_dequantized,+ Strides(strides.begin(), strides.end()),+ Shape(pads.begin(), pads.begin() + num_spatial_dims),+ Shape(pads.begin() + num_spatial_dims, pads.end()),+ Shape(kernel_shape.begin(), kernel_shape.end()),+ count_include_pad == 0,+ ceil_mode != 0 ? RoundingType::CEIL : RoundingType::FLOOR,+ auto_pad == "SAME_UPPER" ? PadType::SAME_UPPER+ : auto_pad == "SAME_LOWER" ? PadType::SAME_LOWER+ : PadType::EXPLICIT);++ return avg_pool;+ });}-ONNX_OP("QLinearLeakyRelu", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_leaky_relu, MICROSOFT_DOMAIN);++bool register_multiple_operators(void) {+ ONNX_OP_M("QLinearSigmoid", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_sigmoid, MICROSOFT_DOMAIN);+ ONNX_OP_M("QLinearLeakyRelu", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_leaky_relu, MICROSOFT_DOMAIN);+ ONNX_OP_M("QLinearAveragePool", OPSET_SINCE(1), com_microsoft::opset_1::qlinear_avg_pool, MICROSOFT_DOMAIN);+ return true;+}++static bool registered = register_multiple_operators();} // namespace opset_1} // namespace com_microsoftdiff --git a/src/frontends/onnx/tests/models/com.microsoft/qlinear_avg_pool.prototxt b/src/frontends/onnx/tests/models/com.microsoft/qlinear_avg_pool.prototxtnew file mode 100644index 00000000000..e8053b50dfa--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/qlinear_avg_pool.prototxt@@ -0,0 +1,117 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "A"+ input: "A_scale"+ input: "A_zero"+ input: "B_scale"+ input: "B_zero"+ output: "R"+ name: "test_Qlinear_avg_pool"+ domain: "com.microsoft"+ op_type: "QLinearAveragePool"+ attribute {+ name: "auto_pad"+ s: "NOTSET"+ type: STRING+ }+ attribute {+ name: "ceil_mode"+ i: 0+ type: INT+ }+ attribute {+ name: "count_include_pad"+ i: 0+ type: INT+ }+ attribute {+ name: "kernel_shape"+ ints: 7+ ints: 7+ type: INTS+ }+ attribute {+ name: "strides"+ ints: 1+ ints: 1+ type: INTS+ }+ }++ initializer {+ data_type: 1+ float_data: 0.023529412+ name: "A_scale"+ }++ initializer {+ data_type: 2+ int32_data: 0+ name: "A_zero"+ }++ initializer {+ data_type: 1+ float_data: 0.01812745+ name: "B_scale"+ }++ initializer {+ data_type: 2+ int32_data: 0+ name: "B_zero"+ }+++ input {+ name: "A"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 7+ }+ dim {+ dim_value: 7+ }+ }+ }+ }+ }++ output {+ name: "R"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+}+opset_import {+ domain: "com.microsoft"+ version: 1+}\ No newline at end of filediff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cppindex 09a96ef3a1e..170476aae05 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -1577,6 +1577,23 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmul_integer_to_float) {test_case.run();}+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_avg_pool) {+ const auto model = convert_model("com.microsoft/qlinear_avg_pool.onnx");+ auto test_case = ov::test::TestCase(model, s_device);++ const std::vector<uint8_t> data_A = {140, 138, 137, 136, 135, 134, 133, 132, 131, 130, 129, 128, 127,+ 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114,+ 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101,+ 100, 99, 98, 97, 96, 95, 94, 93, 92, 91};++ const std::vector<uint8_t> expected_output = {149};++ test_case.add_input<uint8_t>(Shape{1, 1, 7, 7}, data_A);++ test_case.add_expected_output<uint8_t>(Shape{1, 1, 1, 1}, expected_output);+ 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);