Skip to content

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

[ONNX] Enhance tensor handling for external data and add tests for ex… (#34973)

liang134e40c6da3b
diff --git a/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp b/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp
index a6003ab2570..62ab25b4870 100644
--- a/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp
+++ b/src/frontends/onnx/frontend/src/core/graph_iterator_proto.cpp
@@ -286,6 +286,7 @@ bool extract_tensor_external_data(ov::frontend::onnx::TensorMetaInfo& tensor_met
tensor_meta_info.m_is_raw = true;
tensor_meta_info.m_tensor_data = reinterpret_cast<uint8_t*>(ext_data_offset);
tensor_meta_info.m_tensor_data_size = ext_data_length;
+ tensor_meta_info.m_external_location = std::make_shared<std::string>(ext_location);
return true;
} else if (memory_mode == External_MMAP) {
auto cache = graph_iterator->get_mmap_cache();
diff --git a/src/frontends/onnx/frontend/src/core/tensor.cpp b/src/frontends/onnx/frontend/src/core/tensor.cpp
index 04bf8470c8e..2d0b36ce755 100644
--- a/src/frontends/onnx/frontend/src/core/tensor.cpp
+++ b/src/frontends/onnx/frontend/src/core/tensor.cpp
@@ -435,21 +435,23 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
}
ov::element::Type ov_type = get_ov_type();
size_t element_count = get_data_size();
+ const auto shape_elements = shape_size(m_shape);
if (ov::element::is_nibble_type(ov_type)) {
element_count *= 2; // Each byte contains 2 data items
- if (shape_size(m_shape) % 2) {
+ if (shape_elements % 2) {
// Odd elements
element_count--;
}
}
- if (has_external_data()) {
+ std::shared_ptr<ov::AlignedBuffer> constant_buffer;
+ bool external_data_valid = has_external_data();
+ if (external_data_valid) {
const auto ext_data = m_tensor_place != nullptr
? detail::TensorExternalData(*m_tensor_place->get_data_location(),
reinterpret_cast<size_t>(m_tensor_place->get_data()),
m_tensor_place->get_data_size())
: detail::TensorExternalData(*m_tensor_proto);
- std::shared_ptr<ov::AlignedBuffer> constant_buffer;
if (ext_data.data_location() == detail::ORT_MEM_ADDR) {
constant_buffer = ext_data.load_external_mem_data();
} else if (m_mmap_cache) {
@@ -457,7 +459,23 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
} else {
constant_buffer = ext_data.load_external_data(m_model_dir.string());
}
+ if (element_count == 0 && constant_buffer) {
+ element_count = constant_buffer->size() * 8 / ov_type.bitwidth();
+ }
+ }
+
+ if (element_count != shape_elements && !(element_count == 0 && m_shape.empty())) {
+ std::string error_msg =
+ external_data_valid
+ ? "The size of the external data file does not match the byte size of an initializer '"
+ : "The size of the initializer does not match the number of elements implied by its shape for tensor '";
+ error_msg += get_name() + "' in the model";
+ throw error::invalid_external_data(error_msg);
+ }
+ if (element_count == 0) {
+ constant = common::make_failsafe_constant(ov_type);
+ } else if (constant_buffer) {
try {
constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, constant_buffer);
} catch (const ov::Exception&) {
@@ -465,122 +483,112 @@ 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 {
- 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 (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");
}
- 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 (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");
}
if (m_tensor_proto != nullptr && m_tensor_proto->has_name()) {
diff --git a/src/frontends/onnx/frontend/src/core/tensor.hpp b/src/frontends/onnx/frontend/src/core/tensor.hpp
index 3a100fd680b..1a13c18462b 100644
--- a/src/frontends/onnx/frontend/src/core/tensor.hpp
+++ b/src/frontends/onnx/frontend/src/core/tensor.hpp
@@ -354,7 +354,7 @@ private:
size_t get_data_size() const {
if (m_tensor_place != nullptr) {
- if (m_tensor_place->is_raw()) {
+ if (m_tensor_place->is_raw() || m_tensor_place->get_data_location()) {
return m_tensor_place->get_data_size() /
get_onnx_data_size(ov_to_onnx_data_type(m_tensor_place->get_element_type()));
} else {
diff --git a/src/frontends/onnx/tests/models/expand_scalar_failsafe_node_ort_mem.prototxt b/src/frontends/onnx/tests/models/expand_scalar_failsafe_node_ort_mem.prototxt
new file mode 100644
index 00000000000..8bd12ea735d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/expand_scalar_failsafe_node_ort_mem.prototxt
@@ -0,0 +1,53 @@
+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"
+ external_data {
+ key: "location",
+ value: "*/_ORT_MEM_ADDR_/*"
+ }
+ external_data {
+ key: "offset",
+ value: "0"
+ }
+ external_data {
+ key: "length",
+ value: "0"
+ }
+ data_location: 1
+ }
+ 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.cpp
index d1a35bf0bb2..549b8ff7110 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -5570,6 +5570,17 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_expand_scalar_failsafe_node) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_expand_scalar_failsafe_node_ort_mem) {
+ const auto model = convert_model("expand_scalar_failsafe_node_ort_mem.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");