-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrtexec.cpp
318 lines (299 loc) · 10.8 KB
/
Trtexec.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
#include "Trtexec.h"
bool TrtExec::parseOnnxModel()
{
// const char inputName[10] = "input";
EMoiUniquePtr<nvinfer1::IBuilder> builder{nvinfer1::createInferBuilder(iELogger)};
// We need to define explicit batch
const auto explicitBatch = 1U << static_cast<uint32_t>(nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
EMoiUniquePtr<nvinfer1::INetworkDefinition> prediction_network{builder->createNetworkV2(explicitBatch)};
// EMoiUniquePtr< nvinfer1::INetworkDefinition > network{builder->createNetwork()};
EMoiUniquePtr<nvonnxparser::IParser> parser{nvonnxparser::createParser(*prediction_network, iELogger)};
// parse ONNX
if (!parser->parseFromFile(info.onnx_dir.c_str(), static_cast<int>(nvinfer1::ILogger::Severity::kINFO)))
{
ELOG(ERROR) << "ERROR: could not parse the model.";
return false;
}
EMoiUniquePtr<nvinfer1::IBuilderConfig> config{builder->createBuilderConfig()};
if (!config)
{
ELOG(ERROR) << "Create builder config failed.";
return false;
}
config->setFlag(nvinfer1::BuilderFlag::kFP16);
// allow TensorRT to use up to 1GB of GPU memory for tactic selection.
config->setMaxWorkspaceSize(info.workspace);
if (info.dynamicOnnx)
{
builder->setMaxBatchSize(info.maxBatchSize);
// generate TensorRT engine optimized for the target platform
nvinfer1::IOptimizationProfile *profileCalib = builder->createOptimizationProfile();
// We do not need to check the return of setDimension and setCalibrationProfile here as all dims are explicitly set
profileCalib->setDimensions(info.inputName.c_str(), nvinfer1::OptProfileSelector::kMIN, nvinfer1::Dims4{info.minBatchSize, info.minImageChannel, info.minImageHeight, info.minImageWidth});
profileCalib->setDimensions(info.inputName.c_str(), nvinfer1::OptProfileSelector::kOPT, nvinfer1::Dims4{info.optBatchSize, info.optImageChannel, info.optImageHeight, info.optImageWidth});
profileCalib->setDimensions(info.inputName.c_str(), nvinfer1::OptProfileSelector::kMAX, nvinfer1::Dims4{info.maxBatchSize, info.maxImageChannel, info.maxImageHeight, info.maxImageWidth});
config->addOptimizationProfile(profileCalib);
}
else
{
builder->setMaxBatchSize(1);
}
this->prediction_engine.reset(builder->buildEngineWithConfig(*prediction_network, *config));
this->prediction_context.reset(this->prediction_engine->createExecutionContext());
return true;
}
bool TrtExec::saveEngine(const std::string &fileName)
{
std::ofstream engineFile(fileName, std::ios::binary);
if (!engineFile)
{
ELOG(ERROR) << "Cannot open engine file: " << fileName;
return false;
}
EMoiUniquePtr<nvinfer1::IHostMemory> serializedEngine{this->prediction_engine->serialize()};
if (serializedEngine == nullptr)
{
ELOG(ERROR) << "Engine serialization failed";
return false;
}
engineFile.write(static_cast<char *>(serializedEngine->data()), serializedEngine->size());
return !engineFile.fail();
}
bool TrtExec::loadEngine(const std::string &fileName)
{
std::ifstream engineFile(fileName, std::ios::binary);
if (!engineFile)
{
ELOG(ERROR) << "Cannot open engine file: " << fileName;
return false;
}
engineFile.seekg(0, std::ifstream::end);
int64_t fsize = engineFile.tellg();
engineFile.seekg(0, std::ifstream::beg);
std::vector<char> engineData(fsize);
engineFile.read(engineData.data(), fsize);
if (!engineFile.good())
{
ELOG(ERROR) << "Error loading engine file";
return false;
}
EMoiUniquePtr<nvinfer1::IRuntime> runtime{nvinfer1::createInferRuntime(iELogger.getTRTLogger())};
this->prediction_engine.reset(runtime->deserializeCudaEngine(engineData.data(), fsize, nullptr));
this->prediction_context.reset(this->prediction_engine->createExecutionContext());
this->maxBatchSize = this->prediction_engine->getMaxBatchSize();
return this->prediction_engine != nullptr;
}
int32_t TrtExec::getNbBindings()
{
return this->prediction_engine->getNbBindings();
}
nvinfer1::Dims TrtExec::getBindingDimensions(int32_t bindingIndex)
{
return this->prediction_engine->getBindingDimensions(bindingIndex);
}
nvinfer1::DataType TrtExec::getBindingDataType(int32_t bindingIndex)
{
return this->prediction_engine->getBindingDataType(bindingIndex);
}
int TrtExec::getMaxBatchSize()
{
return this->prediction_engine->getMaxBatchSize();
}
bool TrtExec::clearBuffer(bool freeInput, bool freeOutput)
{
this->prediction_input_dims.clear();
this->prediction_output_dims.clear();
try
{
if (freeInput)
for (void *buf : input_buffers)
cudaFree(buf);
if (freeOutput)
for (void *buf : output_buffers)
cudaFree(buf);
}
catch (std::runtime_error &e)
{
ELOG(ERROR) << e.what() << std::endl;
return false;
}
input_buffers.clear();
output_buffers.clear();
// TODO: Properly re wrote this
return true;
}
void ShowHelpAndExit(const char *szBadOption = NULL)
{
bool bThrowError = false;
std::ostringstream oss;
if (szBadOption)
{
bThrowError = true;
oss << "Error parsing \"" << szBadOption << "\"" << std::endl;
}
oss << "Options:" << std::endl
<< " --onnx [PATH] : path to Onnx file" << std::endl
<< " --engine [PATH] : name of output Engine file" << std::endl
<< " --dynamicOnnx : indicate that build engine with Dynamic Batch Size" << std::endl
<< " --minShape [BxCxHxW]: min input shape" << std::endl
<< " --optShape [BxCxHxW]: optimization input shape" << std::endl
<< " --maxShape [BxCxHxW]: max input shape" << std::endl
<< " --workspace [Int] : max workspace size in MB" << std::endl;
oss << std::endl;
if (bThrowError)
throw std::invalid_argument(oss.str());
else
std::cout << oss.str();
}
bool ParseCommandLine(int argc, char *argv[], OnnxParserConfig &config)
{
if (argc <= 1)
{
ShowHelpAndExit();
return false;
}
for (int i = 1; i < argc; i++)
{
if (std::string(argv[i]) == std::string("--help"))
{
ShowHelpAndExit();
return false;
}
else if (std::string(argv[i]) == std::string("--onnx"))
{
if (++i == argc)
{
ShowHelpAndExit("--onnx");
return false;
}
else
config.onnx_dir = std::string(argv[i]);
continue;
}
else if (std::string(argv[i]) == std::string("--engine"))
{
if (++i == argc)
{
ShowHelpAndExit("--engine");
return false;
}
else
config.engine_dir = std::string(argv[i]);
continue;
}
else if (std::string(argv[i]) == std::string("--inputName"))
{
if (++i == argc)
{
ShowHelpAndExit("--inputName");
return false;
}
else
config.inputName = std::string(argv[i]);
continue;
}
else if (std::string(argv[i]) == std::string("--dynamicOnnx"))
{
config.dynamicOnnx = true;
}
else if (std::string(argv[i]) == std::string("--minShape"))
{
if (++i == argc)
{
ShowHelpAndExit("--minShape");
return false;
}
else
{
std::stringstream minShape{argv[i]};
std::vector<std::string> result;
std::string item;
while (getline(minShape, item, 'x'))
{
result.push_back(item);
}
if (result.size() != 4)
{
ShowHelpAndExit("--minShape");
return false;
}
config.minBatchSize = std::atoi(result.at(0).c_str());
config.minImageChannel = std::atoi(result.at(1).c_str());
config.minImageHeight = std::atoi(result.at(2).c_str());
config.minImageWidth = std::atoi(result.at(3).c_str());
}
continue;
}
else if (std::string(argv[i]) == std::string("--optShape"))
{
if (++i == argc)
{
ShowHelpAndExit("--optShape");
return false;
}
else
{
std::stringstream optShape{argv[i]};
std::vector<std::string> result;
std::string item;
while (getline(optShape, item, 'x'))
result.push_back(item);
if (result.size() != 4)
{
ShowHelpAndExit("--optShape");
return false;
}
config.optBatchSize = std::atoi(result.at(0).c_str());
config.optImageChannel = std::atoi(result.at(1).c_str());
config.optImageHeight = std::atoi(result.at(2).c_str());
config.optImageWidth = std::atoi(result.at(3).c_str());
}
continue;
}
else if (std::string(argv[i]) == std::string("--maxShape"))
{
if (++i == argc)
{
ShowHelpAndExit("--maxShape");
return false;
}
else
{
std::stringstream maxShape{argv[i]};
std::vector<std::string> result;
std::string item;
while (getline(maxShape, item, 'x'))
result.push_back(item);
if (result.size() != 4)
{
ShowHelpAndExit("--maxShape");
return false;
}
config.maxBatchSize = std::atoi(result.at(0).c_str());
config.maxImageChannel = std::atoi(result.at(1).c_str());
config.maxImageHeight = std::atoi(result.at(2).c_str());
config.maxImageWidth = std::atoi(result.at(3).c_str());
}
continue;
}
else if (std::string(argv[i]) == std::string("--workspace"))
{
if (++i == argc)
{
ShowHelpAndExit("--workspace");
return false;
}
else
config.workspace = std::stoi(argv[i]) * (1ULL << 20);
continue;
}
else
{
{
ShowHelpAndExit((std::string("input not include ") + std::string(argv[i])).c_str());
return false;
}
}
}
return true;
}