Skip to content

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

Fix shape calculation for read_tensor_data (#29584)

Oleg Pipikinc7147b752e95
diff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cpp
index ee527c9365b..5aad89d4ffb 100644
--- a/src/core/src/runtime/tensor.cpp
+++ b/src/core/src/runtime/tensor.cpp
@@ -136,12 +136,13 @@ ov::Shape calc_static_shape_for_file(const std::filesystem::path& file_name,
slice_size *= partial_shape_copy[id].get_min_length();
}
}
+ OPENVINO_ASSERT(slice_size > 0, "Cannot fit file size into requested PartialShape");
+
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,
@@ -152,14 +153,10 @@ ov::Shape calc_static_shape_for_file(const std::filesystem::path& file_name,
" 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");
+ auto new_dimension = ov::Dimension(elements_to_read) / slice_size;
+ OPENVINO_ASSERT(dynamic_dimension.compatible(new_dimension), "Cannot fit file size into requested PartialShape");
- dynamic_dimension = Dimension(new_dimension_size);
+ dynamic_dimension = std::move(new_dimension);
return partial_shape_copy.get_shape();
}
diff --git a/src/core/tests/tensor_utils.cpp b/src/core/tests/tensor_utils.cpp
index ecbff623296..9cba101bc4a 100644
--- a/src/core/tests/tensor_utils.cpp
+++ b/src/core/tests/tensor_utils.cpp
@@ -235,4 +235,12 @@ TEST_F(FunctionalOffloadTensorTest, read_type_doesnt_fit_file_size) {
ov::Exception);
}
}
+
+TEST_F(FunctionalOffloadTensorTest, read_null_shape) {
+ auto new_shape = shape;
+ // 1 dynamic dimension and 1 null dimension
+ new_shape[0] = 0;
+ new_shape[1] = Dimension::dynamic();
+ { EXPECT_THROW(std::ignore = read_tensor_data(file_name, ov_type, new_shape, 0), ov::Exception); }
+}
} // namespace ov::test