From cdb0c1530d40835f8e4f20a6680d8206138dfb75 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: Wed, 18 Sep 2019 13:11:34 +0900 Subject: [PATCH] [moco-tf] Enable importing Mean (#7543) * [moco-tf] Enable importing Mean This commit will enable importing `Mean` as `TFMean` Signed-off-by: Seok NamKoong * revise trivials --- compiler/moco-tf/src/Op/Mean.cpp | 118 ++++++++++++++++++ compiler/moco-tf/src/Op/Mean.test.cpp | 217 ++++++++++++++++++++++++++++++++++ 2 files changed, 335 insertions(+) create mode 100644 compiler/moco-tf/src/Op/Mean.cpp create mode 100644 compiler/moco-tf/src/Op/Mean.test.cpp diff --git a/compiler/moco-tf/src/Op/Mean.cpp b/compiler/moco-tf/src/Op/Mean.cpp new file mode 100644 index 0000000..8a7d846 --- /dev/null +++ b/compiler/moco-tf/src/Op/Mean.cpp @@ -0,0 +1,118 @@ +/* + * 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/TFMean.h" + +#include "GraphBuilder.h" +#include "GraphBuilderContext.h" + +#include +#include +#include + +#include + +namespace +{ +using namespace moco::tf; + +/** + * @brief GraphUpdate for Mean node + */ +class MeanGraphUpdate final : public GraphUpdate +{ +public: + MeanGraphUpdate(TFMean *node, const TensorName &&input_name, + const TensorName &&reduction_indices_name) + : _node(node), _input_name(input_name), _reduction_indices_name(reduction_indices_name) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFMean *_node; + const TensorName _input_name; + const TensorName _reduction_indices_name; +}; + +void MeanGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *input_node = table->node(_input_name); + loco::Node *reduction_indices_node = table->node(_reduction_indices_name); + _node->input(input_node); + _node->reduction_indices(reduction_indices_node); +} + +} // namespace + +namespace moco +{ +namespace tf +{ + +/** + * @brief GraphBuilder for Mean node + */ +class MeanGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +bool MeanGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + if (node.input_size() != 2) + return false; + + if (plier::tf::has_attrs(node, {"T", "Tidx", "keep_dims"}) == false) + return false; + + auto dtype = plier::tf::get_datatype_attr(node, "Tidx"); + if (dtype != tensorflow::DataType::DT_INT32 && dtype != tensorflow::DataType::DT_INT64) + return false; + + return true; +} + +void MeanGraphBuilder::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 Mean node + auto tf_mean = graph->nodes()->create(); + tf_mean->keep_dims(plier::tf::get_bool_attr(node, "keep_dims")); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_mean); + + auto update = stdex::make_unique(tf_mean, TensorName(node.input(0)), + TensorName(node.input(1))); + updates->enroll(std::move(update)); +} + +} // namespace tf +} // namespace moco + +#include "GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Mean, MeanGraphBuilder) diff --git a/compiler/moco-tf/src/Op/Mean.test.cpp b/compiler/moco-tf/src/Op/Mean.test.cpp new file mode 100644 index 0000000..9a4e3c6 --- /dev/null +++ b/compiler/moco-tf/src/Op/Mean.test.cpp @@ -0,0 +1,217 @@ +/* + * 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/TFMean.h" + +#include +#include + +#include + +#include + +namespace +{ + +// clang-format off +const char *mean_true_pbtxtdata = STRING_CONTENT( +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { type: DT_FLOAT } + } + attr { + key: "shape" + value { + shape { + dim { size: 1 } + dim { size: 3 } + dim { size: 3 } + dim { size: 2 } + } + } + } +} +node { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { type: DT_INT32 } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { size: 2 } + } + int_val: 1 + int_val: 2 + } + } + } +} +node { + name: "Mean" + op: "Mean" + input: "Placeholder" + input: "Const" + attr { + key: "T" + value { type: DT_FLOAT } + } + attr { + key: "Tidx" + value { type: DT_INT32 } + } + attr { + key: "keep_dims" + value { b: true } + } +} +); +// clang-format on + +} // namespace + +TEST(TensorFlowImport, mean_true) +{ + moco::tf::Importer importer; + moco::tf::ModelSignature signature; + + signature.add_input(moco::tf::TensorName("Placeholder", 0)); + signature.add_output(moco::tf::TensorName("Mean", 0)); + + tensorflow::GraphDef graph_def; + EXPECT_TRUE(plier::tf::parse_graphdef(mean_true_pbtxtdata, graph_def)); + std::unique_ptr graph = importer.import(signature, graph_def); + + // what to test: + // - there should exist TFMean + // - input node should not be nullptr + // - reduction_indeces node should not be nullptr + // - keep_dims attribute is set same as pbtxt + + moco::tf::TFMean *mean_node = + moco::tf::test::find_first_node_bytype(graph.get()); + + ASSERT_NE(mean_node, nullptr); + ASSERT_NE(mean_node->input(), nullptr); + ASSERT_NE(mean_node->reduction_indices(), nullptr); + ASSERT_EQ(mean_node->keep_dims(), true); +} + +namespace +{ + +// clang-format off +const char *mean_false_pbtxtdata = STRING_CONTENT( +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { type: DT_FLOAT } + } + attr { + key: "shape" + value { + shape { + dim { size: 1 } + dim { size: 3 } + dim { size: 3 } + dim { size: 2 } + } + } + } +} +node { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { type: DT_INT32 } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { size: 2 } + } + int_val: 1 + int_val: 2 + } + } + } +} +node { + name: "Mean" + op: "Mean" + input: "Placeholder" + input: "Const" + attr { + key: "T" + value { type: DT_FLOAT } + } + attr { + key: "Tidx" + value { type: DT_INT32 } + } + attr { + key: "keep_dims" + value { b: false } + } +} +); +// clang-format on + +} // namespace + +TEST(TensorFlowImport, mean_false) +{ + moco::tf::Importer importer; + moco::tf::ModelSignature signature; + + signature.add_input(moco::tf::TensorName("Placeholder", 0)); + signature.add_output(moco::tf::TensorName("Mean", 0)); + + tensorflow::GraphDef graph_def; + EXPECT_TRUE(plier::tf::parse_graphdef(mean_false_pbtxtdata, graph_def)); + std::unique_ptr graph = importer.import(signature, graph_def); + + // what to test: + // - there should exist TFMean + // - input node should not be nullptr + // - reduction_indeces node should not be nullptr + // - keep_dims attribute is set same as pbtxt + + moco::tf::TFMean *mean_node = + moco::tf::test::find_first_node_bytype(graph.get()); + + ASSERT_NE(mean_node, nullptr); + ASSERT_NE(mean_node->input(), nullptr); + ASSERT_NE(mean_node->reduction_indices(), nullptr); + ASSERT_EQ(mean_node->keep_dims(), false); +} -- 2.7.4