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
Get ROI tensor data offset (#33124)
Bogdan Pereanuc1c0f726dee6
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cppindex 189b5449ea3..dd7af2af238 100644--- a/src/core/tests/ov_tensor_test.cpp+++ b/src/core/tests/ov_tensor_test.cpp@@ -17,6 +17,7 @@#include "openvino/op/parameter.hpp"#include "openvino/reference/utils/coordinate_transform.hpp"#include "openvino/runtime/allocator.hpp"+#include "openvino/runtime/make_tensor.hpp"#include "openvino/runtime/remote_tensor.hpp"#include "openvino/runtime/tensor.hpp"@@ -953,6 +954,23 @@ TEST_F(OVTensorTest, createAllocationOverflow) {HasSubstr("Cannot allocate memory"));}+TEST_F(OVTensorTest, getTensorDataOffsetFromRoiTensor) {+ const ov::Tensor tensor(ov::element::f32, ov::Shape{3, 5, 32, 128});+ ov::Tensor roi_tensor;+ OV_ASSERT_NO_THROW(roi_tensor = ov::Tensor(tensor, {1, 2, 5, 64}, {2, 3, 10, 128}));+ size_t tensor_data_offset = ov::get_tensor_data_offset(*ov::get_tensor_impl(roi_tensor)._ptr);+ EXPECT_EQ(static_cast<const uint8_t*>(roi_tensor.data()),+ static_cast<const uint8_t*>(tensor.data()) + tensor_data_offset);+}++TEST_F(OVTensorTest, getTensorDataOffsetFromGeneralTensor) {+ const ov::Tensor tensor(ov::element::f32, ov::Shape{3, 5, 32, 128});+ size_t tensor_data_offset = ov::get_tensor_data_offset(*ov::get_tensor_impl(tensor)._ptr);+ EXPECT_EQ(static_cast<const uint8_t*>(tensor.data()),+ static_cast<const uint8_t*>(tensor.data()) + tensor_data_offset);+ EXPECT_EQ(0, tensor_data_offset); // special case for general tensor it returns 0+}+struct TestParams {ov::Shape src_shape;ov::Strides src_strides;diff --git a/src/inference/dev_api/openvino/runtime/make_tensor.hpp b/src/inference/dev_api/openvino/runtime/make_tensor.hppindex c7026c3be7a..42ad246cc5a 100644--- a/src/inference/dev_api/openvino/runtime/make_tensor.hpp+++ b/src/inference/dev_api/openvino/runtime/make_tensor.hpp@@ -84,4 +84,17 @@ OPENVINO_RUNTIME_API ov::Tensor make_tensor(const ov::SoPtr<ITensor>& tensor);*/OPENVINO_RUNTIME_API ov::SoPtr<ov::ITensor> get_tensor_impl(const ov::Tensor& tensor);+/**+ * @brief Returns the byte offset of the tensor's data pointer from the base memory allocation+ *+ * @details This function is useful for ROI (Region of Interest) tensors that share memory with a parent tensor+ * but start at a different position within that memory.+ *+ * @param tensor Tensor implementation to get the offset from.+ *+ * @return The offset in bytes from the base memory pointer. Returns 0 if the tensor starts at the+ * beginning of its memory allocation or if no offset is applicable.+ */+OPENVINO_RUNTIME_API size_t get_tensor_data_offset(const ov::ITensor& tensor);+} // namespace ovdiff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cppindex 834aac9ca08..24e8e2d3bfa 100644--- a/src/inference/src/dev/make_tensor.cpp+++ b/src/inference/src/dev/make_tensor.cpp@@ -416,6 +416,10 @@ public:m_shape = std::move(new_shape);}+ size_t get_offset() const {+ return m_offset;+ }+protected:std::shared_ptr<ITensor> m_owner;Shape m_shape;@@ -587,4 +591,11 @@ ov::SoPtr<ov::ITensor> get_tensor_impl(const ov::Tensor& tensor) {return ov::SoPtr<ov::ITensor>(tensor_impl, so);}+size_t get_tensor_data_offset(const ov::ITensor& tensor) {+ if (auto tensor_impl = dynamic_cast<const BaseRoiTensor*>(&tensor)) {+ return tensor_impl->get_offset();+ }+ return 0;+}+} // namespace ov