Skip to content

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

[opset1::Reshape] Deduce symbol relation based on op spec (#27399)

Evgenya Nugmanova539a2aca89dc
diff --git a/src/core/shape_inference/include/reshape_shape_inference.hpp b/src/core/shape_inference/include/reshape_shape_inference.hpp
index 273d803ca8c..d3b9acf5428 100644
--- a/src/core/shape_inference/include/reshape_shape_inference.hpp
+++ b/src/core/shape_inference/include/reshape_shape_inference.hpp
@@ -239,6 +239,20 @@ void set_pattern_symbols(const Node* const op, TShape& shape) {
template <class TShape, typename std::enable_if<!std::is_same<TShape, PartialShape>::value>::type* = nullptr>
void set_pattern_symbols(const Node* const, TShape&) {}
+/** @brief Deducing symbol relations: number of elements in the tensor doesn't change after the Reshape operation. */
+template <class TDim,
+ typename std::enable_if<std::is_same<typename std::decay<TDim>::type, Dimension>::value>::type* = nullptr>
+void deduce_symbol_relations(const Product<TDim>& product) {
+ auto dyn_in = product.get_dynamic_in();
+ auto dyn_out = product.get_dynamic_out();
+ dyn_in.merge(dyn_in, dyn_in, dyn_out);
+}
+
+/** @brief Shapes other than PartialShape have no symbols. */
+template <class TDim,
+ typename std::enable_if<!std::is_same<typename std::decay<TDim>::type, Dimension>::value>::type* = nullptr>
+void deduce_symbol_relations(const Product<TDim>& product) {}
+
} // namespace reshape
namespace v1 {
@@ -342,6 +356,10 @@ std::vector<TRShape> shape_infer(const Reshape* op,
!dim::is_empty(minus_one_dim),
"Non-'-1' output dimensions do not evenly divide the input dimensions");
}
+ } else {
+ if (product.get_static_in() == product.get_static_out() && product.get_static_in() != 0) {
+ deduce_symbol_relations(product);
+ }
}
if (input_shape.is_static() && output_shape.is_static()) {
diff --git a/src/core/tests/type_prop/reshape.cpp b/src/core/tests/type_prop/reshape.cpp
index 7ed87ce4720..3a16c5835c5 100644
--- a/src/core/tests/type_prop/reshape.cpp
+++ b/src/core/tests/type_prop/reshape.cpp
@@ -1333,3 +1333,24 @@ TEST(type_prop, reshape_pattern_dim_has_invalid_bound) {
EXPECT_EQ(reshape->get_output_partial_shape(0), ov::PartialShape({7, -1, 1, 2}));
}
+
+TEST(type_prop, reshape_symbol_deducing) {
+ auto A = std::make_shared<ov::Symbol>();
+ auto B = std::make_shared<ov::Symbol>();
+ auto C = std::make_shared<ov::Symbol>();
+
+ auto in_shape = ov::PartialShape({-1, -1, 768});
+ in_shape[0].set_symbol(A);
+ in_shape[1].set_symbol(B);
+
+ auto out_shape = ov::PartialShape({-1, -1, 12, 64});
+ out_shape[0].set_symbol(A);
+ out_shape[1].set_symbol(C);
+
+ const auto in = std::make_shared<op::v0::Parameter>(element::f32, in_shape);
+ const auto out = std::make_shared<op::v3::ShapeOf>(std::make_shared<op::v0::Parameter>(element::f32, out_shape));
+ const auto reshape = std::make_shared<op::v1::Reshape>(in, out, false);
+
+ EXPECT_EQ(reshape->get_output_partial_shape(0), ov::PartialShape({-1, -1, 12, 64}));
+ EXPECT_TRUE(ov::symbol::are_equal(B, C));
+}
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/shape_inference/custom/reshape.cpp b/src/plugins/intel_cpu/src/shape_inference/custom/reshape.cpp
index e6db58637de..3bb4985591a 100644
--- a/src/plugins/intel_cpu/src/shape_inference/custom/reshape.cpp
+++ b/src/plugins/intel_cpu/src/shape_inference/custom/reshape.cpp
@@ -55,6 +55,8 @@ Result ReshapeShapeInfer::infer(const std::vector<std::reference_wrapper<const V
outputShape[minusOneIdx] = 0;
}
}
+ inputProduct = std::accumulate(inputShape.begin(), inputShape.end(), 1, std::multiplies<Dim>());
+ outputProduct = std::accumulate(outputShape.begin(), outputShape.end(), 1, std::multiplies<Dim>());
if (minusOneCount > 1 || inputProduct != outputProduct) {
OPENVINO_THROW("[cpu]reshape: the shape of input data ", ov::intel_cpu::vec2str(inputShape),
" conflicts with the reshape pattern ", ov::intel_cpu::vec2str(outPattern));