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
Fix DequantizeLinear-21 blocked quantization for axis > 1 (#32918)
Evgeny Kotov4832243b092c
diff --git a/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp b/src/frontends/onnx/frontend/src/op/dequantize_linear.cppindex 4a97fc85dc3..4054ccdd207 100644--- a/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp+++ b/src/frontends/onnx/frontend/src/op/dequantize_linear.cpp@@ -231,17 +231,26 @@ ov::OutputVector dequantize_linear(const ov::frontend::onnx::Node& node) {FRONT_END_GENERAL_CHECK(src_x.get_partial_shape().is_static(),"DequantizeLinear cannot operate with dynamic shapes of input X");- const auto axis = node.get_attribute_value<int64_t>("axis", 1);+ 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));- FRONT_END_GENERAL_CHECK(axis <= 1, "Axis > 1 isn't supported");FRONT_END_GENERAL_CHECK(block_size > 0, "block_size must be greater than zero");- FRONT_END_GENERAL_CHECK(- (axis == 0 && src_x.get_shape()[0] % block_size == 0) || (axis == 1 && src_x.get_shape()[1] % block_size == 0),- "DequantizeLinear doesn't support case when dimension of X cannot be divided by block_size");- bool is_cw_quantize =- (axis == 0 && src_x.get_shape()[0] == block_size) || (axis == 1 && src_x.get_shape()[1] == block_size);+ // Normalize axis to handle negative values+ axis = common::normalize_axis(node.get_description(), axis, src_x.get_partial_shape().rank());++ // Check that dimension at axis is divisible by block_size+ FRONT_END_GENERAL_CHECK(src_x.get_shape()[axis] % block_size == 0,+ "DequantizeLinear doesn't support case when dimension of X at axis ",+ axis,+ " (",+ src_x.get_shape()[axis],+ ") cannot be divided by block_size (",+ block_size,+ ")");++ // Check if this is channel-wise quantization (block_size equals dimension size at axis)+ bool is_cw_quantize = (src_x.get_shape()[axis] == block_size);if (is_cw_quantize) {ov::Output<ov::Node> converted_x = std::make_shared<v0::Convert>(src_x, scale.get_element_type());if (inputs.size() > 2) {@@ -253,25 +262,29 @@ 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], x.shape[2,...]]- // axis == 1, [x.shape[0], block_size, x.shape[1]/block_size, x.shape[2,...]]+ // Build target shape for blocked quantization+ // For all axes: [..., num_blocks, block_size, ...]+ // - axis=0: [num_blocks, block_size, ...] with block_size at position 1+ // - axis>0: [..., num_blocks, block_size, ...] with block_size at position axis+1std::vector<size_t> target_shape_vector;- if (axis == 0) {- 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 {- 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()));+ const auto& input_shape = src_x.get_partial_shape();+ for (int64_t i = 0; i < input_shape.rank().get_length(); i++) {+ if (i == axis) {+ // Always num_blocks first, then block_size+ target_shape_vector.push_back(static_cast<size_t>(input_shape[i].get_length()) / block_size);+ target_shape_vector.push_back(block_size);+ } else {+ // Other axes remain unchanged+ target_shape_vector.push_back(static_cast<size_t>(input_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});++ // Unsqueeze position for scale/zero_point:+ // block_size is always at position axis+1, so unsqueeze at axis+1+ const auto unsqueeze_axis = axis + 1;+ const auto& unsqueezed_axes =+ std::make_shared<v0::Constant>(ov::element::i64, Shape{1}, std::vector<int64_t>{unsqueeze_axis});const auto scale_type = scale.get_element_type();if (inputs.size() > 2) {diff --git a/src/frontends/onnx/tests/models/dequantize_linear_21_axis_2.prototxt b/src/frontends/onnx/tests/models/dequantize_linear_21_axis_2.prototxtnew file mode 100644index 00000000000..5b105482275--- /dev/null+++ b/src/frontends/onnx/tests/models/dequantize_linear_21_axis_2.prototxt@@ -0,0 +1,102 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+graph {+ name: "test_dequantize_21_axis_2"+ input {+ name: "x"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ input {+ name: "scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ input {+ name: "zero_point"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ output {+ name: "y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ node {+ input: "x"+ input: "scale"+ input: "zero_point"+ output: "y"+ name: "DequantizeNode"+ op_type: "DequantizeLinear"+ attribute {+ name: "axis"+ i: 2+ type: INT+ }+ attribute {+ name: "block_size"+ i: 4+ type: INT+ }+ }+}+opset_import {+ version: 21+}diff --git a/src/frontends/onnx/tests/models/dequantize_linear_21_axis_3.prototxt b/src/frontends/onnx/tests/models/dequantize_linear_21_axis_3.prototxtnew file mode 100644index 00000000000..bbfe7c9b18c--- /dev/null+++ b/src/frontends/onnx/tests/models/dequantize_linear_21_axis_3.prototxt@@ -0,0 +1,114 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+graph {+ name: "test_dequantize_21_axis_3"+ input {+ name: "x"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ input {+ name: "zero_point"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ output {+ name: "y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ node {+ input: "x"+ input: "scale"+ input: "zero_point"+ output: "y"+ name: "DequantizeNode"+ op_type: "DequantizeLinear"+ attribute {+ name: "axis"+ i: 3+ type: INT+ }+ attribute {+ name: "block_size"+ i: 2+ type: INT+ }+ }+}+opset_import {+ version: 21+}diff --git a/src/frontends/onnx/tests/models/dequantize_linear_21_axis_neg1.prototxt b/src/frontends/onnx/tests/models/dequantize_linear_21_axis_neg1.prototxtnew file mode 100644index 00000000000..2708c989e03--- /dev/null+++ b/src/frontends/onnx/tests/models/dequantize_linear_21_axis_neg1.prototxt@@ -0,0 +1,102 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+graph {+ name: "test_dequantize_21_axis_neg1"+ input {+ name: "x"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ input {+ name: "zero_point"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ output {+ name: "y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ node {+ input: "x"+ input: "scale"+ input: "zero_point"+ output: "y"+ name: "DequantizeNode"+ op_type: "DequantizeLinear"+ attribute {+ name: "axis"+ i: -1+ type: INT+ }+ attribute {+ name: "block_size"+ i: 2+ type: INT+ }+ }+}+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.cppindex fa48238a354..fb830820e9e 100644--- a/src/frontends/onnx/tests/onnx_import_quant.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_quant.in.cpp@@ -382,8 +382,9 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21_4d) {test_case.add_input(std::vector<uint8_t>{9, 9, 9, 9, 9, 9, 9, 9}); // xtest_case.add_input(std::vector<float>{1.0f, 2.0f, 3.0f, 4.0f}); // scaletest_case.add_input(std::vector<uint8_t>{1, 2, 3, 4}); // zero_point+ // Expected output from ONNX Runtime (sequential blocks, not interleaved)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});+ std::vector<float>{8.0f, 8.0f, 14.0f, 14.0f, 18.0f, 18.0f, 20.0f, 20.0f});test_case.run();}@@ -407,6 +408,106 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21_cw) {test_case.run();}+// Tests for axis > 1 support in DequantizeLinear-21 (blocked quantization)+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21_axis2) {+ auto model = convert_model("dequantize_linear_21_axis_2.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ // Input: [2, 3, 8] with axis=2, block_size=4+ // Generated using ONNX Runtime as reference+ test_case.add_input(std::vector<int8_t>{39, 29, 34, 32, 43, 12, 29, 4, 35, 15, 22, 47, 43, 36, 8, 21,+ 26, 8, 47, 2, 26, 21, 45, 12, 25, 39, 23, 49, 40, 25, 35, 19,+ 2, 48, 40, 23, 24, 26, 11, 18, 31, 15, 11, 22, 18, 24, 30, 46});++ // scale: [2, 3, 2] (axis=2 divided by block_size=4: 8/4=2)+ test_case.add_input(std::vector<float>{1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f});++ // zero_point: [2, 3, 2]+ test_case.add_input(std::vector<int8_t>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});++ // Expected output from ONNX Runtime: [2, 3, 8]+ test_case.add_expected_output<float>(+ {2, 3, 8},+ std::vector<float>{39.0f, 29.0f, 34.0f, 32.0f, 86.0f, 24.0f, 58.0f, 8.0f, 35.0f, 15.0f, 22.0f, 47.0f,+ 86.0f, 72.0f, 16.0f, 42.0f, 26.0f, 8.0f, 47.0f, 2.0f, 52.0f, 42.0f, 90.0f, 24.0f,+ 25.0f, 39.0f, 23.0f, 49.0f, 80.0f, 50.0f, 70.0f, 38.0f, 2.0f, 48.0f, 40.0f, 23.0f,+ 48.0f, 52.0f, 22.0f, 36.0f, 31.0f, 15.0f, 11.0f, 22.0f, 36.0f, 48.0f, 60.0f, 92.0f});+ test_case.run();+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21_axis3) {+ auto model = convert_model("dequantize_linear_21_axis_3.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ // Input: [1, 2, 2, 4] with axis=3, block_size=2+ // Generated using ONNX Runtime as reference+ test_case.add_input(std::vector<int8_t>{5, 1, 14, 19, 9, 12, 18, 10, 6, 3, 7, 17, 9, 2, 17, 13});++ // scale: [1, 2, 2, 2] (axis=3: 4/2=2)+ test_case.add_input(std::vector<float>{1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f});++ // zero_point: [1, 2, 2, 2]+ test_case.add_input(std::vector<int8_t>{0, 0, 0, 0, 0, 0, 0, 0});++ // Expected output from ONNX Runtime: [1, 2, 2, 4]+ test_case.add_expected_output<float>({1, 2, 2, 4},+ std::vector<float>{5.0f,+ 1.0f,+ 28.0f,+ 38.0f,+ 9.0f,+ 12.0f,+ 36.0f,+ 20.0f,+ 6.0f,+ 3.0f,+ 14.0f,+ 34.0f,+ 9.0f,+ 2.0f,+ 34.0f,+ 26.0f});+ test_case.run();+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_opset21_axis_negative) {+ auto model = convert_model("dequantize_linear_21_axis_neg1.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ // Input: [2, 2, 4] with axis=-1 (equivalent to axis=2), block_size=2+ // Generated using ONNX Runtime as reference+ test_case.add_input(std::vector<int8_t>{4, 14, 15, 18, 11, 14, 5, 3, 9, 3, 1, 4, 16, 1, 1, 9});++ // scale: [2, 2, 2] (axis=2: 4/2=2)+ test_case.add_input(std::vector<float>{1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f});++ // zero_point: [2, 2, 2]+ test_case.add_input(std::vector<int8_t>{0, 0, 0, 0, 0, 0, 0, 0});++ // Expected output from ONNX Runtime: [2, 2, 4]+ test_case.add_expected_output<float>({2, 2, 4},+ std::vector<float>{4.0f,+ 14.0f,+ 30.0f,+ 36.0f,+ 11.0f,+ 14.0f,+ 10.0f,+ 6.0f,+ 9.0f,+ 3.0f,+ 2.0f,+ 8.0f,+ 16.0f,+ 1.0f,+ 2.0f,+ 18.0f});+ test_case.run();+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_dequantize_linear_scalar_ignore_axis) {auto model = convert_model("dequantize_linear_scalar_ignore_axis.onnx");diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.pyindex c5e5a873521..9ec155e0f46 100644--- a/src/frontends/onnx/tests/tests_python/test_backend.py+++ b/src/frontends/onnx/tests/tests_python/test_backend.py@@ -629,7 +629,6 @@ tests_expected_to_fail = [),(xfail_issue_139937,- "OnnxBackendNodeModelTest.test_dequantizelinear_blocked_cpu","OnnxBackendNodeModelTest.test_qlinearmatmul_2D_int8_float16_cpu",),(