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)
{
--- /dev/null
+#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);
+}