Skip to content

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

[Snippets] Supported dynamic case in SoftmaxReshapeElimination (#26350)

Alexandra Sidorova00cd04dfc094
diff --git a/src/common/snippets/src/pass/softmax_reshape_elimination.cpp b/src/common/snippets/src/pass/softmax_reshape_elimination.cpp
index 5fd775a9bee..5e5892479d9 100644
--- a/src/common/snippets/src/pass/softmax_reshape_elimination.cpp
+++ b/src/common/snippets/src/pass/softmax_reshape_elimination.cpp
@@ -12,8 +12,10 @@
ov::snippets::pass::SoftmaxReshapeElimination::SoftmaxReshapeElimination() {
MATCHER_SCOPE(SoftmaxReshapeElimination);
- const auto m_reshape0 = ov::pass::pattern::wrap_type<ov::op::v1::Reshape>(ov::pass::pattern::has_static_shape());
- const auto m_softmax = ov::pass::pattern::wrap_type<ov::op::v1::Softmax, ov::op::v8::Softmax>({m_reshape0});
+ const auto m_reshape0 = ov::pass::pattern::wrap_type<ov::op::v1::Reshape>({ov::pass::pattern::any_input(),
+ ov::pass::pattern::wrap_type<ov::op::v0::Constant>()},
+ ov::pass::pattern::consumers_count(1));
+ const auto m_softmax = ov::pass::pattern::wrap_type<ov::op::v1::Softmax, ov::op::v8::Softmax>({m_reshape0}, ov::pass::pattern::consumers_count(1));
const auto m_reshape1 = ov::pass::pattern::wrap_type<ov::op::v1::Reshape>({m_softmax, ov::pass::pattern::wrap_type<ov::op::v0::Constant>()});
register_matcher(std::make_shared<ov::pass::pattern::Matcher>(m_reshape1, matcher_name),
@@ -26,10 +28,11 @@ ov::snippets::pass::SoftmaxReshapeElimination::SoftmaxReshapeElimination() {
const auto input_shape = reshape0->get_input_partial_shape(0);
const auto output_shape = reshape1->get_output_partial_shape(0);
- if (input_shape.is_dynamic() || output_shape.is_dynamic() || input_shape.get_shape() != output_shape.get_shape())
+ const auto softmax_shape = softmax->get_input_partial_shape(0);
+ if (input_shape != output_shape || input_shape.rank() != output_shape.rank())
return false;
- const auto softmax_rank = softmax->get_input_partial_shape(0).rank();
+ const auto softmax_rank = softmax_shape.rank();
int64_t axis = 0;
if (const auto softmax_v8 = ov::as_type_ptr<const ov::op::v8::Softmax>(softmax)) {
axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), softmax_rank, *softmax);
@@ -44,7 +47,11 @@ ov::snippets::pass::SoftmaxReshapeElimination::SoftmaxReshapeElimination() {
return false;
// Dimensions by reduction axis should be equal
- if (input_shape.get_shape().back() != softmax->get_input_shape(0).back())
+ const auto in_last_dim = *input_shape.crbegin();
+ const auto out_last_dim = *output_shape.crbegin();
+ const auto softmax_last_dim = *softmax_shape.crbegin();
+ if (in_last_dim.is_dynamic() || out_last_dim.is_dynamic() || softmax_last_dim.is_dynamic() ||
+ in_last_dim != out_last_dim || in_last_dim != softmax_last_dim)
return false;
// Eliminate Reshape before Softmax
diff --git a/src/common/snippets/tests/src/pass/softmax_reshape_elimination.cpp b/src/common/snippets/tests/src/pass/softmax_reshape_elimination.cpp
index 6e6ee871b16..579f7704973 100644
--- a/src/common/snippets/tests/src/pass/softmax_reshape_elimination.cpp
+++ b/src/common/snippets/tests/src/pass/softmax_reshape_elimination.cpp
@@ -65,3 +65,22 @@ TEST_F(TransformationTestsF, SoftmaxReshapeElimination_IncorrectReshape) {
manager.register_pass<snippets::pass::SoftmaxReshapeElimination>();
}
}
+
+TEST_F(TransformationTestsF, SoftmaxV8ReshapeElimination_DynamicBatch) {
+ {
+ auto data = std::make_shared<ov::op::v0::Parameter>(element::f32, ov::PartialShape{-1, 2, 340, 240});
+ auto shape0 = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{2}, std::vector<int32_t>{-1, 240});
+ auto reshape0 = std::make_shared<ov::op::v1::Reshape>(data, shape0, false);
+ auto softmax_v1 = std::make_shared<ov::op::v8::Softmax>(reshape0, -1);
+ auto shape1 = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<int32_t>{-1, 2, 340, 240});
+ auto reshape1 = std::make_shared<ov::op::v1::Reshape>(softmax_v1, shape1, false);
+ model = std::make_shared<Model>(NodeVector{reshape1}, ParameterVector{data});
+
+ manager.register_pass<snippets::pass::SoftmaxReshapeElimination>();
+ }
+ {
+ auto data = std::make_shared<ov::op::v0::Parameter>(element::f32, ov::PartialShape{-1, 2, 340, 240});
+ auto softmax_v1 = std::make_shared<ov::op::v8::Softmax>(data, 3);
+ model_ref = std::make_shared<Model>(NodeVector{softmax_v1}, ParameterVector{data});
+ }
+}