[moco-tf] Enable importing Mean (#7543)
author남궁석/On-Device Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Wed, 18 Sep 2019 04:11:34 +0000 (13:11 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 18 Sep 2019 04:11:34 +0000 (13:11 +0900)
* [moco-tf] Enable importing Mean

This commit will enable importing `Mean` as `TFMean`

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
* revise trivials

compiler/moco-tf/src/Op/Mean.cpp [new file with mode: 0644]
compiler/moco-tf/src/Op/Mean.test.cpp [new file with mode: 0644]

diff --git a/compiler/moco-tf/src/Op/Mean.cpp b/compiler/moco-tf/src/Op/Mean.cpp
new file mode 100644 (file)
index 0000000..8a7d846
--- /dev/null
@@ -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 <loco.h>
+#include <stdex/Memory.h>
+#include <plier/tf/Convert.h>
+
+#include <tensorflow/core/framework/graph.pb.h>
+
+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<TFMean>();
+  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<MeanGraphUpdate>(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 (file)
index 0000000..9a4e3c6
--- /dev/null
@@ -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 <loco.h>
+#include <plier/tf/TestHelper.h>
+
+#include <gtest/gtest.h>
+
+#include <memory>
+
+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<loco::Graph> 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<moco::tf::TFMean>(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<loco::Graph> 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<moco::tf::TFMean>(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);
+}