-
Notifications
You must be signed in to change notification settings - Fork 547
/
WeightsContext.cpp
529 lines (491 loc) · 18.5 KB
/
WeightsContext.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include "WeightsContext.hpp"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <limits>
namespace onnx2trt
{
void* WeightsContext::ownWeights(
void const* weightValues, const ShapedWeights::DataType dataType, nvinfer1::Dims const& shape, const size_t nBytes)
{
void* reservedWeights{createTempWeights(dataType, shape).values};
std::memcpy(reservedWeights, weightValues, nBytes);
return reservedWeights;
}
int32_t* WeightsContext::convertUINT8(uint8_t const* weightValues, nvinfer1::Dims const& shape)
{
int64_t const nbWeights = volume(shape);
int32_t* int32Weights{
static_cast<int32_t*>(createTempWeights(::ONNX_NAMESPACE::TensorProto::INT32, shape).values)};
for (int64_t i = 0; i < nbWeights; i++)
{
int32Weights[i] = static_cast<int32_t>(weightValues[i]);
}
return int32Weights;
}
float* WeightsContext::convertDouble(double const* weightValues, nvinfer1::Dims const& shape)
{
auto* ctx = this; // For logging macros.
int64_t const nbWeights = volume(shape);
float* floatWeights{
static_cast<float*>(createTempWeights(::ONNX_NAMESPACE::TensorProto::FLOAT, shape).values)};
bool outOfBounds{false};
double const floatMax = static_cast<double>(std::numeric_limits<float>::max());
double const floatMin = static_cast<double>(std::numeric_limits<float>::lowest());
for (int64_t i = 0; i < nbWeights; i++)
{
if (weightValues[i] > floatMax || weightValues[i] < floatMin)
{
floatWeights[i] = static_cast<float>(std::max(std::min(weightValues[i], floatMax), floatMin));
LOG_WARNING("Weight at index " << i << ": " << weightValues[i]
<< " is out of range. Clamping to: " << floatWeights[i]);
outOfBounds = true;
}
else
{
floatWeights[i] = static_cast<float>(weightValues[i]);
}
}
if (outOfBounds)
{
LOG_WARNING("One or more weights outside the range of FLOAT was clamped");
}
return floatWeights;
}
uint8_t* WeightsContext::convertPackedInt32Data(
int32_t const* weightValues, nvinfer1::Dims const& shape, size_t nbytes, int32_t onnxdtype)
{
uint8_t* newWeights{static_cast<uint8_t*>(createTempWeights(onnxdtype, shape).values)};
for (size_t i = 0; i < nbytes; i++)
{
newWeights[i] = static_cast<uint8_t>(weightValues[i]);
}
return newWeights;
}
// Helper function to validate size_t multiplications will not overflow
bool multiplicationWillOverflow(size_t const a, size_t const b)
{
if (b == 0)
{
return false;
}
if (a > std::numeric_limits<size_t>::max() / b)
{
return true;
}
return false;
}
// Helper function to ensure that a ONNX initializer is supportable by TensorRT.
bool validateOnnxInitializer(::ONNX_NAMESPACE::TensorProto const& onnxTensor)
{
// Validate type.
auto onnxDtype = onnxTensor.data_type();
auto typeSize = getDtypeSizeBits(onnxDtype);
if (typeSize == -1 || typeSize == 0)
{
return false;
}
// Validate rank.
auto nbDims = onnxTensor.dims().size();
if (nbDims > nvinfer1::Dims::MAX_DIMS)
{
return false;
}
// Validate volume is within bounds.
size_t vol = 1;
for (int32_t i = 0; i < nbDims; i++)
{
auto dimVal = onnxTensor.dims().Get(i);
if (dimVal == 0)
{
vol = 0;
break;
}
if (vol > std::numeric_limits<size_t>::max() / dimVal)
{
return false;
}
vol = vol * dimVal;
}
// Validate size in bytes is within bounds.
if (vol > std::numeric_limits<size_t>::max() / typeSize)
{
return false;
}
return true;
}
// Function to read bytes from an external file and return the data in a buffer.
bool WeightsContext::parseExternalWeights(
std::string const& file, int64_t offset, int64_t length, std::vector<char>& weightsBuf, size_t& size)
{
auto* ctx = this; // For logging macros.
// Accessing parent directories (i.e. ../) is not allowed. Normalize path first.
auto path = mOnnxFileLocation;
std::string normalizedFile = normalizePath(file);
bool illegalDir{false};
#ifdef _MSC_VER
illegalDir |= normalizedFile.find("..\\") != std::string::npos;
#endif
illegalDir |= normalizedFile.find("../") != std::string::npos;
if (illegalDir)
{
LOG_ERROR("Relative paths to parent (../) are not allowed in ONNX external weights! Normalized path is: "
<< normalizedFile);
return false;
}
// The weight paths in the ONNX model are relative paths to the main ONNX file.
#ifdef _MSC_VER
size_t slash = path.rfind("\\");
// When using WSL path can have "\" or "/". Need to check both options here.
if (slash == std::string::npos)
{
slash = path.rfind("/");
}
#else
size_t slash = path.rfind("/");
#endif
if (slash != std::string::npos)
{
path.replace(slash + 1, path.size() - (slash + 1), normalizedFile);
}
else
{
path = normalizedFile;
}
LOG_VERBOSE("Reading weights from external file: " << path);
std::ifstream relPathFile(path, std::ios::binary | std::ios::ate);
if (!relPathFile)
{
LOG_ERROR("Failed to open file: " << path);
return false;
}
std::streamsize fileSize = relPathFile.tellg();
relPathFile.seekg(offset, std::ios::beg);
int64_t weightsBufSize = length == 0 ? fileSize : length;
weightsBuf.resize(weightsBufSize);
if (!relPathFile.read(weightsBuf.data(), weightsBuf.size()))
{
LOG_ERROR("Failed to read weights from external file: " << path);
return false;
}
size = weightsBuf.size();
return true;
}
// Function to read data from an ONNX Tensor and move it into a ShapedWeights object. Handles external weights as well.
bool WeightsContext::convertOnnxWeights(
::ONNX_NAMESPACE::TensorProto const& onnxTensor, ShapedWeights* weights, bool ownAllWeights)
{
auto* ctx = this; // For logging macros.
// Sanity check for onnxTensors
if (!validateOnnxInitializer(onnxTensor))
{
LOG_ERROR("ONNX initializer " << onnxTensor.name() << " cannot be imported into TensorRT!");
return false;
}
void* dataPtr{nullptr};
size_t nbytes{0};
auto onnxDtype = onnxTensor.data_type();
nvinfer1::Dims shape{};
shape.nbDims = onnxTensor.dims().size();
std::copy_n(onnxTensor.dims().begin(), shape.nbDims, shape.d);
// ONNX weight values can be stored in either the TensorProto itself, or in an external file in the case
// of large models. Check for this here.
auto dataLocation = onnxTensor.data_location();
// External Data
if (dataLocation == 1)
{
std::string location{""};
int64_t offset{0};
int64_t length{0};
// onnxTensor.external_data() is a String : String map that holds metadata about how to read from an external
// file
for (auto onnxMapEntry : onnxTensor.external_data())
{
auto keyName = onnxMapEntry.key();
if (keyName == "location")
{
location = onnxMapEntry.value();
}
else if (keyName == "offset")
{
offset = std::atoll(onnxMapEntry.value().c_str());
}
else if (keyName == "length")
{
length = std::atoll(onnxMapEntry.value().c_str());
}
// Not used at the moment
else if (keyName == "checksum")
{
continue;
}
else
{
LOG_ERROR("Key value of: " << keyName << " was not expected!");
return false;
}
}
// Buffer to hold the data read from the file
std::vector<char> dataBuf;
// Will update dataBuf and nbytes by reference.
if (!parseExternalWeights(location, offset, length, dataBuf, nbytes))
{
return false;
}
// For weights parsed from external files, createTempWeights is necessary to keep them in scope
ShapedWeights externalWeights;
dataPtr = dataBuf.data();
// Cast non-native TRT types to their corresponding proxy types
if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::UINT8)
{
// Cast UINT8 weights to INT32.
dataPtr = convertUINT8(reinterpret_cast<uint8_t const*>(dataPtr), shape);
size_t const sizeOffset = sizeof(int32_t) / sizeof(uint8_t);
if (multiplicationWillOverflow(nbytes, sizeOffset))
{
return false;
}
nbytes = nbytes * sizeOffset;
onnxDtype = ::ONNX_NAMESPACE::TensorProto::INT32;
}
else if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::DOUBLE)
{
// Cast DOUBLE weights to FLOAT.
dataPtr = convertDouble(reinterpret_cast<double const*>(dataPtr), shape);
nbytes = nbytes / (sizeof(double) / sizeof(float));
onnxDtype = ::ONNX_NAMESPACE::TensorProto::FLOAT;
}
// Create the holder for external weights.
externalWeights = createTempWeights(onnxDtype, shape);
// Check if the size of external weights is as expected.
if (externalWeights.size_bytes() != nbytes)
{
LOG_ERROR("Unexpected size for the external weights! Expected size: "
<< externalWeights.size_bytes() << " bytes (shape = " << shape << "). Actual size: " << nbytes
<< " bytes.");
return false;
}
// Copy the weight values into externalWeights.
std::memcpy(externalWeights.values, dataPtr, nbytes);
*weights = externalWeights;
return true;
}
// Weights information is within the TensorProto itself
// Cast non-native TRT types to their corresponding proxy types
if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::UINT8)
{
onnxDtype = ::ONNX_NAMESPACE::TensorProto::INT32;
if (onnxTensor.raw_data().size() > 0)
{
dataPtr = convertUINT8(reinterpret_cast<uint8_t const*>(onnxTensor.raw_data().data()), shape);
size_t const sizeOffset = (sizeof(int32_t) / sizeof(uint8_t));
if (multiplicationWillOverflow(nbytes, sizeOffset))
{
return false;
}
nbytes = onnxTensor.raw_data().size() * sizeOffset;
}
else if (onnxTensor.int32_data().size() > 0)
{
dataPtr = (void*) onnxTensor.int32_data().data();
if (multiplicationWillOverflow(nbytes, sizeof(int32_t)))
{
return false;
}
nbytes = onnxTensor.int32_data().size() * sizeof(int32_t);
if (ownAllWeights)
{
dataPtr = ownWeights(dataPtr, onnxDtype, shape, nbytes);
}
}
}
else if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::DOUBLE)
{
if (onnxTensor.raw_data().size() > 0)
{
dataPtr = convertDouble(reinterpret_cast<double const*>(onnxTensor.raw_data().data()), shape);
nbytes = onnxTensor.raw_data().size() / (sizeof(double) / sizeof(float));
}
else if (onnxTensor.double_data().size() > 0)
{
dataPtr = convertDouble(onnxTensor.double_data().data(), shape);
if (multiplicationWillOverflow(nbytes, sizeof(float)))
{
return false;
}
nbytes = onnxTensor.double_data().size() * sizeof(float);
}
onnxDtype = ::ONNX_NAMESPACE::TensorProto::FLOAT;
}
// Check for supported types that can be found in the int32_data field in the TensorProto
// https://github.com/onnx/onnx/blob/609282efe8d4871f620141223139bbb99bdbe9f6/onnx/onnx.proto#L567
else if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::INT32 || onnxDtype == ::ONNX_NAMESPACE::TensorProto::INT64
|| onnxDtype == ::ONNX_NAMESPACE::TensorProto::FLOAT16 || onnxDtype == ::ONNX_NAMESPACE::TensorProto::BFLOAT16
|| onnxDtype == ::ONNX_NAMESPACE::TensorProto::INT8 || onnxDtype == ::ONNX_NAMESPACE::TensorProto::BOOL
|| onnxDtype == ::ONNX_NAMESPACE::TensorProto::INT4)
{
if (onnxTensor.raw_data().size() > 0)
{
dataPtr = (void*) (onnxTensor.raw_data().data());
nbytes = onnxTensor.raw_data().size();
if (ownAllWeights)
{
dataPtr = ownWeights(dataPtr, onnxDtype, shape, nbytes);
}
}
else
{
nbytes = getTensorOrWeightsSizeBytes(onnxTensor.int32_data().size(), onnxDtype);
switch (onnxDtype)
{
case ::ONNX_NAMESPACE::TensorProto::INT32:
dataPtr = (void*) (onnxTensor.int32_data().data());
if (ownAllWeights)
{
dataPtr = ownWeights(dataPtr, onnxDtype, shape, nbytes);
}
break;
case ::ONNX_NAMESPACE::TensorProto::INT64:
nbytes = getTensorOrWeightsSizeBytes(onnxTensor.int64_data().size(), onnxDtype);
dataPtr = (void*) (onnxTensor.int64_data().data());
if (ownAllWeights)
{
dataPtr = ownWeights(dataPtr, onnxDtype, shape, nbytes);
}
break;
case ::ONNX_NAMESPACE::TensorProto::FLOAT16:
case ::ONNX_NAMESPACE::TensorProto::BFLOAT16:
dataPtr = convertInt32Data<uint16_t>(onnxTensor.int32_data().data(), shape, onnxDtype);
break;
case ::ONNX_NAMESPACE::TensorProto::INT8:
dataPtr = convertInt32Data<int8_t>(onnxTensor.int32_data().data(), shape, onnxDtype);
break;
case ::ONNX_NAMESPACE::TensorProto::BOOL:
dataPtr = convertInt32Data<uint8_t>(onnxTensor.int32_data().data(), shape, onnxDtype);
break;
case ::ONNX_NAMESPACE::TensorProto::INT4:
// int4 data is packed, each int32 element contains one byte (two int4 nibbles)
nbytes = onnxTensor.int32_data().size();
dataPtr = convertPackedInt32Data(onnxTensor.int32_data().data(), shape, nbytes, onnxDtype);
break;
default:
LOG_ERROR("Found unsupported datatype (" << onnxDtype
<< ") when importing initializer: " << onnxTensor.name());
break;
}
}
}
else if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::FLOAT)
{
if (onnxTensor.raw_data().size() > 0)
{
dataPtr = (void*) (onnxTensor.raw_data().data());
nbytes = onnxTensor.raw_data().size();
}
else
{
dataPtr = (void*) (onnxTensor.float_data().data());
if (multiplicationWillOverflow(nbytes, sizeof(float)))
{
return false;
}
nbytes = onnxTensor.float_data().size() * sizeof(float);
}
if (ownAllWeights)
{
dataPtr = ownWeights(dataPtr, onnxDtype, shape, nbytes);
}
}
else if (onnxDtype == ::ONNX_NAMESPACE::TensorProto::FLOAT8E4M3FN)
{
if (onnxTensor.raw_data().size() > 0)
{
dataPtr = (void*) (onnxTensor.raw_data().data());
nbytes = onnxTensor.raw_data().size();
}
else
{
dataPtr = (void*) (onnxTensor.int32_data().data());
nbytes = onnxTensor.int32_data().size();
}
if (ownAllWeights)
{
dataPtr = ownWeights(dataPtr, onnxDtype, shape, nbytes);
}
}
else
{
LOG_ERROR("Found unsupported datatype (" << onnxDtype << ") when importing initializer: " << onnxTensor.name());
return false;
}
onnx2trt::ShapedWeights trt_weights(onnxDtype, dataPtr, shape);
// Sanity check that weights were converted properly
if (trt_weights.size_bytes() != nbytes)
{
LOG_ERROR("Size mismatch when importing initializer: " << onnxTensor.name() << ". Expected size: " << nbytes
<< " , actual size: " << trt_weights.size_bytes());
return false;
}
*weights = trt_weights;
return true;
}
float* WeightsContext::convertFP16Data(void* weightValues, nvinfer1::Dims const& shape)
{
int64_t const nbWeights = volume(shape);
float* newWeights{static_cast<float*>(createTempWeights(::ONNX_NAMESPACE::TensorProto::FLOAT, shape).values)};
half_float::half* tempValues = static_cast<half_float::half*>(weightValues);
for (int64_t i = 0; i < nbWeights; i++)
{
newWeights[i] = tempValues[i];
}
return newWeights;
}
float* WeightsContext::getFP32Values(ShapedWeights const& w)
{
assert((w.type == ::ONNX_NAMESPACE::TensorProto::FLOAT || w.type == ::ONNX_NAMESPACE::TensorProto::FLOAT16)
&& "Conversion only valid from FLOAT or FLOAT16");
return (w.type == ::ONNX_NAMESPACE::TensorProto::FLOAT) ? static_cast<float*>(w.values)
: convertFP16Data(w.values, w.shape);
}
ShapedWeights WeightsContext::createNamedTempWeights(ShapedWeights::DataType type, nvinfer1::Dims const& shape,
std::set<std::string>& namesSet, int64_t& suffixCounter, bool batchNormNode)
{
std::string const& name
= generateUniqueName(namesSet, suffixCounter, batchNormNode ? "tmp_batch_norm_weight" : "tmp_weight");
return createNamedWeights(type, shape, name);
}
ShapedWeights WeightsContext::createTempWeights(ShapedWeights::DataType type, nvinfer1::Dims const& shape)
{
ShapedWeights weights(type, nullptr, shape);
int64_t nbBytes = weights.size_bytes();
// For empty weights, keep the values as nullptr.
if (nbBytes == 0)
{
return weights;
}
void* ptr = operator new(nbBytes);
std::memset(ptr, 0, nbBytes);
mWeightBuffers.push_back(BufferPtr{ptr});
weights.values = ptr;
return weights;
}
ShapedWeights WeightsContext::createNamedWeights(ShapedWeights::DataType type, nvinfer1::Dims const& shape,
std::string const& name, std::set<std::string>* bufferedNames)
{
ShapedWeights weights = createTempWeights(type, shape);
if (bufferedNames)
{
bufferedNames->insert(name);
weights.setName((*bufferedNames->find(name)).c_str());
}
else
{
weights.setName(name.c_str());
}
return weights;
}
} // namespace onnx2trt