Skip to content

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

[core] Return early from ITensor::copy_to for tensors with no elements (#39195)

Andrew Yooeun Chun4afa0dd7c880
diff --git a/src/core/src/runtime/itensor.cpp b/src/core/src/runtime/itensor.cpp
index 38f11162c21..56a9f4d448f 100644
--- a/src/core/src/runtime/itensor.cpp
+++ b/src/core/src/runtime/itensor.cpp
@@ -84,6 +84,10 @@ void ITensor::copy_to(const std::shared_ptr<ov::ITensor>& dst) const {
dst->set_shape(shape);
}
+ if (get_size() == 0) {
+ return;
+ }
+
if (auto remote_tensor_dst = std::dynamic_pointer_cast<ov::IRemoteTensor>(dst)) {
remote_tensor_dst->copy_from(shared_from_this());
return;
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cpp
index a18c8fdbaa8..2f57a0b1594 100644
--- a/src/core/tests/ov_tensor_test.cpp
+++ b/src/core/tests/ov_tensor_test.cpp
@@ -786,6 +786,27 @@ TEST_F(OVTensorTest, readRangeRoiBlobStringTensor) {
}
}
+using EmptyRoiParams = std::pair<ov::Coordinate, ov::Coordinate>;
+
+struct OVTensorTestCopyEmptyRoi : ::testing::TestWithParam<EmptyRoiParams> {};
+
+TEST_P(OVTensorTestCopyEmptyRoi, copy_to_and_from_dense) {
+ const auto& [begin, end] = GetParam();
+ ov::Tensor parent{ov::element::f32, {1, 4, 8, 16}};
+ ov::Tensor roi{parent, begin, end};
+ ov::Tensor dense{ov::element::f32, {1}};
+
+ OV_ASSERT_NO_THROW(roi.copy_to(dense));
+ EXPECT_EQ(dense.get_shape(), roi.get_shape());
+ OV_ASSERT_NO_THROW(dense.copy_to(roi));
+}
+
+INSTANTIATE_TEST_SUITE_P(copy_tests_empty_roi,
+ OVTensorTestCopyEmptyRoi,
+ ::testing::Values(EmptyRoiParams{{0, 0, 2, 0}, {1, 4, 2, 16}}, // 1x4x0x16
+ EmptyRoiParams{{0, 0, 2, 4}, {1, 4, 2, 8}}, // 1x4x0x4
+ EmptyRoiParams{{0, 0, 0, 4}, {1, 4, 8, 4}})); // 1x4x8x0
+
TEST_F(OVTensorTest, createTensorWithZeroDimsCheckStride) {
ov::Shape shape = {0, 0, 0, 0};
auto tensor = ov::Tensor(ov::element::f32, shape);