-
Notifications
You must be signed in to change notification settings - Fork 545
/
ModelRefitter.cpp
418 lines (382 loc) · 15.8 KB
/
ModelRefitter.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
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include "ModelRefitter.hpp"
#include "ShapedWeights.hpp"
#include "onnxProtoUtils.hpp"
#include "toposort.hpp"
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <algorithm>
#include <sys/stat.h>
#include <unordered_map>
#include <vector>
namespace onnx2trt
{
namespace
{
void deserializeOnnxModelFile(char const* onnxModelFile, ::ONNX_NAMESPACE::ModelProto& onnx_model)
{
// Define S_ISREG macro for Windows
#if !defined(S_ISREG)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#endif
struct stat sb;
ONNXTRT_CHECK(!(stat(onnxModelFile, &sb) == 0 && !S_ISREG(sb.st_mode))
&& "Failed to parse the ONNX model; input is not a regular file.",
ErrorCode::kMODEL_DESERIALIZE_FAILED);
GOOGLE_PROTOBUF_VERIFY_VERSION;
bool const fileLoadSuccess = ParseFromFileAsBinary(&onnx_model, onnxModelFile);
ONNXTRT_CHECK(fileLoadSuccess && "Failed to parse the ONNX model!", ErrorCode::kMODEL_DESERIALIZE_FAILED);
}
} // anonymous namespace
std::unordered_set<std::string> ModelRefitter::getRefittableWeights()
{
int32_t numWeights = mRefitter->getAllWeights(0, nullptr);
std::vector<char const*> weightNames{static_cast<size_t>(numWeights)};
mRefitter->getAllWeights(numWeights, weightNames.data());
return std::unordered_set<std::string>{weightNames.begin(), weightNames.end()};
}
template <typename T, typename TConvertFunc>
size_t ModelRefitter::batchnormWeightRefitter(
::ONNX_NAMESPACE::NodeProto const& node, std::vector<ShapedWeights>& inputs, TConvertFunc&& f)
{
auto const& scale = inputs.at(0);
auto const& bias = inputs.at(1);
auto const& mean = inputs.at(2);
auto const& variance = inputs.at(3);
T const* const scaleValues = f(scale);
T const* const biasValues = f(bias);
T const* const meanValues = f(mean);
T const* const varianceValues = f(variance);
T eps = static_cast<T>(1e-5f);
for (auto const& attr : node.attribute())
{
if (attr.name() == "epsilon")
{
eps = static_cast<T>(attr.f());
break;
}
}
// Fold the weights together into a single bias and scale
int32_t const nbChannels = scale.shape.d[0];
ShapedWeights::DataType weightType = typeid(T).hash_code() == typeid(BFloat16).hash_code()
? ::ONNX_NAMESPACE::TensorProto::BFLOAT16
: (typeid(T).hash_code() == typeid(half_float::half).hash_code() ? ::ONNX_NAMESPACE::TensorProto::FLOAT16
: ::ONNX_NAMESPACE::TensorProto::FLOAT);
ShapedWeights combinedScale = mWeightsContext.createNamedTempWeights(
weightType, scale.shape, mBatchNormWeightNames, mBatchNormWeightSuffixCounter, /*batchNormNode=*/true);
ShapedWeights combinedBias = mWeightsContext.createNamedTempWeights(
weightType, bias.shape, mBatchNormWeightNames, mBatchNormWeightSuffixCounter, /*batchNormNode=*/true);
// Validate that all the weights have the same amount of values
bool allSame = scale.count() == bias.count() && mean.count() == scale.count() && variance.count() == scale.count()
&& combinedScale.count() == scale.count() && combinedBias.count() == scale.count();
ONNXTRT_CHECK(allSame && "Inputs to BatchNormalization must have the same shape!", ErrorCode::kREFIT_FAILED);
for (int32_t i = 0; i < nbChannels; ++i)
{
combinedScale.at<T>(i) = scaleValues[i] / sqrtf(varianceValues[i] + eps);
combinedBias.at<T>(i) = biasValues[i] - meanValues[i] * combinedScale.at<T>(i);
}
size_t successfullyRefittedWeights = 0;
if (refittableWeights.count(combinedScale.name))
{
refittableWeights.erase(combinedScale.name);
ONNXTRT_CHECK(
mRefitter->setNamedWeights(combinedScale.name, std::move(combinedScale)) && "Failed to set named weights",
ErrorCode::kREFIT_FAILED);
++successfullyRefittedWeights;
}
if (refittableWeights.count(combinedBias.name))
{
refittableWeights.erase(combinedBias.name);
ONNXTRT_CHECK(
mRefitter->setNamedWeights(combinedBias.name, std::move(combinedBias)) && "Failed to set named weights",
ErrorCode::kREFIT_FAILED);
++successfullyRefittedWeights;
}
return successfullyRefittedWeights;
}
//! Functor for extracting weights from ShapedWeights via cheap pointer cast to T*.
template <typename T>
class QuickCast
{
public:
T const* operator()(ShapedWeights const& w) const
{
return static_cast<T const*>(w.values);
};
};
void ModelRefitter::refitOnnxWeights(::ONNX_NAMESPACE::ModelProto const& onnx_model)
{
nestedDepth = 0;
successfullyRefittedWeights = 0;
size_t const numberOfWeightsToRefit = refittableWeights.size();
refitOnnxGraph(onnx_model.graph());
ONNXTRT_CHECK(successfullyRefittedWeights == numberOfWeightsToRefit && "Failed to refit all the weights.",
ErrorCode::kREFIT_FAILED);
}
void ModelRefitter::refitOnnxGraph(::ONNX_NAMESPACE::GraphProto const& graph)
{
for (::ONNX_NAMESPACE::TensorProto const& initializer : graph.initializer())
{
if (!refittableWeights.count(initializer.name()))
{
continue;
}
// Remove the weight name from the set as some initializers
// might have the same name across different nested constructs (e.g. IF nodes);
// the assumption is that those weights would have the same value
refittableWeights.erase(initializer.name());
if (refittedWeights.count(initializer.name()))
{
LOG_REFITTER_WARNING("Duplicate initializer name ("
<< initializer.name() << ") was found when processing the graph (" << graph.name()
<< "). The refit process would only work properly if both initializers have the same values.");
}
else
{
refittedWeights.insert(initializer.name());
}
ShapedWeights weights;
ONNXTRT_CHECK(mWeightsContext.convertOnnxWeights(initializer, &weights, /*ownAllWeights=*/true)
&& "Failed to import initializer.",
ErrorCode::kUNSUPPORTED_NODE);
ONNXTRT_CHECK(
mRefitter->setNamedWeights(initializer.name().c_str(), std::move(weights)) && "Failed to set named weights",
ErrorCode::kREFIT_FAILED);
++successfullyRefittedWeights;
}
std::vector<size_t> topoOrder;
ONNXTRT_CHECK(
toposort(graph.node(), &topoOrder) && "Failed to sort the model topologically.", ErrorCode::kINVALID_GRAPH);
for (auto const& nodeIdx : topoOrder)
{
::ONNX_NAMESPACE::NodeProto const& node = graph.node(nodeIdx);
refitOnnxNode(node, graph);
}
}
void ModelRefitter::refitOnnxNode(::ONNX_NAMESPACE::NodeProto const& node, ::ONNX_NAMESPACE::GraphProto const& graph)
{
// For nodes that contain subgraphs (Ifs, Loops, Scans),
// ensure that the recursion depth is limited to a set amount.
++nestedDepth;
static size_t const MAX_NESTED_SUBGRAPHS = 24;
ONNXTRT_CHECK((nestedDepth <= MAX_NESTED_SUBGRAPHS)
&& "ONNX graph contains nested structures that exceed the maximum allowed by TensorRT!",
ErrorCode::kUNSUPPORTED_GRAPH);
if (node.op_type() == "Constant")
{
refitOnnxConstantNode(node, graph.name());
}
else if (node.op_type() == "BatchNormalization")
{
refitOnnxBatchNormNode(node, graph);
}
else if (node.op_type() == "If")
{
refitOnnxIfNode(node);
}
else if (node.op_type() == "Loop")
{
refitOnnxLoopNode(node);
}
else if (node.op_type() == "Scan")
{
refitOnnxScanNode(node);
}
--nestedDepth;
}
void ModelRefitter::refitOnnxConstantNode(::ONNX_NAMESPACE::NodeProto const& node, std::string const& graphName)
{
if (!refittableWeights.count(node.output(0)))
{
return;
}
refittableWeights.erase(node.output(0));
if (refittedWeights.count(node.output(0)))
{
LOG_REFITTER_WARNING("Duplicate weight name name ("
<< node.output(0) << ") was found when processing the graph (" << graphName
<< "). The refit process would only work properly if both weights have the same values.");
}
else
{
refittedWeights.insert(node.output(0));
}
ShapedWeights weights;
::ONNX_NAMESPACE::AttributeProto const& nodeAttribute = node.attribute(0);
if (nodeAttribute.name() == "value_float")
{
weights = mWeightsContext.createTempWeights(::ONNX_NAMESPACE::TensorProto::FLOAT, {0, {}});
float value = nodeAttribute.f();
ONNXTRT_CHECK(weights.count() == 1 && "Failed to import Constant node.", ErrorCode::kUNSUPPORTED_NODE);
std::memcpy(weights.values, &value, sizeof(float));
}
else if (nodeAttribute.name() == "value_floats")
{
std::vector<float> values{nodeAttribute.floats().begin(), nodeAttribute.floats().end()};
int64_t valueSize = values.size();
weights = mWeightsContext.createTempWeights(::ONNX_NAMESPACE::TensorProto::FLOAT, {1, {valueSize}});
ONNXTRT_CHECK(
weights.count() == values.size() && "Failed to import Constant node.", ErrorCode::kUNSUPPORTED_NODE);
std::memcpy(weights.values, values.data(), weights.count() * sizeof(float));
}
else if (nodeAttribute.name() == "value_int")
{
weights = mWeightsContext.createTempWeights(::ONNX_NAMESPACE::TensorProto::INT64, {0, {}});
int64_t value = nodeAttribute.i();
ONNXTRT_CHECK(weights.count() == 1 && "Failed to import Constant node.", ErrorCode::kUNSUPPORTED_NODE);
std::memcpy(weights.values, &value, sizeof(int64_t));
}
else if (nodeAttribute.name() == "value_ints")
{
std::vector<int64_t> values{nodeAttribute.ints().begin(), nodeAttribute.ints().end()};
int64_t valueSize = values.size();
weights = mWeightsContext.createTempWeights(::ONNX_NAMESPACE::TensorProto::INT64, {1, {valueSize}});
ONNXTRT_CHECK(
weights.count() == values.size() && "Failed to import Constant node.", ErrorCode::kUNSUPPORTED_NODE);
std::memcpy(weights.values, values.data(), weights.count() * sizeof(int64_t));
}
else
{
::ONNX_NAMESPACE::TensorProto const& onnx_weights_tensor = nodeAttribute.t();
ONNXTRT_CHECK(
mWeightsContext.convertOnnxWeights(onnx_weights_tensor, &weights) && "Failed to import Constant node.",
ErrorCode::kUNSUPPORTED_NODE);
}
ONNXTRT_CHECK(
mRefitter->setNamedWeights(node.output(0).c_str(), std::move(weights)) && "Failed to set named weights",
ErrorCode::kREFIT_FAILED);
++successfullyRefittedWeights;
}
void ModelRefitter::refitOnnxBatchNormNode(
::ONNX_NAMESPACE::NodeProto const& node, ::ONNX_NAMESPACE::GraphProto const& graph)
{
ONNXTRT_CHECK(
node.input().size() == 5 && "BatchNorm node does not have five required inputs.", ErrorCode::kINVALID_NODE);
std::vector<ShapedWeights> batchNormInputs;
// The following looping construct is due to the fact that some tensors
// might be shared among the BatchNorm's inputs
std::vector<std::string> const inputNames(node.input().begin() + 1, node.input().end());
for (size_t inputIdx = 0; inputIdx < inputNames.size(); ++inputIdx)
{
for (::ONNX_NAMESPACE::TensorProto const& initializer : graph.initializer())
{
if (inputNames.at(inputIdx) == initializer.name())
{
ShapedWeights weights;
ONNXTRT_CHECK(
mWeightsContext.convertOnnxWeights(initializer, &weights) && "Failed to import initializer.",
ErrorCode::kUNSUPPORTED_NODE);
weights.name = initializer.name().c_str();
batchNormInputs.push_back(std::move(weights));
break;
}
}
}
// If some of the inputs to the BN node were not actual initializers,
// the weight folding logic from Parser is no longer applicable and
// we must have already refitted the weights directly in refitOnnxGraph()
if (batchNormInputs.size() < 4)
{
return;
}
size_t batchnormRefittedWeights{0};
auto const scaleType = batchNormInputs.at(0).type;
bool const typesEqual = scaleType == batchNormInputs.at(1).type && scaleType == batchNormInputs.at(2).type
&& scaleType == batchNormInputs.at(3).type;
if (typesEqual && scaleType == ::ONNX_NAMESPACE::TensorProto::FLOAT16)
{
batchnormRefittedWeights
= batchnormWeightRefitter<half_float::half>(node, batchNormInputs, QuickCast<half_float::half>());
}
else if (typesEqual && scaleType == ::ONNX_NAMESPACE::TensorProto::BFLOAT16)
{
batchnormRefittedWeights = batchnormWeightRefitter<BFloat16>(node, batchNormInputs, QuickCast<BFloat16>());
}
else
{
// Do calculations in FP32, possibly promoting/demoting arithmetic types of some operands.
batchnormRefittedWeights = batchnormWeightRefitter<float>(
node, batchNormInputs, [this](ShapedWeights const& w) { return mWeightsContext.getFP32Values(w); });
}
successfullyRefittedWeights += batchnormRefittedWeights;
}
void ModelRefitter::refitOnnxIfNode(::ONNX_NAMESPACE::NodeProto const& node)
{
size_t thenGraphOutputSize{};
size_t elseGraphOutputSize{};
for (auto const& attr : node.attribute())
{
if (attr.name() == "then_branch")
{
::ONNX_NAMESPACE::GraphProto const& thenGraph = static_cast<::ONNX_NAMESPACE::GraphProto const&>(attr.g());
refitOnnxGraph(thenGraph);
thenGraphOutputSize = thenGraph.output_size();
}
else if (attr.name() == "else_branch")
{
::ONNX_NAMESPACE::GraphProto const& elseGraph = static_cast<::ONNX_NAMESPACE::GraphProto const&>(attr.g());
refitOnnxGraph(elseGraph);
elseGraphOutputSize = elseGraph.output_size();
}
}
// Number of outputs are the same between the two branches.
ONNXTRT_CHECK(thenGraphOutputSize == elseGraphOutputSize
&& "then/else subgraphs within the IF node should have the same number of outputs",
ErrorCode::kREFIT_FAILED);
}
void ModelRefitter::refitOnnxLoopNode(::ONNX_NAMESPACE::NodeProto const& node)
{
::ONNX_NAMESPACE::GraphProto const& body = static_cast<::ONNX_NAMESPACE::GraphProto const&>(node.attribute(0).g());
refitOnnxGraph(body);
}
void ModelRefitter::refitOnnxScanNode(::ONNX_NAMESPACE::NodeProto const& node)
{
for (auto const& attr : node.attribute())
{
if (attr.name() == "body")
{
::ONNX_NAMESPACE::GraphProto const& body = static_cast<::ONNX_NAMESPACE::GraphProto const&>(attr.g());
refitOnnxGraph(body);
break;
}
}
}
bool ModelRefitter::refitFromBytes(
void const* serializedOnnxModel, size_t serializedOnnxModelSize, char const* modelPath) noexcept
{
ONNXTRT_TRY
{
if (modelPath)
{
// Keep track of the absolute path to the ONNX file.
mWeightsContext.setOnnxFileLocation(modelPath);
}
deserializeOnnxModel(serializedOnnxModel, serializedOnnxModelSize, &onnx_model);
refittableWeights = getRefittableWeights();
refitOnnxWeights(onnx_model);
return true;
}
ONNXTRT_CATCH_LOG(mLogger)
return false;
}
bool ModelRefitter::refitFromFile(char const* onnxModelFile) noexcept
{
ONNXTRT_TRY
{
// Keep track of the absolute path to the ONNX file.
mWeightsContext.setOnnxFileLocation(onnxModelFile);
deserializeOnnxModelFile(onnxModelFile, onnx_model);
refittableWeights = getRefittableWeights();
if (!refittableWeights.empty())
{
refitOnnxWeights(onnx_model);
}
return true;
}
ONNXTRT_CATCH_LOG(mLogger)
return false;
}
} // namespace onnx2trt