From c5dd3ab2843ca6992c0cd85cce001a006077f1c0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=B1=84=EC=84=B1=EC=9A=B0/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 8 Oct 2019 16:51:37 +0900 Subject: [PATCH] [moco-tf] Introduce TFPad IR (#7959) * [moco-tf] Introduce TFPad IR This commit introduces TFPad IR to moco-tf. Signed-off-by: seongwoo * apply comments. * move location of function declaration alphabetical order. * rename input method as convention and remove unnecessary assert. --- compiler/moco-tf/src/Dialect/TFNodes.h | 1 + compiler/moco-tf/src/Dialect/TFNodes.lst | 1 + .../moco-tf/src/Dialect/TFShapeInferenceRule.cpp | 24 ++++++++ .../moco-tf/src/Dialect/TFTypeInferenceRule.cpp | 1 + compiler/moco-tf/src/IR/TFPad.h | 64 ++++++++++++++++++++++ compiler/moco-tf/src/IR/TFPad.test.cpp | 32 +++++++++++ .../moco-tf/src/Transforms/FixShapeTransform.cpp | 2 + 7 files changed, 125 insertions(+) create mode 100644 compiler/moco-tf/src/IR/TFPad.h create mode 100644 compiler/moco-tf/src/IR/TFPad.test.cpp diff --git a/compiler/moco-tf/src/Dialect/TFNodes.h b/compiler/moco-tf/src/Dialect/TFNodes.h index 985ad69..269c8cd 100644 --- a/compiler/moco-tf/src/Dialect/TFNodes.h +++ b/compiler/moco-tf/src/Dialect/TFNodes.h @@ -30,6 +30,7 @@ #include "IR/TFMaxPool.h" #include "IR/TFMean.h" #include "IR/TFMul.h" +#include "IR/TFPad.h" #include "IR/TFRealDiv.h" #include "IR/TFRelu.h" #include "IR/TFRelu6.h" diff --git a/compiler/moco-tf/src/Dialect/TFNodes.lst b/compiler/moco-tf/src/Dialect/TFNodes.lst index 2e3852d..f43eff3 100644 --- a/compiler/moco-tf/src/Dialect/TFNodes.lst +++ b/compiler/moco-tf/src/Dialect/TFNodes.lst @@ -20,6 +20,7 @@ TENSORFLOW_NODE(Identity, TFIdentity) TENSORFLOW_NODE(MaxPool, TFMaxPool) TENSORFLOW_NODE(Mean, TFMean) TENSORFLOW_NODE(Mul, TFMul) +TENSORFLOW_NODE(Pad, TFPad) TENSORFLOW_NODE(RealDiv, TFRealDiv) TENSORFLOW_NODE(Relu, TFRelu) TENSORFLOW_NODE(Relu6, TFRelu6) diff --git a/compiler/moco-tf/src/Dialect/TFShapeInferenceRule.cpp b/compiler/moco-tf/src/Dialect/TFShapeInferenceRule.cpp index ab34bf1..e6772e7 100644 --- a/compiler/moco-tf/src/Dialect/TFShapeInferenceRule.cpp +++ b/compiler/moco-tf/src/Dialect/TFShapeInferenceRule.cpp @@ -227,6 +227,30 @@ public: return moco::tf::as_tensor_shape(output_feature_shape, node->data_layout()); } + loco::NodeShape visit(const moco::tf::TFPad *node) final + { + auto input_shape = node_shape(node->input()); + assert(input_shape.domain() == loco::Domain::Tensor); + + auto const_paddings = dynamic_cast(node->paddings()); + assert(const_paddings); + assert(const_paddings->dtype() == loco::DataType::S32); + assert(const_paddings->rank() == 2); + + loco::TensorShape input_tensor_shape = input_shape.as(); + loco::TensorShape output_tensor_shape; + + output_tensor_shape.rank(input_tensor_shape.rank()); + for (uint32_t axis = 0; axis < input_tensor_shape.rank(); ++axis) + { + output_tensor_shape.dim(axis) = input_tensor_shape.dim(axis).value() + + const_paddings->at(axis * 2) + + const_paddings->at(axis * 2 + 1); + } + + return loco::NodeShape{output_tensor_shape}; + } + public: loco::NodeShape visit(const moco::tf::TFNode *node) final { diff --git a/compiler/moco-tf/src/Dialect/TFTypeInferenceRule.cpp b/compiler/moco-tf/src/Dialect/TFTypeInferenceRule.cpp index 5a513ea..db63b97 100644 --- a/compiler/moco-tf/src/Dialect/TFTypeInferenceRule.cpp +++ b/compiler/moco-tf/src/Dialect/TFTypeInferenceRule.cpp @@ -49,6 +49,7 @@ struct TypeForwardAlgorithm final : public moco::tf::TFNodeVisitorvalue()); } loco::DataType visit(const TFMean *node) { return dtype_get(node->input()); } loco::DataType visit(const TFMul *node) { return dtype_get(node->x()); } + loco::DataType visit(const TFPad *node) { return dtype_get(node->input()); } loco::DataType visit(const TFRealDiv *node) { return dtype_get(node->x()); } loco::DataType visit(const TFRelu *node) { return dtype_get(node->features()); } loco::DataType visit(const TFRelu6 *node) { return dtype_get(node->features()); } diff --git a/compiler/moco-tf/src/IR/TFPad.h b/compiler/moco-tf/src/IR/TFPad.h new file mode 100644 index 0000000..f8d0673 --- /dev/null +++ b/compiler/moco-tf/src/IR/TFPad.h @@ -0,0 +1,64 @@ +/* + * 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_TF_IR_TFPAD_H__ +#define __MOCO_TF_IR_TFPAD_H__ + +#include "Dialect/TFNodeDecl.h" + +namespace moco +{ +namespace tf +{ +/// @note TFPad corresponds to the following GraphDef +/* +node { + name: "Pad" + op: "Pad" + input: "Const_tensor" + input: "Const_paddings" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "Tpaddings" + value { + type: DT_INT32 + } + } +} +*/ + +class TFPad final : public FixedArityNode<2, TFNodeImpl> +{ +public: + TFPad() = default; + +public: + Node *input(void) const { return at(0)->node(); } + void input(Node *node) { at(0)->node(node); } + + Node *paddings(void) const { return at(1)->node(); } + void paddings(Node *node) { at(1)->node(node); } +}; + +} // namespace tf +} // namespace moco + +#endif // __MOCO_TF_IR_TFPAD_H__ diff --git a/compiler/moco-tf/src/IR/TFPad.test.cpp b/compiler/moco-tf/src/IR/TFPad.test.cpp new file mode 100644 index 0000000..75a9d01 --- /dev/null +++ b/compiler/moco-tf/src/IR/TFPad.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 "IR/TFPad.h" + +#include "Dialect/TFDialect.h" + +#include + +TEST(TFPadTest, constructor) +{ + moco::tf::TFPad pad; + + ASSERT_EQ(pad.dialect(), moco::tf::TFDialect::get()); + ASSERT_EQ(pad.opcode(), moco::tf::TFOpcode::Pad); + + ASSERT_EQ(pad.input(), nullptr); + ASSERT_EQ(pad.paddings(), nullptr); +} diff --git a/compiler/moco-tf/src/Transforms/FixShapeTransform.cpp b/compiler/moco-tf/src/Transforms/FixShapeTransform.cpp index 9561f83..f65bccf 100644 --- a/compiler/moco-tf/src/Transforms/FixShapeTransform.cpp +++ b/compiler/moco-tf/src/Transforms/FixShapeTransform.cpp @@ -763,6 +763,8 @@ bool fix_shape(moco::tf::TFMean *node) return true; } +bool fix_shape(moco::tf::TFPad *node) { return false; } + bool fix_shape(moco::tf::TFRealDiv *node) { auto x = node->x(); -- 2.7.4