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] Migrate Quantize shape inference rule to sinf::Algorithm #13812

Merged
merged 3 commits into from
Aug 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Algorithm final : public luci::CircleNodeVisitor<loco::TensorShape>
// loco::TensorShape visit(const luci::CirclePadV2 *node) final;
// loco::TensorShape visit(const luci::CirclePow *node) final;
// loco::TensorShape visit(const luci::CirclePRelu *node) final;
// loco::TensorShape visit(const luci::CircleQuantize *node) final;
loco::TensorShape visit(const luci::CircleQuantize *node) final;
// loco::TensorShape visit(const luci::CircleRange *node) final;
// loco::TensorShape visit(const luci::CircleRank *node) final;
// loco::TensorShape visit(const luci::CircleReduceAny *node) final;
Expand Down
6 changes: 0 additions & 6 deletions compiler/luci/service/src/CircleShapeInferenceRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,12 +2235,6 @@ class ShapeInferenceAlgorithm final : public luci::CircleNodeVisitor<loco::NodeS

loco::NodeShape visit(const luci::CirclePRelu *node) final { return infer_p_relu(node); }

loco::NodeShape visit(const luci::CircleQuantize *node) final
{
const auto input_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
return loco::NodeShape{input_shape};
}

loco::NodeShape visit(const luci::CircleRange *node) final { return infer_range(node); }

loco::NodeShape visit(const luci::CircleRank *) final
Expand Down
15 changes: 15 additions & 0 deletions compiler/luci/service/src/Nodes/CircleQuantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
*/

#include "luci/Service/CircleShapeInference.h"

#include "CircleCloneNode.h"
#include "CircleShapeInferenceHelper.h"

namespace luci
{
Expand All @@ -24,4 +27,16 @@ luci::CircleNode *CloneNodeLet<CN::OPQR>::visit(const luci::CircleQuantize *)
return _graph->nodes()->create<luci::CircleQuantize>();
}

namespace sinf
{

loco::TensorShape Algorithm::visit(const luci::CircleQuantize *node)
{
const auto input = loco::must_cast<CircleNode *>(node->input());
const auto input_shape = circle_shape(input);
return input_shape;
}

} // namespace sinf

} // namespace luci
42 changes: 42 additions & 0 deletions compiler/luci/service/src/Nodes/CircleQuantize.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "luci/Service/CircleNodeClone.h"
#include "luci/Service/CircleShapeInference.h"

#include <gtest/gtest.h>

Expand All @@ -31,3 +32,44 @@ TEST(CloneNodeTest, clone_Quantize)
auto cloned_q = dynamic_cast<luci::CircleQuantize *>(cloned);
ASSERT_NE(nullptr, cloned_q);
}

TEST(ShapeRuleTest, quantize_dynamic_shape)
{
luci::CircleInput input;
luci::CircleQuantize quantize;

input.shape({1, 2, 3, 4});
input.shape_status(luci::ShapeStatus::VALID);
input.dim(2).unset();

quantize.input(&input);

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

ASSERT_TRUE(shape_inf_rule.infer(&quantize, shape));

ASSERT_EQ(4, shape.rank());
ASSERT_TRUE(shape.dim(0).known());
ASSERT_TRUE(shape.dim(1).known());
ASSERT_FALSE(shape.dim(2).known());
ASSERT_TRUE(shape.dim(3).known());
ASSERT_EQ(1, shape.dim(0).value());
ASSERT_EQ(2, shape.dim(1).value());
ASSERT_EQ(0, shape.dim(2).value());
ASSERT_EQ(4, shape.dim(3).value());
}

TEST(ShapeRuleTest, quantize_shape_not_ready_NEG)
{
luci::CircleInput input;
luci::CircleQuantize quantize;

input.shape_status(luci::ShapeStatus::UNDEFINED);
quantize.input(&input);

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

ASSERT_FALSE(shape_inf_rule.infer(&quantize, shape));
}