Define Operations with ObjectManager (#5508)
author이한종/On-Device Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Tue, 2 Jul 2019 07:11:03 +0000 (16:11 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 2 Jul 2019 07:11:03 +0000 (16:11 +0900)
Just like `Operands` were defined as an ObjectManager specialization,
this applies the same for `Operations`.

See also : #5464

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/core/include/model/Operations.h
runtimes/neurun/core/include/util/ObjectManager.h
runtimes/neurun/core/src/graph/Graph.cc
runtimes/neurun/core/src/graph/pass/PermutationInsertionPass.cc
runtimes/neurun/core/src/model/Operations.cc [deleted file]
runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksModel.cc
runtimes/neurun/test/graph/operand/UseDef.cc
runtimes/neurun/test/graph/operation/Set.cc
runtimes/neurun/test/graph/verifier/Verifier.cc
runtimes/neurun/test/interp/ExecManager.cc

index f98c20f..4a1b2ca 100644 (file)
 #ifndef __NEURUN_MODEL_OPERATIONS_H__
 #define __NEURUN_MODEL_OPERATIONS_H__
 
-#include <memory>
-
 #include "model/Index.h"
 #include "model/Operation.h"
-
-#include <unordered_map>
+#include "util/ObjectManager.h"
 
 namespace neurun
 {
 namespace model
 {
 
-class Operations
+class Operations : public util::ObjectManager<OperationIndex, Operation>
 {
-public:
-  Operations() : _index_count(0) {}
-
-public:
-  OperationIndex append(std::unique_ptr<Operation> &&node);
-  void remove(const OperationIndex &index) { _nodes.erase(index); };
-
-public:
-  const Operation &at(const OperationIndex &) const;
-  Operation &at(const OperationIndex &);
-  bool exist(const OperationIndex &) const;
-  uint32_t size() const { return _nodes.size(); }
-  void iterate(const std::function<void(const OperationIndex &, const Operation &)> &fn) const;
-  void iterate(const std::function<void(const OperationIndex &, Operation &)> &fn);
-
-private:
-  const OperationIndex generateIndex();
-
-private:
-  std::unordered_map<OperationIndex, std::unique_ptr<Operation>> _nodes;
-  OperationIndex _index_count;
 };
 
 } // namespace model
index b7472b1..fd2c3f2 100644 (file)
@@ -17,6 +17,9 @@
 #ifndef __NEURUN_UTIL_OBJECT_MANAGER_H__
 #define __NEURUN_UTIL_OBJECT_MANAGER_H__
 
+#include <unordered_map>
+#include <memory>
+
 namespace neurun
 {
 namespace util
index 0bd26a0..93723e8 100644 (file)
@@ -52,7 +52,7 @@ model::OperandIndex Graph::addOperand(const model::Shape &shape, const model::Ty
 model::OperationIndex Graph::addOperation(std::unique_ptr<model::Operation> &&node)
 {
   assert(isBuildingPhase());
-  return _model->operations.append(std::move(node));
+  return _model->operations.push(std::move(node));
 }
 
 void Graph::setOperandValue(const model::OperandIndex &ind, std::unique_ptr<model::Data> &&data)
index 9b04769..9d64609 100644 (file)
@@ -169,7 +169,7 @@ PermutationInsertionPass::insertPermute(const model::OperandIndex &operand_index
   auto insert_node = nnfw::cpp14::make_unique<PermuteNode>(operand_index, out_operand_index,
                                                            input_backend, output_backend);
 
-  auto node_index = _graph.operations().append(std::move(insert_node));
+  auto node_index = _graph.operations().push(std::move(insert_node));
   const auto &node = _graph.operations().at(node_index);
 
   // Subgraph
diff --git a/runtimes/neurun/core/src/model/Operations.cc b/runtimes/neurun/core/src/model/Operations.cc
deleted file mode 100644 (file)
index 5088476..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (c) 2018 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/Operations.h"
-
-#include <cassert>
-
-namespace neurun
-{
-namespace model
-{
-
-const OperationIndex Operations::generateIndex()
-{
-  assert(_index_count.valid());
-
-  return _index_count++;
-}
-
-OperationIndex Operations::append(std::unique_ptr<Operation> &&node)
-{
-  auto index = generateIndex();
-
-  _nodes[index] = std::move(node);
-  return index;
-}
-
-const Operation &Operations::at(const OperationIndex &index) const { return *(_nodes.at(index)); }
-
-Operation &Operations::at(const OperationIndex &index) { return *(_nodes.at(index)); }
-
-bool Operations::exist(const OperationIndex &index) const
-{
-  return _nodes.find(index) != _nodes.end();
-}
-
-void Operations::iterate(
-    const std::function<void(const OperationIndex &, const Operation &)> &fn) const
-{
-  for (auto it = _nodes.begin(); it != _nodes.end(); ++it)
-  {
-    fn(it->first, *it->second);
-  }
-}
-
-void Operations::iterate(const std::function<void(const OperationIndex &, Operation &)> &fn)
-{
-  for (auto it = _nodes.begin(); it != _nodes.end(); ++it)
-  {
-    fn(it->first, *it->second);
-  }
-}
-
-} // namespace model
-} // namespace neurun
index 4739170..268eb0c 100644 (file)
@@ -105,7 +105,7 @@ bool ANeuralNetworksModel::addOperation(ANeuralNetworksOperationType type, uint3
     OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
 
     auto node = factory.create(type, param, _model->operands);
-    _model->operations.append(std::unique_ptr<neurun::model::Operation>{node});
+    _model->operations.push(std::unique_ptr<neurun::model::Operation>{node});
   }
   catch (const std::exception &e)
   {
@@ -132,7 +132,7 @@ bool ANeuralNetworksModel::addOperationEx(ANeuralNetworksOperationTypeEx type, u
     OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
 
     auto node = factory.create(type, param, _model->operands);
-    _model->operations.append(std::unique_ptr<neurun::model::Operation>{node});
+    _model->operations.push(std::unique_ptr<neurun::model::Operation>{node});
   }
   catch (const std::exception &e)
   {
index c0d14a0..3afd6da 100644 (file)
@@ -49,16 +49,16 @@ TEST(graph_operand_usedef, usedef_test)
 
   // MockNode1
   auto operand_index1 = model->operands.emplace(shape, type);
-  auto mocknode_index1 = model->operations.append(
+  auto mocknode_index1 = model->operations.push(
       nnfw::cpp14::make_unique<MockNode>(IndexSet{input_operand}, IndexSet{operand_index1}));
 
   // MockNode2
   auto operand_index2 = model->operands.emplace(shape, type);
-  auto mocknode_index2 = model->operations.append(
+  auto mocknode_index2 = model->operations.push(
       nnfw::cpp14::make_unique<MockNode>(IndexSet{input_operand}, IndexSet{operand_index2}));
 
   // MockNode3(two input)
-  auto multiinput_index = model->operations.append(nnfw::cpp14::make_unique<MockNode>(
+  auto multiinput_index = model->operations.push(nnfw::cpp14::make_unique<MockNode>(
       IndexSet{operand_index1, operand_index2}, IndexSet{output_operand}));
 
   neurun::graph::Graph graph{std::move(model)};
index b94ad2a..3c5fd83 100644 (file)
@@ -26,7 +26,7 @@ using neurun::model::OperationIndex;
 TEST(graph_operation_Set, operation_test)
 {
   Operations ops;
-  ops.append(
+  ops.push(
       std::unique_ptr<Operation>(new neurun_test::graph::SimpleMockNode({1, 2, 3, 4}, {5, 6, 7})));
   OperationIndex idx{0u};
   ASSERT_EQ(ops.at(idx).getInputs().size(), 4);
index 4a1cc79..45e4d72 100644 (file)
@@ -40,7 +40,7 @@ TEST(Verifier, dag_checker)
   model->inputs.append(operand1);
   model->outputs.append(operand2);
 
-  model->operations.append(
+  model->operations.push(
       nnfw::cpp14::make_unique<MockNode>(IndexSet{operand1}, IndexSet{operand2}));
 
   neurun::graph::Graph graph{std::move(model)};
index 2de05ea..fd970a2 100644 (file)
@@ -61,7 +61,7 @@ protected:
     param.activation = neurun::model::Activation::NONE;
     auto input_set = OperandIndexSequence{operand_lhs, operand_rhs};
     auto output_set = OperandIndexSequence{operand_result};
-    model->operations.append(
+    model->operations.push(
         nnfw::cpp14::make_unique<operation::AddNode>(input_set, output_set, param));
 
     // Identify model inputs and outputs
@@ -112,14 +112,14 @@ protected:
     param1.activation = neurun::model::Activation::NONE;
     auto input_set1 = OperandIndexSequence{operand_lhs, operand_rhs1};
     auto output_set1 = OperandIndexSequence{operand_result1};
-    model->operations.append(
+    model->operations.push(
         nnfw::cpp14::make_unique<operation::AddNode>(input_set1, output_set1, param1));
 
     operation::AddNode::Param param2;
     param2.activation = neurun::model::Activation::NONE;
     auto input_set2 = OperandIndexSequence{operand_result1, operand_rhs2};
     auto output_set2 = OperandIndexSequence{operand_result2};
-    model->operations.append(
+    model->operations.push(
         nnfw::cpp14::make_unique<operation::AddNode>(input_set2, output_set2, param2));
 
     // Identify model inputs and outputs