Skip to content

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

[ONNX FE] add support to dequantize linear with multi dimension input (#32578)

bopeng12344fd105a0c9ad
diff --git a/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp b/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp
index 7baa1a548dc..4a97fc85dc3 100644
--- a/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp
+++ b/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp
@@ -228,8 +228,6 @@ ov::OutputVector dequantize_linear(const ov::frontend::onnx::Node& 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");
@@ -255,19 +253,24 @@ ov::OutputVector dequantize_linear(const ov::frontend::onnx::Node& node) {
return {scaled_x};
}
+ // Compatible to multi-dimension of input, not only 2D.
// For further broadcasting scales and zp - reshape input to a shape
- // axis == 0, [x.shape[0]/block_size, block_size, x.shape[1]]
- // axis == 1, [x.shape[0], block_size, x.shape[1]/block_size]
- ov::Output<ov::Node> broadcastable_x;
+ // axis == 0, [x.shape[0]/block_size, block_size, x.shape[1], x.shape[2,...]]
+ // axis == 1, [x.shape[0], block_size, x.shape[1]/block_size, x.shape[2,...]]
+ std::vector<size_t> target_shape_vector;
if (axis == 0) {
- broadcastable_x = op::util::reshape(
- src_x,
- Shape{static_cast<size_t>(src_x.get_shape()[0]) / block_size, block_size, src_x.get_shape()[1]});
+ target_shape_vector = {static_cast<size_t>(src_x.get_partial_shape()[0].get_length()) / block_size,
+ block_size,
+ static_cast<size_t>(src_x.get_partial_shape()[1].get_length())};
} else {
- broadcastable_x = op::util::reshape(
- src_x,
- Shape{src_x.get_shape()[0], block_size, static_cast<size_t>(src_x.get_shape()[1]) / block_size});
+ target_shape_vector = {static_cast<size_t>(src_x.get_partial_shape()[0].get_length()),
+ block_size,
+ static_cast<size_t>(src_x.get_partial_shape()[1].get_length()) / block_size};
}
+ for (int64_t i = 2; i < src_x.get_partial_shape().rank().get_length(); i++) {
+ target_shape_vector.push_back(static_cast<size_t>(src_x.get_partial_shape()[i].get_length()));
+ }
+ ov::Output<ov::Node> broadcastable_x = op::util::reshape(src_x, ov::Shape(target_shape_vector));
const auto& unsqueezed_axes = std::make_shared<v0::Constant>(ov::element::i64, Shape{1}, std::vector<int64_t>{1});
const auto scale_type = scale.get_element_type();
diff --git a/src/frontends/onnx/tests/models/dequantize_linear_21_4d.prototxt b/src/frontends/onnx/tests/models/dequantize_linear_21_4d.prototxt
new file mode 100644
index 00000000000..6df8481ad52
--- /dev/null
+++ b/src/frontends/onnx/tests/models/dequantize_linear_21_4d.prototxt
@@ -0,0 +1,114 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test"
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "zero_point"
+ type {
+ tensor_type {
+ elem_type: 2
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "output"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ node {
+ input: "X"
+ input: "scale"
+ input: "zero_point"
+ output: "output"
+ name: "DequantizeNode"
+ op_type: "DequantizeLinear"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 2
+ type: INT
+ }
+ }
+}
+opset_import {
+ version: 21
+}
\ No newline at end of file
diff --git a/src/frontends/onnx/tests/onnx_import_quant.in.cpp b/src/frontends/onnx/tests/onnx_import_quant.in.cpp
index a6dbe6d9b09..fa48238a354 100644
--- a/src/frontends/onnx/tests/onnx_import_quant.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_quant.in.cpp
@@ -374,6 +374,19 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_1d_zero_scale_uint8_
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21_4d) {
+ auto model = convert_model("dequantize_linear_21_4d.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ test_case.add_input(std::vector<uint8_t>{9, 9, 9, 9, 9, 9, 9, 9}); // x
+ test_case.add_input(std::vector<float>{1.0f, 2.0f, 3.0f, 4.0f}); // scale
+ test_case.add_input(std::vector<uint8_t>{1, 2, 3, 4}); // zero_point
+ test_case.add_expected_output<float>({2, 4, 1, 1},
+ std::vector<float>{8.0f, 14.0f, 8.0f, 14.0f, 18.0f, 20.0f, 18.0f, 20.0f});
+ test_case.run();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21) {
auto model = convert_model("dequantize_linear_21.onnx");