From c0614ab928198eb6312b74093e016b72bd0e4e98 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=ED=95=9C=EC=A2=85/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Wed, 8 Aug 2018 20:07:23 +0900 Subject: [PATCH] [neurun] Graph : Introduce operation `Set` (#2215) `neurun::graph::operation::Set` holds operations and also has ownership of them. Signed-off-by: Hanjoung Lee --- runtimes/neurun/src/graph/operation/Set.cc | 35 ++++++++++++++++++++++++++ runtimes/neurun/src/graph/operation/Set.h | 40 ++++++++++++++++++++++++++++++ runtimes/neurun/test/operation/Set.cc | 27 ++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 runtimes/neurun/src/graph/operation/Set.cc create mode 100644 runtimes/neurun/src/graph/operation/Set.h create mode 100644 runtimes/neurun/test/operation/Set.cc diff --git a/runtimes/neurun/src/graph/operation/Set.cc b/runtimes/neurun/src/graph/operation/Set.cc new file mode 100644 index 0000000..5e1a2b0 --- /dev/null +++ b/runtimes/neurun/src/graph/operation/Set.cc @@ -0,0 +1,35 @@ +#include "Set.h" + +namespace neurun +{ +namespace graph +{ +namespace operation +{ + +Index Set::append(std::unique_ptr &&node) +{ + uint32_t index = _nodes.size(); + + _nodes.emplace_back(std::move(node)); + + return Index{index}; +} + +const Node &Set::at(const Index &index) const { return *(_nodes.at(index.asInt())); } + +Node &Set::at(const Index &index) { return *(_nodes.at(index.asInt())); } + +bool Set::exist(const Index &index) const { return index.asInt() < _nodes.size(); } + +void Set::iterate(const std::function &fn) const +{ + for (uint32_t index = 0; index < _nodes.size(); index++) + { + fn(Index{index}, *_nodes[index]); + } +} + +} // namespace operation +} // namespace graph +} // namespace neurun diff --git a/runtimes/neurun/src/graph/operation/Set.h b/runtimes/neurun/src/graph/operation/Set.h new file mode 100644 index 0000000..d0bc536 --- /dev/null +++ b/runtimes/neurun/src/graph/operation/Set.h @@ -0,0 +1,40 @@ +#ifndef __NEURUN_GRAPH_OPERATION_SET_H__ +#define __NEURUN_GRAPH_OPERATION_SET_H__ + +#include + +#include "graph/operation/Index.h" +#include "Node.h" +#include "internal/Model.h" + +namespace neurun +{ +namespace graph +{ +namespace operation +{ + +class Set +{ +public: + Set() = default; + +public: + Index append(std::unique_ptr &&node); + +public: + const Node &at(const Index &) const; + Node &at(const Index &); + bool exist(const Index &) const; + uint32_t size() const { return _nodes.size(); } + void iterate(const std::function &fn) const; + +private: + std::vector> _nodes; +}; + +} // namespace operation +} // namespace graph +} // namespace neurun + +#endif // __NEURUN_GRAPH_OPERATION_SET_H__ diff --git a/runtimes/neurun/test/operation/Set.cc b/runtimes/neurun/test/operation/Set.cc new file mode 100644 index 0000000..759618e --- /dev/null +++ b/runtimes/neurun/test/operation/Set.cc @@ -0,0 +1,27 @@ +#include + +#include "graph/operation/Set.h" + +using neurun::graph::operation::Set; +using neurun::graph::operation::Node; +using neurun::graph::operation::Index; + +class TestNode : public Node +{ +public: + TestNode() = default; + +public: + virtual neurun::graph::operand::IndexSet inputs() const { return {1, 2, 3, 4}; } + virtual neurun::graph::operand::IndexSet outputs() const { return {1, 2, 3}; } + virtual const ::internal::tflite::op::Node *op() const { return nullptr; } +}; + +TEST(graph_operation_Set, operation_test) +{ + Set set; + set.append(std::unique_ptr(new TestNode())); + Index idx{0u}; + ASSERT_EQ(set.at(idx).inputs().size(), 4); + ASSERT_EQ(set.at(idx).outputs().size(), 3); +} -- 2.7.4