From 8f0ea3d847ba29e3c24dd4e267b75d814eb251e5 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: Thu, 25 Apr 2019 07:22:23 +0900 Subject: [PATCH] [moco/ONNX] Introduce symbolTable in GraphBuilderContext (#3353) This commit will introduce symbolTable in GraphBuilderContext symbolTable is needed for linking each nodes Signed-off-by: Seok NamKoong --- .../lib/frontend/onnx/src/GraphBuilderContext.cpp | 92 ++++++++++++++++++++++ .../lib/frontend/onnx/src/GraphBuilderContext.h | 52 +++++++++++- 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp diff --git a/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp b/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp new file mode 100644 index 0000000..00d3f4b --- /dev/null +++ b/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.cpp @@ -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 diff --git a/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.h b/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.h index 36daefc..f1f394b 100644 --- a/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.h +++ b/contrib/moco/lib/frontend/onnx/src/GraphBuilderContext.h @@ -20,6 +20,8 @@ #include #include +#include +#include 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; + using MapNodeNames_t = std::map>; + + 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 -- 2.7.4