ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 __ONERT_IR_GRAPH_H__
18#define __ONERT_IR_GRAPH_H__
19
20#include <functional>
21#include <unordered_map>
22
23#include "ir/IGraph.h"
24#include "ir/Model.h"
25#include "ir/Operands.h"
26#include "ir/Operations.h"
27
28namespace onert::ir
29{
30
31class Graph : public IGraph
32{
33private:
34 enum class Phase
35 {
36 BUILDING,
37 MODEL
38 };
39
40public:
41 explicit Graph(void);
42 explicit Graph(const Graph &);
43
44 ~Graph(void);
45
46 // Graph Building
47public:
48 OperandIndex addOperand(const Shape &shape, const TypeInfo &type);
60 OperandIndex addOperand(OperandIndex index, std::unique_ptr<Operand> &&operand);
61 OperationIndex addOperation(std::unique_ptr<IOperation> &&node);
73 OperationIndex addOperation(OperationIndex index, std::unique_ptr<IOperation> &&operation);
86 OperationIndex replaceOperation(OperationIndex index, std::unique_ptr<IOperation> &&operation);
87 void setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data);
88 void changeShape(const OperandIndex &ind, const ir::Shape &new_shape) override;
89 void addInput(const OperandIndex &ind, const std::string &name = "");
90 void addOutput(const OperandIndex &ind, const std::string &name = "");
91 void verify(void) const;
92 void removeOperand(const OperandIndex &ind) { _operands.remove(ind); }
93
94private:
95 bool checkOperandsForOperation(const IOperation &operation);
96 void linkOperandToOperation(OperationIndex index, const IOperation &operation);
97 void initializeUseDef();
98 // TODO Rename to `sweepUnusedOperands`
99 // TODO Make this public
100 void sweepGarbageOperands();
101
102 // Accessors
103public:
104 const OperandIndexSequence &getInputs() const override { return _inputs; }
105 OperandIndexSequence &getInputs() { return _inputs; }
106 const OperandIndexSequence &getOutputs() const override { return _outputs; }
107 OperandIndexSequence &getOutputs() { return _outputs; }
108 IOIndex getInputIndex(const std::string &name) const override;
109 IOIndex getOutputIndex(const std::string &name) const override;
110 const Operands &operands() const override { return _operands; }
111 Operands &operands() { return _operands; } // TODO Remove this non-const accessor
112 const Operations &operations() const override { return _operations; }
113 Operations &operations() { return _operations; }
114
115 // Topological sort
116public:
117 std::vector<ir::OperationIndex> topolSortOperations() const;
118
119private:
120 Operations _operations;
121 Operands _operands;
122 OperandIndexSequence _inputs;
123 OperandIndexSequence _outputs;
124 // TODO: Apply Heterogeneous lookup for unordered containers (transparent hashing) since C++20
125 // to use `std::string_view` with lookup functions in unordered containers
126 std::unordered_map<std::string, IOIndex> _name_to_input;
127 std::unordered_map<std::string, IOIndex> _name_to_output;
128};
129
130} // namespace onert::ir
131
132#endif // __ONERT_IR_GRAPH_H__
Operations & operations()
Definition Graph.h:113
IOIndex getInputIndex(const std::string &name) const override
Definition Graph.cc:135
std::vector< ir::OperationIndex > topolSortOperations() const
Definition Graph.cc:182
OperandIndex addOperand(const Shape &shape, const TypeInfo &type)
Definition Graph.cc:33
void removeOperand(const OperandIndex &ind)
Definition Graph.h:92
OperationIndex addOperation(std::unique_ptr< IOperation > &&node)
Definition Graph.cc:67
Operands & operands()
Definition Graph.h:111
Graph(const Graph &)
void verify(void) const
Definition Graph.cc:147
void setOperandValue(const OperandIndex &ind, std::shared_ptr< Data > data)
Definition Graph.cc:109
void changeShape(const OperandIndex &ind, const ir::Shape &new_shape) override
Definition Graph.cc:115
void addOutput(const OperandIndex &ind, const std::string &name="")
Definition Graph.cc:128
IOIndex getOutputIndex(const std::string &name) const override
Definition Graph.cc:141
const Operands & operands() const override
Definition Graph.h:110
OperandIndexSequence & getOutputs()
Definition Graph.h:107
const OperandIndexSequence & getInputs() const override
Definition Graph.h:104
OperationIndex replaceOperation(OperationIndex index, std::unique_ptr< IOperation > &&operation)
Replace an operation which the graph already has.
Definition Graph.cc:92
const Operations & operations() const override
Definition Graph.h:112
OperandIndexSequence & getInputs()
Definition Graph.h:105
void addInput(const OperandIndex &ind, const std::string &name="")
Definition Graph.cc:121
const OperandIndexSequence & getOutputs() const override
Definition Graph.h:106
void remove(const Index &index)
Remove the object that is associated with the given index.