[neurun] Add insertOperation in graph (#2424)
author김수진/동작제어Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Wed, 29 Aug 2018 00:49:48 +0000 (09:49 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 29 Aug 2018 00:49:48 +0000 (09:49 +0900)
* [neurun] Add insertOperation in graph

This commit adds insertOperation which inserts between 2 operations in graph.

Signed-off-by: sjsujinkim <sjsujin.kim@samsung.com>
* some change test operand

* remove unused declaration

* get a pair of operand and operation index as parameter

* Add todo comment into insertOperation

* Change pair to seperate params

* Revise insertion test

* fix format

* Remove unused utility include

* rebase

* Change test comment

* Remove test header

runtimes/neurun/src/graph/Graph.cc
runtimes/neurun/src/graph/Graph.h
runtimes/neurun/test/graph/operation/Insert.cc [new file with mode: 0644]

index 0012a68..f0c4084 100644 (file)
@@ -27,6 +27,50 @@ operation::Index Graph::addOperation(std::unique_ptr<operation::Node> &&node)
   return _operations.append(std::move(node));
 }
 
+// TODO : If operand's use-def information is introduced,
+//        Following API and implements would be refactored.
+/**
+ * @brief Insert operation into between an operand and next operation.
+ *
+ * @param prev_operand_index is an previous operand index of insertion.
+ * @param next_operation_index is an next operation index of insertion.
+ * @param node is an operation::Node to insert.
+ *
+ * @return operation::Index
+ */
+operation::Index Graph::insertOperation(const operand::Index &prev_operand_index,
+                                        const operation::Index &next_operation_index,
+                                        std::unique_ptr<operation::Node> &&node)
+{
+  auto &next_operation = _operations.at(next_operation_index);
+  auto next_input_indexes = next_operation.getInputs();
+
+  assert(next_input_indexes.contains(prev_operand_index));
+
+  node->setInputs({prev_operand_index});
+
+  // For multi input operation (ex. concat)
+  operand::IndexSet index_set;
+  auto cur_output_indexes = node->getOutputs();
+  assert(cur_output_indexes.size() == 1); // Assume output of inserted node size always 1
+  // TODO : If the API for setting input one by one is introduced, it would be changed to simple.
+  for (auto next_input_index : next_input_indexes.list())
+  {
+    if (prev_operand_index == next_input_index)
+    {
+      index_set.append(cur_output_indexes.at(operand::IO::Index{0}));
+    }
+    else
+    {
+      index_set.append(next_input_index);
+    }
+  }
+
+  next_operation.setInputs(index_set);
+
+  return _operations.append(std::move(node));
+}
+
 void Graph::setOperandValue(const operand::Index &ind,
                             std::unique_ptr<::neurun::internal::operand::Data> &&data)
 {
index c8fd80b..cbf4e2b 100644 (file)
@@ -66,6 +66,9 @@ public:
   operand::Index addOperand(const ::neurun::internal::operand::Shape &shape,
                             const ::neurun::internal::operand::TypeInfo &type);
   operation::Index addOperation(std::unique_ptr<operation::Node> &&node);
+  operation::Index insertOperation(const operand::Index &prev_operand_index,
+                                   const operation::Index &next_operation_index,
+                                   std::unique_ptr<operation::Node> &&node);
   void setOperandValue(const operand::Index &ind,
                        std::unique_ptr<::neurun::internal::operand::Data> &&data);
   void addInput(const operand::Index &ind);
diff --git a/runtimes/neurun/test/graph/operation/Insert.cc b/runtimes/neurun/test/graph/operation/Insert.cc
new file mode 100644 (file)
index 0000000..1ef4bf1
--- /dev/null
@@ -0,0 +1,194 @@
+#include <gtest/gtest.h>
+
+#include "graph/Graph.h"
+#include "graph/verifier/IVerifier.h"
+#include "nnfw/std/memory.h"
+#include "graph/operand/Index.h"
+
+#include <typeindex>
+
+using IOIndex = neurun::graph::operand::IO::Index;
+using Index = neurun::graph::operand::Index;
+using IndexSet = neurun::graph::operand::IndexSet;
+
+namespace
+{
+
+class MockNode : public neurun::graph::operation::Node
+{
+public:
+  MockNode(Index input, Index output) : _input{input}, _output{output}
+  {
+    // DO NOTHING
+  }
+
+public:
+  virtual void accept(neurun::graph::operation::NodeVisitor &&) const override {}
+
+public:
+  virtual IndexSet getInputs() const override { return {_input}; }
+  virtual IndexSet getOutputs() const override { return {_output}; }
+  virtual void setInputs(const IndexSet &indexes) override { _input = indexes.at(IOIndex{0}); }
+  virtual void setOutputs(const IndexSet &indexes) override { _output = indexes.at(IOIndex{0}); }
+  virtual const ::internal::tflite::op::Node *op() const override { return nullptr; }
+
+private:
+  Index _input;
+  Index _output;
+};
+
+class MultiInputMockNode : public neurun::graph::operation::Node
+{
+public:
+  MultiInputMockNode(IndexSet inputs, Index output) : _output{output}
+  {
+    for (auto index : inputs.list())
+    {
+      _inputs.emplace_back(index);
+    }
+  }
+
+public:
+  virtual void accept(neurun::graph::operation::NodeVisitor &&) const override {}
+
+public:
+  virtual IndexSet getInputs() const override
+  {
+    IndexSet set;
+    for (auto index : _inputs)
+    {
+      set.append({index});
+    }
+    return set;
+  }
+
+  virtual IndexSet getOutputs() const override { return {_output}; }
+  virtual void setInputs(const IndexSet &indexes) override
+  {
+    std::vector<Index> inputs;
+    for (auto index : indexes.list())
+    {
+      inputs.emplace_back(index);
+    }
+    _inputs = inputs;
+  }
+  virtual void setOutputs(const IndexSet &indexes) override { _output = indexes.at(IOIndex{0}); }
+  virtual const ::internal::tflite::op::Node *op() const override { return nullptr; }
+
+private:
+  std::vector<Index> _inputs;
+  Index _output;
+};
+
+} // namespace anonymous
+
+TEST(graph_operation_manipulation, operation_insertion)
+{
+  neurun::graph::Graph graph;
+  neurun::graph::verifier::DAGChecker verifier;
+
+  neurun::internal::operand::Shape shape{1u};
+  neurun::internal::operand::TypeInfo type{ANEURALNETWORKS_TENSOR_INT32, 0, 0};
+  shape.dim(0) = 3;
+
+  // Model Input/Output
+  auto input_operand = graph.addOperand(shape, type);
+  auto output_operand = graph.addOperand(shape, type);
+
+  graph.addInput(input_operand);
+  graph.addOutput(output_operand);
+
+  // MockNode1
+  auto operand1 = graph.addOperand(shape, type);
+  auto mocknode_index1 = graph.addOperation(nnfw::make_unique<MockNode>(input_operand, operand1));
+  // MockNode2
+  auto operand2 = graph.addOperand(shape, type);
+  auto mocknode_index2 = graph.addOperation(nnfw::make_unique<MockNode>(operand1, operand2));
+  // MockNode3
+  auto mocknode_index3 = graph.addOperation(nnfw::make_unique<MockNode>(operand2, output_operand));
+
+  ASSERT_EQ(verifier.verify(graph), true);
+
+  // Insert node1 (between 1 and 2)
+  auto inserted_operand1 = graph.addOperand(shape, type);
+  auto inserted_index1 = graph.insertOperation(
+      operand1, mocknode_index2, nnfw::make_unique<MockNode>(operand1, inserted_operand1));
+
+  ASSERT_EQ(inserted_index1.asInt(), 3);
+
+  // Insert node2 (between 2 and 3)
+  auto inserted_operand2 = graph.addOperand(shape, type);
+  auto inserted_index2 = graph.insertOperation(
+      operand2, mocknode_index3, nnfw::make_unique<MockNode>(operand2, inserted_operand2));
+
+  ASSERT_EQ(inserted_index2.asInt(), 4);
+
+  // Check tensor indexes
+  const auto &operations = graph.operations();
+  ASSERT_EQ(operations.at(mocknode_index1).getOutputs().at(Index{0}),
+            operations.at(inserted_index1).getInputs().at(Index{0}));
+  ASSERT_EQ(operations.at(inserted_index1).getOutputs().at(Index{0}),
+            operations.at(mocknode_index2).getInputs().at(Index{0}));
+  ASSERT_EQ(operations.at(mocknode_index2).getOutputs().at(Index{0}),
+            operations.at(inserted_index2).getInputs().at(Index{0}));
+  ASSERT_EQ(operations.at(inserted_index2).getOutputs().at(Index{0}),
+            operations.at(mocknode_index3).getInputs().at(Index{0}));
+
+  ASSERT_EQ(verifier.verify(graph), true);
+}
+
+TEST(graph_operation_manipulation, operation_insertion_multi_input)
+{
+  neurun::graph::Graph graph;
+  neurun::graph::verifier::DAGChecker verifier;
+
+  neurun::internal::operand::Shape shape{1u};
+  neurun::internal::operand::TypeInfo type{ANEURALNETWORKS_TENSOR_INT32, 0, 0};
+  shape.dim(0) = 3;
+
+  // Model Input/Output
+  auto input_operand = graph.addOperand(shape, type);
+  auto output_operand = graph.addOperand(shape, type);
+
+  graph.addInput(input_operand);
+  graph.addOutput(output_operand);
+
+  // MockNode1
+  auto operand1 = graph.addOperand(shape, type);
+  auto mocknode_index1 = graph.addOperation(nnfw::make_unique<MockNode>(input_operand, operand1));
+  // MockNode2
+  auto operand2 = graph.addOperand(shape, type);
+  auto mocknode_index2 = graph.addOperation(nnfw::make_unique<MockNode>(input_operand, operand2));
+  // MultiInputMockNode
+  auto multiinput_index = graph.addOperation(
+      nnfw::make_unique<MultiInputMockNode>(IndexSet{operand1, operand2}, output_operand));
+
+  ASSERT_EQ(verifier.verify(graph), true);
+
+  // Insert node1 (between 1 and multi)
+  auto inserted_operand1 = graph.addOperand(shape, type);
+  auto inserted_index1 = graph.insertOperation(
+      operand1, multiinput_index, nnfw::make_unique<MockNode>(operand1, inserted_operand1));
+
+  ASSERT_EQ(inserted_index1.asInt(), 3);
+
+  // Insert node2 (between 2 and multi)
+  auto inserted_operand2 = graph.addOperand(shape, type);
+  auto inserted_index2 = graph.insertOperation(
+      operand2, multiinput_index, nnfw::make_unique<MockNode>(operand2, inserted_operand2));
+
+  ASSERT_EQ(inserted_index2.asInt(), 4);
+
+  // Check tensor indexes
+  const auto &operations = graph.operations();
+  ASSERT_EQ(operations.at(mocknode_index1).getOutputs().at(Index{0}),
+            operations.at(inserted_index1).getInputs().at(Index{0}));
+  ASSERT_EQ(operations.at(inserted_index1).getOutputs().at(Index{0}),
+            operations.at(multiinput_index).getInputs().at(Index{0}));
+  ASSERT_EQ(operations.at(mocknode_index2).getOutputs().at(Index{0}),
+            operations.at(inserted_index2).getInputs().at(Index{0}));
+  ASSERT_EQ(operations.at(inserted_index2).getOutputs().at(Index{0}),
+            operations.at(multiinput_index).getInputs().at(Index{1}));
+
+  ASSERT_EQ(verifier.verify(graph), true);
+}