Skip to content

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

[CPU] fix custom strideslice shape infer issue when using continuous new axis mask (#28302)

Yuan Hub25e8a85dd14
diff --git a/src/plugins/intel_cpu/src/shape_inference/custom/strided_slice.cpp b/src/plugins/intel_cpu/src/shape_inference/custom/strided_slice.cpp
index a26a53c54d7..7e42b531e62 100644
--- a/src/plugins/intel_cpu/src/shape_inference/custom/strided_slice.cpp
+++ b/src/plugins/intel_cpu/src/shape_inference/custom/strided_slice.cpp
@@ -56,20 +56,33 @@ Result StridedSliceShapeInfer::infer(const std::vector<std::reference_wrapper<co
}
};
- for (size_t in_idx = 0, out_idx = 0; in_idx < shapeIn.size(); ++in_idx) {
- if (m_new_axis_mask_set.count(in_idx)) {
- // deal with new_axis_mask
- m_outputShape[out_idx] = 1;
+ const auto shapeInSize = shapeIn.size();
+ const auto newAxisMaskSize = m_new_axis_mask_set.size();
+ const auto maxAxisSize = shapeInSize + newAxisMaskSize;
+ const auto outputShapeSize = m_outputShape.size();
+ bool newAxis = false;
+ bool shrinkAxis = false;
+ // because has already initialized the elements of m_outputShape as value 1,
+ // so don't need m_outputShape[out_idx] = 1 when newAxis,
+ // so also don't need to check if there are new axis at the end of ShapeIn.
+ for (size_t axis_idx = 0, out_idx = 0, in_idx = 0;
+ axis_idx < maxAxisSize && in_idx < shapeInSize && out_idx < outputShapeSize;
+ axis_idx++) {
+ newAxis = m_new_axis_mask_set.count(axis_idx);
+ shrinkAxis = m_shrink_axis_mask_set.count(axis_idx);
+ if (newAxis) {
+ // from test when shrinkAxis && newAxis, only newAxis is working in NgraphShapeInfer,
+ // so merge if(newAxis) and if(shrinkAxis && newAxis) together.
out_idx++;
- // deal with current axis
- m_outputShape[out_idx] = gen_new_sliced_value(out_idx, in_idx);
- out_idx++;
- } else if (!m_shrink_axis_mask_set.count(in_idx)) {
- // deal with begin_mask and end_mask
- m_outputShape[out_idx] = gen_new_sliced_value(in_idx, in_idx);
+ } else if (shrinkAxis) {
+ in_idx++;
+ } else {
+ m_outputShape[out_idx] = gen_new_sliced_value(axis_idx, in_idx);
+ in_idx++;
out_idx++;
}
}
+
return {{m_outputShape}, ShapeInferStatus::success};
}
diff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/strided_slice.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/strided_slice.cpp
index b63c8d18682..db9c7f318d6 100644
--- a/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/strided_slice.cpp
+++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/strided_slice.cpp
@@ -120,6 +120,30 @@ INSTANTIATE_TEST_SUITE_P(
make_tuple(unit_test::ShapeVector{{2000, 128}, {3}, {3}, {3}}, std::vector<std::vector<int32_t>>{{0, 0, 0}, {0, 128, 0}, {1, 1, 1}},
std::vector<int64_t>{0, 1, 1}, std::vector<int64_t>{0, 0, 1}, std::vector<int64_t>{1, 0, 0}, std::vector<int64_t>(3, 0),
StaticShape({1, 128, 128})),
+ make_tuple(unit_test::ShapeVector{{3, 192}, {5}, {5}, {5}}, std::vector<std::vector<int32_t>>{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}},
+ std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{0, 1, 1, 1, 0}, std::vector<int64_t>{},
+ StaticShape({3, 1, 1, 1, 192})),
+ make_tuple(unit_test::ShapeVector{{3, 192}, {5}, {5}, {5}}, std::vector<std::vector<int32_t>>{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}},
+ std::vector<int64_t>{1, 1, 0, 0, 0}, std::vector<int64_t>{1, 1, 0, 0, 0}, std::vector<int64_t>{0, 0, 1, 1, 1}, std::vector<int64_t>{},
+ StaticShape({3, 192, 1, 1, 1})),
+ make_tuple(unit_test::ShapeVector{{3, 2, 192}, {5}, {5}, {5}}, std::vector<std::vector<int32_t>>{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}},
+ std::vector<int64_t>{1, 0, 1, 0, 1}, std::vector<int64_t>{1, 0, 1, 0, 1}, std::vector<int64_t>{0, 1, 0, 1, 0}, std::vector<int64_t>{},
+ StaticShape({3, 1, 2, 1, 192})),
+ make_tuple(unit_test::ShapeVector{{3, 2, 192}, {5}, {5}, {5}}, std::vector<std::vector<int32_t>>{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}},
+ std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{0, 1, 0, 1, 0},
+ std::vector<int64_t>{0, 0, 1, 0, 0},
+ StaticShape({3, 1, 1, 192})),
+ make_tuple(unit_test::ShapeVector{{3, 2, 192}, {5}, {5}, {5}}, std::vector<std::vector<int32_t>>{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}},
+ std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{0, 1, 1, 0, 0},
+ std::vector<int64_t>{0, 0, 0, 1, 1},
+ StaticShape({3, 1, 1})),
+ make_tuple(unit_test::ShapeVector{{3, 192}, {5}, {5}, {5}}, std::vector<std::vector<int32_t>>{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}},
+ std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{1, 0, 0, 0, 1}, std::vector<int64_t>{0, 1, 1, 1, 0},
+ std::vector<int64_t>{0, 1, 1, 1, 0},
+ StaticShape({3, 1, 1, 1, 192})),
+ make_tuple(unit_test::ShapeVector{{3, 192}, {2}, {2}, {2}}, std::vector<std::vector<int32_t>>{{0, 0}, {0, 0}, {1, 1}},
+ std::vector<int64_t>{0, 1}, std::vector<int64_t>{0, 1}, std::vector<int64_t>{0, 0}, std::vector<int64_t>{1, 0},
+ StaticShape({192})),
make_tuple(unit_test::ShapeVector{{1, 2, 3}, {2}, {2}, {2}}, std::vector<std::vector<int32_t>>{{0, 0}, {0, 0}, {1, 1}},
std::vector<int64_t>{1, 0}, std::vector<int64_t>{1, 0}, std::vector<int64_t>{0, 1}, std::vector<int64_t>{0, 0},
StaticShape({1, 1, 2, 3}))),