[moco/ONNX] Introduce symbolTable in GraphBuilderContext (#3353)
author남궁석/On-Device Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Wed, 24 Apr 2019 22:22:23 +0000 (07:22 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Wed, 24 Apr 2019 22:22:23 +0000 (07:22 +0900)
This commit will introduce symbolTable in GraphBuilderContext
symbolTable is needed for linking each nodes

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp [new file with mode: 0644]
contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.h

diff --git a/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp b/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp
new file mode 100644 (file)
index 0000000..00d3f4b
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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 "GraphBuilderContext.h"
+
+namespace moco
+{
+namespace onnx
+{
+
+void SymbolTable::enroll(const std::string &node_name, loco::Node *node)
+{
+  MapNameNode_t::iterator iter = _namenode.find(node_name);
+
+  if (iter != _namenode.end())
+  {
+    throw std::runtime_error{"Error: Duplicate node name in Graph: " + node_name};
+  }
+
+  _namenode[node_name] = node;
+}
+
+loco::Node *SymbolTable::node(const std::string &node_name)
+{
+  MapNameNode_t::iterator iter = _namenode.find(node_name);
+
+  if (iter == _namenode.end())
+  {
+    throw std::runtime_error{"Error: Cannot find node with name in Graph: " + node_name};
+  }
+
+  return iter->second;
+}
+
+void SymbolTable::list(loco::Node *node, const std::string &name)
+{
+  MapNodeNames_t::iterator iter = _nodenames.find(node);
+
+  if (iter == _nodenames.end())
+  {
+    // add a new vector for the first name
+    _nodenames[node] = {name};
+    return;
+  }
+
+  _nodenames[node].push_back(name);
+}
+
+unsigned SymbolTable::size(loco::Node *node)
+{
+  MapNodeNames_t::iterator iter = _nodenames.find(node);
+
+  if (iter == _nodenames.end())
+  {
+    return 0;
+  }
+
+  return iter->second.size();
+}
+
+const std::string &SymbolTable::name(loco::Node *node, unsigned index)
+{
+  MapNodeNames_t::iterator iter = _nodenames.find(node);
+
+  if (iter == _nodenames.end())
+  {
+    throw std::runtime_error{"Error: Cannot find names given node"};
+  }
+
+  if (index >= iter->second.size())
+  {
+    throw std::runtime_error{"Error: Invalid name index for given node"};
+  }
+
+  return iter->second.at(index);
+}
+
+} // namespace onnx
+} // namespace moco
index 36daefc..f1f394b 100644 (file)
@@ -20,6 +20,8 @@
 #include <loco.h>
 
 #include <map>
+#include <string>
+#include <vector>
 
 namespace moco
 {
@@ -27,12 +29,53 @@ namespace onnx
 {
 
 /**
+ * @brief Class to store relations of Nodes and string names
+ */
+class SymbolTable
+{
+public:
+  /**
+   * @brief  Registers one node for a name
+   */
+  void enroll(const std::string &node_name, loco::Node *node);
+  /**
+   * @brief  Queries enrolled(registered) with name and return node if found
+   *         Will throw runtime_error if not found
+   *         Table is independent with registering with list()
+   */
+  loco::Node *node(const std::string &node_name);
+
+  /**
+   * @brief  Registers multiple (appends) names for a node
+   *         Table is independent with registering with enroll()
+   */
+  void list(loco::Node *node, const std::string &name);
+  /**
+   * @brief  Returns number of listed(registered) names for a node
+   */
+  unsigned size(loco::Node *node);
+  /**
+   * @brief  Queries listed(registered) with node and index(from 0 to size-1)
+   *         Will throw runtime_error if node is not found or index is out of bounds
+   */
+  const std::string &name(loco::Node *node, unsigned index);
+
+private:
+  using MapNameNode_t = std::map<std::string, loco::Node *>;
+  using MapNodeNames_t = std::map<loco::Node *, std::vector<std::string>>;
+
+  MapNameNode_t _namenode;
+  MapNodeNames_t _nodenames;
+};
+
+/**
 * @brief Class to store context to build IR from onnx
 */
 class GraphBuilderContext
 {
 public:
-  GraphBuilderContext(loco::Graph *g) : _g(g)
+  GraphBuilderContext(loco::Graph *g, SymbolTable *nodes, SymbolTable *input_names)
+      : _g(g), _nodes(nodes), _input_names(input_names)
   {
     // DO NOTHING
   }
@@ -40,8 +83,15 @@ public:
   GraphBuilderContext(const GraphBuilderContext &) = delete;
   GraphBuilderContext(GraphBuilderContext &&) = delete;
 
+public:
+  loco::Graph *graph() { return _g; }
+  SymbolTable *nodes() { return _nodes; }
+  SymbolTable *input_names() { return _input_names; }
+
 private:
   loco::Graph *_g;
+  SymbolTable *_nodes;
+  SymbolTable *_input_names;
 };
 
 } // namespace onnx