Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT][luci/service] Support CircleOutputDummy as reshape's shape #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion compiler/luci/service/src/Nodes/CircleReshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ namespace sinf
* @note CircleReshape always has two inputs: `tensor` and `shape`.
* The `shape` can be CircleConst, CircleOutputDummy, or CircleNode.
* - If the `shape` is CircleConst, the shape is inferred from the constant.
* - If the `shape` is CircleOutputDummy, the shape is inferred from:
* - the attribute if it exists.
* - the node itself if the attribute does not exist.
* - Else, the shape is inferred from the node iteself.
* - TODO support CircleOutputDummy and CircleNode
* - TODO support CircleNode
*/
loco::TensorShape Algorithm::visit(const luci::CircleReshape *node)
{
Expand Down Expand Up @@ -99,6 +102,29 @@ loco::TensorShape Algorithm::visit(const luci::CircleReshape *node)
}
}
}
else if (auto dummy_shape_node = dynamic_cast<luci::CircleOutputDummy *>(node->shape()))
{
if (node->newShape()->rank() > 0)
{
shape_by_input.rank(node->newShape()->rank());

for (uint32_t axis = 0; axis < shape_by_input.rank(); ++axis)
{
shape_by_input.dim(axis) = node->newShape()->dim(axis);
if (node->newShape()->dim(axis) < 0)
{
shape_by_input.dim(axis).unset();
}
}
}
else
{
// If the `shape` is CircleOutputDummy and the attribute does not exist,
// this status cannot be handled by this shape inference rule.
// TODO support no `shape` and no attribute case
shape_by_input = circle_shape(node);
}
}
else
{
// We use shape from the node itself
Expand Down
64 changes: 64 additions & 0 deletions compiler/luci/service/src/Nodes/CircleReshape.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,67 @@ TEST(ShapeRuleTest, reshape_should_infer)
ASSERT_TRUE(output_shape.dim(1).known());
ASSERT_EQ(4, output_shape.dim(1).value());
}

TEST(ShapeRuleTest, reshape_by_dummy_static)
{
auto g = loco::make_graph();
auto node_reshape = g->nodes()->create<luci::CircleReshape>();
auto tensor_input = g->nodes()->create<luci::CircleInput>();
auto shape_dummy = g->nodes()->create<luci::CircleOutputDummy>();

tensor_input->dtype(loco::DataType::S32);
tensor_input->shape({2, 3, 4});
tensor_input->shape_status(luci::ShapeStatus::VALID);

shape_dummy->dtype(loco::DataType::S32);
shape_dummy->shape_status(luci::ShapeStatus::VALID);

node_reshape->tensor(tensor_input);
node_reshape->shape(shape_dummy);
node_reshape->newShape()->rank(2);
node_reshape->newShape()->dim(0) = 6;
node_reshape->newShape()->dim(1) = 4;

loco::TensorShape output_shape;
luci::sinf::Rule shape_inf_rule;

ASSERT_TRUE(shape_inf_rule.infer(node_reshape, output_shape));

ASSERT_EQ(2, output_shape.rank());
ASSERT_TRUE(output_shape.dim(0).known());
ASSERT_TRUE(output_shape.dim(1).known());
ASSERT_EQ(6, output_shape.dim(0).value());
ASSERT_EQ(4, output_shape.dim(1).value());
}

TEST(ShapeRuleTest, reshape_by_dummy_dynamic)
{
auto g = loco::make_graph();
auto node_reshape = g->nodes()->create<luci::CircleReshape>();
auto tensor_input = g->nodes()->create<luci::CircleInput>();
auto shape_dummy = g->nodes()->create<luci::CircleOutputDummy>();

tensor_input->dtype(loco::DataType::S32);
tensor_input->shape({2, 3, 4});
tensor_input->shape_status(luci::ShapeStatus::VALID);

shape_dummy->dtype(loco::DataType::S32);
shape_dummy->shape_status(luci::ShapeStatus::VALID);

node_reshape->tensor(tensor_input);
node_reshape->shape(shape_dummy);
node_reshape->newShape()->rank(2);
node_reshape->newShape()->dim(0) = -1;
node_reshape->newShape()->dim(1) = 4;

loco::TensorShape output_shape;
luci::sinf::Rule shape_inf_rule;

ASSERT_TRUE(shape_inf_rule.infer(node_reshape, output_shape));

ASSERT_EQ(2, output_shape.rank());
ASSERT_TRUE(output_shape.dim(0).known());
ASSERT_TRUE(output_shape.dim(1).known());
ASSERT_EQ(6, output_shape.dim(0).value());
ASSERT_EQ(4, output_shape.dim(1).value());
}