From c5d86214dded5e960135d2821864dfccf52a7dc8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=82=A8=EA=B6=81=EC=84=9D/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 23 Aug 2019 14:34:50 +0900 Subject: [PATCH] [moco-tf] Enable importing Tanh (#6849) This commit will enable importing `Tanh` as `TFTanh` Signed-off-by: Seok NamKoong --- compiler/moco-tf/src/Op/Tanh.cpp | 102 +++++++++++++++++++++++++++++++++ compiler/moco-tf/src/Op/Tanh.test.cpp | 103 ++++++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 compiler/moco-tf/src/Op/Tanh.cpp create mode 100644 compiler/moco-tf/src/Op/Tanh.test.cpp diff --git a/compiler/moco-tf/src/Op/Tanh.cpp b/compiler/moco-tf/src/Op/Tanh.cpp new file mode 100644 index 0000000..b465401 --- /dev/null +++ b/compiler/moco-tf/src/Op/Tanh.cpp @@ -0,0 +1,102 @@ +/* + * 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/TFTanh.h" + +#include "GraphBuilder.h" +#include "GraphBuilderContext.h" + +#include +#include + +#include + +namespace +{ + +using namespace moco::tf; + +/** + * @brief GraphUpdate for TF Tanh node + */ +class TFTanhGraphUpdate final : public GraphUpdate +{ +public: + TFTanhGraphUpdate(TFTanh *node, TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFTanh *_node; + TensorName _name; +}; + +void TFTanhGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->x(target); +} + +} // namespace + +namespace moco +{ +namespace tf +{ + +/** + * @brief GraphBuilder for Tanh node + */ +class TanhGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +bool TanhGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return true; +} + +void TanhGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Tanh node + auto tf_tanh = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_tanh); + + // Queue node input update + auto tf_tanh_update = stdex::make_unique(tf_tanh, TensorName(node.input(0))); + updates->enroll(std::move(tf_tanh_update)); +} + +} // namespace tf +} // namespace moco + +#include "GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Tanh, TanhGraphBuilder) diff --git a/compiler/moco-tf/src/Op/Tanh.test.cpp b/compiler/moco-tf/src/Op/Tanh.test.cpp new file mode 100644 index 0000000..578ef22 --- /dev/null +++ b/compiler/moco-tf/src/Op/Tanh.test.cpp @@ -0,0 +1,103 @@ +/* + * 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 "TestHelper.h" + +#include "Importer.h" + +#include "IR/TFTanh.h" + +#include +#include + +#include + +#include + +#include +#include + +using namespace moco::tf::test; + +namespace +{ +// clang-format off +const char *tanh_basic_pbtxt = STRING_CONTENT( +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 2 + } + dim { + size: 1 + } + dim { + size: 2 + } + } + } + } +} +node { + name: "output/tanh" + op: "Tanh" + input: "Placeholder" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +); +// clang-format on + +} // namespace + +TEST(TensorFlowImport, tf_tanh_basic) +{ + // load graph + moco::tf::Importer importer; + moco::tf::ModelSignature signature; + signature.add_output(moco::tf::TensorName("output/tanh", 0)); + + tensorflow::GraphDef graph_def; + EXPECT_TRUE(plier::tf::parse_graphdef(tanh_basic_pbtxt, graph_def)); + std::unique_ptr graph = importer.import(signature, graph_def); + + // what to test: + // - TFTanh node should exist + // - input x() should not be null + + auto tanh_node = moco::tf::test::find_first_node_bytype(graph.get()); + + ASSERT_NE(tanh_node, nullptr); + ASSERT_NE(tanh_node->x(), nullptr); +} -- 2.7.4