Skip to content

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

[ONNX] Added initial support of DequantizeLinear-21 (#26915)

Georgy Krivoruchko80d6fdf52772
diff --git a/src/frontends/onnx/frontend/CMakeLists.txt b/src/frontends/onnx/frontend/CMakeLists.txt
index 80fd16e2ed6..f07b6cf999f 100644
--- a/src/frontends/onnx/frontend/CMakeLists.txt
+++ b/src/frontends/onnx/frontend/CMakeLists.txt
@@ -77,7 +77,7 @@ ov_add_frontend(NAME onnx
FILEDESCRIPTION "FrontEnd to load and convert ONNX file format"
LINK_LIBRARIES openvino_onnx_common openvino::core::dev)
-set(ONNX_OPSET_VERSION 20 CACHE INTERNAL "Supported version of ONNX operator set")
+set(ONNX_OPSET_VERSION 21 CACHE INTERNAL "Supported version of ONNX operator set")
target_compile_definitions(${TARGET_NAME} PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION})
if(BUILD_SHARED_LIBS)
diff --git a/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp b/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp
index fe2ea3106e3..b09bc73467b 100644
--- a/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp
+++ b/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp
@@ -11,10 +11,15 @@
#include "openvino/frontend/exception.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
+#include "openvino/op/convert_like.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/reshape.hpp"
+#include "openvino/op/shape_of.hpp"
#include "openvino/op/subtract.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
#include "utils/common.hpp"
+#include "utils/reshape.hpp"
using namespace ov::op;
namespace ov {
@@ -188,8 +193,78 @@ ov::OutputVector dequantize_linear(const ov::frontend::onnx::Node& node) {
// these reshapes make sure that dequantization happens over the specified axis
return detail::dequantize_linear(x, scale, zero_point, node.get_attribute_value<int64_t>("axis", 1), node);
}
-ONNX_OP("DequantizeLinear", OPSET_SINCE(13), ai_onnx::opset_13::dequantize_linear);
+ONNX_OP("DequantizeLinear", {13, 18}, ai_onnx::opset_13::dequantize_linear);
} // namespace opset_13
+
+namespace opset_19 {
+ONNX_OP("DequantizeLinear", {19, 20}, ai_onnx::opset_13::dequantize_linear);
+} // namespace opset_19
+
+namespace opset_21 {
+ov::OutputVector dequantize_linear(const ov::frontend::onnx::Node& node) {
+ common::default_op_checks(node, 2);
+
+ const ov::OutputVector inputs{node.get_ov_inputs()};
+ const auto& src_x = inputs[0];
+ ov::Output<ov::Node> scale = inputs[1];
+ const auto& scale_shape = scale.get_partial_shape();
+ ov::Output<ov::Node> zp;
+
+ // When no blocking dequantization is required - use regular DequantizeLinear
+ if (scale_shape.rank().is_static() && scale_shape.rank().get_length() <= 1) {
+ return ai_onnx::opset_13::dequantize_linear(node);
+ }
+
+ FRONT_END_GENERAL_CHECK(scale_shape.rank().is_static(), "Rank of the input data tensor has to be known (static).");
+ FRONT_END_GENERAL_CHECK(scale_shape.rank().get_length() == 2,
+ "DequantizeLinear cannot operate with more than 2D scales");
+ FRONT_END_GENERAL_CHECK(src_x.get_partial_shape().is_static(),
+ "DequantizeLinear cannot operate with dynamic shapes of input X");
+
+ const auto& unsqueezed_axes = std::make_shared<v0::Constant>(ov::element::i64, Shape{1}, std::vector<int64_t>{1});
+
+ if (inputs.size() > 2) {
+ zp = inputs[2];
+ if (zp.get_element_type() != scale.get_element_type()) {
+ zp = std::make_shared<v1::ConvertLike>(zp, scale);
+ }
+ zp = std::make_shared<v0::Unsqueeze>(zp, unsqueezed_axes);
+ }
+
+ const auto axis = node.get_attribute_value<int64_t>("axis", 1);
+ const auto block_size = static_cast<size_t>(node.get_attribute_value<int64_t>("block_size", 0));
+ const auto scale_type = scale.get_element_type();
+
+ FRONT_END_GENERAL_CHECK(axis == 0, "Axis != 0 isn't supported");
+ FRONT_END_GENERAL_CHECK(block_size > 0, "block_size must be greater than zero");
+ FRONT_END_GENERAL_CHECK(
+ src_x.get_shape()[0] % block_size == 0,
+ "DequantizeLinear doesn't support case when first dimension of X cannot be divided by block_size");
+
+ const auto& x = src_x.get_element_type() == scale_type ? src_x : std::make_shared<v1::ConvertLike>(src_x, scale);
+ // For further broadcasting scales and zp - reshape input to a shape [x.shape[0]/block_size, block_size, x.shape[1]]
+ ov::Output<ov::Node> broadcastable_x =
+ op::util::reshape(x, Shape{static_cast<size_t>(x.get_shape()[0]) / block_size, block_size, x.get_shape()[1]});
+
+ // Adding additional dimension for broadcasting
+ scale = std::make_shared<v0::Unsqueeze>(scale, unsqueezed_axes);
+
+ if (zp.get_node_shared_ptr()) {
+ broadcastable_x = std::make_shared<v1::Subtract>(broadcastable_x, zp);
+ }
+
+ const auto& scaled_x = std::make_shared<v1::Multiply>(broadcastable_x, scale);
+
+ // Returning back a shape
+ const auto& reshaped_scaled_x =
+ std::make_shared<v1::Reshape>(scaled_x, std::make_shared<v0::ShapeOf>(src_x), false);
+
+ reshaped_scaled_x->set_friendly_name(node.get_name());
+
+ return {reshaped_scaled_x};
+}
+ONNX_OP("DequantizeLinear", OPSET_SINCE(21), ai_onnx::opset_21::dequantize_linear);
+} // namespace opset_21
} // namespace ai_onnx
} // namespace onnx
} // namespace frontend
diff --git a/src/frontends/onnx/tests/models/dequantize_linear_21.prototxt b/src/frontends/onnx/tests/models/dequantize_linear_21.prototxt
new file mode 100644
index 00000000000..0378ad13ce0
--- /dev/null
+++ b/src/frontends/onnx/tests/models/dequantize_linear_21.prototxt
@@ -0,0 +1,63 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test_dequantize_21"
+ initializer {
+ dims: 6
+ dims: 3
+ data_type: 21
+ name: "data"
+ raw_data: "\x99\x99\x99\x99\x99\x99\x99\x99\x99"
+ }
+ initializer {
+ dims: 2
+ dims: 3
+ data_type: 1
+ name: "scale"
+ raw_data: "\x00\x00\x80\x3f\x00\x00\x80\x3f\x00\x00\x80\x3f\x00\x00\x80\x3f\x00\x00\x80\x3f\x00\x00\x80\x3f"
+ }
+ initializer {
+ dims: 2
+ dims: 3
+ data_type: 21
+ name: "zp"
+ raw_data: "\x78\x56\x34"
+ }
+ node {
+ input: "data"
+ input: "scale"
+ input: "zp"
+ output: "output"
+ name: "DequantizeNode"
+ op_type: "DequantizeLinear"
+ attribute {
+ name: "axis"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 3
+ type: INT
+ }
+ }
+ output {
+ name: "output"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/onnx_import_quant.in.cpp b/src/frontends/onnx/tests/onnx_import_quant.in.cpp
index c2d48c292cb..ad85ef98ede 100644
--- a/src/frontends/onnx/tests/onnx_import_quant.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_quant.in.cpp
@@ -317,6 +317,16 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_1d_zero_scale_uint8_
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21) {
+ auto model = convert_model("dequantize_linear_21.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ test_case.add_expected_output<float>({6, 3},
+ std::vector<float>{1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6, 4, 5, 6});
+ test_case.run();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_scalar_ignore_axis) {
auto model = convert_model("dequantize_linear_scalar_ignore_axis.onnx");