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 access violation for Broadcast operator when 0-size shape with scalar input (#34874)
liang02f54d48d6a1
diff --git a/src/frontends/onnx/frontend/src/core/tensor.cpp b/src/frontends/onnx/frontend/src/core/tensor.cppindex c39a6696e31..04bf8470c8e 100644--- a/src/frontends/onnx/frontend/src/core/tensor.cpp+++ b/src/frontends/onnx/frontend/src/core/tensor.cpp@@ -465,114 +465,122 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {"The size of the external data file does not match the byte size of an initializer '" + get_name() +"' in the model");}- } else if (element_count == shape_size(m_shape) && m_tensor_proto != nullptr) {- switch (m_tensor_proto->data_type()) {- case TensorProto_DataType::TensorProto_DataType_FLOAT:- case TensorProto_DataType::TensorProto_DataType_DOUBLE:- case TensorProto_DataType::TensorProto_DataType_INT32:- case TensorProto_DataType::TensorProto_DataType_INT64:- case TensorProto_DataType::TensorProto_DataType_UINT32:- case TensorProto_DataType::TensorProto_DataType_UINT64:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data_ptr());- break;- case TensorProto_DataType::TensorProto_DataType_INT4:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());- break;- case TensorProto_DataType::TensorProto_DataType_INT8:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());- break;- case TensorProto_DataType::TensorProto_DataType_INT16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int16_t>().data());- break;- case TensorProto_DataType::TensorProto_DataType_UINT4:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());- break;- case TensorProto_DataType::TensorProto_DataType_UINT8:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());- break;- case TensorProto_DataType::TensorProto_DataType_UINT16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint16_t>().data());- break;- case TensorProto_DataType::TensorProto_DataType_BOOL:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<char>().data());- break;- case TensorProto_DataType::TensorProto_DataType_BFLOAT16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::bfloat16>().data());- break;- case TensorProto_DataType::TensorProto_DataType_FLOAT16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float16>().data());- break;- case TensorProto_DataType::TensorProto_DataType_FLOAT8E4M3FN:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e4m3>().data());- break;- case TensorProto_DataType::TensorProto_DataType_FLOAT8E5M2:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e5m2>().data());- break;- case TensorProto_DataType::TensorProto_DataType_STRING:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<std::string>().data());- break;- default:- ONNX_UNSUPPORTED_DATA_TYPE(- m_tensor_proto->data_type(),- "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, INT8, INT16, INT32, INT64, "- "UINT4, UINT8, UINT16, UINT32, UINT64, STRING");+ } else {+ const auto shape_elements = shape_size(m_shape);+ if (element_count != shape_elements && !(element_count == 0 && m_shape.empty())) {+ FRONT_END_THROW(+ "The number of elements implied by the data size does not match the shape of an initializer '" ++ get_name() + "' in the model");}- } else if (element_count == shape_size(m_shape) && m_tensor_place != nullptr) {- switch (m_tensor_place->get_element_type()) {- case ov::element::f32:- case ov::element::f64:- case ov::element::i32:- case ov::element::i64:- case ov::element::u32:- case ov::element::u64:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data_ptr());- break;- case ov::element::i4:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());- break;- case ov::element::i8:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());- break;- case ov::element::i16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int16_t>().data());- break;- case ov::element::u4:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());- break;- case ov::element::u8:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());- break;- case ov::element::u16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint16_t>().data());- break;- case ov::element::boolean:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<char>().data());- break;- case ov::element::bf16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::bfloat16>().data());- break;- case ov::element::f16:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float16>().data());- break;- case ov::element::f8e4m3:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e4m3>().data());- break;- case ov::element::f8e5m2:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e5m2>().data());- break;- case ov::element::string:- constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<std::string>().data());- break;- default:- ONNX_UNSUPPORTED_DATA_TYPE(- m_tensor_proto->data_type(),- "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, INT8, INT16, INT32, INT64, "- "UINT4, UINT8, UINT16, UINT32, UINT64, STRING");+ if (element_count == 0) {+ constant = common::make_failsafe_constant(ov_type);+ } else if (m_tensor_proto != nullptr) {+ switch (m_tensor_proto->data_type()) {+ case TensorProto_DataType::TensorProto_DataType_FLOAT:+ case TensorProto_DataType::TensorProto_DataType_DOUBLE:+ case TensorProto_DataType::TensorProto_DataType_INT32:+ case TensorProto_DataType::TensorProto_DataType_INT64:+ case TensorProto_DataType::TensorProto_DataType_UINT32:+ case TensorProto_DataType::TensorProto_DataType_UINT64:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data_ptr());+ break;+ case TensorProto_DataType::TensorProto_DataType_INT4:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_INT8:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_INT16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int16_t>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_UINT4:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_UINT8:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_UINT16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint16_t>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_BOOL:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<char>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_BFLOAT16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::bfloat16>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_FLOAT16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float16>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_FLOAT8E4M3FN:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e4m3>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_FLOAT8E5M2:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e5m2>().data());+ break;+ case TensorProto_DataType::TensorProto_DataType_STRING:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<std::string>().data());+ break;+ default:+ ONNX_UNSUPPORTED_DATA_TYPE(m_tensor_proto->data_type(),+ "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, "+ "INT8, INT16, INT32, INT64, "+ "UINT4, UINT8, UINT16, UINT32, UINT64, STRING");+ }+ } else if (m_tensor_place != nullptr) {+ switch (m_tensor_place->get_element_type()) {+ case ov::element::f32:+ case ov::element::f64:+ case ov::element::i32:+ case ov::element::i64:+ case ov::element::u32:+ case ov::element::u64:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data_ptr());+ break;+ case ov::element::i4:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());+ break;+ case ov::element::i8:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int8_t>().data());+ break;+ case ov::element::i16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<int16_t>().data());+ break;+ case ov::element::u4:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());+ break;+ case ov::element::u8:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint8_t>().data());+ break;+ case ov::element::u16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<uint16_t>().data());+ break;+ case ov::element::boolean:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<char>().data());+ break;+ case ov::element::bf16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::bfloat16>().data());+ break;+ case ov::element::f16:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float16>().data());+ break;+ case ov::element::f8e4m3:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e4m3>().data());+ break;+ case ov::element::f8e5m2:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<ov::float8_e5m2>().data());+ break;+ case ov::element::string:+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, get_data<std::string>().data());+ break;+ default:+ ONNX_UNSUPPORTED_DATA_TYPE(m_tensor_proto->data_type(),+ "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, "+ "INT8, INT16, INT32, INT64, "+ "UINT4, UINT8, UINT16, UINT32, UINT64, STRING");+ }+ } else {+ FRONT_END_THROW("Tensor shape doesn't match data size");}- } else if (element_count == 0 && m_shape.size() == 0) {- constant = common::make_failsafe_constant(ov_type);- } else {- FRONT_END_THROW("Tensor shape doesn't match data size");}if (m_tensor_proto != nullptr && m_tensor_proto->has_name()) {diff --git a/src/frontends/onnx/frontend/src/op/reshape.cpp b/src/frontends/onnx/frontend/src/op/reshape.cppindex 6b4a1b46553..3975b02559b 100644--- a/src/frontends/onnx/frontend/src/op/reshape.cpp+++ b/src/frontends/onnx/frontend/src/op/reshape.cpp@@ -6,6 +6,7 @@#include "core/operator_set.hpp"#include "exceptions.hpp"+#include "utils/common.hpp"#include "utils/reshape.hpp"using namespace ov::op;@@ -23,6 +24,12 @@ ov::OutputVector reshape(const ov::frontend::onnx::Node& node) {// Since opset 5 the target shape is provided as inputif (ov_inputs.size() == 2) {pattern = ov_inputs.at(1);+ if (common::is_failsafe_node(pattern.get_node_shared_ptr())) {+ // in case the "shape" input is connected to a failsafe node created in place of an invalid initializer+ // the target shape should be ignored and this Expand operation should not modify its input tensor+ // the Broadcast created below should be eliminated later on by an appropriate optimization pass+ pattern = v0::Constant::create(ov::element::i64, {0}, {});+ }} else {// Added in onnx reshape version 14special_zero = !node.get_attribute_value<int64_t>("allowzero", 0);diff --git a/src/frontends/onnx/tests/models/expand_scalar_failsafe_node.prototxt b/src/frontends/onnx/tests/models/expand_scalar_failsafe_node.prototxtnew file mode 100644index 00000000000..69a8a993bea--- /dev/null+++ b/src/frontends/onnx/tests/models/expand_scalar_failsafe_node.prototxt@@ -0,0 +1,41 @@+ir_version: 13+producer_name: "OpenVINO ONNX Frontend"+graph {+ name: ""+ node {+ input: "inputA"+ input: "shape"+ output: "expandOutput_0"+ name: "_0"+ op_type: "Expand"+ }+ initializer {+ dims: 0+ data_type: 7+ name: "shape"+ raw_data: ""+ }+ input {+ name: "inputA"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+ output {+ name: "expandOutput_0"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+}+opset_import {+ version: 25+}diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cppindex 6500e1fcb7e..0384bfb6ca7 100644--- a/src/frontends/onnx/tests/onnx_import.in.cpp+++ b/src/frontends/onnx/tests/onnx_import.in.cpp@@ -5559,6 +5559,17 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_expand_failsafe_node) {test_case.run();}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_expand_scalar_failsafe_node) {+ const auto model = convert_model("expand_scalar_failsafe_node.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ const auto input_data = std::vector<float>{1.0f};+ test_case.add_input<float>(input_data);+ // the target shape is an empty constant so the Expand operation should not modify the input shape+ test_case.add_expected_output<float>(input_data);+ test_case.run();+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_scan15_fib_like) {const auto model = convert_model("scan15_fib_like.onnx");