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 QuantizeLinear-21 operator with blocksize support (#31104)
liang2d1e52525faa
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.mdindex d8cff1bc8e9..98966fff6f8 100644--- a/src/frontends/onnx/docs/supported_ops.md+++ b/src/frontends/onnx/docs/supported_ops.md@@ -132,7 +132,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)| |Pow |1 |15, 13, 12, 7, 1 | || |QLinearConv |10 |10 | || |QLinearMatMul |10 |21, 10 | |-| |QuantizeLinear |13, 10 |21, 19, 13, 10 | |+| |QuantizeLinear |21, 13, 10 |21, 19, 13, 10 | || |RNN |1 |22, 14, 7, 1 | || |RandomNormal |1 |22, 1 | || |RandomNormalLike |1 |22, 1 | |diff --git a/src/frontends/onnx/frontend/src/op/quantize_linear.cpp b/src/frontends/onnx/frontend/src/op/quantize_linear.cppindex 6c9f0390691..b9ec56d0e63 100644--- a/src/frontends/onnx/frontend/src/op/quantize_linear.cpp+++ b/src/frontends/onnx/frontend/src/op/quantize_linear.cpp@@ -144,6 +144,16 @@ std::shared_ptr<ov::Node> make_fake_quantize(const ov::Output<ov::Node>& y_scalestd::make_shared<v0::FakeQuantize>(data, input_low, input_high, output_low, output_high, levels),destination_type);}++bool is_per_tensor_quantization(const ov::Output<ov::Node>& scale, const ov::Output<ov::Node>& zero_point) {+ auto scale_shape = scale.get_partial_shape();+ auto zero_point_shape = zero_point.get_partial_shape();+ return scale_shape.rank().is_static() &&+ (scale_shape.rank().get_length() == 0 || (scale_shape.rank().get_length() == 1 && scale_shape[0] == 1)) &&+ zero_point_shape.rank().is_static() &&+ (zero_point_shape.rank().get_length() == 0 ||+ (zero_point_shape.rank().get_length() == 1 && zero_point_shape[0] == 1));+}} // namespace detailnamespace opset_1 {@@ -164,10 +174,12 @@ ONNX_OP("QuantizeLinear", {1, 12}, ai_onnx::opset_1::quantize_linear);namespace opset_13 {namespace detail {+const int64_t BLOCKED_QUANTIZATION_DISABLED_SIZE = 0;ov::OutputVector quantize_linear(ov::Output<ov::Node> x,ov::Output<ov::Node> y_scale,ov::Output<ov::Node> y_zero_point,int64_t axis,+ int64_t block_size,Node node) {namespace detail = ov::frontend::onnx::ai_onnx::detail;@@ -184,6 +196,36 @@ ov::OutputVector quantize_linear(ov::Output<ov::Node> x,const auto& y_scale_shape = y_scale.get_partial_shape();const auto& y_zero_point_shape = y_zero_point.get_partial_shape();+ if (block_size > 1) {+ if (x_shape.rank().is_static() && x_shape.rank().get_length() > 0 && x_shape[axis].is_static()) {+ CHECK_VALID_NODE(node,+ x_shape[axis].get_length() % block_size == 0,+ "The input data axis size: ",+ x_shape[axis],+ " must be divisible by the block size: ",+ block_size);+ ov::Shape target_shape(x_shape.rank().get_length() + 1, 1);+ // Copy the shape to dimensions before the axis+ for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {+ target_shape[i] = x_shape[i].get_length();+ }+ target_shape[axis] = static_cast<size_t>(x_shape[axis].get_length() / block_size);+ target_shape[axis + 1] = static_cast<size_t>(block_size);+ // Copy the shape to dimensions after the axis+ for (size_t i = axis + 2; i < target_shape.size(); ++i) {+ target_shape[i] = x_shape[i - 1].get_length();+ }+ auto input_x = ov::op::util::reshape(x, target_shape);++ target_shape[axis + 1] = 1;+ y_scale = ov::op::util::reshape(y_scale, target_shape);+ y_zero_point = ov::op::util::reshape(y_zero_point, target_shape);++ auto fake_quantize = detail::make_fake_quantize(y_scale, y_zero_point, input_x);+ return {ov::op::util::reshape(fake_quantize, x_shape.to_shape())};+ }+ }+if (y_scale_shape.rank().is_static() && y_scale_shape.rank().get_length() == 1 && x_shape.rank().is_static() &&x_shape.rank().get_length() > 0 && x_shape[axis].is_static()) {CHECK_VALID_NODE(node,@@ -220,30 +262,48 @@ ov::OutputVector quantize_linear(ov::Output<ov::Node> x,ov::OutputVector quantize_linear(const ov::frontend::onnx::Node& node) {const ov::OutputVector inputs{node.get_ov_inputs()};-- FRONT_END_GENERAL_CHECK(2 <= inputs.size() && inputs.size() <= 3,- "The QuantizeLinear op expects 2 required and one optional "- "input. Got: ",- inputs.size());+ common::default_op_checks(node, 2, 3);const auto& x = inputs[0];const auto& scale = inputs[1];const auto zero_point = ai_onnx::detail::get_zero_point(inputs);// per-tensor quantization, axis attribute ignored- if (scale.get_partial_shape().rank().is_static() &&- (scale.get_partial_shape().rank().get_length() == 0 ||- (scale.get_partial_shape().rank().get_length() == 1 && scale.get_partial_shape()[0] == 1)) &&- zero_point.get_partial_shape().rank().is_static() &&- (zero_point.get_partial_shape().rank().get_length() == 0 ||- (zero_point.get_partial_shape().rank().get_length() == 1 && zero_point.get_partial_shape()[0] == 1))) {+ if (ai_onnx::detail::is_per_tensor_quantization(scale, zero_point)) {return ai_onnx::opset_1::quantize_linear(node);}-- return detail::quantize_linear(x, scale, zero_point, node.get_attribute_value<int64_t>("axis", 1), node);+ return detail::quantize_linear(x,+ scale,+ zero_point,+ node.get_attribute_value<int64_t>("axis", 1),+ detail::BLOCKED_QUANTIZATION_DISABLED_SIZE,+ node);}-ONNX_OP("QuantizeLinear", OPSET_SINCE(13), ai_onnx::opset_13::quantize_linear);+ONNX_OP("QuantizeLinear", OPSET_RANGE(13, 19), ai_onnx::opset_13::quantize_linear);} // namespace opset_13++namespace opset_21 {+ov::OutputVector quantize_linear(const ov::frontend::onnx::Node& node) {+ const ov::OutputVector inputs{node.get_ov_inputs()};+ common::default_op_checks(node, 2, 3);++ const auto& x = inputs[0];+ const auto& scale = inputs[1];+ const auto zero_point = ai_onnx::detail::get_zero_point(inputs);++ if (ai_onnx::detail::is_per_tensor_quantization(scale, zero_point)) {+ return ai_onnx::opset_1::quantize_linear(node);+ }+ return opset_13::detail::quantize_linear(+ x,+ scale,+ zero_point,+ node.get_attribute_value<int64_t>("axis", 1),+ node.get_attribute_value<int64_t>("block_size", opset_13::detail::BLOCKED_QUANTIZATION_DISABLED_SIZE),+ node);+}+ONNX_OP("QuantizeLinear", OPSET_SINCE(21), ai_onnx::opset_21::quantize_linear);+} // namespace opset_21} // namespace ai_onnx} // namespace onnx} // namespace frontenddiff --git a/src/frontends/onnx/frontend/src/utils/common.cpp b/src/frontends/onnx/frontend/src/utils/common.cppindex 041ada73f9c..ab639922739 100644--- a/src/frontends/onnx/frontend/src/utils/common.cpp+++ b/src/frontends/onnx/frontend/src/utils/common.cpp@@ -88,6 +88,18 @@ void default_op_checks(const Node& node, size_t min_inputs_size) {inputs.size());}+void default_op_checks(const Node& node, size_t min_inputs_size, size_t max_inputs_size) {+ const auto& inputs = node.get_ov_inputs();+ FRONT_END_OP_CONVERSION_CHECK(inputs.size() >= min_inputs_size && inputs.size() <= max_inputs_size,+ node.op_type(),+ " expected ",+ std::to_string(min_inputs_size),+ " to ",+ std::to_string(max_inputs_size),+ " inputs, got: ",+ inputs.size());+}+std::shared_ptr<ov::Node> get_monotonic_range_along_node_rank(const ov::Output<ov::Node>& value,int64_t start_value,int64_t step) {diff --git a/src/frontends/onnx/frontend/src/utils/common.hpp b/src/frontends/onnx/frontend/src/utils/common.hppindex 104ae593460..377d23994fe 100644--- a/src/frontends/onnx/frontend/src/utils/common.hpp+++ b/src/frontends/onnx/frontend/src/utils/common.hpp@@ -31,6 +31,12 @@ const ov::element::Type& get_ov_element_type(std::int64_t onnx_type);/// \param[in] min_inputs_size Minimal amount of inputs expectedvoid default_op_checks(const Node& node, size_t min_inputs_size);+/// \brief Function does a default checks for a node. Raise an exception if checks are failed+/// \param[in] node Node to check+/// \param[in] min_inputs_size Minimal amount of inputs expected+/// \param[in] max_inputs_size Maximum amount of inputs expected+void default_op_checks(const Node& node, size_t min_inputs_size, size_t max_inputs_size);+/// \brief Return a monotonic sequence.////// \note Limitations: this function may not work for very large integer valuesdiff --git a/src/frontends/onnx/tests/models/quantization/quantize_linear_blocksize.prototxt b/src/frontends/onnx/tests/models/quantization/quantize_linear_blocksize.prototxtnew file mode 100644index 00000000000..04873e6cd87--- /dev/null+++ b/src/frontends/onnx/tests/models/quantization/quantize_linear_blocksize.prototxt@@ -0,0 +1,112 @@+ir_version: 10+producer_name: "OpenVINO ONNX Frontend"+graph {+ name: "test quantize linear with block_size"+ node {+ input: "x"+ input: "scale"+ input: "zero_point"+ output: "y"+ name: "_0"+ op_type: "QuantizeLinear"+ attribute {+ name: "saturate"+ i: 1+ type: INT+ }+ attribute {+ name: "axis"+ i: 1+ type: INT+ }+ attribute {+ name: "block_size"+ i: 2+ type: INT+ }+ attribute {+ name: "output_dtype"+ i: 0+ type: INT+ }+ }+ input {+ name: "x"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 4+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "zero_point"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ output {+ name: "y"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 4+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+}+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 2f5f06b5dbd..a6dbe6d9b09 100644--- a/src/frontends/onnx/tests/onnx_import_quant.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_quant.in.cpp@@ -1165,3 +1165,16 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_opsets_10_and_13_axis1test_case.add_expected_output(std::vector<std::uint8_t>{32, 97, 50, 166});test_case.run();}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_blocksize) {+ auto model = convert_model("quantization/quantize_linear_blocksize.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input(+ std::vector<float>{-2.549168109893799f, -4.794857501983643f, 8.413617134094238f, 6.108623504638672f});+ test_case.add_input(std::vector<float>{0.2800687253475189f, 9.343092918395996f});+ test_case.add_input(std::vector<uint8_t>{128, 189});++ test_case.add_expected_output(std::vector<std::uint8_t>{119, 111, 190, 190});+ test_case.run();+}