From e668ea02c27a3cf15c8ed0201122b61e02343486 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=84=B8=ED=9D=AC/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 2 Oct 2019 12:38:00 +0900 Subject: [PATCH] [moco] Introduce IR for Add AvgPool BiasAdd (#7890) This will introduce IR for Add, AvgPool and BiasAdd node Signed-off-by: SaeHie Park --- compiler/moco/lang/include/moco/IR/Nodes/TFAdd.h | 56 ++++++++++++ .../moco/lang/include/moco/IR/Nodes/TFAvgPool.h | 101 +++++++++++++++++++++ .../moco/lang/include/moco/IR/Nodes/TFBiasAdd.h | 68 ++++++++++++++ compiler/moco/lang/include/moco/IR/TFNodes.h | 6 +- compiler/moco/lang/include/moco/IR/TFNodes.lst | 6 +- compiler/moco/lang/src/IR/Nodes/TFAdd.test.cpp | 31 +++++++ compiler/moco/lang/src/IR/Nodes/TFAvgPool.test.cpp | 34 +++++++ compiler/moco/lang/src/IR/Nodes/TFBiasAdd.test.cpp | 32 +++++++ 8 files changed, 328 insertions(+), 6 deletions(-) create mode 100644 compiler/moco/lang/include/moco/IR/Nodes/TFAdd.h create mode 100644 compiler/moco/lang/include/moco/IR/Nodes/TFAvgPool.h create mode 100644 compiler/moco/lang/include/moco/IR/Nodes/TFBiasAdd.h create mode 100644 compiler/moco/lang/src/IR/Nodes/TFAdd.test.cpp create mode 100644 compiler/moco/lang/src/IR/Nodes/TFAvgPool.test.cpp create mode 100644 compiler/moco/lang/src/IR/Nodes/TFBiasAdd.test.cpp diff --git a/compiler/moco/lang/include/moco/IR/Nodes/TFAdd.h b/compiler/moco/lang/include/moco/IR/Nodes/TFAdd.h new file mode 100644 index 0000000..13b064f --- /dev/null +++ b/compiler/moco/lang/include/moco/IR/Nodes/TFAdd.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IR_TFADD_H__ +#define __MOCO_IR_TFADD_H__ + +#include "moco/IR/TFNodeDecl.h" + +namespace moco +{ + +/// @note TFAdd corresponds to the following GraphDef +/* +node { + name: "add" + op: "Add" + input: "x" + input: "y" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +*/ + +class TFAdd final : public FixedArityNode<2, TFNodeImpl> +{ +public: + TFAdd() = default; + +public: + Node *x(void) const { return at(0)->node(); } + void x(Node *node) { at(0)->node(node); } + + Node *y(void) const { return at(1)->node(); } + void y(Node *node) { at(1)->node(node); } +}; + +} // namespace moco + +#endif // __MOCO_IR_TFADD_H__ diff --git a/compiler/moco/lang/include/moco/IR/Nodes/TFAvgPool.h b/compiler/moco/lang/include/moco/IR/Nodes/TFAvgPool.h new file mode 100644 index 0000000..65486a6 --- /dev/null +++ b/compiler/moco/lang/include/moco/IR/Nodes/TFAvgPool.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IR_TFAVGPOOL_H__ +#define __MOCO_IR_TFAVGPOOL_H__ + +#include "moco/IR/TFNodeDecl.h" + +#include + +namespace moco +{ + +/// @note TFAvgPool corresponds to the following GraphDef +/* +node { + name: "avgpool" + op: "AvgPool" + input: "placeholder" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "ksize" + value { + list { + i: 1 i: 3 i: 3 i: 1 + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 i: 1 i: 1 i: 1 + } + } + } +} +*/ + +class TFAvgPool final : public FixedArityNode<1, TFNodeImpl> +{ +public: + TFAvgPool() = default; + +public: + Node *input(void) const { return at(0)->node(); } + void input(Node *node) { return at(0)->node(node); } + +public: + const TFDataLayout &data_layout(void) const { return _data_layout; } + void data_layout(const TFDataLayout &data_layout) { _data_layout = data_layout; } + + const TFPadding &padding(void) const { return _padding; } + void padding(const TFPadding &padding) { _padding = padding; } + + const std::vector &ksize(void) const { return _ksize; } + void ksize(const std::vector &ksize) { _ksize = ksize; } + + const std::vector &strides(void) const { return _strides; } + void strides(const std::vector &strides) { _strides = strides; } + +private: + TFDataLayout _data_layout; + TFPadding _padding; + std::vector _ksize; + std::vector _strides; +}; + +} // namespace moco + +#endif // __MOCO_IR_TFAVGPOOL_H__ diff --git a/compiler/moco/lang/include/moco/IR/Nodes/TFBiasAdd.h b/compiler/moco/lang/include/moco/IR/Nodes/TFBiasAdd.h new file mode 100644 index 0000000..11e309c --- /dev/null +++ b/compiler/moco/lang/include/moco/IR/Nodes/TFBiasAdd.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IR_TFBIASADD_H__ +#define __MOCO_IR_TFBIASADD_H__ + +#include "moco/IR/TFNodeDecl.h" + +namespace moco +{ + +/// @note TFBiasAdd corresponds to the following GraphDef +/* +node { + name: "bias_add_01" + op: "BiasAdd" + input: "input_01" + input: "bias_add_01/bias" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +*/ + +class TFBiasAdd final : public FixedArityNode<2, TFNodeImpl> +{ +public: + TFBiasAdd() = default; + +public: + Node *value(void) const { return at(0)->node(); } + void value(Node *node) { return at(0)->node(node); } + + Node *bias(void) const { return at(1)->node(); } + void bias(Node *node) { return at(1)->node(node); } + + const TFDataLayout data_layout(void) const { return _data_layout; } + void data_layout(const TFDataLayout &data_layout) { _data_layout = data_layout; } + +private: + TFDataLayout _data_layout; +}; + +} // namespace moco + +#endif // __MOCO_IR_TFBIASADD_H__ diff --git a/compiler/moco/lang/include/moco/IR/TFNodes.h b/compiler/moco/lang/include/moco/IR/TFNodes.h index b666acd..cb1d3b1 100644 --- a/compiler/moco/lang/include/moco/IR/TFNodes.h +++ b/compiler/moco/lang/include/moco/IR/TFNodes.h @@ -17,9 +17,9 @@ #ifndef __MOCO_IR_TFNODES_H__ #define __MOCO_IR_TFNODES_H__ -//#include "moco/IR/Nodes/TFAdd.h" -//#include "moco/IR/Nodes/TFAvgPool.h" -//#include "moco/IR/Nodes/TFBiasAdd.h" +#include "moco/IR/Nodes/TFAdd.h" +#include "moco/IR/Nodes/TFAvgPool.h" +#include "moco/IR/Nodes/TFBiasAdd.h" //#include "moco/IR/Nodes/TFConcatV2.h" //#include "moco/IR/Nodes/TFConst.h" //#include "moco/IR/Nodes/TFConv2D.h" diff --git a/compiler/moco/lang/include/moco/IR/TFNodes.lst b/compiler/moco/lang/include/moco/IR/TFNodes.lst index 0e30cd4..9132cd8 100644 --- a/compiler/moco/lang/include/moco/IR/TFNodes.lst +++ b/compiler/moco/lang/include/moco/IR/TFNodes.lst @@ -7,9 +7,9 @@ // // TENSORFLOW_NODE(OPCODE, CLASS) -//TENSORFLOW_NODE(Add, TFAdd) -//TENSORFLOW_NODE(AvgPool, TFAvgPool) -//TENSORFLOW_NODE(BiasAdd, TFBiasAdd) +TENSORFLOW_NODE(Add, TFAdd) +TENSORFLOW_NODE(AvgPool, TFAvgPool) +TENSORFLOW_NODE(BiasAdd, TFBiasAdd) //TENSORFLOW_NODE(ConcatV2, TFConcatV2) //TENSORFLOW_NODE(Const, TFConst) //TENSORFLOW_NODE(Conv2D, TFConv2D) diff --git a/compiler/moco/lang/src/IR/Nodes/TFAdd.test.cpp b/compiler/moco/lang/src/IR/Nodes/TFAdd.test.cpp new file mode 100644 index 0000000..d2cfb6a --- /dev/null +++ b/compiler/moco/lang/src/IR/Nodes/TFAdd.test.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 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 "moco/IR/Nodes/TFAdd.h" +#include "moco/IR/TFDialect.h" + +#include + +TEST(TFAddTest, constructor) +{ + moco::TFAdd add_node; + + ASSERT_EQ(add_node.dialect(), moco::TFDialect::get()); + ASSERT_EQ(add_node.opcode(), moco::TFOpcode::Add); + + ASSERT_EQ(add_node.x(), nullptr); + ASSERT_EQ(add_node.y(), nullptr); +} diff --git a/compiler/moco/lang/src/IR/Nodes/TFAvgPool.test.cpp b/compiler/moco/lang/src/IR/Nodes/TFAvgPool.test.cpp new file mode 100644 index 0000000..3059855 --- /dev/null +++ b/compiler/moco/lang/src/IR/Nodes/TFAvgPool.test.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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 "moco/IR/Nodes/TFAvgPool.h" +#include "moco/IR/TFDialect.h" + +#include + +TEST(TFAvgPoolTest, constructor) +{ + moco::TFAvgPool avgpool; + + ASSERT_EQ(avgpool.dialect(), moco::TFDialect::get()); + ASSERT_EQ(avgpool.opcode(), moco::TFOpcode::AvgPool); + + ASSERT_EQ(avgpool.input(), nullptr); + ASSERT_EQ(avgpool.data_layout(), ""); + ASSERT_EQ(avgpool.padding(), ""); + ASSERT_EQ(avgpool.ksize(), std::vector({})); + ASSERT_EQ(avgpool.strides(), std::vector({})); +} diff --git a/compiler/moco/lang/src/IR/Nodes/TFBiasAdd.test.cpp b/compiler/moco/lang/src/IR/Nodes/TFBiasAdd.test.cpp new file mode 100644 index 0000000..4a15a49 --- /dev/null +++ b/compiler/moco/lang/src/IR/Nodes/TFBiasAdd.test.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 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 "moco/IR/Nodes/TFBiasAdd.h" +#include "moco/IR/TFDialect.h" + +#include + +TEST(TFBiasAddTest, constructor) +{ + moco::TFBiasAdd bias_add; + + ASSERT_EQ(bias_add.dialect(), moco::TFDialect::get()); + ASSERT_EQ(bias_add.opcode(), moco::TFOpcode::BiasAdd); + + ASSERT_EQ(bias_add.value(), nullptr); + ASSERT_EQ(bias_add.bias(), nullptr); + ASSERT_EQ(bias_add.data_layout(), ""); +} -- 2.7.4