Skip to content

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

Create tensor from IBuffer and expose its memory hints (#39227)

Oleg Pipikin5d07300df3ee
diff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cpp
index 06526c71852..139ab2854e0 100644
--- a/src/core/src/runtime/tensor.cpp
+++ b/src/core/src/runtime/tensor.cpp
@@ -18,7 +18,6 @@
#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/file_util.hpp"
#include "openvino/util/mmap_object.hpp"
#include "openvino/util/parallel_read_streambuf.hpp"
@@ -229,14 +228,8 @@ Tensor read_tensor_data_mmap_impl(std::shared_ptr<MappedMemory> mapped_memory,
const element::Type& element_type,
const PartialShape& partial_shape) {
const auto static_shape = resolve_static_shape(mapped_memory->size(), element_type, partial_shape);
- const auto shared_buffer = std::make_shared<SharedBuffer<std::shared_ptr<MappedMemory>>>(mapped_memory->data(),
- mapped_memory->size(),
- mapped_memory);
- auto tensor = wrap_obj_to_viewtensor(shared_buffer, shared_buffer->get_ptr(), element_type, static_shape);
- if (const auto source_id = mapped_memory->get_id()) {
- set_tensor_source_id(tensor, *source_id);
- }
- return tensor;
+ const auto tensor_impl = make_tensor(element_type, static_shape, std::move(mapped_memory));
+ return make_tensor(tensor_impl);
}
} // namespace
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cpp
index f997b8bec8e..40671059c0f 100644
--- a/src/core/tests/ov_tensor_test.cpp
+++ b/src/core/tests/ov_tensor_test.cpp
@@ -5,8 +5,12 @@
#include <gmock/gmock.h>
#include <cstdint>
+#include <cstring>
#include <filesystem>
#include <fstream>
+#include <numeric>
+#include <utility>
+#include <vector>
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
@@ -24,7 +28,7 @@
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/remote_tensor.hpp"
#include "openvino/runtime/tensor.hpp"
-
+#include "openvino/util/mmap_object.hpp"
namespace ov::test {
using OVTensorTest = ::testing::Test;
using testing::_, testing::HasSubstr;
@@ -1343,4 +1347,123 @@ TEST_F(OVTensorTest, sourceIdAllocatedTensorHasNoSourceId) {
EXPECT_EQ(ov::get_tensor_source_id(tensor), std::nullopt);
}
+namespace {
+class MockMappedMemory final : public ov::MappedMemory {
+public:
+ explicit MockMappedMemory(size_t size) : m_data(size) {}
+
+ const std::byte* data() const noexcept override {
+ return m_data.data();
+ }
+ std::byte* data() noexcept override {
+ return m_data.data();
+ }
+ size_t size() const noexcept override {
+ return m_data.size();
+ }
+ std::optional<uint64_t> get_id() const noexcept override {
+ return 7u;
+ }
+
+ MOCK_METHOD(void, hint_evict, (size_t offset, size_t size), (noexcept, override));
+ MOCK_METHOD(void, hint_prefetch, (size_t offset, size_t size), (noexcept, override));
+ MOCK_METHOD(void, hint_prefetch_async, (size_t offset, size_t size), (override));
+
+private:
+ std::vector<std::byte> m_data;
+};
+
+ov::Tensor make_buffer_tensor(const ov::element::Type& type,
+ const ov::Shape& shape,
+ std::shared_ptr<ov::util::IBuffer> buffer) {
+ const auto tensor_impl = ov::make_tensor(type, shape, std::move(buffer));
+ return ov::make_tensor(tensor_impl);
+}
+} // namespace
+
+TEST_F(OVTensorTest, bufferTensorViewsBufferAndTakesItsId) {
+ auto mapping = std::make_shared<MockMappedMemory>(64);
+ const auto tensor = make_buffer_tensor(ov::element::u8, ov::Shape{64}, mapping);
+ EXPECT_EQ(tensor.data(), static_cast<const void*>(mapping->data()));
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::optional<uint64_t>{7u});
+
+ EXPECT_CALL(*mapping, hint_prefetch(0, ov::auto_size));
+ auto hints = ov::get_memory_hints(tensor);
+ EXPECT_EQ(hints.get(), static_cast<ov::util::IMemoryHints*>(mapping.get()));
+ hints->hint_prefetch();
+}
+
+TEST_F(OVTensorTest, bufferTensorCopyTo) {
+ auto mapping = std::make_shared<MockMappedMemory>(16);
+ std::iota(reinterpret_cast<uint8_t*>(mapping->data()), reinterpret_cast<uint8_t*>(mapping->data()) + 16, 0);
+ const auto tensor = make_buffer_tensor(ov::element::u8, ov::Shape{4, 4}, mapping);
+
+ ov::Tensor dst{ov::element::u8, ov::Shape{4, 4}};
+ tensor.copy_to(dst);
+ EXPECT_EQ(std::memcmp(dst.data(), mapping->data(), 16), 0);
+
+ ov::Tensor roi_dst{ov::element::u8, ov::Shape{2, 2}};
+ ov::Tensor{tensor, {1, 1}, {3, 3}}.copy_to(roi_dst);
+ EXPECT_THAT(std::vector<uint8_t>(roi_dst.data<uint8_t>(), roi_dst.data<uint8_t>() + 4),
+ testing::ElementsAre(5, 6, 9, 10));
+}
+
+TEST_F(OVTensorTest, memoryHintsCoverTensorRangeOfMapping) {
+ auto mapping = std::make_shared<MockMappedMemory>(1024);
+ const auto tensor = make_buffer_tensor(ov::element::u8, ov::Shape{1000}, mapping);
+ const ov::Tensor roi{tensor, {100}, {300}};
+
+ EXPECT_CALL(*mapping, hint_prefetch(0, 1000));
+ EXPECT_CALL(*mapping, hint_evict(0, 1000));
+ EXPECT_CALL(*mapping, hint_prefetch(100, 200));
+ EXPECT_CALL(*mapping, hint_evict(100, 200));
+
+ auto hints = ov::get_memory_hints(tensor);
+ ASSERT_NE(hints, nullptr);
+ hints->hint_prefetch();
+ hints->hint_evict();
+
+ auto roi_hints = ov::get_memory_hints(ov::Tensor{roi, {0}, {200}});
+ ASSERT_NE(roi_hints, nullptr);
+ roi_hints->hint_prefetch();
+ roi_hints->hint_evict();
+}
+
+TEST_F(OVTensorTest, memoryHintsNotAvailableWithoutBufferOrForStridedRoi) {
+ float data[16] = {};
+ ov::Tensor tensor{ov::element::f32, ov::Shape{4, 4}, data};
+ EXPECT_EQ(ov::get_memory_hints(tensor), nullptr);
+
+ const auto mapped = make_buffer_tensor(ov::element::f32, ov::Shape{4, 4}, std::make_shared<MockMappedMemory>(64));
+ EXPECT_EQ(ov::get_memory_hints(ov::Tensor{mapped, {0, 1}, {4, 3}}), nullptr);
+}
+
+TEST_F(OVTensorTest, bufferTensorRejectsMissingOrTooSmallBuffer) {
+ EXPECT_THROW(ov::make_tensor(ov::element::u8, ov::Shape{1}, std::shared_ptr<ov::util::IBuffer>{}), ov::Exception);
+ EXPECT_THROW(ov::make_tensor(ov::element::u8, ov::Shape{65}, std::make_shared<MockMappedMemory>(64)),
+ ov::Exception);
+}
+
+TEST_F(OVTensorTest, memoryHintsAvailableForMmapTensor) {
+ auto tmp_path = ov::test::utils::generateTestFilePrefix() + "_ov_memory_hints_test.bin";
+ {
+ std::ofstream f(tmp_path, std::ios::binary);
+ float data[6] = {1.f, 2.f, 3.f, 4.f, 5.f, 6.f};
+ f.write(reinterpret_cast<const char*>(data), sizeof(data));
+ }
+
+ {
+ auto tensor = ov::read_tensor_data(tmp_path, ov::element::f32, ov::PartialShape{2, 3}, 0, true);
+ auto hints = ov::get_memory_hints(tensor);
+ ASSERT_NE(hints, nullptr);
+ hints->hint_evict();
+ hints->hint_prefetch();
+ EXPECT_EQ(tensor.data<const float>()[5], 6.f);
+ EXPECT_EQ(
+ ov::get_memory_hints(ov::read_tensor_data(tmp_path, ov::element::f32, ov::PartialShape{2, 3}, 0, false)),
+ nullptr);
+ }
+ std::filesystem::remove(tmp_path);
+}
+
} // namespace ov::test
diff --git a/src/inference/dev_api/openvino/runtime/make_tensor.hpp b/src/inference/dev_api/openvino/runtime/make_tensor.hpp
index d44dbdc668c..c48aefb59f2 100644
--- a/src/inference/dev_api/openvino/runtime/make_tensor.hpp
+++ b/src/inference/dev_api/openvino/runtime/make_tensor.hpp
@@ -18,6 +18,11 @@
namespace ov {
+namespace util {
+class IBuffer;
+class IMemoryHints;
+} // namespace util
+
/**
* @brief Constructs Tensor using element type and shape. Allocate internal host storage using default allocator
* @param type Tensor element type
@@ -56,6 +61,17 @@ OPENVINO_RUNTIME_API std::shared_ptr<ITensor> make_tensor(const element::Type ty
const void* host_ptr,
const Strides& strides = {});
+/**
+ * @brief Constructs read-only Tensor viewing the beginning of @p buffer and keeping it alive.
+ * @note The tensor takes the source ID of the buffer and gives access to its memory hints, see get_memory_hints().
+ * @param type Tensor element type
+ * @param shape Tensor shape, its byte size must not exceed the buffer size
+ * @param buffer Buffer holding the tensor data
+ */
+OPENVINO_RUNTIME_API std::shared_ptr<ITensor> make_tensor(const element::Type type,
+ const Shape& shape,
+ std::shared_ptr<util::IBuffer> buffer);
+
/**
* @brief Constructs region of interest (ROI) tensor form another tensor.
* @note Does not perform memory allocation internally
@@ -119,4 +135,14 @@ OPENVINO_RUNTIME_API std::optional<uint64_t> get_tensor_source_id(const ov::Tens
*/
OPENVINO_RUNTIME_API void set_tensor_source_id(ov::Tensor& tensor, uint64_t id);
+/**
+ * @brief Returns residency hints for exactly the tensor bytes; an ROI tensor gets hints for its own sub-range.
+ *
+ * @param tensor OpenVINO Tensor.
+ *
+ * @return The hints, or nullptr if the tensor was not made from a buffer, is not continuous, or covers a sub-range of
+ * a buffer other than a file mapping.
+ */
+OPENVINO_RUNTIME_API std::shared_ptr<ov::util::IMemoryHints> get_memory_hints(const ov::Tensor& tensor);
+
} // namespace ov
diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cpp
index 9ef2032db14..de4806710c2 100644
--- a/src/inference/src/dev/make_tensor.cpp
+++ b/src/inference/src/dev/make_tensor.cpp
@@ -6,12 +6,14 @@
#include <memory>
#include <mutex>
+#include <utility>
#include "openvino/core/memory_util.hpp"
#include "openvino/core/type/element_type_info.hpp"
#include "openvino/runtime/iremote_tensor.hpp"
#include "openvino/runtime/properties.hpp"
#include "openvino/runtime/tensor.hpp"
+#include "openvino/util/mmap_object.hpp"
#ifdef PROXY_PLUGIN_ENABLED
# include "openvino/proxy/plugin.hpp"
#endif
@@ -39,6 +41,28 @@ Shape make_roi_shape(const Shape& tensor_shape, const Coordinate& begin, const C
return roi_shape;
}
+
+// Hints for the [offset, offset + size) range of a file mapping.
+class MappedRangeHints final : public util::IMemoryHints {
+public:
+ MappedRangeHints(std::shared_ptr<MappedMemory> memory, size_t offset, size_t size)
+ : m_memory{std::move(memory)},
+ m_offset{offset},
+ m_size{size} {}
+
+ void hint_evict() noexcept override {
+ m_memory->hint_evict(m_offset, m_size);
+ }
+
+ void hint_prefetch() noexcept override {
+ m_memory->hint_prefetch(m_offset, m_size);
+ }
+
+private:
+ std::shared_ptr<MappedMemory> m_memory;
+ size_t m_offset;
+ size_t m_size;
+};
} // namespace
/**
@@ -188,6 +212,35 @@ public:
}
};
+/**
+ * @brief Read-only view tensor on the beginning of a buffer which it keeps alive
+ */
+class BufferReadOnlyViewTensor : public ReadOnlyViewTensor {
+public:
+ BufferReadOnlyViewTensor(const element::Type element_type,
+ const Shape& shape,
+ std::shared_ptr<util::IBuffer> buffer)
+ : ReadOnlyViewTensor{element_type, shape, buffer->data()},
+ m_buffer{std::move(buffer)} {
+ OPENVINO_ASSERT(get_byte_size() <= m_buffer->size(), "Tensor does not fit into the buffer");
+ m_source_id = m_buffer->get_id();
+ }
+
+ const std::shared_ptr<util::IBuffer>& get_buffer() const {
+ return m_buffer;
+ }
+
+private:
+ std::shared_ptr<util::IBuffer> m_buffer;
+};
+
+std::shared_ptr<ITensor> make_tensor(const element::Type type,
+ const Shape& shape,
+ std::shared_ptr<util::IBuffer> buffer) {
+ OPENVINO_ASSERT(buffer, "Buffer is not set");
+ return std::make_shared<BufferReadOnlyViewTensor>(type, shape, std::move(buffer));
+}
+
/**
* @brief View tensor on external memory with strides
*/
@@ -454,6 +507,10 @@ public:
return m_offset;
}
+ const std::shared_ptr<ITensor>& get_owner() const {
+ return m_owner;
+ }
+
protected:
std::shared_ptr<ITensor> m_owner;
Shape m_shape;
@@ -653,4 +710,31 @@ void set_tensor_source_id(ov::Tensor& tensor, uint64_t id) {
}
}
+namespace {
+std::shared_ptr<BufferReadOnlyViewTensor> get_buffer_owner(const std::shared_ptr<ITensor>& itensor) {
+ if (auto roi = std::dynamic_pointer_cast<BaseRoiTensor>(itensor)) {
+ return get_buffer_owner(roi->get_owner());
+ }
+ return std::dynamic_pointer_cast<BufferReadOnlyViewTensor>(itensor);
+}
+} // namespace
+
+std::shared_ptr<util::IMemoryHints> get_memory_hints(const ov::Tensor& tensor) {
+ const auto owner = get_buffer_owner(get_tensor_impl(tensor)._ptr);
+ if (!owner || !tensor.is_continuous()) {
+ return nullptr;
+ }
+ const auto& buffer = owner->get_buffer();
+ const auto offset = static_cast<size_t>(static_cast<const std::byte*>(tensor.data()) - buffer->data());
+ const auto size = tensor.get_byte_size();
+ if (offset == 0 && size == buffer->size()) {
+ return buffer;
+ }
+ // shortcut: only file mappings give hints for a sub-range, add ranged hints to IMemoryHints if others need it.
+ if (auto mapping = std::dynamic_pointer_cast<MappedMemory>(buffer)) {
+ return std::make_shared<MappedRangeHints>(std::move(mapping), offset, size);
+ }
+ return nullptr;
+}
+
} // namespace ov