From e3e31392e6b19b1fca9ac292473db9c33d71640e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=B2=9C=EA=B5=90/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 5 Aug 2019 17:13:18 +0900 Subject: [PATCH] [moco-tf] Import Squeeze (#6205) * [moco-tf] Import Squeeze This commit adds import stage for tensorflow Squeeze operation. Signed-off-by: Cheongyo Bahk * Comments on what to test --- compiler/moco-tf/src/Op/Squeeze.cpp | 121 +++++++++++++++++++++++ compiler/moco-tf/src/Op/Squeeze.test.cpp | 162 +++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 compiler/moco-tf/src/Op/Squeeze.cpp create mode 100644 compiler/moco-tf/src/Op/Squeeze.test.cpp diff --git a/compiler/moco-tf/src/Op/Squeeze.cpp b/compiler/moco-tf/src/Op/Squeeze.cpp new file mode 100644 index 0000000..a7aca37 --- /dev/null +++ b/compiler/moco-tf/src/Op/Squeeze.cpp @@ -0,0 +1,121 @@ +/* + * 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/TFSqueeze.h" + +#include "GraphBuilder.h" +#include "GraphBuilderContext.h" + +#include +#include +#include + +#include + +namespace +{ +using namespace moco::tf; + +/** + * @brief GraphUpdate for Squeeze node + */ +class SqueezeGraphUpdate final : public GraphUpdate +{ +public: + SqueezeGraphUpdate(TFSqueeze *node, const TensorName &&input_name) + : _node(node), _input_name(input_name) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFSqueeze *_node; + const TensorName _input_name; +}; + +void SqueezeGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *input_node = table->node(_input_name); + _node->input(input_node); +} + +} // namespace + +namespace moco +{ +namespace tf +{ + +/** + * @brief GraphBuilder for Squeeze node + */ +class SqueezeGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +bool SqueezeGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return plier::tf::has_attrs(node, {"T"}); +} + +void SqueezeGraphBuilder::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(); + + if (plier::tf::has_attrs(node, {"axis"})) + { + // TODO support 'axis' attribute + std::runtime_error("Import Squeeze: 'axis' attribute is not supported yet"); + } + + std::vector squeeze_dims; + if (plier::tf::has_attrs(node, {"squeeze_dims"})) + { + auto squeeze_dim_list = plier::tf::get_list_attr(node, {"squeeze_dims"}); + // TODO assert squeeze_dims are mutually different? + squeeze_dims = plier::tf::as_int64_list(squeeze_dim_list); + } + // Note that it is possible that NodeDef does not have squeeze_dims attribute. + // In that case, TFSqueeze also has empty squeeze_dims, + + // creating TF dialect Squeeze node + auto tf_squeeze = graph->nodes()->create(); + tf_squeeze->squeeze_dims(squeeze_dims); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_squeeze); + + auto update = stdex::make_unique(tf_squeeze, TensorName(node.input(0))); + updates->enroll(std::move(update)); +} + +} // namespace tf +} // namespace moco + +#include "GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Squeeze, SqueezeGraphBuilder) diff --git a/compiler/moco-tf/src/Op/Squeeze.test.cpp b/compiler/moco-tf/src/Op/Squeeze.test.cpp new file mode 100644 index 0000000..179183b --- /dev/null +++ b/compiler/moco-tf/src/Op/Squeeze.test.cpp @@ -0,0 +1,162 @@ +/* + * 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/TFSqueeze.h" + +#include +#include + +#include + +#include + +namespace +{ + +// clang-format off +const char *squeeze_all_pbtxtdata = STRING_CONTENT( +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { type: DT_FLOAT } + } + attr { + key: "shape" + value { + shape { + dim { size: 2 } + dim { size: 1 } + dim { size: 3 } + dim { size: 1 } + } + } + } +} +node { + name: "Squeeze" + op: "Squeeze" + input: "Placeholder" + attr { + key: "T" + value { type: DT_FLOAT } + } +} +); +// clang-format on + +} // namespace + +TEST(TensorFlowImport, squeeze_all) +{ + moco::tf::Importer importer; + moco::tf::ModelSignature signature; + + signature.add_input(moco::tf::TensorName("Placeholder", 0)); + signature.add_output(moco::tf::TensorName("Squeeze", 0)); + + tensorflow::GraphDef graph_def; + EXPECT_TRUE(plier::tf::parse_graphdef(squeeze_all_pbtxtdata, graph_def)); + std::unique_ptr graph = importer.import(signature, graph_def); + + // what to test: + // - there should exist TFSqueeze + // - input node should not be null + // - squeeze_dims attribute is set same as pbtxt + + moco::tf::TFSqueeze *squeeze_node = + moco::tf::test::find_first_node_bytype(graph.get()); + + ASSERT_NE(squeeze_node, nullptr); + ASSERT_NE(squeeze_node->input(), nullptr); + ASSERT_EQ(squeeze_node->squeeze_dims().size(), 0); +} + +namespace +{ + +// clang-format off +const char *squeeze_some_pbtxtdata = STRING_CONTENT( +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { type: DT_FLOAT } + } + attr { + key: "shape" + value { + shape { + dim { size: 2 } + dim { size: 1 } + dim { size: 3 } + dim { size: 1 } + } + } + } +} +node { + name: "Squeeze" + op: "Squeeze" + input: "Placeholder" + attr { + key: "T" + value { type: DT_FLOAT } + } + attr { + key: "squeeze_dims" + value { + list { i: 1 } + } + } +} +); +// clang-format on + +} // namespace + +TEST(TensorFlowImport, squeeze_some) +{ + moco::tf::Importer importer; + moco::tf::ModelSignature signature; + + signature.add_input(moco::tf::TensorName("Placeholder", 0)); + signature.add_output(moco::tf::TensorName("Squeeze", 0)); + + tensorflow::GraphDef graph_def; + EXPECT_TRUE(plier::tf::parse_graphdef(squeeze_some_pbtxtdata, graph_def)); + std::unique_ptr graph = importer.import(signature, graph_def); + + // what to test: + // - there should exist TFSqueeze + // - input node should not be null + // - squeeze_dims attribute is set same as pbtxt + + moco::tf::TFSqueeze *squeeze_node = + moco::tf::test::find_first_node_bytype(graph.get()); + + ASSERT_NE(squeeze_node, nullptr); + ASSERT_NE(squeeze_node->input(), nullptr); + ASSERT_EQ(squeeze_node->squeeze_dims().size(), 1); + ASSERT_EQ(squeeze_node->squeeze_dims().at(0), 1); +} + +// TODO Add test case for negative squeeze dim -- 2.7.4