[moco] Convert and TestHelper (#8330)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 21 Oct 2019 03:09:08 +0000 (12:09 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 21 Oct 2019 03:09:08 +0000 (12:09 +0900)
This will introduce Convert for Import and TestHelper for Import testing

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
compiler/moco/import/CMakeLists.txt
compiler/moco/import/src/Convert.cpp [new file with mode: 0644]
compiler/moco/import/src/Convert.h [new file with mode: 0644]
compiler/moco/import/src/TestHelper.h [new file with mode: 0644]
compiler/moco/import/src/TestHelper.test.cpp [new file with mode: 0644]

index 4aee2ef..eb13c46 100644 (file)
@@ -9,6 +9,7 @@ target_link_libraries(moco_import PUBLIC moco_lang)
 target_link_libraries(moco_import PUBLIC mio_tf)
 target_link_libraries(moco_import PUBLIC stdex)
 target_link_libraries(moco_import PRIVATE nncc_common)
+target_link_libraries(moco_import PRIVATE plier_tf)
 
 if(NOT ENABLE_TEST)
   return()
@@ -19,3 +20,4 @@ nnas_find_package(GTest REQUIRED)
 GTest_AddTest(moco_import_test ${TESTS})
 target_include_directories(moco_import_test PRIVATE src)
 target_link_libraries(moco_import_test moco_import)
+target_link_libraries(moco_import_test plier_tf)
diff --git a/compiler/moco/import/src/Convert.cpp b/compiler/moco/import/src/Convert.cpp
new file mode 100644 (file)
index 0000000..6285f5e
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright 2017 The TensorFlow Authors. 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 "Convert.h"
+
+#include <algorithm>
+#include <cctype>
+
+// TODO move to some common file
+namespace moco
+{
+
+std::string str_toupper(std::string s)
+{
+  // from https://en.cppreference.com/w/cpp/string/byte/toupper
+  std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
+  return s;
+}
+
+} // namespace moco
diff --git a/compiler/moco/import/src/Convert.h b/compiler/moco/import/src/Convert.h
new file mode 100644 (file)
index 0000000..77dab37
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright 2017 The TensorFlow Authors. 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 __CONVERT_H__
+#define __CONVERT_H__
+
+#include <string>
+
+// TODO move to some common file
+namespace moco
+{
+
+std::string str_toupper(std::string s);
+
+} // namespace moco
+
+#endif // __CONVERT_H__
diff --git a/compiler/moco/import/src/TestHelper.h b/compiler/moco/import/src/TestHelper.h
new file mode 100644 (file)
index 0000000..386b9f2
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * 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 __TEST_HELPER_H__
+#define __TEST_HELPER_H__
+
+#include "moco/Import/GraphBuilder.h"
+
+#include <moco/IR/TFNode.h>
+#include <loco.h>
+#include <plier/tf/TestHelper.h>
+
+#include <tensorflow/core/framework/graph.pb.h>
+
+#define STRING_CONTENT(content) #content
+
+namespace moco
+{
+namespace test
+{
+
+template <typename T> T *find_first_node_bytype(loco::Graph *g)
+{
+  T *first_node = nullptr;
+  loco::Graph::NodeContext *nodes = g->nodes();
+  uint32_t count = nodes->size();
+
+  for (uint32_t i = 0; i < count; ++i)
+  {
+    first_node = dynamic_cast<T *>(nodes->at(i));
+    if (first_node != nullptr)
+      break;
+  }
+
+  return first_node;
+}
+
+} // namespace test
+} // namespace moco
+
+namespace moco
+{
+namespace test
+{
+
+class TFNodeBuildTester
+{
+public:
+  TFNodeBuildTester();
+
+public:
+  void inputs(const std::vector<std::string> &names);
+  void output(const char *name);
+  moco::TFNode *output(void);
+
+  void run(tensorflow::NodeDef &node_def, moco::GraphBuilder &graph_builder);
+
+private:
+  std::unique_ptr<moco::SymbolTable> _tensor_names;
+  std::unique_ptr<loco::Graph> _graph;
+
+  std::vector<moco::TFNode *> _inputs;
+  const char *_output{nullptr};
+};
+
+} // namespace test
+} // namespace moco
+
+#endif // __TEST_HELPER_H__
diff --git a/compiler/moco/import/src/TestHelper.test.cpp b/compiler/moco/import/src/TestHelper.test.cpp
new file mode 100644 (file)
index 0000000..6d60cd5
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * 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 <moco/IR/Nodes/TFConst.h>
+#include <stdex/Memory.h>
+
+#include <gtest/gtest.h>
+
+namespace moco
+{
+namespace test
+{
+
+TFNodeBuildTester::TFNodeBuildTester()
+{
+  _graph = loco::make_graph();
+  _tensor_names = stdex::make_unique<moco::SymbolTable>();
+}
+
+void TFNodeBuildTester::inputs(const std::vector<std::string> &names)
+{
+  for (auto name : names)
+  {
+    auto input = _graph->nodes()->create<moco::TFConst>();
+    moco::TensorName name_01(name, 0);
+    _tensor_names->enroll(name_01, input);
+
+    _inputs.push_back(input);
+  }
+}
+
+void TFNodeBuildTester::output(const char *name) { _output = name; }
+
+moco::TFNode *TFNodeBuildTester::output(void)
+{
+  assert(_output != nullptr);
+
+  moco::TensorName tname(_output, 0);
+  return static_cast<moco::TFNode *>(_tensor_names->node(tname));
+}
+
+void TFNodeBuildTester::run(tensorflow::NodeDef &nodedef, moco::GraphBuilder &graphbuilder)
+{
+  assert(_output != nullptr);
+
+  auto node_defs = stdex::make_unique<moco::NodeDefTable>();
+  auto updates = stdex::make_unique<moco::UpdateQueue>();
+
+  moco::GraphBuilderContext gb_context(_graph.get(), node_defs.get(), _tensor_names.get(),
+                                       updates.get());
+
+  EXPECT_TRUE(graphbuilder.validate(nodedef));
+  graphbuilder.build(nodedef, &gb_context);
+
+  for (auto &update : updates->queue())
+  {
+    update->input(_tensor_names.get());
+  }
+
+  auto tfnode = output();
+  ASSERT_NE(tfnode, nullptr);
+
+  int idx = 0;
+  ASSERT_EQ(tfnode->arity(), _inputs.size());
+  for (auto input : _inputs)
+  {
+    ASSERT_EQ(tfnode->arg(idx++), input);
+  }
+}
+
+} // namespace test
+} // namespace moco