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

[luci/service] Support Range op Shape Inference for Non-const Param #14010

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
16 changes: 1 addition & 15 deletions compiler/luci/service/src/Nodes/CircleRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,7 @@ loco::TensorShape Algorithm::visit(const luci::CircleRange *node)

if (start_node == nullptr || limit_node == nullptr || delta_node == nullptr)
{
// We use shape from the node itself
loco::TensorShape shape;
shape.rank(node->rank());
for (uint32_t r = 0; r < node->rank(); ++r)
{
// TODO remove this copy from `use_own(node);`
// Shape inference rules in this file did not consider unknown dimension.
// If some node has unknown dimension, 0 is inserted and wrong shape
// inference was done as a result.
// To fix this, new shape inference algorithm is being implemented.
// Until new inference algorithm is fully implemented, unknown dimension
// would be represented as 1 along with TFLite expression.
shape.dim(r) = node->dim(r).known() ? node->dim(r).value() : 1;
}
return shape;
return output_shape;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

above

output_shape.rank(1);

now output rank is 1. how is this valid?

Copy link
Contributor Author

@kyeong8139 kyeong8139 Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Range operation, the output is a sequence of numbers.
so output_shape.rank() is 1 both const and non-const params.
However, when params are non constant, dim(0).value() is determine in runtime.

+)
According to TensorFlow, Return is 'An 1-D Tensor of type dtype.' https://www.tensorflow.org/api_docs/python/tf/range#returns

}

double start = 0, limit = 0, delta = 0;
Expand Down
53 changes: 53 additions & 0 deletions compiler/luci/service/src/Nodes/CircleRange.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,56 @@ TEST(ShapeRuleTest, range_zero_delta_NEG)

ASSERT_ANY_THROW(shape_inf_rule.infer(&range, shape));
}

TEST(ShapeRuleTest, range_non_const_param)
{
luci::CircleInput start, limit, delta;
luci::CircleRange range;

start.dtype(loco::DataType::S32);
start.shape({1});
start.shape_status(luci::ShapeStatus::VALID);

limit.dtype(loco::DataType::S32);
limit.shape({1});
limit.shape_status(luci::ShapeStatus::VALID);

delta.dtype(loco::DataType::S32);
delta.shape({1});
delta.shape_status(luci::ShapeStatus::VALID);

range.start(&start);
range.limit(&limit);
range.delta(&delta);

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

ASSERT_TRUE(shape_inf_rule.infer(&range, shape));
ASSERT_EQ(1, shape.rank());
ASSERT_FALSE(shape.dim(0).known());
ASSERT_EQ(0, shape.dim(0).value());
}

TEST(ShapeRuleTest, range_nullptr_start_NEG)
{
luci::CircleInput limit, delta;
luci::CircleRange range;

limit.dtype(loco::DataType::S32);
limit.shape({1});
limit.shape_status(luci::ShapeStatus::VALID);

delta.dtype(loco::DataType::S32);
delta.shape({1});
delta.shape_status(luci::ShapeStatus::VALID);

range.start(nullptr);
range.limit(&limit);
range.delta(&delta);

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

ASSERT_ANY_THROW(shape_inf_rule.infer(&range, shape));
}