Elffuss BraidHub

kikocis/ncnn public

master · 8.6 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#ifndef PNNX_IR_H
16#define PNNX_IR_H
17
18#include <limits.h>
19#include <complex>
20#include <initializer_list>
21#include <limits>
22#include <map>
23#include <set>
24#include <string>
25#include <vector>
26
27#if BUILD_TORCH2PNNX
28namespace torch {
29namespace jit {
30struct Value;
31struct Node;
32} // namespace jit
33} // namespace torch
34namespace at {
35class Tensor;
36}
37#endif // BUILD_TORCH2PNNX
38
39#if BUILD_ONNX2PNNX
40namespace onnx {
41class AttributeProto;
42class TensorProto;
43class ValueInfoProto;
44} // namespace onnx
45namespace pnnx {
46namespace onnx2pnnx {
47class OnnxAttributeProxy;
48} // namespace onnx2pnnx
49} // namespace pnnx
50#endif // BUILD_ONNX2PNNX
51
52namespace pnnx {
53
54class Parameter
55{
56public:
57 Parameter()
58 : type(0)
59 {
60 }
61 Parameter(bool _b)
62 : type(1), b(_b)
63 {
64 }
65 Parameter(int _i)
66 : type(2), i(_i)
67 {
68 }
69 Parameter(long _l)
70 : type(2)
71 {
72 if (_l == std::numeric_limits<long>::max()) _l = INT_MAX;
73 if (_l == std::numeric_limits<long>::min()) _l = INT_MIN;
74 i = (int)_l;
75 }
76 Parameter(long long _l)
77 : type(2)
78 {
79 if (_l == std::numeric_limits<long long>::max()) _l = INT_MAX;
80 if (_l == std::numeric_limits<long long>::min()) _l = INT_MIN;
81 i = (int)_l;
82 }
83 Parameter(float _f)
84 : type(3), f(_f)
85 {
86 }
87 Parameter(double _d)
88 : type(3), f((float)_d)
89 {
90 }
91 Parameter(const char* _s)
92 : type(4), s(_s)
93 {
94 }
95 Parameter(const std::string& _s)
96 : type(4), s(_s)
97 {
98 }
99 Parameter(const std::initializer_list<int>& _ai)
100 : type(5), ai(_ai)
101 {
102 }
103 Parameter(const std::initializer_list<int64_t>& _ai)
104 : type(5)
105 {
106 for (const auto& x : _ai)
107 {
108 int64_t _l = x;
109 if (_l == std::numeric_limits<int64_t>::max()) _l = INT_MAX;
110 if (_l == std::numeric_limits<int64_t>::min()) _l = INT_MIN;
111 ai.push_back((int)_l);
112 }
113 }
114 Parameter(const std::vector<int>& _ai)
115 : type(5), ai(_ai)
116 {
117 }
118 Parameter(const std::vector<int64_t>& _ai)
119 : type(5)
120 {
121 for (const auto& x : _ai)
122 {
123 int64_t _l = x;
124 if (_l == std::numeric_limits<int64_t>::max()) _l = INT_MAX;
125 if (_l == std::numeric_limits<int64_t>::min()) _l = INT_MIN;
126 ai.push_back((int)_l);
127 }
128 }
129 Parameter(const std::initializer_list<float>& _af)
130 : type(6), af(_af)
131 {
132 }
133 Parameter(const std::initializer_list<double>& _af)
134 : type(6)
135 {
136 for (const auto& x : _af)
137 af.push_back((float)x);
138 }
139 Parameter(const std::vector<float>& _af)
140 : type(6), af(_af)
141 {
142 }
143 Parameter(const std::vector<double>& _af)
144 : type(6)
145 {
146 for (const auto& x : _af)
147 af.push_back((float)x);
148 }
149 Parameter(const std::initializer_list<const char*>& _as)
150 : type(7)
151 {
152 for (const auto& x : _as)
153 as.push_back(std::string(x));
154 }
155 Parameter(const std::initializer_list<std::string>& _as)
156 : type(7), as(_as)
157 {
158 }
159 Parameter(const std::vector<std::string>& _as)
160 : type(7), as(_as)
161 {
162 }
163 Parameter(const std::complex<float>& _c)
164 : type(10), c(_c)
165 {
166 }
167 Parameter(const std::complex<double>& _c)
168 : type(10), c(_c)
169 {
170 }
171 Parameter(const std::initializer_list<std::complex<float> >& _ac)
172 : type(11), ac(_ac)
173 {
174 }
175 Parameter(const std::initializer_list<std::complex<double> >& _ac)
176 : type(11)
177 {
178 for (const auto& x : _ac)
179 ac.push_back(std::complex<float>(x));
180 }
181 Parameter(const std::vector<std::complex<float> >& _ac)
182 : type(11), ac(_ac)
183 {
184 }
185 Parameter(const std::vector<std::complex<double> >& _ac)
186 : type(11)
187 {
188 for (const auto& x : _ac)
189 ac.push_back(std::complex<float>(x));
190 }
191
192#if BUILD_TORCH2PNNX
193 Parameter(const torch::jit::Node* value_node);
194 Parameter(const torch::jit::Value* value);
195#endif // BUILD_TORCH2PNNX
196#if BUILD_ONNX2PNNX
197 Parameter(const onnx::AttributeProto& attr);
198 Parameter(const onnx2pnnx::OnnxAttributeProxy& attr);
199#endif // BUILD_ONNX2PNNX
200
201 static Parameter parse_from_string(const std::string& value);
202 static std::string encode_to_string(const Parameter& param);
203
204 // 0=null 1=b 2=i 3=f 4=s 5=ai 6=af 7=as 8=others 10=c 11=ac
205 int type;
206
207 // value
208 bool b;
209 int i;
210 float f;
211 std::complex<float> c;
212 std::vector<int> ai;
213 std::vector<float> af;
214 std::vector<std::complex<float> > ac;
215
216 // keep std::string typed member the last for cross cxxabi compatibility
217 std::string s;
218 std::vector<std::string> as;
219};
220
221bool operator==(const Parameter& lhs, const Parameter& rhs);
222
223class Attribute
224{
225public:
226 Attribute()
227 : type(0)
228 {
229 }
230
231#if BUILD_TORCH2PNNX
232 Attribute(const at::Tensor& t);
233#endif
234#if BUILD_ONNX2PNNX
235 Attribute(const onnx::TensorProto& t);
236#endif
237
238 Attribute(const std::initializer_list<int>& shape, const std::vector<float>& t);
239
240 size_t elemsize() const;
241 int elemcount() const;
242
243 // convenient routines for manipulate fp32/fp16 weight
244 std::vector<float> get_float32_data() const;
245 void set_float32_data(const std::vector<float>& data);
246
247 // 0=null 1=f32 2=f64 3=f16 4=i32 5=i64 6=i16 7=i8 8=u8 9=bool 10=c64 11=c128 12=c32 13=bf16
248 int type;
249 std::vector<int> shape;
250
251 std::vector<char> data;
252
253 std::map<std::string, Parameter> params;
254};
255
256bool operator==(const Attribute& lhs, const Attribute& rhs);
257
258// concat two attributes along the first axis
259Attribute operator+(const Attribute& a, const Attribute& b);
260
261class Operator;
262class Operand
263{
264public:
265 void remove_consumer(const Operator* c);
266
267 Operator* producer;
268 std::vector<Operator*> consumers;
269
270 // 0=null 1=f32 2=f64 3=f16 4=i32 5=i64 6=i16 7=i8 8=u8 9=bool 10=c64 11=c128 12=c32 13=bf16
271 int type;
272 std::vector<int> shape;
273
274 // keep std::string typed member the last for cross cxxabi compatibility
275 std::string name;
276
277 std::map<std::string, Parameter> params;
278
279private:
280 friend class Graph;
281 Operand()
282 {
283 type = 0;
284 }
285};
286
287class Operator
288{
289public:
290 bool has_param(const std::string& key) const;
291 bool has_attr(const std::string& key) const;
292 bool has_input(const std::string& key) const;
293 Operand* named_input(const std::string& key);
294 const Operand* named_input(const std::string& key) const;
295
296 std::vector<Operand*> inputs;
297 std::vector<Operand*> outputs;
298
299 // keep std::string typed member the last for cross cxxabi compatibility
300 std::string type;
301 std::string name;
302
303 std::vector<std::string> inputnames;
304 std::map<std::string, Parameter> params;
305 std::map<std::string, Attribute> attrs;
306
307private:
308 friend class Graph;
309 Operator()
310 {
311 }
312};
313
314class Graph
315{
316public:
317 Graph();
318 ~Graph();
319
320 int load(const std::string& parampath, const std::string& binpath);
321 int save(const std::string& parampath, const std::string& binpath);
322
323 int python(const std::string& pypath, const std::string& binpath);
324
325 int parse(const std::string& param);
326
327 Operator* new_operator(const std::string& type, const std::string& name);
328
329 Operator* new_operator_before(const std::string& type, const std::string& name, const Operator* cur);
330
331 Operator* new_operator_after(const std::string& type, const std::string& name, const Operator* cur);
332
333#if BUILD_TORCH2PNNX
334 Operand* new_operand(const torch::jit::Value* v);
335#endif
336#if BUILD_ONNX2PNNX
337 Operand* new_operand(const onnx::ValueInfoProto& value);
338 Operand* new_operand(const onnx::TensorProto& t);
339#endif
340
341 Operand* new_operand(const std::string& name);
342
343 Operand* get_operand(const std::string& name);
344 const Operand* get_operand(const std::string& name) const;
345
346 std::vector<Operator*> ops;
347 std::vector<Operand*> operands;
348
349private:
350 Graph(const Graph& rhs);
351 Graph& operator=(const Graph& rhs);
352};
353
354} // namespace pnnx
355
356#endif // PNNX_IR_H