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
Shape inference improvements for `SearchSorted-15` (#26980)
Przemyslaw Wysocki58073cad59fe
diff --git a/src/core/include/openvino/op/search_sorted.hpp b/src/core/include/openvino/op/search_sorted.hppindex 78650942ee8..c370ba46b2f 100644--- a/src/core/include/openvino/op/search_sorted.hpp+++ b/src/core/include/openvino/op/search_sorted.hpp@@ -36,8 +36,6 @@ public:m_right_mode = right_mode;}- bool validate() const;-private:bool m_right_mode{};};diff --git a/src/core/shape_inference/include/search_sorted_shape_inference.hpp b/src/core/shape_inference/include/search_sorted_shape_inference.hppindex 7ea0598cffb..4b9d888891e 100644--- a/src/core/shape_inference/include/search_sorted_shape_inference.hpp+++ b/src/core/shape_inference/include/search_sorted_shape_inference.hpp@@ -12,28 +12,40 @@ namespace op {namespace v15 {template <class TShape, class TRShape = result_shape_t<TShape>>std::vector<TRShape> shape_infer(const SearchSorted* op, const std::vector<TShape>& input_shapes) {- // [HACK]: By convention, shape_infer should also perform node validation..- op->validate();const auto& sorted_shape = input_shapes[0];const auto& values_shape = input_shapes[1];+ const auto is_sorted_rank_static = sorted_shape.rank().is_static();+ const auto is_values_rank_static = values_shape.rank().is_static();- auto output_shape = values_shape;-- // 1. If we know that the sorted sequence is 1D, than output shape can be anything.- if (sorted_shape.rank().is_static() && sorted_shape.rank().get_length() == 1) {- return {std::move(output_shape)};+ if (!is_sorted_rank_static || sorted_shape.size() == 1) {+ // If the sorted sequence is 1D, then any shape of the values input is allowed.+ // The shape of the output is the same as the shape of the values.+ return {values_shape};}- // 2. ND tensor case or rank not known.- auto sorted_shape_last_dynamic = sorted_shape;- if (sorted_shape.rank().is_static()) {- sorted_shape_last_dynamic[sorted_shape.rank().get_length() - 1] = Dimension::dynamic();+ const auto sorted_in_rank = sorted_shape.size();+ NODE_SHAPE_INFER_CHECK(op, input_shapes, sorted_in_rank > 0, "The sorted sequence input cannot be a scalar.");++ TRShape output_shape;+ if (!is_values_rank_static) {+ output_shape = sorted_shape;+ output_shape[sorted_in_rank - 1] = Dimension::dynamic();+ } else {+ output_shape = values_shape;+ NODE_SHAPE_INFER_CHECK(+ op,+ input_shapes,+ sorted_in_rank == values_shape.size(),+ "If the shape of sorted sequence is not 1D, the ranks of the inputs have to be compatible.");+ using TDim = typename TShape::value_type;+ for (size_t i = 0; i < sorted_in_rank - 1; ++i) {+ NODE_SHAPE_INFER_CHECK(op,+ input_shapes,+ TDim::merge(output_shape[i], values_shape[i], sorted_shape[i]),+ "All dimensions but the last one have to be compatible.");+ }}- const bool sorted_values_merge_success = TShape::merge_into(output_shape, sorted_shape_last_dynamic);-- NODE_VALIDATION_CHECK(op, sorted_values_merge_success, "Shapes of sorted sequence and values are not compatible.");-return {std::move(output_shape)};}} // namespace v15diff --git a/src/core/src/op/search_sorted.cpp b/src/core/src/op/search_sorted.cppindex d3f26a674ee..8b9bb012b27 100644--- a/src/core/src/op/search_sorted.cpp+++ b/src/core/src/op/search_sorted.cpp@@ -18,34 +18,11 @@ SearchSorted::SearchSorted(const Output<Node>& sorted_sequence, const Output<Nodconstructor_validate_and_infer_types();}-bool SearchSorted::validate() const {- NODE_VALIDATION_CHECK(this, get_input_size() == 2);+void SearchSorted::validate_and_infer_types() {+ OV_OP_SCOPE(v15_SearchSorted_validate_and_infer_types);NODE_VALIDATION_CHECK(this,get_input_element_type(0).compatible(get_input_element_type(1)),"Sorted sequence and values must have the same element type.");-- const auto& sorted_shape = get_input_partial_shape(0);- const auto& values_shape = get_input_partial_shape(1);-- if (sorted_shape.rank().is_static() && values_shape.rank().is_static() && sorted_shape.rank().get_length() > 1) {- NODE_VALIDATION_CHECK(this,- sorted_shape.rank().get_length() == values_shape.rank().get_length(),- "Sorted sequence and values have different ranks.");-- for (int64_t i = 0; i < sorted_shape.rank().get_length() - 1; ++i) {- NODE_VALIDATION_CHECK(this,- sorted_shape[i].compatible(values_shape[i]),- "Sorted sequence and values has different ",- i,- " dimension.");- }- }-- return true;-}--void SearchSorted::validate_and_infer_types() {- OV_OP_SCOPE(v15_SearchSorted_validate_and_infer_types);const auto& output_shapes = shape_infer(this, ov::util::get_node_input_partial_shapes(*this));set_output_type(0, ov::element::i64, output_shapes[0]);}diff --git a/src/core/tests/type_prop/search_sorted.cpp b/src/core/tests/type_prop/search_sorted.cppindex efc2c865416..6dd10ad0ac3 100644--- a/src/core/tests/type_prop/search_sorted.cpp+++ b/src/core/tests/type_prop/search_sorted.cpp@@ -57,6 +57,10 @@ TEST(type_prop, search_sorted_shape_infer_sorted_1d_values_dynamic) {PerformShapeTest({8}, {-1, -1, 3}, {-1, -1, 3});}+TEST(type_prop, search_sorted_shape_infer_scalar_values) {+ PerformShapeTest({100}, {}, {});+}+TEST(type_prop, search_sorted_shape_infer_both_dynamic_1) {PerformShapeTest({1, -1, 7, -1}, {-1, 3, -1, 10}, {1, 3, 7, 10});}@@ -93,6 +97,19 @@ TEST(type_prop, search_sorted_shape_infer_both_dynamic_9) {PerformShapeTest({-1, -1}, PartialShape::dynamic(), {-1, -1});}+TEST(type_prop, search_sorted_shape_symbols) {+ PartialShape sorted_shape{1, 3, 7, 100};+ PartialShape values_shape{-1, -1, -1, 10};+ auto sorted_symbols = set_shape_symbols(sorted_shape);+ auto values_symbols = set_shape_symbols(values_shape);+ auto sorted = make_shared<op::v0::Parameter>(element::i32, sorted_shape);+ auto values = make_shared<op::v0::Parameter>(element::i32, values_shape);+ auto search_sorted_op = make_shared<op::v15::SearchSorted>(sorted, values);+ EXPECT_EQ(search_sorted_op->get_element_type(), element::i64);+ EXPECT_THAT(get_shape_symbols(search_sorted_op->get_output_partial_shape(0)),+ testing::ElementsAre(values_symbols[0], values_symbols[1], values_symbols[2], values_symbols[3]));+}+TEST(type_prop, search_sorted_shape_infer_different_types) {auto sorted = make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 3, 6});auto values = make_shared<ov::op::v0::Parameter>(element::i32, Shape{1, 3, 6});@@ -102,13 +119,27 @@ TEST(type_prop, search_sorted_shape_infer_different_types) {TEST(type_prop, search_sorted_shape_infer_wrong_rank) {auto sorted = make_shared<ov::op::v0::Parameter>(element::i32, Shape{1, 1, 3, 6});auto values = make_shared<ov::op::v0::Parameter>(element::i32, Shape{1, 3, 6});- EXPECT_THROW_SUBSTRING(sorted, values, std::string("Sorted sequence and values have different ranks"));+ EXPECT_THROW_SUBSTRING(sorted,+ values,+ std::string("sequence is not 1D, the ranks of the inputs have to be compatible"));}TEST(type_prop, search_sorted_shape_infer_wrong_dim) {auto sorted = make_shared<ov::op::v0::Parameter>(element::i32, Shape{1, 1, 3, 6});auto values = make_shared<ov::op::v0::Parameter>(element::i32, Shape{1, 1, 5, 6});- EXPECT_THROW_SUBSTRING(sorted, values, std::string(" different 2 dimension."));+ EXPECT_THROW_SUBSTRING(sorted, values, std::string("All dimensions but the last one have to be compatible"));+}++TEST(type_prop, search_sorted_shape_infer_scalar_sorted_sequence) {+ auto sorted = make_shared<ov::op::v0::Parameter>(element::i32, Shape{});+ auto values = make_shared<ov::op::v0::Parameter>(element::i32, Shape{1, 1, 5, 6});+ EXPECT_THROW_SUBSTRING(sorted, values, std::string("The sorted sequence input cannot be a scalar"));+}++TEST(type_prop, search_sorted_shape_infer_scalar_values_and_nd_sequence) {+ auto sorted = make_shared<ov::op::v0::Parameter>(element::i32, Shape{2, 2});+ auto values = make_shared<ov::op::v0::Parameter>(element::i32, Shape{});+ EXPECT_THROW_SUBSTRING(sorted, values, std::string("the ranks of the inputs have to be compatible"));}#undef EXPECT_THROW_SUBSTRING\ No newline at end of filediff --git a/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp b/src/plugins/intel_cpu/src/shape_inference/shape_inference.cppindex e5d87c57871..5db6f97bba8 100644--- a/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp+++ b/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp@@ -101,6 +101,7 @@#include "scaled_dot_product_attention_shape_inference.hpp"#include "scatter_elements_update_shape_inference.hpp"#include "scatter_nd_base_shape_inference.hpp"+#include "search_sorted_shape_inference.hpp"#include "select_shape_inference.hpp"#include "shape_nodes.hpp"#include "shuffle_channels_shape_inference.hpp"@@ -405,6 +406,7 @@ using IStaticShapeInferFactory =template <>const IStaticShapeInferFactory::TRegistry IStaticShapeInferFactory::registry{// opset15+ _OV_OP_SHAPE_INFER_MASK_REG(op::v15::SearchSorted, ShapeInferTA, util::bit::mask()),_OV_OP_SHAPE_INFER_MASK_REG(op::v15::StringTensorUnpack, ShapeInferTA, util::bit::mask(0)),_OV_OP_SHAPE_INFER_MASK_REG(op::v15::StringTensorPack, ShapeInferTA, util::bit::mask(0, 1)),_OV_OP_SHAPE_INFER_MASK_REG(opset15::EmbeddingBagOffsets, ShapeInferTA, util::bit::mask()),diff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/search_sorted_shape_inference_test.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/search_sorted_shape_inference_test.cppnew file mode 100644index 00000000000..ac0b4763b7b--- /dev/null+++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/search_sorted_shape_inference_test.cpp@@ -0,0 +1,114 @@+// Copyright (C) 2018-2024 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include "common_test_utils/test_assertions.hpp"+#include "utils.hpp"++using namespace ov;+using namespace ov::intel_cpu;+using ov::op::v0::Constant;+using ov::op::v0::Parameter;+using testing::HasSubstr;++class SearchSortedShapeInferenceTest : public OpStaticShapeInferenceTest<op::v15::SearchSorted> {};++TEST_F(SearchSortedShapeInferenceTest, same_dimensions_nd_inputs) {+ const auto sorted = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{1, 3, 6}, StaticShape{1, 3, 6}};+ const auto output_shapes = shape_inference(op.get(), input_shapes);+ EXPECT_EQ(output_shapes.size(), 1);+ EXPECT_EQ(output_shapes.front(), StaticShape({1, 3, 6}));+}++TEST_F(SearchSortedShapeInferenceTest, scalar_values) {+ const auto sorted = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{3}, StaticShape{}};+ const auto output_shapes = shape_inference(op.get(), input_shapes);+ EXPECT_EQ(output_shapes.size(), 1);+ EXPECT_EQ(output_shapes.front(), StaticShape{});+}++TEST_F(SearchSortedShapeInferenceTest, different_last_dim) {+ const auto sorted = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{1, 3, 7, 100}, StaticShape{1, 3, 7, 10}};+ const auto output_shapes = shape_inference(op.get(), input_shapes);+ EXPECT_EQ(output_shapes.size(), 1);+ EXPECT_EQ(output_shapes.front(), StaticShape({1, 3, 7, 10}));+}++TEST_F(SearchSortedShapeInferenceTest, 1d_inputs) {+ const auto sorted = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{5}, StaticShape{20}};+ const auto output_shapes = shape_inference(op.get(), input_shapes);+ EXPECT_EQ(output_shapes.size(), 1);+ EXPECT_EQ(output_shapes.front(), StaticShape({20}));+}++TEST_F(SearchSortedShapeInferenceTest, 1d_sequence) {+ const auto sorted = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{50}, StaticShape{1, 3, 7, 10}};+ const auto output_shapes = shape_inference(op.get(), input_shapes);+ EXPECT_EQ(output_shapes.size(), 1);+ EXPECT_EQ(output_shapes.front(), StaticShape({1, 3, 7, 10}));+}++TEST_F(SearchSortedShapeInferenceTest, element_type_consistency_validation) {+ const auto sorted = std::make_shared<Parameter>(element::i64, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ OV_EXPECT_THROW(std::ignore = make_op(sorted, values),+ NodeValidationFailure,+ testing::HasSubstr("must have the same element type"));+}++TEST_F(SearchSortedShapeInferenceTest, input_shapes_ranks_validation) {+ const auto sorted = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{1, 3, 6}, StaticShape{1, 3, 6, 7}};+ OV_EXPECT_THROW(std::ignore = shape_inference(op.get(), input_shapes),+ NodeValidationFailure,+ testing::HasSubstr("the ranks of the inputs have to be compatible"));+}++TEST_F(SearchSortedShapeInferenceTest, input_shapes_compatibility) {+ const auto sorted = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{1, 3, 6}, StaticShape{1, 6, 6}};+ OV_EXPECT_THROW(std::ignore = shape_inference(op.get(), input_shapes),+ NodeValidationFailure,+ testing::HasSubstr("All dimensions but the last one have to be compatible"));+}++TEST_F(SearchSortedShapeInferenceTest, scalar_sorted_sequence) {+ const auto sorted = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{}, StaticShape{1, 6, 6}};+ OV_EXPECT_THROW(std::ignore = shape_inference(op.get(), input_shapes),+ NodeValidationFailure,+ testing::HasSubstr("The sorted sequence input cannot be a scalar"));+}++TEST_F(SearchSortedShapeInferenceTest, scalar_values_and_ND_sequence) {+ const auto sorted = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto values = std::make_shared<Parameter>(element::i32, PartialShape::dynamic());+ const auto op = make_op(sorted, values);+ const auto input_shapes = ShapeVector{StaticShape{2, 3}, StaticShape{}};+ OV_EXPECT_THROW(std::ignore = shape_inference(op.get(), input_shapes),+ NodeValidationFailure,+ testing::HasSubstr("the ranks of the inputs have to be compatible"));+}