ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Graph.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "mir/Graph.h"
18
19#include <algorithm>
20#include <deque>
21#include <unordered_map>
22
23namespace mir
24{
25
32static void replaceUsages(Operation *op, Operation *with)
33{
34 assert(op->getNumOutputs() == with->getNumOutputs());
35 for (std::size_t i = 0; i < op->getNumOutputs(); ++i)
36 {
37 Operation::Output *output = op->getOutput(i);
38 output->replaceAllUsesWith(with->getOutput(i));
39 }
40}
41
42std::vector<Operation *> getSortedNodes(Graph *graph)
43{
44 std::deque<Operation *> ready_nodes;
45 std::unordered_map<Operation *, std::size_t> num_visited_input_edges;
46
47 // Use input vector first to maintain correct input order
48 for (Operation *op : graph->getInputs())
49 {
50 ready_nodes.push_back(op);
51 }
52
53 for (Operation *op : graph->getNodes())
54 {
55 // Skip already pushed input node
56 if ((op->getNumInputs() == 0) && (op->getType() != Operation::Type::input))
57 {
58 ready_nodes.push_back(op);
59 }
60 }
61
62 std::vector<Operation *> sorted_nodes;
63 while (!ready_nodes.empty())
64 {
65 Operation *src_node = ready_nodes.front();
66 ready_nodes.pop_front();
67 sorted_nodes.push_back(src_node);
68 for (Operation::Output &output : src_node->getOutputs())
69 {
70 for (const auto use : output.getUses())
71 {
72 Operation *dst_node = use.getNode();
73 if (++num_visited_input_edges[dst_node] == dst_node->getNumInputs())
74 {
75 ready_nodes.push_back(dst_node);
76 }
77 }
78 }
79 }
80
81 return sorted_nodes;
82}
83
84void Graph::accept(IVisitor *visitor)
85{
86 for (Operation *node : getSortedNodes(this))
87 {
88 node->accept(visitor);
89 }
90}
91
93{
94 for (auto &node : _ops)
95 {
96 delete node;
97 }
98}
99
100void Graph::registerOp(Operation *op)
101{
102 _ops.emplace(op);
103
104 if (auto *input_op = dynamic_cast<ops::InputOp *>(op))
105 _inputs.emplace_back(input_op);
106
107 if (auto *output_op = dynamic_cast<ops::OutputOp *>(op))
108 _outputs.emplace_back(output_op);
109}
110
112{
113 replaceUsages(op, with);
114 removeNode(op);
115}
116
118{
119#ifndef NDEBUG
120 for (const auto &output : op->getOutputs())
121 {
122 assert(output.getUses().empty() && "Trying to remove a node that has uses.");
123 }
124#endif
125
126 for (std::size_t i = 0; i < op->getNumInputs(); ++i)
127 {
128 op->getInput(i)->removeUse(Operation::Use(op, i));
129 }
130
131 if (op->getType() == Operation::Type::input)
132 _inputs.erase(
133 std::remove(_inputs.begin(), _inputs.end(), op)); // NOLINT(bugprone-inaccurate-erase)
134
135 if (op->getType() == Operation::Type::output)
136 _outputs.erase(
137 std::remove(_outputs.begin(), _outputs.end(), op)); // NOLINT(bugprone-inaccurate-erase)
138
139 _ops.erase(op);
140 delete op;
141}
142
143} // namespace mir
void replaceNode(Operation *op, Operation *with)
Subsitude node in graph with another keeping all edges.
Definition Graph.cpp:111
virtual ~Graph()
Definition Graph.cpp:92
void removeNode(Operation *op)
remove node from graph, along with its links in other nodes
Definition Graph.cpp:117
void accept(IVisitor *visitor)
Definition Graph.cpp:84
Interface for visitors Use in MIR component if you want to enforce to implement visits for all operat...
Definition Visitor.h:38
void removeUse(Use use)
Removes the specified use from the uses of this output.
Definition Operation.cpp:26
Type getType() const
Definition Operation.h:123
std::size_t getNumInputs() const
Definition Operation.h:128
Output * getInput(std::size_t index)
Definition Operation.h:137
std::deque< Output > & getOutputs()
Definition Operation.h:134
void accept(IVisitor *v)
Definition Operation.cpp:56
std::vector< Operation * > getSortedNodes(Graph *graph)
Returns nodes of the graph sorted topologically.
Definition Graph.cpp:42