From b4da310b25a5c7a8449e23b715597f24feb5bb1b Mon Sep 17 00:00:00 2001 From: Mateusz Bencer Date: Wed, 19 Mar 2025 19:28:42 +0100 Subject: [PATCH 1/2] [luci] Use static dimensions from new_shape attribute This PR adds use static dimension from new_shape attribute even if only some of them are static and the rest dynamic. ONE-DCO-1.0-Signed-off-by: Mateusz Bencer m.bencer@partner.samsung.com --- .../luci/service/src/Nodes/CircleReshape.cpp | 12 ++++-- .../service/src/Nodes/CircleReshape.test.cpp | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/compiler/luci/service/src/Nodes/CircleReshape.cpp b/compiler/luci/service/src/Nodes/CircleReshape.cpp index 0450ba8358d..e45655c54c9 100644 --- a/compiler/luci/service/src/Nodes/CircleReshape.cpp +++ b/compiler/luci/service/src/Nodes/CircleReshape.cpp @@ -120,8 +120,7 @@ loco::TensorShape Algorithm::visit(const luci::CircleReshape *node) // for non-existing `shape`, we can use `newShape` if it's valid auto new_shape = node->newShape(); auto rank = new_shape->rank(); - auto shape_dummy = dynamic_cast(node->shape()); - if (shape_dummy && rank > 0) + if (rank > 0) { is_static_shape = true; shape_by_input.rank(rank); @@ -154,7 +153,14 @@ loco::TensorShape Algorithm::visit(const luci::CircleReshape *node) for (uint32_t axis = 0; axis < shape_by_attr.rank(); ++axis) { - shape_by_attr.dim(axis) = node->newShape()->dim(axis); + if (node->newShape()->dim(axis) > 0) + { + shape_by_attr.dim(axis) = node->newShape()->dim(axis); + } + else + { + shape_by_attr.dim(axis).unset(); // unset means unknown dimension + } } } diff --git a/compiler/luci/service/src/Nodes/CircleReshape.test.cpp b/compiler/luci/service/src/Nodes/CircleReshape.test.cpp index 653cb690d18..9e4e53128f0 100644 --- a/compiler/luci/service/src/Nodes/CircleReshape.test.cpp +++ b/compiler/luci/service/src/Nodes/CircleReshape.test.cpp @@ -197,3 +197,40 @@ TEST(ShapeRuleTest, reshape_by_newShape) ASSERT_EQ(2, output_shape.dim(0).value()); ASSERT_EQ(12, output_shape.dim(1).value()); } + +TEST(ShapeRuleTest, reshape_by_newShape_dynamic) +{ + auto g = loco::make_graph(); + auto node_reshape = g->nodes()->create(); + auto tensor_input = g->nodes()->create(); + auto target_shape = g->nodes()->create(); + ; + + tensor_input->dtype(loco::DataType::S32); + tensor_input->shape({2, 3, 4}); + tensor_input->shape_status(luci::ShapeStatus::VALID); + + target_shape->dtype(loco::DataType::S32); + target_shape->rank(1); + target_shape->shape_status(luci::ShapeStatus::VALID); + + node_reshape->tensor(tensor_input); + node_reshape->shape(target_shape); + + // reshape to {dynamic, 4, dynamic} + node_reshape->newShape()->rank(3); + node_reshape->newShape()->dim(0) = -1; + node_reshape->newShape()->dim(1) = 4; + node_reshape->newShape()->dim(2) = -1; + + loco::TensorShape output_shape; + luci::sinf::Rule shape_inf_rule; + + ASSERT_TRUE(shape_inf_rule.infer(node_reshape, output_shape)); + + ASSERT_EQ(3, output_shape.rank()); + ASSERT_FALSE(output_shape.dim(0).known()); + ASSERT_TRUE(output_shape.dim(1).known()); + ASSERT_EQ(4, output_shape.dim(1).value()); + ASSERT_FALSE(output_shape.dim(2).known()); +} From d3b46c2ce0a886ab5d283b41971d163565258785 Mon Sep 17 00:00:00 2001 From: Mateusz Bencer Date: Thu, 20 Mar 2025 10:17:50 +0100 Subject: [PATCH 2/2] review remarks --- compiler/luci/service/src/Nodes/CircleReshape.test.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/luci/service/src/Nodes/CircleReshape.test.cpp b/compiler/luci/service/src/Nodes/CircleReshape.test.cpp index 9e4e53128f0..a5a4f0532d9 100644 --- a/compiler/luci/service/src/Nodes/CircleReshape.test.cpp +++ b/compiler/luci/service/src/Nodes/CircleReshape.test.cpp @@ -204,7 +204,6 @@ TEST(ShapeRuleTest, reshape_by_newShape_dynamic) auto node_reshape = g->nodes()->create(); auto tensor_input = g->nodes()->create(); auto target_shape = g->nodes()->create(); - ; tensor_input->dtype(loco::DataType::S32); tensor_input->shape({2, 3, 4}); @@ -220,7 +219,7 @@ TEST(ShapeRuleTest, reshape_by_newShape_dynamic) // reshape to {dynamic, 4, dynamic} node_reshape->newShape()->rank(3); node_reshape->newShape()->dim(0) = -1; - node_reshape->newShape()->dim(1) = 4; + node_reshape->newShape()->dim(1) = 2; node_reshape->newShape()->dim(2) = -1; loco::TensorShape output_shape; @@ -231,6 +230,6 @@ TEST(ShapeRuleTest, reshape_by_newShape_dynamic) ASSERT_EQ(3, output_shape.rank()); ASSERT_FALSE(output_shape.dim(0).known()); ASSERT_TRUE(output_shape.dim(1).known()); - ASSERT_EQ(4, output_shape.dim(1).value()); + ASSERT_EQ(2, output_shape.dim(1).value()); ASSERT_FALSE(output_shape.dim(2).known()); }