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::ir
29{
30
31class Graph : public IGraph
32{
33public:
34 explicit Graph(void);
35 explicit Graph(const Graph &);
36
37 ~Graph(void);
38
39 // Graph Building
40public:
41 OperandIndex addOperand(const Shape &shape, const TypeInfo &type);
53 OperandIndex addOperand(OperandIndex index, std::unique_ptr<Operand> &&operand);
54 OperationIndex addOperation(std::unique_ptr<IOperation> &&node);
66 OperationIndex addOperation(OperationIndex index, std::unique_ptr<IOperation> &&operation);
79 OperationIndex replaceOperation(OperationIndex index, std::unique_ptr<IOperation> &&operation);
80 void setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data);
81 void changeShape(const OperandIndex &ind, const ir::Shape &new_shape) override;
82 void addInput(const OperandIndex &ind, const std::string &name = "");
83 void addOutput(const OperandIndex &ind, const std::string &name = "");
84 void verify(void) const;
85 void removeOperand(const OperandIndex &ind) { _operands.remove(ind); }
86
87private:
88 bool checkOperandsForOperation(const IOperation &operation);
89 void linkOperandToOperation(OperationIndex index, const IOperation &operation);
90 void initializeUseDef();
91 // TODO Rename to `sweepUnusedOperands`
92 // TODO Make this public
93 void sweepGarbageOperands();
94
95 // Accessors
96public:
97 const OperandIndexSequence &getInputs() const override { return _inputs; }
98 OperandIndexSequence &getInputs() { return _inputs; }
99 const OperandIndexSequence &getOutputs() const override { return _outputs; }
100 OperandIndexSequence &getOutputs() { return _outputs; }
101 IOIndex getInputIndex(const std::string &name) const override;
102 IOIndex getOutputIndex(const std::string &name) const override;
103 const Operands &operands() const override { return _operands; }
104 Operands &operands() { return _operands; } // TODO Remove this non-const accessor
105 const Operations &operations() const override { return _operations; }
106 Operations &operations() { return _operations; }
107
108 // Topological sort
109public:
110 std::vector<ir::OperationIndex> topolSortOperations() const;
111
112private:
113 Operations _operations;
114 Operands _operands;
115 OperandIndexSequence _inputs;
116 OperandIndexSequence _outputs;
117 // TODO: Apply Heterogeneous lookup for unordered containers (transparent hashing) since C++20
118 // to use `std::string_view` with lookup functions in unordered containers
119 std::unordered_map<std::string, IOIndex> _name_to_input;
120 std::unordered_map<std::string, IOIndex> _name_to_output;
121};
122
123} // namespace onert::ir
124
125#endif // __ONERT_IR_GRAPH_H__
Operations & operations()
Definition Graph.h:106
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:85
OperationIndex addOperation(std::unique_ptr< IOperation > &&node)
Definition Graph.cc:67
Operands & operands()
Definition Graph.h:104
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:103
OperandIndexSequence & getOutputs()
Definition Graph.h:100
const OperandIndexSequence & getInputs() const override
Definition Graph.h:97
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:105
OperandIndexSequence & getInputs()
Definition Graph.h:98
void addInput(const OperandIndex &ind, const std::string &name="")
Definition Graph.cc:121
const OperandIndexSequence & getOutputs() const override
Definition Graph.h:99
void remove(const Index &index)
Remove the object that is associated with the given index.