ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Graph.h
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#ifndef _MIR_GRAPH_H_
18#define _MIR_GRAPH_H_
19
20#include <string>
21#include <vector>
22#include <type_traits>
23#include <unordered_set>
24#include <unordered_map>
25#include <set>
26
27#include "mir/Operation.h"
28#include "mir/ops/InputOp.h"
29#include "mir/ops/OutputOp.h"
30
31namespace mir
32{
33
34class Graph
35{
36public:
37 explicit Graph() = default;
38
39 virtual ~Graph();
40
41 template <typename T, typename... Args> Operation *create(Args &&...args)
42 {
43 auto op = new T(std::forward<Args>(args)...);
44 op->setId(_last_node_id++);
45 registerOp(op);
46 return op;
47 }
48
52 Operation *copyOpWithInputs(Operation *old_op, const std::vector<Operation::Output *> &inputs)
53 {
54 assert(inputs.size() == old_op->getNumInputs());
55 auto op = old_op->copyWithInputs(inputs);
56 op->setId(_last_node_id++);
57 registerOp(op);
58 return op;
59 }
60
61 void accept(IVisitor *visitor);
62
67 std::unordered_set<Operation *> getNodes() const { return _ops; }
68
73 std::vector<ops::InputOp *> getInputs() const { return _inputs; }
74
79 std::vector<ops::OutputOp *> getOutputs() const { return _outputs; }
80
85 void removeNode(Operation *op);
86
92 void replaceNode(Operation *op, Operation *with);
93
94private:
95 void registerOp(Operation *op);
96
97 std::unordered_set<Operation *> _ops;
98 size_t _last_node_id = 0;
99 // TODO Change these to unordered_sets.
100 std::vector<ops::InputOp *> _inputs;
101 std::vector<ops::OutputOp *> _outputs;
102};
103
111std::vector<Operation *> getSortedNodes(Graph *graph);
112
113} // namespace mir
114
115#endif //_MIR_GRAPH_H_
void replaceNode(Operation *op, Operation *with)
Subsitude node in graph with another keeping all edges.
Definition Graph.cpp:111
std::unordered_set< Operation * > getNodes() const
Returns all graph nodes.
Definition Graph.h:67
Operation * copyOpWithInputs(Operation *old_op, const std::vector< Operation::Output * > &inputs)
Copies old_op with new inputs and registers it into graph.
Definition Graph.h:52
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
std::vector< ops::InputOp * > getInputs() const
Returns all graph input nodes.
Definition Graph.h:73
Graph()=default
std::vector< ops::OutputOp * > getOutputs() const
Returns all graph output nodes.
Definition Graph.h:79
void accept(IVisitor *visitor)
Definition Graph.cpp:84
Operation * create(Args &&...args)
Definition Graph.h:41
Interface for visitors Use in MIR component if you want to enforce to implement visits for all operat...
Definition Visitor.h:38
std::size_t getNumInputs() const
Definition Operation.h:128
virtual Operation * copyWithInputs(const std::vector< Output * > &inputs)=0
void setId(std::size_t id)
Definition Operation.h:126
std::vector< Operation * > getSortedNodes(Graph *graph)
Returns nodes of the graph sorted topologically.
Definition Graph.cpp:42