forked from onnx/onnx-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnnxAttrs.cpp
374 lines (346 loc) · 10 KB
/
OnnxAttrs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include "OnnxAttrs.hpp"
#include "ShapedWeights.hpp"
#include "onnx2trt_utils.hpp"
#include <onnx/onnx_pb.h>
template <>
float OnnxAttrs::get<float>(const std::string& key) const
{
return this->at(key)->f();
}
template <>
int OnnxAttrs::get<int>(const std::string& key) const
{
return this->at(key)->i();
}
template <>
bool OnnxAttrs::get<bool>(const std::string& key) const
{
int value = this->at(key)->i();
assert(value == bool(value));
return bool(value);
}
template <>
std::string OnnxAttrs::get<std::string>(const std::string& key) const
{
return this->at(key)->s();
}
template <>
std::vector<int> OnnxAttrs::get<std::vector<int>>(const std::string& key) const
{
auto attr = this->at(key)->ints();
return std::vector<int>(attr.begin(), attr.end());
}
template <>
std::vector<int64_t> OnnxAttrs::get<std::vector<int64_t>>(const std::string& key) const
{
auto attr = this->at(key)->ints();
return std::vector<int64_t>(attr.begin(), attr.end());
}
template <>
std::vector<float> OnnxAttrs::get<std::vector<float>>(const std::string& key) const
{
auto attr = this->at(key)->floats();
return std::vector<float>(attr.begin(), attr.end());
}
template <>
nvinfer1::Dims OnnxAttrs::get<nvinfer1::Dims>(const std::string& key) const
{
auto values = this->get<std::vector<int>>(key);
nvinfer1::Dims dims;
dims.nbDims = values.size();
std::copy(values.begin(), values.end(), dims.d);
// Note: No dimension type information is included
return dims;
}
template <>
nvinfer1::DimsHW OnnxAttrs::get<nvinfer1::DimsHW>(const std::string& key) const
{
nvinfer1::Dims dims = this->get<nvinfer1::Dims>(key);
assert(dims.nbDims == 2);
return nvinfer1::DimsHW(dims.d[0], dims.d[1]);
}
template <>
nvinfer1::Permutation OnnxAttrs::get<nvinfer1::Permutation>(const std::string& key) const
{
auto values = this->get<std::vector<int>>(key);
nvinfer1::Permutation perm;
std::copy(values.begin(), values.end(), perm.order);
// Fill unused values with identity permutation
for (int i = values.size(); i < nvinfer1::Dims::MAX_DIMS; ++i)
{
perm.order[i] = i;
}
return perm;
}
template <>
onnx2trt::ShapedWeights OnnxAttrs::get<onnx2trt::ShapedWeights>(const std::string& key) const
{
::ONNX_NAMESPACE::TensorProto const& onnx_weights_tensor = this->at(key)->t();
onnx2trt::ShapedWeights weights;
bool success = convertOnnxWeights(onnx_weights_tensor, &weights, mCtx);
if (!success)
{
throw std::runtime_error{"Unable to convert ONNX weights"};
}
return weights;
}
template <>
nvinfer1::DataType OnnxAttrs::get<nvinfer1::DataType>(const std::string& key) const
{
::ONNX_NAMESPACE::TensorProto::DataType onnx_dtype
= static_cast<::ONNX_NAMESPACE::TensorProto::DataType>(this->at(key)->i());
nvinfer1::DataType dtype{};
if (!onnx2trt::convertDtype(onnx_dtype, &dtype))
{
dtype = static_cast<nvinfer1::DataType>(-1);
}
return dtype;
}
template <>
std::vector<nvinfer1::DataType> OnnxAttrs::get<std::vector<nvinfer1::DataType>>(const std::string& key) const
{
auto attr = this->at(key)->ints();
auto onnx_dtypes = std::vector<int64_t>(attr.begin(), attr.end());
std::vector<nvinfer1::DataType> dtypes{};
for (auto onnx_dtype : onnx_dtypes)
{
nvinfer1::DataType dtype{};
if (!onnx2trt::convertDtype(static_cast<int32_t>(onnx_dtype), &dtype))
{
dtype = static_cast<nvinfer1::DataType>(-1);
}
dtypes.push_back(dtype);
}
return dtypes;
}
inline nvinfer1::ActivationType activationStringToEnum(const std::string& type)
{
if (type == "Relu")
{
return nvinfer1::ActivationType::kRELU;
}
if (type == "Tanh")
{
return nvinfer1::ActivationType::kTANH;
}
if (type == "Sigmoid")
{
return nvinfer1::ActivationType::kSIGMOID;
}
if (type == "LeakyRelu")
{
return nvinfer1::ActivationType::kLEAKY_RELU;
}
if (type == "ThresholdedRelu")
{
return nvinfer1::ActivationType::kTHRESHOLDED_RELU;
}
if (type == "ScaledTanh")
{
return nvinfer1::ActivationType::kSCALED_TANH;
}
if (type == "HardSigmoid")
{
return nvinfer1::ActivationType::kHARD_SIGMOID;
}
if (type == "Elu")
{
return nvinfer1::ActivationType::kELU;
}
if (type == "Softsign")
{
return nvinfer1::ActivationType::kSOFTSIGN;
}
if (type == "Softplus")
{
return nvinfer1::ActivationType::kSOFTPLUS;
}
throw std::runtime_error("Unknown activation type: " + type);
}
template <>
nvinfer1::ActivationType OnnxAttrs::get<nvinfer1::ActivationType>(const std::string& key) const
{
const std::string type = this->get<std::string>(key);
return activationStringToEnum(type);
}
template <>
std::vector<nvinfer1::ActivationType> OnnxAttrs::get<std::vector<nvinfer1::ActivationType>>(
const std::string& key) const
{
const auto strings = this->at(key)->strings();
std::vector<nvinfer1::ActivationType> actTypes;
for (const auto& str : strings)
{
actTypes.emplace_back(activationStringToEnum(str));
}
return actTypes;
}
template <>
const ::ONNX_NAMESPACE::GraphProto& OnnxAttrs::get<const ::ONNX_NAMESPACE::GraphProto&>(const std::string& key) const
{
return this->at(key)->g();
}
template <>
nvinfer1::RNNOperation OnnxAttrs::get<nvinfer1::RNNOperation>(const std::string& key) const
{
std::string op = this->get<std::string>(key);
if (op == std::string("relu"))
{
return nvinfer1::RNNOperation::kRELU;
}
if (op == std::string("tanh"))
{
return nvinfer1::RNNOperation::kTANH;
}
if (op == std::string("lstm"))
{
return nvinfer1::RNNOperation::kLSTM;
}
if (op == std::string("gru"))
{
return nvinfer1::RNNOperation::kGRU;
}
throw std::runtime_error("Unknown RNNOperation: " + op);
}
template <>
nvinfer1::RNNInputMode OnnxAttrs::get<nvinfer1::RNNInputMode>(const std::string& key) const
{
std::string mode = this->get<std::string>(key);
if (mode == std::string("skip"))
{
return nvinfer1::RNNInputMode::kSKIP;
}
if (mode == std::string("linear"))
{
return nvinfer1::RNNInputMode::kLINEAR;
}
throw std::runtime_error("Unknown RNNInputMode: " + mode);
}
template <>
nvinfer1::RNNDirection OnnxAttrs::get<nvinfer1::RNNDirection>(const std::string& key) const
{
std::string direction = this->get<std::string>(key);
if (direction == std::string("unidirection"))
{
return nvinfer1::RNNDirection::kUNIDIRECTION;
}
if (direction == std::string("bidirection"))
{
return nvinfer1::RNNDirection::kBIDIRECTION;
}
throw std::runtime_error("Unknown RNNDirection: " + direction);
}
template <>
std::vector<std::string> OnnxAttrs::get<std::vector<std::string>>(const std::string& key) const
{
auto attr = this->at(key)->strings();
return std::vector<std::string>(attr.begin(), attr.end());
}
template <>
nvinfer1::ScaleMode OnnxAttrs::get<nvinfer1::ScaleMode>(const std::string& key) const
{
std::string s = this->get<std::string>(key);
if (s == "uniform")
{
return nvinfer1::ScaleMode::kUNIFORM;
}
if (s == "channel")
{
return nvinfer1::ScaleMode::kCHANNEL;
}
if (s == "elementwise")
{
return nvinfer1::ScaleMode::kELEMENTWISE;
}
throw std::runtime_error("Unknown ScaleMode: " + s);
}
template <>
nvinfer1::MatrixOperation OnnxAttrs::get<nvinfer1::MatrixOperation>(const std::string& key) const
{
std::string s = this->get<std::string>(key);
if (s == "none")
{
return nvinfer1::MatrixOperation::kNONE;
}
if (s == "transpose")
{
return nvinfer1::MatrixOperation::kTRANSPOSE;
}
if (s == "vector")
{
return nvinfer1::MatrixOperation::kVECTOR;
}
throw std::runtime_error("Unknown MatrixOperation: " + s);
}
template <>
nvinfer1::ResizeMode OnnxAttrs::get<nvinfer1::ResizeMode>(const std::string& key) const
{
const auto& mode = this->get<std::string>(key);
if (mode == "nearest")
{
return nvinfer1::ResizeMode::kNEAREST;
}
if (mode == "linear" || mode == "bilinear")
{
return nvinfer1::ResizeMode::kLINEAR;
}
throw std::runtime_error("Unknown ResizeMode: " + mode);
}
template <>
nvinfer1::ResizeCoordinateTransformation OnnxAttrs::get<nvinfer1::ResizeCoordinateTransformation>(
const std::string& key) const
{
const auto& transformation = this->get<std::string>(key);
if (transformation == "align_corners")
{
return nvinfer1::ResizeCoordinateTransformation::kALIGN_CORNERS;
}
if (transformation == "asymmetric")
{
return nvinfer1::ResizeCoordinateTransformation::kASYMMETRIC;
}
if (transformation == "half_pixel")
{
return nvinfer1::ResizeCoordinateTransformation::kHALF_PIXEL;
}
throw std::runtime_error("Unknown ResizeCoordinateTransformation: " + transformation);
}
template <>
nvinfer1::ResizeSelector OnnxAttrs::get<nvinfer1::ResizeSelector>(const std::string& key) const
{
const auto& selector = this->get<std::string>(key);
if (selector == "formula")
{
return nvinfer1::ResizeSelector::kFORMULA;
}
if (selector == "upper")
{
return nvinfer1::ResizeSelector::kUPPER;
}
throw std::runtime_error("Unknown ResizeSelector: " + selector);
}
template <>
nvinfer1::ResizeRoundMode OnnxAttrs::get<nvinfer1::ResizeRoundMode>(const std::string& key) const
{
const auto& roundMode = this->get<std::string>(key);
if (roundMode == "half_up")
{
return nvinfer1::ResizeRoundMode::kHALF_UP;
}
if (roundMode == "half_down")
{
return nvinfer1::ResizeRoundMode::kHALF_DOWN;
}
if (roundMode == "floor")
{
return nvinfer1::ResizeRoundMode::kFLOOR;
}
if (roundMode == "ceil")
{
return nvinfer1::ResizeRoundMode::kCEIL;
}
throw std::runtime_error("Unknown ResizeRoundMode: " + roundMode);
}