From f43c9cdede9589c0bccd0dfb37996dbcf5e8e29d Mon Sep 17 00:00:00 2001 From: "seockho.kim" Date: Fri, 13 Sep 2024 18:24:32 +0900 Subject: [PATCH] [luci/partition] Support RmsNorm operation This commit supports RmsNorm operation in luci partition. ONE-DCO-1.0-Signed-off-by: Seockho Kim seockho.kim@samsung.com --- .../luci/partition/include/luci/ConnectNode.h | 1 + .../partition/src/Nodes/CircleRmsNorm.cpp | 42 ++++++++ .../src/Nodes/CircleRmsNorm.test.cpp | 97 +++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 compiler/luci/partition/src/Nodes/CircleRmsNorm.cpp create mode 100644 compiler/luci/partition/src/Nodes/CircleRmsNorm.test.cpp diff --git a/compiler/luci/partition/include/luci/ConnectNode.h b/compiler/luci/partition/include/luci/ConnectNode.h index 7539aaf6bee..592dd3b4d29 100644 --- a/compiler/luci/partition/include/luci/ConnectNode.h +++ b/compiler/luci/partition/include/luci/ConnectNode.h @@ -187,6 +187,7 @@ class ConnectNode final : public luci::CircleNodeVisitor void visit(const luci::CircleBCQGather *) final; void visit(const luci::CircleGRU *) final; void visit(const luci::CircleInstanceNorm *) final; + void visit(const luci::CircleRmsNorm *) final; // NOTE CircleInput and CircleOutput are not handled here as these need // link with graph I/O diff --git a/compiler/luci/partition/src/Nodes/CircleRmsNorm.cpp b/compiler/luci/partition/src/Nodes/CircleRmsNorm.cpp new file mode 100644 index 00000000000..fa7f58af357 --- /dev/null +++ b/compiler/luci/partition/src/Nodes/CircleRmsNorm.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "luci/ConnectNode.h" + +namespace +{ + +void connect(luci::ConnectNode *cn, const luci::CircleRmsNorm *node) +{ + auto *cloned = loco::must_cast(cn->find_clone(node)); + + luci::CircleNode *input = loco::must_cast(node->input()); + luci::CircleNode *gamma = loco::must_cast(node->gamma()); + luci::CircleNode *beta = loco::must_cast(node->beta()); + + cloned->input(cn->find_clone(input)); + cloned->gamma(cn->find_clone(gamma)); + cloned->beta(cn->find_clone(beta)); +} + +} // namespace + +namespace luci +{ + +void ConnectNode::visit(const luci::CircleRmsNorm *node) { connect(this, node); } + +} // namespace luci diff --git a/compiler/luci/partition/src/Nodes/CircleRmsNorm.test.cpp b/compiler/luci/partition/src/Nodes/CircleRmsNorm.test.cpp new file mode 100644 index 00000000000..625e66c2a14 --- /dev/null +++ b/compiler/luci/partition/src/Nodes/CircleRmsNorm.test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "luci/ConnectNode.h" + +#include "ConnectNode.test.h" + +#include + +#include + +namespace +{ + +using namespace luci::test; + +class NodeGraphlet : public NodeGraphletT +{ +public: + NodeGraphlet() = default; + +public: + void init(loco::Graph *g) override { NodeGraphletT::init(g); } +}; + +class TestNodeGraph : public TestIsOGraph<3>, public NodeGraphlet +{ +public: + TestNodeGraph() = default; + +public: + void init(const ShapeU32 shape) + { + TestIsOGraph<3>::init({shape, shape, shape}, shape); + NodeGraphlet::init(g()); + + node()->input(input(0)); + node()->gamma(input(1)); + node()->beta(input(2)); + + output()->from(node()); + } +}; + +} // namespace + +TEST(ConnectNodeTest, connect_RmsNorm) +{ + TestNodeGraph tng; + tng.init({2, 3}); + + ConnectionTestHelper cth; + cth.prepare_inputs(&tng); + + auto *node = tng.node(); + ASSERT_NO_THROW(loco::must_cast(node)); + + auto *clone = luci::clone_node(node, cth.graph_clone()); + ASSERT_NO_THROW(loco::must_cast(clone)); + + cth.clone_connect(node, clone); + + ASSERT_EQ(3, clone->arity()); + ASSERT_EQ(cth.inputs(0), clone->arg(0)); + ASSERT_EQ(cth.inputs(1), clone->arg(1)); + ASSERT_EQ(cth.inputs(2), clone->arg(2)); +} + +TEST(ConnectNodeTest, connect_RmsNorm_NEG) +{ + TestNodeGraph tng; + tng.init({2, 3}); + + ConnectionTestHelper cth; + cth.prepare_inputs_miss(&tng); + + auto *node = tng.node(); + ASSERT_NO_THROW(loco::must_cast(node)); + + auto *clone = luci::clone_node(node, cth.graph_clone()); + ASSERT_NO_THROW(loco::must_cast(clone)); + + EXPECT_ANY_THROW(cth.clone_connect(node, clone)); +}