From b027766b0a86dd07b6dc9cbd8bb4cd34c712cd63 Mon Sep 17 00:00:00 2001 From: Mateusz Tabaka Date: Mon, 27 Nov 2023 12:30:54 +0100 Subject: [PATCH] Stop constantfold_subgraph on nonconstfoldable ShapeOf (#21171) * Stop constantfold_subgraph on nonconstfoldable ShapeOf Ticket: CVS-124628 * pass tensor to Constant constructor --------- Co-authored-by: Ivan Tikhonov --- src/core/src/validation_util.cpp | 9 +++++++++ src/core/tests/validation_utils.cpp | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/core/src/validation_util.cpp b/src/core/src/validation_util.cpp index 13db184c04b3ee..3e77d9df8dd858 100644 --- a/src/core/src/validation_util.cpp +++ b/src/core/src/validation_util.cpp @@ -1188,6 +1188,15 @@ std::shared_ptr ov::util::constantfold_subgraph(const Outp if (num_inputs == 0) return nullptr; + if (subgraph_sink.get_tensor().has_and_set_bound()) { + const auto& lower = subgraph_sink.get_tensor().get_lower_value(); + return std::make_shared(lower); + } + + if (ov::is_type(node) && node->get_input_partial_shape(0).is_dynamic()) { + return nullptr; + } + OutputVector inputs; inputs.reserve(num_inputs); for (size_t i = 0; i < num_inputs; i++) { diff --git a/src/core/tests/validation_utils.cpp b/src/core/tests/validation_utils.cpp index bb764230409e0f..2d22affa74be0e 100644 --- a/src/core/tests/validation_utils.cpp +++ b/src/core/tests/validation_utils.cpp @@ -71,3 +71,19 @@ TEST(constantfold_subgraph, split) { auto actual = ret->cast_vector(); ASSERT_EQ(expected, actual); } + +TEST(constantfold_subgraph, shapeof) { + auto param = std::make_shared(ov::element::f32, ov::PartialShape{-1, 3, -1}); + auto shapeof = std::make_shared(param); + auto zero = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{1}, {0}); + auto one = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{1}, {1}); + auto two = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{1}, {2}); + auto stop = std::make_shared(shapeof, one /*start*/, two /*stop*/, one /*step*/, zero /*axis*/); + auto slice = std::make_shared(param, one /*start*/, stop, one /*step*/, one /*axis*/); + + auto ret = ov::util::constantfold_subgraph(stop); + ASSERT_NE(ret, nullptr); + auto actual = ret->cast_vector(); + std::vector expected{3}; + ASSERT_EQ(expected, actual); +}