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
Mmaped tensors (#29175)
Oleg Pipikin4ecaa5dbe990
diff --git a/src/core/dev_api/openvino/runtime/shared_buffer.hpp b/src/core/dev_api/openvino/runtime/shared_buffer.hppindex 3ea97db6a19..6c8cd04f8ef 100644--- a/src/core/dev_api/openvino/runtime/shared_buffer.hpp+++ b/src/core/dev_api/openvino/runtime/shared_buffer.hpp@@ -13,7 +13,7 @@ template <typename T>class SharedBuffer : public ov::AlignedBuffer {public:SharedBuffer(char* data, size_t size, const T& shared_object) : _shared_object(shared_object) {- m_allocated_buffer = data;+ m_allocated_buffer = nullptr;m_aligned_buffer = data;m_byte_size = size;}diff --git a/src/core/include/openvino/runtime/tensor.hpp b/src/core/include/openvino/runtime/tensor.hppindex 7cc1352ab32..f77e69d2500 100644--- a/src/core/include/openvino/runtime/tensor.hpp+++ b/src/core/include/openvino/runtime/tensor.hpp@@ -9,9 +9,11 @@*/#pragma once+#include <filesystem>#include <type_traits>#include "openvino/core/coordinate.hpp"+#include "openvino/core/partial_shape.hpp"#include "openvino/core/rtti.hpp"#include "openvino/core/shape.hpp"#include "openvino/core/type/element_type.hpp"@@ -259,4 +261,18 @@ public:*/using TensorVector = std::vector<Tensor>;+/// \brief Read a tensor content from a file. Only raw data is loaded.+/// \param file_name Path to file to read.+/// \param element_type Element type, when not specified the it is assumed as element::u8.+/// \param shape Shape for resulting tensor. If provided shape is static, specified number of elements is read only.+/// File should contain enough bytes, an exception is raised otherwise.+/// One of the dimensions can be dynamic. In this case it will be determined automatically based on the+/// length of the file content and `offset`. Default value is [?].+/// \param offset_in_bytes Read file starting from specified offset. Default is 0. The remining size of the file should+/// be compatible with shape.+OPENVINO_API+Tensor read_tensor_data(const std::filesystem::path& file_name,+ const element::Type& element_type = element::u8,+ const PartialShape& shape = PartialShape::dynamic(1),+ std::size_t offset_in_bytes = 0);} // namespace ovdiff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cppindex 90ecf442728..cdb54fcc61f 100644--- a/src/core/src/runtime/tensor.cpp+++ b/src/core/src/runtime/tensor.cpp@@ -11,9 +11,13 @@#include "openvino/core/shape.hpp"#include "openvino/core/shape_util.hpp"#include "openvino/core/strides.hpp"+#include "openvino/core/tensor_util.hpp"+#include "openvino/core/type/element_iterator.hpp"#include "openvino/runtime/itensor.hpp"#include "openvino/runtime/make_tensor.hpp"#include "openvino/runtime/remote_tensor.hpp"+#include "openvino/runtime/shared_buffer.hpp"+#include "openvino/util/mmap_object.hpp"namespace ov {@@ -108,4 +112,76 @@ bool Tensor::is_continuous() const {OV_TENSOR_STATEMENT(return _impl->is_continuous());}+namespace {+ov::Shape calc_static_shape_for_file(const std::filesystem::path& file_name,+ const ov::element::Type& element_type,+ const ov::PartialShape& partial_shape,+ size_t offset) {+ auto file_size = std::filesystem::file_size(file_name);+ if (partial_shape.is_static()) {+ auto static_shape = partial_shape.get_shape();+ OPENVINO_ASSERT((ov::shape_size(static_shape)) * element_type.bitwidth() + offset * 8 == file_size * 8,+ "Cannot fit file size into requested static PartialShape");+ return static_shape;+ }+ auto partial_shape_copy = partial_shape;+ auto rank = partial_shape_copy.rank();+ OPENVINO_ASSERT(rank.is_static(), "Rank cannot be dynamic");+ std::vector<size_t> dynamic_dimension_numbers;+ size_t slice_size = 1;+ for (size_t id = 0; id < partial_shape_copy.size(); ++id) {+ if (partial_shape_copy[id].is_dynamic()) {+ dynamic_dimension_numbers.push_back(id);+ } else {+ slice_size *= partial_shape_copy[id].get_min_length();+ }+ }+ OPENVINO_ASSERT(dynamic_dimension_numbers.size() == 1,+ "Only one dynamic dimension in input shape is supported but got: ",+ dynamic_dimension_numbers.size());+ auto& dynamic_dimension = partial_shape_copy[dynamic_dimension_numbers[0]];++ OPENVINO_ASSERT(file_size > offset, "Offset is bigger than size of file to read.");+ auto file_size_to_read = file_size - offset;++ OPENVINO_ASSERT((file_size_to_read * 8) % element_type.bitwidth() == 0,+ "cannot fit ",+ element_type.get_type_name(),+ " into ",+ file_size_to_read,+ " bytes");+ auto elements_to_read = file_size_to_read * 8 / element_type.bitwidth();++ auto new_dimension_size = elements_to_read / slice_size;+ OPENVINO_ASSERT(new_dimension_size * slice_size == elements_to_read,+ "Cannot fit file size into requested PartialShape");++ OPENVINO_ASSERT(dynamic_dimension.compatible(new_dimension_size),+ "Cannot fit file size into requested PartialShape");++ dynamic_dimension = Dimension(new_dimension_size);+ return partial_shape_copy.get_shape();+}+} // namespace++Tensor read_tensor_data(const std::filesystem::path& file_name,+ const ov::element::Type& element_type,+ const ov::PartialShape& partial_shape,+ size_t offset_in_bytes) {+ OPENVINO_ASSERT(element_type != ov::element::string);+ auto static_shape = calc_static_shape_for_file(file_name, element_type, partial_shape, offset_in_bytes);++ 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;+}} // namespace ovdiff --git a/src/core/tests/tensor_utils.cpp b/src/core/tests/tensor_utils.cppnew file mode 100644index 00000000000..f38770f720c--- /dev/null+++ b/src/core/tests/tensor_utils.cpp@@ -0,0 +1,204 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "common_test_utils/common_utils.hpp"+#include "common_test_utils/data_utils.hpp"+#include "openvino/core/type/element_iterator.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/reference/convert.hpp"+#include "openvino/runtime/tensor.hpp"++namespace ov::test {+template <typename element_type>+class ParametredOffloadTensorTest : public ::testing::Test {+public:+ static constexpr ov::element::Type ov_type = ov::element::from<element_type>();++ void SetUp() override {+ shape = {10, 20, 30, 40};+ auto static_shape = shape.get_shape();+ initial_tensor = Tensor(ov_type, static_shape);+ std::vector<float> init_values(initial_tensor.get_size());+ ov::test::utils::fill_data_random(init_values.data(), initial_tensor.get_size(), 10, 0, 100);++ ov::reference::convert(ov::element::iterator<ov::element::f32>(init_values.data()),+ ov::element::iterator<ov_type>(initial_tensor.data()),+ shape_size(static_shape));++ file_name = ov::test::utils::generateTestFilePrefix();+ data_size = this->initial_tensor.get_byte_size();+ {+ std::ofstream fout(this->file_name, std::ios::binary);+ fout.write(reinterpret_cast<char*>(this->initial_tensor.data()), data_size);+ }+ ASSERT_TRUE(std::filesystem::exists(this->file_name));+ }++ void TearDown() override {+ remove_file();+ }++ void remove_file() {+ if (std::filesystem::exists(file_name))+ std::filesystem::remove(file_name);+ }++ ov::PartialShape shape;+ ov::Tensor initial_tensor;+ std::filesystem::path file_name;+ size_t data_size;+};++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));+ EXPECT_EQ(0, memcmp(tensor.data(), this->initial_tensor.data(), this->data_size));+ }+}++REGISTER_TYPED_TEST_SUITE_P(ParametredOffloadTensorTest, read_tensor);++using TypesToTest = ::testing::Types<float,+ double,+ int8_t,+ int16_t,+ int32_t,+ int64_t,+ uint8_t,+ uint16_t,+ uint32_t,+ uint64_t,+ ov::bfloat16,+ ov::float8_e4m3,+ ov::float8_e5m2,+ ov::float4_e2m1,+ ov::float8_e8m0>;++INSTANTIATE_TYPED_TEST_SUITE_P(OffloadTensorTest, ParametredOffloadTensorTest, TypesToTest);++TEST(OffloadTensorTest, string_tensor_throws) {+ auto file_name = ov::test::utils::generateTestFilePrefix();+ {+ std::ofstream fout(file_name, std::ios::binary);+ fout << "Hello, world!";+ fout.close();+ ASSERT_TRUE(std::filesystem::exists(file_name));+ EXPECT_THROW(read_tensor_data(file_name, ov::element::string), ov::Exception);+ std::filesystem::remove(file_name);+ }+}++class FunctionalOffloadTensorTest : public ::testing::Test {+public:+ void SetUp() override {+ auto elements_number = ov::shape_size(shape.get_shape());+ data_size = elements_number * sizeof(float);+ init_values.resize(elements_number);+ ov::test::utils::fill_data_random(init_values.data(), elements_number, 10, 0, 100);++ file_name = ov::test::utils::generateTestFilePrefix();+ {+ std::ofstream fout(file_name, std::ios::binary);+ fout.write(reinterpret_cast<char*>(init_values.data()), data_size);+ }+ ASSERT_TRUE(std::filesystem::exists(file_name));+ }++ void TearDown() override {+ remove_file();+ }++ void remove_file() {+ if (std::filesystem::exists(file_name))+ std::filesystem::remove(file_name);+ }++ ov::element::Type ov_type{ov::element::f32};+ ov::PartialShape shape{1, 2, 3, 4};+ size_t data_size;+ std::vector<float> init_values;+ std::string file_name;+};++TEST_F(FunctionalOffloadTensorTest, read_with_offset) {+ {+ float dummy = 0;+ std::ofstream fout(file_name, std::ios::binary);+ fout.write(reinterpret_cast<char*>(&dummy), sizeof(float));+ fout.write(reinterpret_cast<char*>(init_values.data()), data_size);+ }+ ASSERT_TRUE(std::filesystem::exists(file_name));++ {+ ov::Tensor tensor;+ EXPECT_NO_THROW(tensor = read_tensor_data(file_name, ov_type, shape, sizeof(float)));+ EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));+ }+}++TEST_F(FunctionalOffloadTensorTest, read_small_file) {+ auto new_shape = shape;+ new_shape[0] = 10;+ { EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, new_shape, 0), ov::Exception); }+}++TEST_F(FunctionalOffloadTensorTest, read_too_big_offset) {+ {+ // offset + data_size > file_size+ EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, shape, 1), ov::Exception);+ // offset == file_size+ EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, shape, data_size), ov::Exception);+ // offset > file_size+ EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, shape, data_size + 1), ov::Exception);+ }+}++TEST_F(FunctionalOffloadTensorTest, read_dynamic_shape) {+ {+ ov::Tensor tensor;+ EXPECT_NO_THROW(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));+ EXPECT_EQ(0, memcmp(tensor.data(), init_values.data(), data_size));+ }+}++TEST_F(FunctionalOffloadTensorTest, read_1_dynamic_dimension) {+ {+ ov::Tensor tensor;+ auto shape_with_1_dynamic_dimension = shape;+ size_t dynamic_dimension_number = shape_with_1_dynamic_dimension.size() - 1;+ shape_with_1_dynamic_dimension[dynamic_dimension_number] = -1;+ EXPECT_NO_THROW(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));+ }+}++TEST_F(FunctionalOffloadTensorTest, read_wrong_dynamic_shape) {+ {+ auto shape_with_1_dynamic_dimension = shape;+ shape_with_1_dynamic_dimension[shape_with_1_dynamic_dimension.size() - 1] = -1;+ shape_with_1_dynamic_dimension[shape_with_1_dynamic_dimension.size() - 2] = 100;+ EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, shape_with_1_dynamic_dimension, 0),+ ov::Exception);+ }+}++TEST_F(FunctionalOffloadTensorTest, read_type_doesnt_fit_file_size) {+ {+ std::ofstream fout(file_name, std::ios::binary);+ fout.write(reinterpret_cast<char*>(init_values.data()), data_size - 1);+ }+ ASSERT_TRUE(std::filesystem::exists(file_name));++ { EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov::element::f32), ov::Exception); }+}+} // namespace ov::test