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
[TRANSFORMATIONS] Allow NOP-elimination for shapes with dynamic 0th dimension (#27939)
Andrii Staikov59188b77a95b
diff --git a/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp b/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cppindex 8b26d74dc2a..3883e94b74d 100644--- a/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp@@ -132,6 +132,23 @@ static bool eliminate_nop(const shared_ptr<Node>& node) {return false;}+// Check if first dim is dynamic, other dims are static+static bool only_first_dim_dynamic(const PartialShape& pshape) {+ if (pshape.rank().is_static() && pshape.size() > 0) {+ if (pshape[0].is_dynamic()) {+ for (size_t i = 1; i < pshape.size(); ++i) {+ if (pshape[i].is_dynamic()) {+ return false;+ }+ }++ return true;+ }+ }++ return false;+}+static bool eliminate_reshape_v1(const shared_ptr<Node>& node) {auto input = node->input_value(0);@@ -139,14 +156,17 @@ static bool eliminate_reshape_v1(const shared_ptr<Node>& node) {if (input.get_partial_shape().same_scheme(node->get_output_partial_shape(0)))return replace_output_update_name(node->output(0), input);}-// check if reshape is not identity opif (input.get_partial_shape().is_dynamic() || node->get_output_partial_shape(0).is_dynamic()) {- OPENVINO_DEBUG(node, " has dynamic shapes.");- return false;+ if (!only_first_dim_dynamic(input.get_partial_shape()) ||+ !only_first_dim_dynamic(node->get_output_partial_shape(0))) {+ OPENVINO_DEBUG(node, " has dynamic shapes with not only 0th dimension dynamic.");+ return false;+ }}+// remove identity op- if (input.get_shape() == node->get_output_shape(0)) {+ if (input.get_partial_shape() == node->get_output_partial_shape(0)) {return replace_output_update_name(node->output(0), input);}// eliminate redundant reshape, squeeze, or unsqueeze@@ -156,15 +176,19 @@ static bool eliminate_reshape_v1(const shared_ptr<Node>& node) {if (input_node->get_output_target_inputs(0).size() != 1)return false;- auto shape = node->get_output_shape(0);+ auto shape = node->get_output_partial_shape(0);// remove interchangeable nodes- if (input_node->get_input_partial_shape(0).is_static() && input_node->get_input_shape(0) == shape) {+ if (input_node->get_input_partial_shape(0).is_static() &&+ input_node->get_input_partial_shape(0) == node->get_output_partial_shape(0)) {return replace_output_update_name(node->output(0), input_node->input_value(0));} else {vector<int64_t> vi;- vi.assign(shape.begin(), shape.end());- auto pat = ov::op::v0::Constant::create<int64_t>(element::i64, Shape{vi.size()}, vi);+ vi.reserve(shape.size());+ for (const auto& dim : shape) {+ vi.push_back(dim.is_dynamic() ? -1 : dim.get_length());+ }+ auto pat = ov::op::v0::Constant::create<int64_t>(element::i64, Shape{shape.size()}, vi);auto new_reshape = make_shared<ov::op::v1::Reshape>(input.get_node()->input_value(0), pat, false);new_reshape->set_friendly_name(node->get_friendly_name());copy_runtime_info({input_node, node}, new_reshape);diff --git a/src/common/transformations/tests/common_optimizations/nop_elimination.cpp b/src/common/transformations/tests/common_optimizations/nop_elimination.cppindex 19b5fefd79b..1245bd26e0d 100644--- a/src/common/transformations/tests/common_optimizations/nop_elimination.cpp+++ b/src/common/transformations/tests/common_optimizations/nop_elimination.cpp@@ -233,6 +233,38 @@ TEST(nop_elimination, squeeze_unsqueeze_elimination_dynamic_without_squeeze_axisEXPECT_NO_THROW(pass_manager.run_passes(f));}+TEST_F(TransformationTestsF, reshape_reshape_elimination_v1_dynamic) {+ {+ auto input = make_shared<op::v0::Parameter>(element::f32, PartialShape({-1, 32, 1, 128}));++ auto top_reshape_const = op::v0::Constant::create(element::i32, Shape{4}, {-1, 32, 1, 128});+ auto top_reshape = std::make_shared<op::v1::Reshape>(input, top_reshape_const, false);++ auto bottom_reshape_const = op::v0::Constant::create(element::i32, Shape{2}, {-1, 4096});+ auto bottom_reshape = std::make_shared<op::v1::Reshape>(top_reshape, bottom_reshape_const, false);++ auto add_param = make_shared<op::v0::Parameter>(element::f32, PartialShape({-1, 4096}));+ auto add = std::make_shared<op::v1::Add>(bottom_reshape, add_param);+ model = std::make_shared<ov::Model>(NodeVector{add}, ParameterVector{input, add_param});+ }+ {+ auto input = make_shared<op::v0::Parameter>(element::f32, PartialShape({-1, 32, 1, 128}));++ auto bottom_reshape_const = op::v0::Constant::create(element::i32, Shape{2}, {-1, 4096});+ auto bottom_reshape = std::make_shared<op::v1::Reshape>(input, bottom_reshape_const, false);++ auto add_param = make_shared<op::v0::Parameter>(element::f32, PartialShape({-1, 4096}));+ auto add = std::make_shared<op::v1::Add>(bottom_reshape, add_param);+ model_ref = std::make_shared<ov::Model>(NodeVector{add}, ParameterVector{input, add_param});+ }++ manager.register_pass<ov::pass::NopElimination>();+ manager.run_passes(model);++ auto res = comparator.compare(model, model_ref);+ ASSERT_TRUE(res.valid) << res.message;+}+TEST(nop_elimination, reshape_elimination_v1_dynamic_negative) {auto arg = std::make_shared<op::v0::Parameter>(element::i64, PartialShape::dynamic());auto pattern = make_shared<op::v0::Parameter>(element::i64, PartialShape::dynamic(1));