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..a5a4f0532d9 100644 --- a/compiler/luci/service/src/Nodes/CircleReshape.test.cpp +++ b/compiler/luci/service/src/Nodes/CircleReshape.test.cpp @@ -197,3 +197,39 @@ 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) = 2; + 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(2, output_shape.dim(1).value()); + ASSERT_FALSE(output_shape.dim(2).known()); +}