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 __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
29{
30namespace ir
31{
32
33class Graph : public IGraph
34{
35private:
36 enum class Phase
37 {
38 BUILDING,
39 MODEL
40 };
41
42public:
43 explicit Graph(void);
44 explicit Graph(const Graph &);
45
46 ~Graph(void);
47
48 // Graph Building
49public:
50 OperandIndex addOperand(const Shape &shape, const TypeInfo &type);
62 OperandIndex addOperand(OperandIndex index, std::unique_ptr<Operand> &&operand);
63 OperationIndex addOperation(std::unique_ptr<IOperation> &&node);
75 OperationIndex addOperation(OperationIndex index, std::unique_ptr<IOperation> &&operation);
88 OperationIndex replaceOperation(OperationIndex index, std::unique_ptr<IOperation> &&operation);
89 void setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data);
90 void changeShape(const OperandIndex &ind, const ir::Shape &new_shape) override;
91 void addInput(const OperandIndex &ind, const std::string &name = "");
92 void addOutput(const OperandIndex &ind, const std::string &name = "");
93 void verify(void) const;
94 void removeOperand(const OperandIndex &ind) { _operands.remove(ind); }
95
96private:
97 bool checkOperandsForOperation(const IOperation &operation);
98 void linkOperandToOperation(OperationIndex index, const IOperation &operation);
99 void initializeUseDef();
100 // TODO Rename to `sweepUnusedOperands`
101 // TODO Make this public
102 void sweepGarbageOperands();
103
104 // Accessors
105public:
106 const OperandIndexSequence &getInputs() const override { return _inputs; }
107 OperandIndexSequence &getInputs() { return _inputs; }
108 const OperandIndexSequence &getOutputs() const override { return _outputs; }
109 OperandIndexSequence &getOutputs() { return _outputs; }
110 IOIndex getInputIndex(const std::string &name) const override;
111 IOIndex getOutputIndex(const std::string &name) const override;
112 const Operands &operands() const override { return _operands; }
113 Operands &operands() { return _operands; } // TODO Remove this non-const accessor
114 const Operations &operations() const override { return _operations; }
115 Operations &operations() { return _operations; }
116
117 // Topological sort
118public:
119 std::vector<ir::OperationIndex> topolSortOperations() const;
120
121private:
122 Operations _operations;
123 Operands _operands;
124 OperandIndexSequence _inputs;
125 OperandIndexSequence _outputs;
126 std::unordered_map<std::string, IOIndex> _name_to_input;
127 std::unordered_map<std::string, IOIndex> _name_to_output;
128};
129
130} // namespace ir
131} // namespace onert
132
133#endif // __ONERT_IR_GRAPH_H__
Operations & operations()
Definition Graph.h:115
IOIndex getInputIndex(const std::string &name) const override
Definition Graph.cc:137
std::vector< ir::OperationIndex > topolSortOperations() const
Definition Graph.cc:184
OperandIndex addOperand(const Shape &shape, const TypeInfo &type)
Definition Graph.cc:35
void removeOperand(const OperandIndex &ind)
Definition Graph.h:94
OperationIndex addOperation(std::unique_ptr< IOperation > &&node)
Definition Graph.cc:69
Operands & operands()
Definition Graph.h:113
Graph(const Graph &)
void verify(void) const
Definition Graph.cc:149
void setOperandValue(const OperandIndex &ind, std::shared_ptr< Data > data)
Definition Graph.cc:111
void changeShape(const OperandIndex &ind, const ir::Shape &new_shape) override
Definition Graph.cc:117
void addOutput(const OperandIndex &ind, const std::string &name="")
Definition Graph.cc:130
IOIndex getOutputIndex(const std::string &name) const override
Definition Graph.cc:143
const Operands & operands() const override
Definition Graph.h:112
OperandIndexSequence & getOutputs()
Definition Graph.h:109
const OperandIndexSequence & getInputs() const override
Definition Graph.h:106
OperationIndex replaceOperation(OperationIndex index, std::unique_ptr< IOperation > &&operation)
Replace an operation which the graph already has.
Definition Graph.cc:94
const Operations & operations() const override
Definition Graph.h:114
OperandIndexSequence & getInputs()
Definition Graph.h:107
void addInput(const OperandIndex &ind, const std::string &name="")
Definition Graph.cc:123
const OperandIndexSequence & getOutputs() const override
Definition Graph.h:108
void remove(const Index &index)
Remove the object that is associated with the given index.