Skip to content

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

Source id for ov::Tensor (#34607)

Oleg Pipikined55e5f2bd53
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hpp
index 8d0b2844b85..38c002ed43e 100644
--- a/src/common/util/include/openvino/util/common_util.hpp
+++ b/src/common/util/include/openvino/util/common_util.hpp
@@ -307,6 +307,5 @@ constexpr bool mul_overflow(T x, T y, T& result) {
return false;
#endif
}
-
} // namespace util
} // namespace ov
diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hpp
index 692c98d5ad9..66931b852b0 100644
--- a/src/common/util/include/openvino/util/file_util.hpp
+++ b/src/common/util/include/openvino/util/file_util.hpp
@@ -5,12 +5,14 @@
#pragma once
#include <cstdint>
+#include <filesystem>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <string>
#include <vector>
+#include "openvino/util/common_util.hpp"
#include "openvino/util/file_path.hpp"
#include "openvino/util/util.hpp"
#include "openvino/util/wstring_convert_util.hpp"
@@ -274,4 +276,7 @@ void save_binary(const std::filesystem::path& path, const void* binary, size_t b
*/
const char* trim_file_name(const char* const fname);
+inline uint64_t get_id_for_file(const std::filesystem::path& path, size_t offset, size_t size) {
+ return util::u64_hash_combine(std::filesystem::hash_value(path), {offset, size});
+}
} // namespace ov::util
diff --git a/src/common/util/src/os/lin/lin_mmap_object.cpp b/src/common/util/src/os/lin/lin_mmap_object.cpp
index fd151082d20..9319fcffbf4 100644
--- a/src/common/util/src/os/lin/lin_mmap_object.cpp
+++ b/src/common/util/src/os/lin/lin_mmap_object.cpp
@@ -81,7 +81,7 @@ public:
" for mapping. Ensure that file exists and has appropriate permissions.");
}
set_from_fd(fd, offset, size);
- m_id = util::u64_hash_combine(std::filesystem::hash_value(path), {offset, size});
+ m_id = util::get_id_for_file(path, offset, size);
}
void set_from_fd(const int fd, const size_t offset, const size_t size) {
diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cpp
index d9f9f9b3b30..4fc05d05972 100644
--- a/src/common/util/src/os/win/win_mmap_object.cpp
+++ b/src/common/util/src/os/win/win_mmap_object.cpp
@@ -78,7 +78,7 @@ public:
const auto h =
::CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
map(path, h, offset, size);
- m_id = util::u64_hash_combine(std::filesystem::hash_value(path), {offset, size});
+ m_id = util::get_id_for_file(path, offset, size);
}
void set_from_handle(HANDLE h, size_t offset, size_t size) {
diff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cpp
index 7a5016392bc..c9abf18e627 100644
--- a/src/core/src/runtime/tensor.cpp
+++ b/src/core/src/runtime/tensor.cpp
@@ -19,6 +19,7 @@
#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"
namespace ov {
@@ -218,7 +219,9 @@ Tensor read_tensor_data_mmap_impl(std::shared_ptr<ov::MappedMemory> mapped_memor
std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data(),
mapped_memory->size(),
mapped_memory);
- return wrap_obj_to_viewtensor(shared_buffer, shared_buffer->get_ptr(), element_type, static_shape);
+ auto tensor = wrap_obj_to_viewtensor(shared_buffer, shared_buffer->get_ptr(), element_type, static_shape);
+ set_tensor_source_id(tensor, mapped_memory->get_id());
+ return tensor;
}
size_t get_size_for_mapping(const ov::element::Type& element_type, const ov::PartialShape& partial_shape) {
@@ -259,7 +262,9 @@ Tensor read_tensor_data(const std::filesystem::path& file_name,
const auto static_shape = resolve_static_shape(available_size, element_type, partial_shape);
const auto tensor = std::make_shared<ov::Tensor>(element_type, static_shape);
read_tensor_via_ifstream(file_name, *tensor.get(), offset_in_bytes);
- return wrap_obj_to_viewtensor(tensor, tensor->data(), element_type, static_shape);
+ auto result = wrap_obj_to_viewtensor(tensor, tensor->data(), element_type, static_shape);
+ set_tensor_source_id(result, util::get_id_for_file(file_name, offset_in_bytes, tensor->get_byte_size()));
+ return result;
}
}
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cpp
index 515c3b1e3e5..6dd2fa2e1de 100644
--- a/src/core/tests/ov_tensor_test.cpp
+++ b/src/core/tests/ov_tensor_test.cpp
@@ -5,7 +5,10 @@
#include <gmock/gmock.h>
#include <cstdint>
+#include <filesystem>
+#include <fstream>
+#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/partial_shape.hpp"
@@ -16,6 +19,7 @@
#include "openvino/op/constant.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/reference/utils/coordinate_transform.hpp"
+#include "openvino/runtime/aligned_buffer.hpp"
#include "openvino/runtime/allocator.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/remote_tensor.hpp"
@@ -1245,4 +1249,98 @@ INSTANTIATE_TEST_SUITE_P(copy_tests_strings,
}
)));
// clang-format on
+
+TEST_F(OVTensorTest, sourceIdSetGet) {
+ float data[6] = {};
+ ov::Tensor tensor{ov::element::f32, ov::Shape{2, 3}, data};
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::nullopt);
+ ov::set_tensor_source_id(tensor, 42u);
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::optional<uint64_t>{42u});
+}
+
+TEST_F(OVTensorTest, sourceIdViewTensor) {
+ float data[6] = {};
+ ov::Tensor tensor{ov::element::f32, ov::Shape{2, 3}, data};
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::nullopt);
+ ov::set_tensor_source_id(tensor, 99u);
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::optional<uint64_t>{99u});
+}
+
+TEST_F(OVTensorTest, sourceIdRoiTensorHasNoSourceId) {
+ float data[16] = {};
+ ov::Tensor parent{ov::element::f32, ov::Shape{4, 4}, data};
+ ov::set_tensor_source_id(parent, 7u);
+ EXPECT_EQ(ov::get_tensor_source_id(parent), std::optional<uint64_t>{7u});
+
+ // ROI tensor does not expose source_id (only ViewTensor does)
+ ov::Tensor roi{parent, {1, 0}, {3, 4}};
+ EXPECT_EQ(ov::get_tensor_source_id(roi), std::nullopt);
+
+ // Setting source_id on ROI is a no-op — parent is unchanged
+ ov::set_tensor_source_id(roi, 11u);
+ EXPECT_EQ(ov::get_tensor_source_id(parent), std::optional<uint64_t>{7u});
+}
+
+TEST_F(OVTensorTest, sourceIdCopyToDoesNotCopyId) {
+ float src_data[6] = {}, dst_data[6] = {};
+ ov::Tensor src{ov::element::f32, ov::Shape{2, 3}, src_data};
+ ov::set_tensor_source_id(src, 42u);
+
+ ov::Tensor dst{ov::element::f32, ov::Shape{2, 3}, dst_data};
+ EXPECT_EQ(ov::get_tensor_source_id(dst), std::nullopt);
+
+ src.copy_to(dst);
+ EXPECT_EQ(ov::get_tensor_source_id(dst), std::nullopt);
+}
+
+TEST_F(OVTensorTest, sourceIdFreeHelpers) {
+ float data[6] = {};
+ ov::Tensor tensor{ov::element::f32, ov::Shape{2, 3}, data};
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::nullopt);
+ ov::set_tensor_source_id(tensor, 55u);
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::optional<uint64_t>{55u});
+}
+
+TEST_F(OVTensorTest, sourceIdMmapTensorHasNonZeroId) {
+ // Create a temporary file with some data
+ auto tmp_path = ov::test::utils::generateTestFilePrefix() + "_ov_source_id_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 source_id = ov::get_tensor_source_id(tensor);
+ ASSERT_TRUE(source_id.has_value());
+ EXPECT_NE(source_id.value(), 0u);
+ }
+ std::filesystem::remove(tmp_path);
+}
+
+TEST_F(OVTensorTest, sourceIdIfstreamTensorHasNonZeroId) {
+ auto tmp_path = ov::test::utils::generateTestFilePrefix() + "_ov_source_id_ifstream_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, false);
+ auto source_id = ov::get_tensor_source_id(tensor);
+ ASSERT_TRUE(source_id.has_value());
+ EXPECT_NE(source_id.value(), 0u);
+ }
+ std::filesystem::remove(tmp_path);
+}
+
+TEST_F(OVTensorTest, sourceIdAllocatedTensorHasNoSourceId) {
+ // Allocated tensors (without external data pointer) are not ViewTensors
+ // and therefore do not have a source_id
+ ov::Tensor tensor{ov::element::f32, ov::Shape{2, 3}};
+ EXPECT_EQ(ov::get_tensor_source_id(tensor), std::nullopt);
+}
+
} // 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 725c2c41f0f..d44dbdc668c 100644
--- a/src/inference/dev_api/openvino/runtime/make_tensor.hpp
+++ b/src/inference/dev_api/openvino/runtime/make_tensor.hpp
@@ -9,10 +9,12 @@
#pragma once
+#include <optional>
+
+#include "openvino/runtime/allocator.hpp"
#include "openvino/runtime/common.hpp"
#include "openvino/runtime/itensor.hpp"
#include "openvino/runtime/so_ptr.hpp"
-#include "openvino/runtime/allocator.hpp"
namespace ov {
@@ -97,4 +99,24 @@ OPENVINO_RUNTIME_API ov::SoPtr<ov::ITensor> get_tensor_impl(const ov::Tensor& te
*/
OPENVINO_RUNTIME_API size_t get_tensor_data_offset(const ov::ITensor& tensor);
+/**
+ * @brief Returns the data source ID associated with a tensor
+ *
+ * @details The source ID identifies the origin of the tensor's data (e.g., a memory-mapped file).
+ * A value of std::nullopt means "no ID" / invalid.
+ *
+ * @param tensor Tensor implementation to get the source ID from.
+ *
+ * @return The source ID, or std::nullopt if not set.
+ */
+OPENVINO_RUNTIME_API std::optional<uint64_t> get_tensor_source_id(const ov::Tensor& tensor);
+
+/**
+ * @brief Sets the data source ID for a tensor if the tensor implementation supports it.
+ *
+ * @param tensor OpenVINO Tensor to set the source ID on.
+ * @param id The source ID to associate with the tensor.
+ */
+OPENVINO_RUNTIME_API void set_tensor_source_id(ov::Tensor& tensor, uint64_t id);
+
} // namespace ov
diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cpp
index b51257b0f62..d95002f0bf0 100644
--- a/src/inference/src/dev/make_tensor.cpp
+++ b/src/inference/src/dev/make_tensor.cpp
@@ -120,6 +120,14 @@ public:
return m_strides;
}
+ std::optional<uint64_t> get_source_id() const {
+ return m_source_id;
+ }
+
+ void set_source_id(uint64_t id) {
+ m_source_id = id;
+ }
+
protected:
bool is_pointer_representable(const element::Type& element_type) const {
if (element_type.is_dynamic()) {
@@ -157,6 +165,7 @@ protected:
mutable Strides m_strides;
mutable std::once_flag m_strides_once;
void* m_ptr;
+ std::optional<uint64_t> m_source_id;
};
/**
@@ -623,4 +632,17 @@ size_t get_tensor_data_offset(const ov::ITensor& tensor) {
return 0;
}
+std::optional<uint64_t> get_tensor_source_id(const ov::Tensor& tensor) {
+ if (auto itensor = std::dynamic_pointer_cast<ViewTensor>(get_tensor_impl(tensor)._ptr)) {
+ return itensor->get_source_id();
+ }
+ return std::nullopt;
+}
+
+void set_tensor_source_id(ov::Tensor& tensor, uint64_t id) {
+ if (auto itensor = std::dynamic_pointer_cast<ViewTensor>(get_tensor_impl(tensor)._ptr)) {
+ itensor->set_source_id(id);
+ }
+}
+
} // namespace ov