summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author김용섭/On-Device Lab(SR)/Engineer/삼성전자 <yons.kim@samsung.com>2019-04-16 08:28:08 (GMT)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>2019-04-16 08:28:08 (GMT)
commit98728aa36574fa5ec657159420eaed3a3498b7cd (patch)
tree957e874ac8d76f5e8c0cb42ce93bd52aaeb7f53e
parent71c34c60d8ef732f11896fd150deda8650aec159 (diff)
downloadnnfw-98728aa36574fa5ec657159420eaed3a3498b7cd.zip
nnfw-98728aa36574fa5ec657159420eaed3a3498b7cd.tar.gz
nnfw-98728aa36574fa5ec657159420eaed3a3498b7cd.tar.bz2
[neurun] Introduce SubgraphContext class (#4995)
This class would be used as a context class which has Subgraphs instead of SubgraphSet in Graph Signed-off-by: Yongseop Kim <yons.kim@samsung.com>
-rw-r--r--runtimes/neurun/core/include/model/operation/SubgraphContext.h75
-rw-r--r--runtimes/neurun/core/src/model/operation/SubgraphContext.cc120
2 files changed, 195 insertions, 0 deletions
diff --git a/runtimes/neurun/core/include/model/operation/SubgraphContext.h b/runtimes/neurun/core/include/model/operation/SubgraphContext.h
new file mode 100644
index 0000000..100f7b2
--- /dev/null
+++ b/runtimes/neurun/core/include/model/operation/SubgraphContext.h
@@ -0,0 +1,75 @@
+/*
+ * 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 __NEURUN_MODEL_OPERATION_SUBGRAPH_CONTEXT_H__
+#define __NEURUN_MODEL_OPERATION_SUBGRAPH_CONTEXT_H__
+
+#include <memory>
+
+#include "Subgraph.h"
+
+#include <unordered_map>
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+class SubgraphContext
+{
+public:
+ SubgraphContext() : _index_count(0) {}
+
+public:
+ Index append(std::unique_ptr<Subgraph> &&subg);
+ Index append(const Index &index, const Node &node);
+ void remove(const Index &index) { _subgs.erase(index); };
+
+public:
+ const Subgraph &at(const Index &) const;
+ Subgraph &at(const Index &);
+ std::unique_ptr<Subgraph> releaseAt(const Index &index) { return std::move(_subgs.at(index)); }
+ Subgraph *getAt(const Index &index) { return _subgs.at(index).get(); }
+ bool exist(const Index &) const;
+
+ bool hasNode(const Index &node_index) const;
+ Index findNode(const Index &node_index) const;
+
+ uint32_t size() const { return _subgs.size(); }
+
+ void iterate(const std::function<void(const Index &, const Subgraph &)> &fn) const;
+ void iterate(const std::function<void(const Index &, Subgraph &)> &fn);
+
+ // TODO: Extract this into external helper function
+ void dump(const std::string &msg) const;
+
+ void clear() { _subgs.clear(); }
+
+private:
+ Index generateIndex();
+
+private:
+ std::unordered_map<Index, std::unique_ptr<Subgraph>> _subgs;
+ uint32_t _index_count;
+};
+
+} // namespace operation
+} // namespace model
+} // namespace neurun
+
+#endif // __NEURUN_MODEL_OPERATION_SUBGRAPH_CONTEXT_H__
diff --git a/runtimes/neurun/core/src/model/operation/SubgraphContext.cc b/runtimes/neurun/core/src/model/operation/SubgraphContext.cc
new file mode 100644
index 0000000..367fbdf
--- /dev/null
+++ b/runtimes/neurun/core/src/model/operation/SubgraphContext.cc
@@ -0,0 +1,120 @@
+/*
+ * 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 "model/operation/SubgraphContext.h"
+#include "util/logging.h"
+#include "cpp14/memory.h"
+
+#include <cassert>
+#include <string>
+#include <stdint.h>
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+Index SubgraphContext::generateIndex()
+{
+ assert((_index_count) <= UINT32_MAX);
+
+ return Index{_index_count++};
+}
+
+Index SubgraphContext::append(std::unique_ptr<Subgraph> &&subgraph)
+{
+ auto index = generateIndex();
+ _subgs[index] = std::move(subgraph);
+ return index;
+}
+
+Index SubgraphContext::append(const Index &index, const Node &node)
+{
+ std::unique_ptr<Subgraph> subg = nnfw::cpp14::make_unique<model::operation::Subgraph>();
+ subg->appendOperation(index, node);
+ return append(std::move(subg));
+}
+
+const Subgraph &SubgraphContext::at(const Index &index) const { return *(_subgs.at(index)); }
+
+Subgraph &SubgraphContext::at(const Index &index) { return *(_subgs.at(index)); }
+
+bool SubgraphContext::exist(const Index &index) const { return _subgs.find(index) != _subgs.end(); }
+
+bool SubgraphContext::hasNode(const Index &node_index) const
+{
+ for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
+ {
+ const auto &subg = *it->second;
+ for (const auto &elem : subg.operations())
+ {
+ if (elem.index == node_index)
+ return true;
+ }
+ }
+
+ return false;
+}
+
+Index SubgraphContext::findNode(const Index &node_index) const
+{
+ for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
+ {
+ auto &subg_index = it->first;
+ const auto &subg = *it->second;
+ for (const auto &elem : subg.operations())
+ {
+ if (elem.index == node_index)
+ {
+ return subg_index;
+ }
+ }
+ }
+
+ assert(true && "DO NOT ENTER HERE");
+ return Index(UINT32_MAX); // CAN'T ENTER HERE
+}
+
+void SubgraphContext::iterate(const std::function<void(const Index &, const Subgraph &)> &fn) const
+{
+ for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
+ {
+ fn(it->first, *it->second);
+ }
+}
+
+void SubgraphContext::iterate(const std::function<void(const Index &, Subgraph &)> &fn)
+{
+ for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
+ {
+ fn(it->first, *it->second);
+ }
+}
+
+// TODO: Extract this into external helper function
+void SubgraphContext::dump(const std::string &msg) const
+{
+ VERBOSE(SubgraphContext) << "SubgraphContext(" << msg << ")" << std::endl;
+ iterate([&](const model::operation::Index &idx, const model::operation::Subgraph &subg) {
+ VERBOSE(SubgraphContext) << idx.value() << "] " << subg.getStr() << std::endl;
+ });
+}
+
+} // namespace operation
+} // namespace model
+} // namespace neurun