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_FE] QuantizeLinear: create zero rank shapes in case of per tensor quantization (#33822)
Vladislav Golubev9131e4e70859
diff --git a/src/frontends/onnx/frontend/src/op/quantize_linear.cpp b/src/frontends/onnx/frontend/src/op/quantize_linear.cppindex 0fec6237809..36b901e6a8f 100644--- a/src/frontends/onnx/frontend/src/op/quantize_linear.cpp+++ b/src/frontends/onnx/frontend/src/op/quantize_linear.cpp@@ -26,7 +26,7 @@ ov::Output<ov::Node> get_zero_point(const ov::OutputVector& inputs) {if (inputs.size() > 2) {return inputs.at(2);} else {- return std::make_shared<v0::Constant>(ov::element::u8, ov::Shape{1}, std::uint8_t(0));+ return std::make_shared<v0::Constant>(ov::element::u8, ov::Shape{}, std::uint8_t(0));}}@@ -65,33 +65,34 @@ std::tuple<std::shared_ptr<ov::Node>, std::shared_ptr<ov::Node>> get_output_bandconst ov::element::Type& data_type) {std::shared_ptr<ov::Node> output_low;std::shared_ptr<ov::Node> output_high;+ const ov::Shape shape{};// These values could be used in a ConvertQuantizeDequantize transformation and// should be alignedswitch (destination_type) {case ov::element::i4:- output_low = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, -8);- output_high = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 7);+ output_low = std::make_shared<v0::Constant>(data_type, shape, -8);+ output_high = std::make_shared<v0::Constant>(data_type, shape, 7);break;case ov::element::u4:- output_low = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 0);- output_high = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 15);+ output_low = std::make_shared<v0::Constant>(data_type, shape, 0);+ output_high = std::make_shared<v0::Constant>(data_type, shape, 15);break;case ov::element::i8:- output_low = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, -128);- output_high = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 127);+ output_low = std::make_shared<v0::Constant>(data_type, shape, -128);+ output_high = std::make_shared<v0::Constant>(data_type, shape, 127);break;case ov::element::u8:- output_low = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 0);- output_high = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 255);+ output_low = std::make_shared<v0::Constant>(data_type, shape, 0);+ output_high = std::make_shared<v0::Constant>(data_type, shape, 255);break;case ov::element::i16:- output_low = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, -32768);- output_high = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 32767);+ output_low = std::make_shared<v0::Constant>(data_type, shape, -32768);+ output_high = std::make_shared<v0::Constant>(data_type, shape, 32767);break;case ov::element::u16:- output_low = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 0);- output_high = std::make_shared<v0::Constant>(data_type, ov::Shape{1}, 65535);+ output_low = std::make_shared<v0::Constant>(data_type, shape, 0);+ output_high = std::make_shared<v0::Constant>(data_type, shape, 65535);break;default:OPENVINO_THROW("Unsupported element type for QuantizeLinear");@@ -146,13 +147,11 @@ std::shared_ptr<ov::Node> make_fake_quantize(const ov::Output<ov::Node>& y_scale}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));+ auto is_per_tensor = [](const ov::Output<ov::Node>& input) {+ const auto& shape = input.get_partial_shape();+ return shape.is_static() && ov::shape_size(shape.to_shape()) == 1;+ };+ return is_per_tensor(scale) && is_per_tensor(zero_point);}} // namespace detaildiff --git a/src/frontends/onnx/tests/models/quantize_linear_const_scalar_data.prototxt b/src/frontends/onnx/tests/models/quantize_linear_const_scalar_data.prototxtnew file mode 100644index 00000000000..f7d5d658b4f--- /dev/null+++ b/src/frontends/onnx/tests/models/quantize_linear_const_scalar_data.prototxt@@ -0,0 +1,66 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "y_scale"+ input: "y_zero_point"+ output: "Y"+ name: "QuantizeLinear"+ op_type: "QuantizeLinear"+ }+ name: "test_graph"+ initializer {+ data_type: 2+ name: "y_zero_point"+ raw_data: "\000"+ }+ initializer {+ name: "y_scale"+ data_type: 1+ float_data: 0.5+ }+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+ input {+ name: "y_scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+ input {+ name: "y_zero_point"+ type {+ tensor_type {+ elem_type: 2+ shape {+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 2+ shape {+ }+ }+ }+ }+}+opset_import {+ version: 10+}diff --git a/src/frontends/onnx/tests/onnx_import_quant.in.cpp b/src/frontends/onnx/tests/onnx_import_quant.in.cppindex ceb2f977846..3b60b4c2f76 100644--- a/src/frontends/onnx/tests/onnx_import_quant.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_quant.in.cpp@@ -34,9 +34,33 @@ static std::string s_device = backend_name_to_device("${BACKEND_NAME}");using Inputs = std::vector<std::vector<float>>;using Outputs = std::vector<std::vector<float>>;+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_const_scalar_data) {+ auto model = convert_model("quantize_linear_const_scalar_data.onnx");++ auto fq = ov::as_type_ptr<op::v0::FakeQuantize>(model->get_parameters().back()->get_users().back());+ EXPECT_NE(fq, nullptr);+ // Check that all inputs are scalars+ for (size_t i = 0; i <= 4; ++i) {+ EXPECT_EQ(fq->input_value(i).get_shape(), Shape{}) << "Input " << i << " is not a scalar";+ }++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input(std::vector<float>{32.25f});++ test_case.add_expected_output(std::vector<std::uint8_t>{64});+ test_case.run();+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_const_scale_const_zero_p) {auto model = convert_model("quantize_linear_const.onnx");+ auto fq = ov::as_type_ptr<op::v0::FakeQuantize>(model->get_parameters().back()->get_users().back());+ EXPECT_NE(fq, nullptr);+ // Check that all bound inputs (input_low, input_high, output_low, output_high) are scalars+ for (size_t i = 1; i <= 4; ++i) {+ EXPECT_EQ(fq->input_value(i).get_shape(), Shape{}) << "Input " << i << " is not a scalar";+ }+auto test_case = ov::test::TestCase(model, s_device);test_case.add_input(std::vector<float>{32.25f, 48.34f, 50.f, 83.f});