forked from onnx/onnx-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonnx2trt_utils.hpp
406 lines (374 loc) · 14.4 KB
/
onnx2trt_utils.hpp
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "ShapedWeights.hpp"
#include "trt_utils.hpp"
#include "OnnxAttrs.hpp"
#include <onnx/onnx_pb.h>
#include <onnx/onnxifi.h>
#include <NvInfer.h>
#include <iostream>
using std::cerr;
using std::endl;
inline std::ostream& operator<<(std::ostream& stream, nvinfer1::Dims const& shape) {
if( shape.nbDims == 0 ) {
return stream;
}
stream << "(" << shape.d[0];
for( int i=1; i<shape.nbDims; ++i ) {
stream << ", " << shape.d[i];
}
stream << ")";
return stream;
}
inline std::ostream& operator<<(std::ostream& stream, nvinfer1::DataType const& dtype) {
switch( dtype ) {
case nvinfer1::DataType::kFLOAT: return stream << "float32";
case nvinfer1::DataType::kHALF: return stream << "float16";
case nvinfer1::DataType::kINT8: return stream << "int8";
#if NV_TENSORRT_MAJOR >= 4
case nvinfer1::DataType::kINT32: return stream << "int32";
#endif
default: throw std::runtime_error("Unknown dtype");
}
}
// TODO: Remove this when finished debugging
inline std::ostream& operator<<(std::ostream& stream, nvinfer1::Permutation const& perm) {
int ndims = nvinfer1::Dims::MAX_DIMS;
stream << "(" << perm.order[0];
for( int i=1; i<ndims; ++i ) {
stream << ", " << perm.order[i];
}
stream << ")";
return stream;
}
/*
// TODO: Remove this when finished debugging
inline std::ostream& operator<<(std::ostream& stream, google::protobuf::Message const& message) {
stream << print_onnx_to_string(message);
return stream;
}
*/
namespace onnx2trt {
inline int get_dtype_size(int32_t onnx_dtype) {
switch( onnx_dtype ) {
case ::ONNX_NAMESPACE::TensorProto::FLOAT16: return 2;
case ::ONNX_NAMESPACE::TensorProto::FLOAT: return 4;
case ::ONNX_NAMESPACE::TensorProto::DOUBLE: return 8;
case ::ONNX_NAMESPACE::TensorProto::COMPLEX64: return 8;
case ::ONNX_NAMESPACE::TensorProto::COMPLEX128: return 16;
case ::ONNX_NAMESPACE::TensorProto::UINT8: return 1;
case ::ONNX_NAMESPACE::TensorProto::INT8: return 1;
case ::ONNX_NAMESPACE::TensorProto::UINT16: return 2;
case ::ONNX_NAMESPACE::TensorProto::INT16: return 2;
case ::ONNX_NAMESPACE::TensorProto::UINT32: return 4;
case ::ONNX_NAMESPACE::TensorProto::INT32: return 4;
case ::ONNX_NAMESPACE::TensorProto::UINT64: return 8;
case ::ONNX_NAMESPACE::TensorProto::INT64: return 8;
// TODO: Add BOOL if necessary...
// TODO: Some sort of error handling
default: return -1;//throw std::invalid_argument("Unsupported TRT data type: " +
// std::to_string((int)trt_dtype));
}
}
inline const char* get_dtype_name(int32_t onnx_dtype) {
switch( onnx_dtype ) {
case ::ONNX_NAMESPACE::TensorProto::FLOAT: return "FLOAT";
case ::ONNX_NAMESPACE::TensorProto::UINT8: return "UINT8";
case ::ONNX_NAMESPACE::TensorProto::INT8: return "INT8";
case ::ONNX_NAMESPACE::TensorProto::UINT16: return "UINT16";
case ::ONNX_NAMESPACE::TensorProto::INT16: return "INT16";
case ::ONNX_NAMESPACE::TensorProto::INT32: return "INT32";
case ::ONNX_NAMESPACE::TensorProto::INT64: return "INT64";
case ::ONNX_NAMESPACE::TensorProto::STRING: return "STRING";
case ::ONNX_NAMESPACE::TensorProto::BOOL: return "BOOL";
case ::ONNX_NAMESPACE::TensorProto::FLOAT16: return "FLOAT16";
case ::ONNX_NAMESPACE::TensorProto::DOUBLE: return "DOUBLE";
case ::ONNX_NAMESPACE::TensorProto::UINT32: return "UINT32";
case ::ONNX_NAMESPACE::TensorProto::UINT64: return "UINT64";
case ::ONNX_NAMESPACE::TensorProto::COMPLEX64: return "COMPLEX64";
case ::ONNX_NAMESPACE::TensorProto::COMPLEX128: return "COMPLEX128";
default: return "<UNKNOWN>";
}
}
inline bool convert_dtype(int32_t onnx_dtype,
nvinfer1::DataType* trt_dtype) {
switch( onnx_dtype ) {
case ::ONNX_NAMESPACE::TensorProto::FLOAT: *trt_dtype = nvinfer1::DataType::kFLOAT; break;
case ::ONNX_NAMESPACE::TensorProto::INT8: *trt_dtype = nvinfer1::DataType::kINT8; break;
case ::ONNX_NAMESPACE::TensorProto::FLOAT16: *trt_dtype = nvinfer1::DataType::kHALF; break;
#if NV_TENSORRT_MAJOR >= 4
case ::ONNX_NAMESPACE::TensorProto::INT32: *trt_dtype = nvinfer1::DataType::kINT32; break;
#endif
default:
cerr << "Unsupported ONNX data type: " << get_dtype_name(onnx_dtype)
<< " (" << std::to_string(onnx_dtype) << ")" << endl;
return false;
}
return true;
}
template<typename OnnxDims>
inline nvinfer1::Dims convert_dims(OnnxDims const& onnx_dims) {
enum { BATCH_DIM = 0 };
std::vector<int> onnx_dims_vector;
for( auto const& onnx_dim : onnx_dims ) {
// TODO: Unknown dimensions are represented using onnx_dim.dim_param
onnx_dims_vector.push_back(onnx_dim.dim_value());
}
nvinfer1::Dims trt_dims;
trt_dims.nbDims = onnx_dims_vector.size();
assert(trt_dims.nbDims <= nvinfer1::Dims::MAX_DIMS);
std::copy(onnx_dims_vector.begin(), onnx_dims_vector.end(), trt_dims.d);
trt_dims = set_dims_CHW(remove_dim(trt_dims, BATCH_DIM));
return trt_dims;
}
inline bool convert_weight_descriptor(onnxTensorDescriptorV1 const &desc,
onnx2trt::ShapedWeights *weights) {
nvinfer1::Dims shape;
shape.nbDims = desc.dimensions;
// Special case for scalars
if( shape.nbDims == 0 ) {
shape.nbDims = 1;
shape.d[0] = 1;
shape.type[0] = nvinfer1::DimensionType::kCHANNEL;
} else {
std::copy(desc.shape, desc.shape + desc.dimensions, shape.d);
}
size_t element_count = 1;
for (int i = 0; i < shape.nbDims; ++i) {
element_count *= shape.d[i];
}
void* data_ptr;
size_t nbytes;
int32_t dtype;
data_ptr = (void*)(desc. buffer);
if (desc.dataType == ONNXIFI_DATATYPE_FLOAT32) {
dtype = ::ONNX_NAMESPACE::TensorProto::FLOAT;
nbytes = element_count * sizeof(float);
} else if (desc.dataType == ONNXIFI_DATATYPE_FLOAT16) {
dtype = ::ONNX_NAMESPACE::TensorProto::FLOAT16;
nbytes = element_count * sizeof(float) / 2;
} else if (desc.dataType == ONNXIFI_DATATYPE_INT32) {
dtype = ::ONNX_NAMESPACE::TensorProto::INT32;
nbytes = element_count * sizeof(int32_t);
} else if (desc.dataType == ONNXIFI_DATATYPE_INT64) {
dtype = ::ONNX_NAMESPACE::TensorProto::INT64;
nbytes = element_count * sizeof(int64_t);
} else {
// Unsupported format
return false;
}
onnx2trt::ShapedWeights trt_weights(dtype, data_ptr, shape);
(void)nbytes;
assert(trt_weights.size_bytes() == nbytes);
*weights = trt_weights;
return true;
}
inline bool convert_onnx_weights(::ONNX_NAMESPACE::TensorProto const& onnx_tensor,
onnx2trt::ShapedWeights* weights) {
nvinfer1::Dims shape;
shape.nbDims = onnx_tensor.dims().size();
std::copy(onnx_tensor.dims().begin(), onnx_tensor.dims().end(),
shape.d);
// Special case for scalars
if( shape.nbDims == 0 ) {
shape.nbDims = 1;
shape.d[0] = 1;
shape.type[0] = nvinfer1::DimensionType::kCHANNEL;
}
auto dtype = onnx_tensor.data_type();
void* data_ptr; // TODO: See if can make const*
size_t nbytes;
if( onnx_tensor.raw_data().size() > 0 ) {
data_ptr = (void*)onnx_tensor.raw_data().data();
nbytes = onnx_tensor.raw_data().size();
} else if( onnx_tensor.float_data().size() > 0 ) {
assert(dtype == ::ONNX_NAMESPACE::TensorProto::FLOAT);
data_ptr = (void*)onnx_tensor.float_data().data();
nbytes = onnx_tensor.float_data().size() * sizeof(float);
} else if( onnx_tensor.int32_data().size() > 0 ) {
assert(dtype == ::ONNX_NAMESPACE::TensorProto::INT32 ||
dtype == ::ONNX_NAMESPACE::TensorProto::INT16 ||
dtype == ::ONNX_NAMESPACE::TensorProto::INT8 ||
dtype == ::ONNX_NAMESPACE::TensorProto::UINT16 ||
dtype == ::ONNX_NAMESPACE::TensorProto::UINT8 ||
dtype == ::ONNX_NAMESPACE::TensorProto::BOOL ||
dtype == ::ONNX_NAMESPACE::TensorProto::FLOAT16);
data_ptr = (void*)onnx_tensor.int32_data().data();
nbytes = onnx_tensor.int32_data().size() * sizeof(int32_t);
} else if( onnx_tensor.int64_data().size() > 0 ) {
assert(dtype == ::ONNX_NAMESPACE::TensorProto::INT64);
data_ptr = (void*)onnx_tensor.int64_data().data();
nbytes = onnx_tensor.int64_data().size() * sizeof(int64_t);
} else {
// Unsupported ONNX tensor format!
return false;
}
onnx2trt::ShapedWeights trt_weights(dtype, data_ptr, shape);
(void)nbytes;
assert(trt_weights.size_bytes() == nbytes);
*weights = trt_weights;
return true;
}
// Returns the input if it is already a tensor. If it is of type ShapedWeights, adds a new
// constant layer to the TRT network and returns its output.
inline nvinfer1::ITensor& convertToTensor(TensorOrWeights& input, IImporterContext* ctx)
{
if (input.is_tensor())
{
return input.tensor();
}
else
{
// Handle non-tensor indices input by adding a new constant layer to the network.
const ShapedWeights& weights = input.weights();
return *(ctx->network()->addConstant(weights.shape, weights)->getOutput(0));
}
}
inline nvinfer1::ITensor& convert_output_weight_to_tensor(TensorOrWeights& input, IImporterContext* ctx)
{
if (input.is_tensor())
{
return input.tensor();
}
else
{
// Convert weight output to tensor output. Strip batch dimension here.
const ShapedWeights& weights = input.weights();
nvinfer1::Dims tensor_shape = weights.shape;
tensor_shape= set_dims_CHW(remove_dim(tensor_shape, 0));
return *(ctx->network()->addConstant(tensor_shape, weights)->getOutput(0));
}
}
inline int div_ceil(int n, int d) {
return (n - 1) / d + 1;
}
// Convert an ONNX axis into a TRT axis
inline Status convert_axis(int& axis, int nbDims)
{
// Support negative indexing
if (axis < 0)
{
axis += nbDims;
}
// If axis was positive, subtract 1 to strip batch dimension
else
{
axis = axis - 1;
}
ASSERT(axis >= 0 && axis < nbDims, ErrorCode::kUNSUPPORTED_NODE);
return Status::success();
}
inline int get_conv_output_size(int input_size, int filter_size,
int stride, int dilation_rate,
int total_padding) {
// This is based on the CUDNN formula
int effective_input_size = input_size + total_padding;
int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
return div_ceil(effective_input_size - (effective_filter_size - 1), stride);
}
// Helper function to help extract the index of a potential -1 dimension in the reshape node
inline Status get_infer_dim(int& infer_dim, nvinfer1::Dims const& new_shape) {
for (int i = 0; i < new_shape.nbDims; ++i) {
if (new_shape.d[i] < 0) {
// -1 bears special meaning, which means the current dimension can
// be inferred while keepin the total number of elements the same.
// https://github.com/onnx/onnx/blob/9b9f595107e3fc0295d50f6294d43879df17552f/onnx/defs/tensor/defs.cc#L73-L75
ASSERT(new_shape.d[i] == -1, ErrorCode::kUNSUPPORTED_NODE);
// We can only one dimension that has -1
ASSERT(infer_dim == -1, ErrorCode::kUNSUPPORTED_NODE);
infer_dim = i;
}
}
return Status::success();
}
void get_kernel_params(::ONNX_NAMESPACE::NodeProto const& onnx_node,
nvinfer1::DimsHW const& input_shape,
nvinfer1::DimsHW* kernel_size,
nvinfer1::DimsHW* strides,
nvinfer1::DimsHW* beg_padding,
nvinfer1::DimsHW* end_padding,
nvinfer1::DimsHW* dilations=nullptr,
nvinfer1::DimsHW const* output_shape=nullptr,
bool enable_padding_trick=true);
inline nvinfer1::ScaleMode get_scale_mode(nvinfer1::Dims const& weights_shape) {
if( weights_shape.nbDims == 1 ) {
if( weights_shape.d[0] == 1 ) {
return nvinfer1::ScaleMode::kUNIFORM;
} else {
return nvinfer1::ScaleMode::kCHANNEL;
}
} else {
return nvinfer1::ScaleMode::kELEMENTWISE;
}
}
inline void update_padded_values(std::vector<float>&pad_values, const nvinfer1::DimsHW beg_padding,
const nvinfer1::DimsHW end_padding, const nvinfer1::Dims padded_shape, const float pad_value)
{
int pad_h = padded_shape.d[1];
int pad_w = padded_shape.d[2];
int num_elements = pad_values.size();
// Handle H padding. First beg_padding.h * pad_w and last end_padding.h * pad_w
// elements need to be updated to pad_value
if (beg_padding.h() != 0)
{
int end = beg_padding.h() * pad_w;
for (int i = 0; i < end; i++)
{
pad_values[i] = pad_value;
}
}
if (end_padding.h() != 0)
{
for (int start = (pad_h - end_padding.h()) * pad_w;
start < num_elements; start++)
{
pad_values[start] = pad_value;
}
}
// Handle W padding. First beg_padding.w() and last end_padding.w()
// elements of each row needs to be updated to pad_value
if (beg_padding.w() != 0)
{
for (int h_dim = 0; h_dim < pad_h; h_dim++)
{
for (int w_dim = 0; w_dim < beg_padding.w(); w_dim++)
{
int row_base_index = h_dim*pad_h;
pad_values[row_base_index + w_dim] = pad_value;
}
}
}
if (end_padding.w() != 0)
{
for (int h_dim = 0; h_dim < pad_h; h_dim++)
{
for (int w_dim = pad_w - end_padding.w();
w_dim < pad_w; w_dim++)
{
int row_base_index = h_dim*pad_h;
pad_values[row_base_index + w_dim] = pad_value;
}
}
}
}
} // namespace onnx2trt