Skip to content

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

[Core] Enhance shape inference checks for GenerateProposals (#39403)

Tomasz Jankowski2f55d27b6f8b
diff --git a/src/core/shape_inference/include/generate_proposals_shape_inference.hpp b/src/core/shape_inference/include/generate_proposals_shape_inference.hpp
index 57a91008eb0..4f4363c565f 100644
--- a/src/core/shape_inference/include/generate_proposals_shape_inference.hpp
+++ b/src/core/shape_inference/include/generate_proposals_shape_inference.hpp
@@ -5,6 +5,7 @@
#pragma once
#include <openvino/op/generate_proposals.hpp>
+#include <tuple>
#include "utils.hpp"
@@ -34,7 +35,8 @@ std::vector<TRShape> shape_infer(const GenerateProposals* op, const std::vector<
(im_info_shape[1].compatible(3) || im_info_shape[1].compatible(4)),
"The 'input_im_info' shape[1] is expected to be a compatible with [3] or [4]. Got: ",
im_info_shape);
- Dimension::merge(num_batches, im_info_shape[0], num_batches);
+ // First merge into a dynamic dimension, so it cannot fail.
+ std::ignore = Dimension::merge(num_batches, im_info_shape[0], num_batches);
}
const auto anchors_shape_rank = anchors_shape.rank();
@@ -63,10 +65,20 @@ std::vector<TRShape> shape_infer(const GenerateProposals* op, const std::vector<
scores_shape_rank.compatible(4),
"The 'input_scores' input is expected to be a 4D. Got: ",
scores_shape);
- if (deltas_shape_rank.is_static())
- Dimension::merge(num_batches, deltas_shape[0], num_batches);
- if (scores_shape_rank.is_static())
- Dimension::merge(num_batches, scores_shape[0], num_batches);
+ if (deltas_shape_rank.is_static()) {
+ NODE_SHAPE_INFER_CHECK(op,
+ input_shapes,
+ Dimension::merge(num_batches, deltas_shape[0], num_batches),
+ "Batch size of 'input_deltas' is not compatible with other inputs. Got: ",
+ deltas_shape[0]);
+ }
+ if (scores_shape_rank.is_static()) {
+ NODE_SHAPE_INFER_CHECK(op,
+ input_shapes,
+ Dimension::merge(num_batches, scores_shape[0], num_batches),
+ "Batch size of 'input_scores' is not compatible with other inputs. Got: ",
+ scores_shape[0]);
+ }
if (deltas_shape_rank.is_static() && scores_shape_rank.is_static()) {
NODE_SHAPE_INFER_CHECK(op,
diff --git a/src/core/shape_inference/include/slice_shape_inference_utils.hpp b/src/core/shape_inference/include/slice_shape_inference_utils.hpp
index a0f65006abe..5262636c191 100644
--- a/src/core/shape_inference/include/slice_shape_inference_utils.hpp
+++ b/src/core/shape_inference/include/slice_shape_inference_utils.hpp
@@ -4,6 +4,8 @@
#pragma once
+#include <tuple>
+
#include "dimension_util.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/op/constant.hpp"
@@ -226,7 +228,8 @@ inline void merge_symbol_if_size_preserved(ov::Dimension& sliced,
const int64_t step) {
if (sliced == dim && sliced != ov::Dimension::dynamic() &&
(dim.is_static() || is_size_preserving_slice(dim, start, stop, step))) {
- ov::Dimension::merge(sliced, sliced, dim);
+ // Equal, non-empty intervals always intersect, so the merge cannot fail.
+ std::ignore = ov::Dimension::merge(sliced, sliced, dim);
}
}
diff --git a/src/core/tests/type_prop/generate_proposals.cpp b/src/core/tests/type_prop/generate_proposals.cpp
index 7dae72526d1..218d1456c18 100644
--- a/src/core/tests/type_prop/generate_proposals.cpp
+++ b/src/core/tests/type_prop/generate_proposals.cpp
@@ -6,8 +6,11 @@
#include <gtest/gtest.h>
+#include <string>
+#include <tuple>
#include <vector>
+#include "common_test_utils/test_assertions.hpp"
#include "common_test_utils/type_prop.hpp"
using namespace ov;
@@ -142,6 +145,45 @@ TEST(type_prop, generate_proposals) {
<< "GenerateProposals node was created with invalid data.";
}
+struct BatchMismatchParams {
+ Dimension im_info_batch;
+ Dimension deltas_batch;
+ Dimension scores_batch;
+ std::string expected_msg;
+};
+
+class GenerateProposalsBatchMismatchTest : public TestWithParam<BatchMismatchParams> {};
+
+// The 'input_im_info' merge runs first against a dynamic batch, so it cannot fail and is not covered here.
+INSTANTIATE_TEST_SUITE_P(
+ type_prop,
+ GenerateProposalsBatchMismatchTest,
+ Values(
+ BatchMismatchParams{2, 3, Dimension::dynamic(), "Batch size of 'input_deltas' is not compatible"},
+ BatchMismatchParams{{1, 2}, {3, 4}, Dimension::dynamic(), "Batch size of 'input_deltas' is not compatible"},
+ BatchMismatchParams{2, Dimension::dynamic(), 3, "Batch size of 'input_scores' is not compatible"},
+ BatchMismatchParams{Dimension::dynamic(), 2, 3, "Batch size of 'input_scores' is not compatible"},
+ BatchMismatchParams{{1, 2}, Dimension::dynamic(), {3, 4}, "Batch size of 'input_scores' is not compatible"}));
+
+TEST_P(GenerateProposalsBatchMismatchTest, batch_mismatch_throws) {
+ const auto& [im_info_batch, deltas_batch, scores_batch, expected_msg] = GetParam();
+
+ Attrs attrs;
+ attrs.min_size = 0.0f;
+ attrs.nms_threshold = 0.699999988079071f;
+ attrs.post_nms_count = 1000;
+ attrs.pre_nms_count = 1000;
+
+ const auto im_info = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{im_info_batch, 4});
+ const auto anchors = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{200, 336, 3, 4});
+ const auto deltas = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{deltas_batch, 12, 200, 336});
+ const auto scores = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{scores_batch, 3, 200, 336});
+
+ OV_EXPECT_THROW(std::ignore = std::make_shared<GenerateProposals>(im_info, anchors, deltas, scores, attrs),
+ NodeValidationFailure,
+ HasSubstr(expected_msg));
+}
+
TEST(type_prop, generate_proposals_dynamic) {
struct ShapesAndAttrs {
PartialShape im_info_shape;