Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/core/src/runtime/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,27 @@ Tensor read_tensor_data(const std::filesystem::path& file_name,
bool mmap) {
OPENVINO_ASSERT(element_type != ov::element::string);
auto static_shape = calc_static_shape_for_file(file_name, element_type, partial_shape, offset_in_bytes);
const void* data_ptr;
std::shared_ptr<void> shared_obj;
if (mmap) {
auto mapped_memory = ov::load_mmap_object(file_name);
auto shared_buffer = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(
mapped_memory->data() + offset_in_bytes,
mapped_memory->size() - offset_in_bytes,
mapped_memory);

auto view_tensor = Tensor(element_type, static_shape, shared_buffer->get_ptr());
auto impl = get_tensor_impl(view_tensor);
impl._so = shared_buffer;
view_tensor = make_tensor(impl);
return view_tensor;
data_ptr = shared_buffer->get_ptr();
shared_obj = shared_buffer;
} else {
ov::Tensor tensor(element_type, static_shape);
read_tensor_data(file_name, tensor, offset_in_bytes);
return tensor;
auto tensor = std::make_shared<ov::Tensor>(element_type, static_shape);
read_tensor_data(file_name, *tensor.get(), offset_in_bytes);
data_ptr = tensor->data();
shared_obj = tensor;
}
auto view_tensor = Tensor(element_type, static_shape, data_ptr);
auto impl = get_tensor_impl(view_tensor);
impl._so = shared_obj;
view_tensor = make_tensor(impl);
return view_tensor;

}
} // namespace ov
43 changes: 23 additions & 20 deletions src/core/tests/tensor_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ TYPED_TEST_SUITE_P(ParametredOffloadTensorTest);

TYPED_TEST_P(ParametredOffloadTensorTest, read_tensor) {
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(this->file_name, this->ov_type, this->shape, 0));
const auto tensor = read_tensor_data(this->file_name, this->ov_type, this->shape, 0);
EXPECT_EQ(0, memcmp(tensor.data(), this->initial_tensor.data(), this->data_size));
}
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(this->file_name, this->ov_type, this->shape, 0, false));
const auto tensor = read_tensor_data(this->file_name, this->ov_type, this->shape, 0, false);
EXPECT_EQ(0, memcmp(tensor.data(), this->initial_tensor.data(), this->data_size));
}
}
Expand Down Expand Up @@ -135,13 +133,11 @@ TEST_F(FunctionalOffloadTensorTest, read_with_offset) {
ASSERT_TRUE(std::filesystem::exists(file_name));

{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, shape, sizeof(float)));
const ov::Tensor tensor = read_tensor_data(file_name, ov_type, shape, sizeof(float));
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, shape, sizeof(float), false));
const ov::Tensor tensor = read_tensor_data(file_name, ov_type, shape, sizeof(float), false);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
}
Expand Down Expand Up @@ -171,23 +167,19 @@ TEST_F(FunctionalOffloadTensorTest, read_too_big_offset) {

TEST_F(FunctionalOffloadTensorTest, read_dynamic_shape) {
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, PartialShape::dynamic(1), 0));
const ov::Tensor tensor = read_tensor_data(file_name, ov_type, PartialShape::dynamic(1), 0);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, PartialShape::dynamic(1), 0, false));
const ov::Tensor tensor = read_tensor_data(file_name, ov_type, PartialShape::dynamic(1), 0, false);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name));
const ov::Tensor tensor = read_tensor_data(file_name);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov::element::u8, PartialShape::dynamic(1), 0, false));
const ov::Tensor tensor = read_tensor_data(file_name, ov::element::u8, PartialShape::dynamic(1), 0, false);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
}
Expand All @@ -197,14 +189,12 @@ TEST_F(FunctionalOffloadTensorTest, read_1_dynamic_dimension) {
size_t dynamic_dimension_number = shape_with_1_dynamic_dimension.size() - 1;
shape_with_1_dynamic_dimension[dynamic_dimension_number] = -1;
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, shape_with_1_dynamic_dimension, 0));
const ov::Tensor tensor = read_tensor_data(file_name, ov_type, shape_with_1_dynamic_dimension, 0);
EXPECT_EQ(tensor.get_shape()[dynamic_dimension_number], shape.get_shape()[dynamic_dimension_number]);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
{
ov::Tensor tensor;
EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, shape_with_1_dynamic_dimension, 0, false));
const ov::Tensor tensor = read_tensor_data(file_name, ov_type, shape_with_1_dynamic_dimension, 0, false);
EXPECT_EQ(tensor.get_shape()[dynamic_dimension_number], shape.get_shape()[dynamic_dimension_number]);
EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));
}
Expand Down Expand Up @@ -243,4 +233,17 @@ TEST_F(FunctionalOffloadTensorTest, read_null_shape) {
new_shape[1] = Dimension::dynamic();
{ EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, new_shape, 0), ov::Exception); }
}

TEST_F(FunctionalOffloadTensorTest, not_const_throw) {
{
const auto const_tensor = read_tensor_data(file_name);
EXPECT_NO_THROW(const_tensor.data());
}
{
auto tensor = read_tensor_data(file_name);
EXPECT_THROW(tensor.data(), ov::Exception);
const auto& const_tensor = tensor;
EXPECT_NO_THROW(const_tensor.data());
}
}
} // namespace ov::test
Loading