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
Improved StridedSlice shape inference for non-const begin/end inputs (#30431)
AndreiFilip7ea076fca3ab
diff --git a/src/core/shape_inference/include/slice_shape_inference_utils.hpp b/src/core/shape_inference/include/slice_shape_inference_utils.hppindex 2d0ec31f1bf..62abe9d395f 100644--- a/src/core/shape_inference/include/slice_shape_inference_utils.hpp+++ b/src/core/shape_inference/include/slice_shape_inference_utils.hpp@@ -163,6 +163,27 @@ TDim make_dim(const TDim& dim, const Bounds& start, const Bounds& stop, int64_treturn {lb, ub};}++/**+ * \brief Computes the size of the default strides ([1, 1, ..., 1]) of a StridedSlice.+ *+ * \param begin Begin argument of a StridedSlice.+ * \param end End argument of a StridedSlice.+ *+ * \return The static size of the strides or a negative value if the size is dynamic.+ */+template <typename TShape>+int64_t default_stride_size(const TShape& begin_shape, const TShape& end_shape) {+ if (begin_shape.rank().is_static() && begin_shape.rank().get_length() == 1 && begin_shape[0].is_static()) {+ return begin_shape[0].get_length();+ }+ if (end_shape.rank().is_static() && end_shape.rank().get_length() == 1 && end_shape[0].is_static()) {+ return end_shape[0].get_length();+ }++ return std::numeric_limits<int64_t>::lowest(); // dynamic rank+}+} // namespace slice} // namespace op} // namespace ovdiff --git a/src/core/shape_inference/include/strided_slice_shape_inference.hpp b/src/core/shape_inference/include/strided_slice_shape_inference.hppindex 8bbccc406aa..a72cc74e57f 100644--- a/src/core/shape_inference/include/strided_slice_shape_inference.hpp+++ b/src/core/shape_inference/include/strided_slice_shape_inference.hpp@@ -65,9 +65,8 @@ std::vector<TRShape> shape_infer(const StridedSlice* op,std::optional<std::vector<int64_t>> strides;if (input_shapes.size() > 3) {strides = get_input_const_data_as<TRShape, int64_t>(op, 3, ta);- } else if (begin) {- // generate default strides- strides.emplace(begin->size(), 1);+ } else if (const auto strides_size = ov::op::slice::default_stride_size(begin_shape, end_shape); strides_size > 0) {+ strides = std::vector<int64_t>(strides_size, 1);}// compute and check a number of axes for which begin, end, and strides are defined@@ -121,6 +120,8 @@ std::vector<TRShape> shape_infer(const StridedSlice* op,auto& out = output_shapes.front();out.resize(0);++ std::optional<std::pair<size_t, DimType>> last_dynamic_dim;int64_t input_shape_idx = 0;for (int64_t axis = 0; axis < number_axes; ++axis) {// add all dimensions hidden under the ellipsis mask if ellipsis mask is set@@ -158,7 +159,7 @@ std::vector<TRShape> shape_infer(const StridedSlice* op,input_shape_idx++;}// calculating dimension (begin, end, begin_mask, end_mask, stride)- else if (begin && end && strides) {+ else if (strides) {// set default value for stride or use given valueconst auto& input_dim = input_shape[input_shape_idx];auto stride =@@ -178,16 +179,33 @@ std::vector<TRShape> shape_infer(const StridedSlice* op,const auto& default_start = is_reverse_stride ? norm_dim_bounds : default_fstart;const auto& default_stop = is_reverse_stride ? default_rstop : norm_dim_bounds;- const auto& start = begin_mask.count(axis) ? default_start : (*begin)[axis];- const auto& stop = end_mask.count(axis) ? default_stop : (*end)[axis];- auto sliced_dim = slice::make_dim(input_dim, start, stop, stride);+ const auto start = begin_mask.count(axis) ? &default_start : begin ? &(*begin)[axis] : nullptr;+ const auto stop = end_mask.count(axis) ? &default_stop : end ? &(*end)[axis] : nullptr;++ if constexpr (std::is_same_v<DimType, ov::Dimension>) {+ if (!begin || !end) {+ last_dynamic_dim = std::make_pair(out.size(),+ slice::make_dim(DimType(0, input_dim.get_max_length()),+ default_start,+ default_stop,+ stride));+ }++ // non-constant begin/end produce dynamic output dimensions unless the axis is ignored by setting+ // the corresponding mask to 1; since the stride is constant it can still be applied on the bounds+ auto sliced_dim =+ start && stop ? slice::make_dim(input_dim, *start, *stop, stride) : last_dynamic_dim->second;- if (std::is_same<DimType, ov::Dimension>::value &&- (sliced_dim == input_dim && sliced_dim != Dimension::dynamic())) {// for equal ov::Dimension do merge to get input label (always success)- DimType::merge(sliced_dim, sliced_dim, input_dim);+ if (sliced_dim == input_dim && sliced_dim != Dimension::dynamic()) {+ DimType::merge(sliced_dim, sliced_dim, input_dim);+ }++ out.push_back(std::move(sliced_dim));+ } else {+ auto sliced_dim = slice::make_dim(input_dim, *start, *stop, stride);+ out.push_back(std::move(sliced_dim));}- out.push_back(std::move(sliced_dim));input_shape_idx++;} else {@@ -198,6 +216,13 @@ std::vector<TRShape> shape_infer(const StridedSlice* op,}}+ if constexpr (std::is_same_v<DimType, ov::Dimension>) {+ // TODO: #167791 at least one dim must remain dynamic for now+ if (last_dynamic_dim.has_value() && out.is_static()) {+ out[last_dynamic_dim->first] = std::move(last_dynamic_dim->second);+ }+ }+// get remaining valuesfor (; input_shape_idx < input_shape.rank().get_length(); ++input_shape_idx) {out.push_back(input_shape[input_shape_idx]);diff --git a/src/core/src/op/strided_slice.cpp b/src/core/src/op/strided_slice.cppindex 09d773c462d..c71c5b343a6 100644--- a/src/core/src/op/strided_slice.cpp+++ b/src/core/src/op/strided_slice.cpp@@ -19,6 +19,7 @@#include "openvino/op/util/slice_plan.hpp"#include "openvino/pass/constant_folding.hpp"#include "openvino/reference/strided_slice.hpp"+#include "slice_shape_inference_utils.hpp"#include "strided_slice_shape_inference.hpp"#include "utils.hpp"@@ -47,24 +48,18 @@ StridedSlice::StridedSlice(const Output<Node>& data,}namespace {-std::shared_ptr<Node> calculate_default_strides(const Output<Node>& begin, const Output<Node>& end) {- const auto& begin_pshape = begin.get_partial_shape();- const auto& end_pshape = end.get_partial_shape();-- size_t strides_length = 0;- if (begin_pshape.rank().is_static() && begin_pshape.rank().get_length() == 1 && begin_pshape[0].is_static()) {- strides_length = begin_pshape[0].get_length();- } else if (end_pshape.rank().is_static() && end_pshape.rank().get_length() == 1 && end_pshape[0].is_static()) {- strides_length = end_pshape[0].get_length();- } else // dynamic case- {- OPENVINO_ASSERT(begin_pshape.rank().is_static() && begin_pshape.rank().get_length() == 1,- "Begin input must be 1D");- return std::make_shared<v1::Broadcast>(v0::Constant::create(element::i64, {}, {1}),- std::make_shared<v0::ShapeOf>(begin));+std::shared_ptr<Node> create_default_strides(const Output<Node>& begin, const Output<Node>& end) {+ const auto strides_size = ov::op::slice::default_stride_size(begin.get_partial_shape(), end.get_partial_shape());+ if (strides_size > 0) {+ return v0::Constant::create(element::i64, Shape{static_cast<size_t>(strides_size)}, {1});}- return v0::Constant::create(element::i64, Shape{strides_length}, std::vector<int64_t>(strides_length, 1));+ // dynamic case+ const auto& begin_pshape = begin.get_partial_shape();+ OPENVINO_ASSERT(begin_pshape.rank().is_static() && begin_pshape.rank().get_length() == 1, "Begin input must be 1D");++ return std::make_shared<v1::Broadcast>(v0::Constant::create(element::i64, {}, {1}),+ std::make_shared<v0::ShapeOf>(begin));}/**@@ -94,7 +89,7 @@ StridedSlice::StridedSlice(const Output<Node>& data,: StridedSlice(data,begin,end,- calculate_default_strides(begin, end),+ create_default_strides(begin, end),begin_mask,end_mask,new_axis_mask,@@ -146,7 +141,7 @@ void StridedSlice::validate_and_infer_types() {// Fill up strides input with default strides if not set by this point.if (get_input_size() < 4) {- set_argument(3, calculate_default_strides(get_input_node_ptr(1)->output(0), get_input_node_ptr(2)->output(0)));+ set_argument(3, create_default_strides(get_input_node_ptr(1)->output(0), get_input_node_ptr(2)->output(0)));}set_input_is_relevant_to_shape(1);@@ -347,6 +342,7 @@ bool StridedSlice::constant_fold(OutputVector& output_values, const OutputVector}return is_folded;}+} // namespace v1} // namespace op} // namespace ovdiff --git a/src/core/tests/type_prop/strided_slice.cpp b/src/core/tests/type_prop/strided_slice.cppindex 9ddf7073103..18d2b99da70 100644--- a/src/core/tests/type_prop/strided_slice.cpp+++ b/src/core/tests/type_prop/strided_slice.cpp@@ -670,6 +670,128 @@ INSTANTIATE_TEST_SUITE_P(type_prop,}),StridedSliceShapeInferTest::get_test_case_name);+struct ConstStridedSliceTestParams {+ std::string case_name;+ PartialShape input_shape;+ PartialShape begin_shape;+ PartialShape end_shape;+ std::vector<int64_t> strides;+ std::vector<int64_t> begin_mask;+ std::vector<int64_t> end_mask;+ std::vector<int64_t> new_axis_mask;+ std::vector<int64_t> shrink_axis_mask;+ std::vector<int64_t> ellipsis_mask;++ PartialShape ref_shape;+ ov::element::Type ref_type;+};++struct ConstStridedSliceShapeInferTest : public TypePropOpTest<op::v1::StridedSlice>,+ public WithParamInterface<ConstStridedSliceTestParams> {+public:+ static std::string get_test_case_name(const TestParamInfo<ConstStridedSliceTestParams>& obj) {+ return obj.param.case_name;+ }+};++TEST_P(ConstStridedSliceShapeInferTest, begin_end_are_not_constants) {+ const auto& params = GetParam();++ const auto input_data = std::make_shared<op::v0::Parameter>(params.ref_type, params.input_shape);+ const auto begin = std::make_shared<op::v0::Parameter>(ov::element::i32, params.begin_shape);+ const auto end = std::make_shared<op::v0::Parameter>(ov::element::i32, params.end_shape);+ const auto strides = ov::op::v0::Constant::create(ov::element::i32, Shape{params.strides.size()}, params.strides);++ const auto strided_slice = make_op(input_data,+ begin,+ end,+ strides,+ params.begin_mask,+ params.end_mask,+ params.new_axis_mask,+ params.shrink_axis_mask,+ params.ellipsis_mask);++ EXPECT_EQ(strided_slice->get_element_type(), params.ref_type);+ EXPECT_EQ(strided_slice->get_output_size(), 1);+ EXPECT_EQ(strided_slice->get_output_partial_shape(0), params.ref_shape);+}++INSTANTIATE_TEST_SUITE_P(type_prop,+ ConstStridedSliceShapeInferTest,+ Values(+ ConstStridedSliceTestParams{+ "single_axis_end_default_strides",+ {1, 500, 2}, // input_shape+ {2}, // begin shape+ {2}, // end shape+ {1, 1}, // strides+ {1, 1}, // begin mask+ {1, 0}, // end mask+ {0, 0}, // new axis mask+ {0, 0}, // shrink axis mask+ {0, 0}, // ellipsis mask+ {1, {0, 500}, {2}}, // reference shape+ element::f32 // reference type+ },+ ConstStridedSliceTestParams{+ "single_axis_begin_const_strides",+ {8, 16, 32}, // input_shape+ {2}, // begin shape+ {2}, // end shape+ {-2, -4}, // strides+ {0, 1}, // begin mask+ {1, 1}, // end mask+ {0, 0}, // new axis mask+ {0, 0}, // shrink axis mask+ {0, 0}, // ellipsis mask+ {{0, 4}, {4}, {32}}, // reference shape+ element::f32 // reference type+ },+ ConstStridedSliceTestParams{+ "single_axis_begin_single_axis_end_default_strides",+ {8, 16, 32}, // input_shape+ {3}, // begin shape+ {3}, // end shape+ {1, 1, 1}, // strides+ {1, 0, 1}, // begin mask+ {1, 1, 0}, // end mask+ {0, 0, 0}, // new axis mask+ {0, 0, 0}, // shrink axis mask+ {0, 0, 0}, // ellipsis mask+ {8, {0, 16}, {0, 32}}, // reference shape+ element::f32 // reference type+ },+ ConstStridedSliceTestParams{+ "single_axis_begin_single_axis_end_const_strides",+ {8, 16, 32}, // input_shape+ {3}, // begin shape+ {3}, // end shape+ {2, 2, 6}, // strides+ {1, 0, 1}, // begin mask+ {1, 1, 0}, // end mask+ {0, 0, 0}, // new axis mask+ {0, 0, 0}, // shrink axis mask+ {0, 0, 0}, // ellipsis mask+ {4, {0, 8}, {0, 6}}, // reference shape+ element::f32 // reference type+ },+ ConstStridedSliceTestParams{+ "ignored_begin_ignored_end_const_strides",+ {8, 16, 32}, // input_shape+ {3}, // begin shape+ {3}, // end shape+ {2, 2, 6}, // strides+ {1, 1, 1}, // begin mask+ {1, 1, 1}, // end mask+ {0, 0, 0}, // new axis mask+ {0, 0, 0}, // shrink axis mask+ {0, 0, 0}, // ellipsis mask+ {4, 8, {0, 6}}, // reference shape TODO: #167791+ element::f32 // reference type+ }),+ ConstStridedSliceShapeInferTest::get_test_case_name);+using StridedSliceIntervalParams =std::tuple<ov::PartialShape, ov::PartialShape, ov::PartialShape, int64_t, int64_t, int64_t, ov::PartialShape>;