Skip to content

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

[core] Check constant byte on construction from aligned buffer (#34463)

Pawel Raaszb83813ed8e24
diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cpp
index 3b6febca141..6aa8aee640d 100644
--- a/src/core/src/op/constant.cpp
+++ b/src/core/src/op/constant.cpp
@@ -302,6 +302,14 @@ Constant::Constant(const element::Type& type, const Shape& shape, const std::sha
m_shape(shape),
m_byte_strides(calc_byte_strides(m_shape, m_element_type)),
m_data(data) {
+ const auto constant_size = ov::util::get_memory_size_safe(m_element_type, m_shape);
+ OPENVINO_ASSERT(constant_size, "Cannot calculate byte size for type: ", m_element_type, " and shape: ", m_shape);
+ const auto data_size = ((*constant_size > 0) && data) ? data->size() : 0;
+ OPENVINO_ASSERT(*constant_size == data_size,
+ "The Constant byte size and input data byte size not same: ",
+ *constant_size,
+ " != ",
+ data_size);
constructor_validate_and_infer_types();
}
@@ -383,7 +391,6 @@ std::string Constant::convert_value_to_string(size_t index) const {
}
size_t Constant::get_byte_size() const {
- // Returns 0 when shape is "empty" (equals 0).
// TODO: refactor shape_size(m_shape) calculations and store it as a member.
return shape_size(m_shape) ? m_data->size() : 0;
}
diff --git a/src/core/tests/constant.cpp b/src/core/tests/constant.cpp
index 5d85dc44f79..6878ceb8324 100644
--- a/src/core/tests/constant.cpp
+++ b/src/core/tests/constant.cpp
@@ -2663,6 +2663,7 @@ TEST(constant, hold_shared_memory_different_precision) {
EXPECT_EQ(c.get_data_ptr(), storage.data());
EXPECT_EQ(c.get_vector<uint8_t>(), std::vector<uint8_t>({1, 0, 0, 0, 2, 0}));
EXPECT_EQ(c.cast_vector<uint8_t>(), std::vector<uint8_t>({1, 0, 0, 0, 2, 0}));
+ EXPECT_EQ(c.get_byte_size(), 6);
}
TEST(constant, own_shared_memory) {
@@ -2865,6 +2866,17 @@ TEST(constant, dynamic_type_cast_vector_throws) {
EXPECT_THROW(c.cast_vector<int64_t>(), ov::Exception);
}
+TEST(constant, create_with_incorrect_buffer_size_or_shape_and_precision) {
+ auto buffer = std::make_shared<ov::AlignedBuffer>(100);
+ EXPECT_THROW(std::ignore = ov::op::v0::Constant(element::u8, Shape{10}, buffer), ov::Exception);
+}
+
+TEST(constant, create_with_zero_dim_shape) {
+ auto c = ov::op::v0::Constant(element::u8, Shape{10, 0});
+
+ EXPECT_EQ(c.get_byte_size(), 0);
+}
+
} // namespace test
} // namespace ov
diff --git a/src/core/tests/weight_sharing_util_test.cpp b/src/core/tests/weight_sharing_util_test.cpp
index 6380330a37a..bcd084c653c 100644
--- a/src/core/tests/weight_sharing_util_test.cpp
+++ b/src/core/tests/weight_sharing_util_test.cpp
@@ -84,9 +84,9 @@ const auto create_test_model_weights_from_file = [](const std::filesystem::path&
w_buff->size() / 2,
w_buff);
- auto param = std::make_shared<Parameter>(ov::element::f32, Shape{2, 200});
- auto add = std::make_shared<Add>(param, std::make_shared<Constant>(element::f32, Shape{1, 200}, w1));
- add = std::make_shared<Add>(add, std::make_shared<Constant>(element::f32, Shape{1, 200}, w2));
+ auto param = std::make_shared<Parameter>(ov::element::f32, Shape{2, 2000});
+ auto add = std::make_shared<Add>(param, std::make_shared<Constant>(element::f32, Shape{1, 2000}, w1));
+ add = std::make_shared<Add>(add, std::make_shared<Constant>(element::f32, Shape{1, 2000}, w2));
return std::make_shared<ov::Model>(add->outputs(), "Test model weight from file");
};
@@ -102,7 +102,7 @@ TEST_F(WeightShareExtensionTest, get_constant_id_with_descriptor) {
create_test_weights_file(w_path);
auto w_buffer = ov::load_mmap_object(w_path);
auto w1 = std::make_shared<SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(w_buffer->data() + 200,
- w_buffer->size() - 200,
+ sizeof(float) * 200,
w_buffer);
auto constant = Constant(element::f32, Shape{200}, w1);
@@ -117,7 +117,7 @@ TEST_F(WeightShareExtensionTest, get_constant_source_buffer_check_id) {
auto w_buffer = ov::load_mmap_object(w_path);
auto w1 = std::make_shared<SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(w_buffer->data() + 200,
- w_buffer->size() - 200,
+ sizeof(float) * 200,
w_buffer);
auto constant = Constant(element::f32, Shape{200}, w1);
diff --git a/src/frontends/onnx/frontend/src/core/tensor.cpp b/src/frontends/onnx/frontend/src/core/tensor.cpp
index f6e6e39b5be..c39a6696e31 100644
--- a/src/frontends/onnx/frontend/src/core/tensor.cpp
+++ b/src/frontends/onnx/frontend/src/core/tensor.cpp
@@ -448,25 +448,19 @@ std::shared_ptr<ov::op::v0::Constant> Tensor::get_ov_constant() const {
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 = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, ext_data.load_external_mem_data());
+ constant_buffer = ext_data.load_external_mem_data();
} else if (m_mmap_cache) {
- constant = std::make_shared<ov::op::v0::Constant>(
- ov_type,
- m_shape,
- ext_data.load_external_mmap_data(m_model_dir.string(), m_mmap_cache));
+ constant_buffer = ext_data.load_external_mmap_data(m_model_dir.string(), m_mmap_cache);
} else {
- constant = std::make_shared<ov::op::v0::Constant>(ov_type,
- m_shape,
- ext_data.load_external_data(m_model_dir.string()));
- }
- // ext_data.size() might be zero, need to recalc by using info about actually red data (for byte-size)
- element_count = constant->get_byte_size() / ov_type.size();
- if (ov::element::is_nibble_type(ov_type)) {
- element_count *= 2; // Each byte contains 2 data items, so byte size must be multiplicated
+ constant_buffer = ext_data.load_external_data(m_model_dir.string());
}
- if (element_count != ov::shape_size(m_shape) ||
- (ext_data.size() != 0 && constant->get_byte_size() != ext_data.size())) {
+
+ try {
+ constant = std::make_shared<ov::op::v0::Constant>(ov_type, m_shape, constant_buffer);
+ } catch (const ov::Exception&) {
throw error::invalid_external_data(
"The size of the external data file does not match the byte size of an initializer '" + get_name() +
"' in the model");
diff --git a/src/frontends/onnx/tests/onnx_reader_external_data.cpp b/src/frontends/onnx/tests/onnx_reader_external_data.cpp
index 6e11aab7a46..624cf46d354 100644
--- a/src/frontends/onnx/tests/onnx_reader_external_data.cpp
+++ b/src/frontends/onnx/tests/onnx_reader_external_data.cpp
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include <gtest/gtest.h>
+#include <gmock/gmock.h>
#include <onnx/onnx_pb.h>
#include <algorithm>
@@ -23,6 +23,7 @@ using namespace std;
using namespace ov;
using namespace frontend;
using namespace frontend::onnx::tests;
+using testing::AnyOf, testing::HasSubstr;
// API 2.0 tests
class OnnxFeMmapFixture : public ::testing::TestWithParam<bool> {};
@@ -120,11 +121,10 @@ TEST_P(OnnxFeMmapFixture, onnx_external_data_incorrect_size_exception) {
const auto model = core.read_model(path);
FAIL() << "Incorrect size of external data not detected";
} catch (const Exception& ex) {
- EXPECT_PRED_FORMAT2(
- testing::IsSubstring,
- string(
- "The size of the external data file does not match the byte size of an initializer 'A' in the model"),
- ex.what());
+ EXPECT_THAT(
+ ex.what(),
+ HasSubstr(
+ "The size of the external data file does not match the byte size of an initializer 'A' in the model"));
} catch (...) {
FAIL() << "Importing onnx model failed for unexpected reason";
}