ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Operation.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_OPERATION_H_
18#define _MIR_OPERATION_H_
19
20#include "mir/TensorType.h"
21
22#include <deque>
23#include <string>
24#include <limits>
25#include <vector>
26
27namespace mir
28{
29
30class IVisitor;
31
33{
34public:
35 enum class Type
36 {
37#define HANDLE_OP(OpType, OpClass) OpType,
38#include "mir/Operations.inc"
39#undef HANDLE_OP
40 };
41
43 struct Use
44 {
45 Use(Operation *node, std::size_t index) : _node(node), _index(index) {}
46
47 Operation *getNode() const { return _node; }
48
49 std::size_t getIndex() const { return _index; }
50
51 bool operator==(const Use &other) { return _node == other._node && _index == other._index; }
52
53 private:
54 Operation *_node;
55 std::size_t _index;
56 };
57
59 class Output
60 {
61 public:
62 Output(Operation *node, std::size_t index) : _node(node), _index(index) {}
63
64 ~Output() = default;
65
66 Output(const Output &) = delete;
67 Output(Output &&) = delete;
68 Output &operator=(const Output &) = delete;
69 Output &operator=(Output &&) = delete;
70
72 Operation *getNode() { return _node; }
73 const Operation *getNode() const { return _node; }
74
76 std::size_t getIndex() const { return _index; }
77
79 const std::vector<Use> &getUses() const { return _uses; }
80
82 void addUse(Use use) { _uses.push_back(use); }
83
85 void removeUse(Use use);
86
88 void replaceAllUsesWith(Output *new_def);
89
91 const TensorType &getType() const { return _type; }
92
95 void setType(const TensorType &type) { _type = type; }
96
97 // Convenient accessors.
98 DataType getElementType() const { return getType().getElementType(); }
99 const Shape &getShape() const { return getType().getShape(); }
100
101 // TODO Remove in favor of `setType`.
102 void setShape(const Shape &shape) { setType(TensorType(_type.getElementType(), shape)); }
103
104 const std::string &getName() const { return _name; }
105 void setName(const std::string &name) { _name = name; }
106
109 {
111 }
112
113 private:
114 Operation *_node;
115 std::size_t _index;
116 std::vector<Use> _uses;
117 TensorType _type;
118 std::string _name;
119 };
120
121 virtual ~Operation() = default;
122
123 Type getType() const { return _type; }
124
125 std::size_t getId() const { return _id; }
126 void setId(std::size_t id) { _id = id; }
127
128 std::size_t getNumInputs() const { return _inputs.size(); }
129 std::size_t getNumOutputs() const { return _outputs.size(); }
130
131 std::deque<Output *> &getInputs() { return _inputs; }
132 const std::deque<Output *> &getInputs() const { return _inputs; }
133
134 std::deque<Output> &getOutputs() { return _outputs; }
135 const std::deque<Output> &getOutputs() const { return _outputs; }
136
137 Output *getInput(std::size_t index)
138 {
139 assert(index < _inputs.size());
140 return _inputs[index];
141 }
142
143 const Output *getInput(std::size_t index) const
144 {
145 assert(index < _inputs.size());
146 return _inputs[index];
147 }
148
149 Output *getOutput(std::size_t index)
150 {
151 assert(index < _outputs.size());
152 return &_outputs[index];
153 }
154
155 const Output *getOutput(std::size_t index) const
156 {
157 assert(index < _outputs.size());
158 return &_outputs[index];
159 }
160
161 const Shape &getInputShape(std::size_t index) const { return getInput(index)->getShape(); }
162
163 const Shape &getOutputShape(std::size_t index) const { return getOutput(index)->getShape(); }
164
165 void accept(IVisitor *v);
166
167 virtual Operation *copyWithInputs(const std::vector<Output *> &inputs) = 0;
168
169protected:
170 Operation(Type type, const std::vector<Output *> &inputs, std::size_t num_outputs = 1);
171
172 void setOutputType(std::size_t index, const TensorType &type) { getOutput(index)->setType(type); }
173
174private:
175 Type _type;
176 std::size_t _id = std::numeric_limits<std::size_t>::max();
177 std::deque<Output *> _inputs;
178 std::deque<Output> _outputs;
179};
180
184const std::string &getTypeName(Operation::Type type);
185
186} // namespace mir
187
188#endif //_MIR_OPERATION_H_
Interface for visitors Use in MIR component if you want to enforce to implement visits for all operat...
Definition Visitor.h:38
Represents an output of a node.
Definition Operation.h:60
DataType getElementType() const
Definition Operation.h:98
Output & operator=(Output &&)=delete
Output(const Output &)=delete
Output & operator=(const Output &)=delete
void setQuantization(const mir::AffineQuantization &quant)
Set AffineQuantization to Ouput.
Definition Operation.h:108
const Operation * getNode() const
Definition Operation.h:73
void addUse(Use use)
Adds the specified use to the uses of this output.
Definition Operation.h:82
std::size_t getIndex() const
Returns the index of this output among all the outputs of the node.
Definition Operation.h:76
Output(Operation *node, std::size_t index)
Definition Operation.h:62
void setType(const TensorType &type)
Sets the type of this output.
Definition Operation.h:95
const std::vector< Use > & getUses() const
Returns the inputs that consume this output.
Definition Operation.h:79
void setName(const std::string &name)
Definition Operation.h:105
const std::string & getName() const
Definition Operation.h:104
Output(Output &&)=delete
const Shape & getShape() const
Definition Operation.h:99
void setShape(const Shape &shape)
Definition Operation.h:102
Operation * getNode()
Returns the node this is an output of.
Definition Operation.h:72
const TensorType & getType() const
Gets the type of this output.
Definition Operation.h:91
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
const std::deque< Output > & getOutputs() const
Definition Operation.h:135
virtual ~Operation()=default
void accept(IVisitor *v)
Definition Operation.cpp:56
const Output * getInput(std::size_t index) const
Definition Operation.h:143
Output * getOutput(std::size_t index)
Definition Operation.h:149
const Shape & getInputShape(std::size_t index) const
Definition Operation.h:161
virtual Operation * copyWithInputs(const std::vector< Output * > &inputs)=0
std::deque< Output * > & getInputs()
Definition Operation.h:131
const Shape & getOutputShape(std::size_t index) const
Definition Operation.h:163
const Output * getOutput(std::size_t index) const
Definition Operation.h:155
void setId(std::size_t id)
Definition Operation.h:126
std::size_t getId() const
Definition Operation.h:125
const std::deque< Output * > & getInputs() const
Definition Operation.h:132
void setOutputType(std::size_t index, const TensorType &type)
Definition Operation.h:172
std::size_t getNumOutputs() const
Definition Operation.h:129
DataType getElementType() const
Definition TensorType.h:41
DataType
Definition DataType.h:27
const std::string & getTypeName(Operation::Type type)
Definition Operation.cpp:71
NNFW_TYPE getType(const char *type="")
Represents a use of an operation output.
Definition Operation.h:44
bool operator==(const Use &other)
Definition Operation.h:51
std::size_t getIndex() const
Definition Operation.h:49
Operation * getNode() const
Definition Operation.h:47
Use(Operation *node, std::size_t index)
Definition Operation.h:45