forked from onnx/onnx-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
303 lines (274 loc) · 12.4 KB
/
main.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
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include "NvOnnxParser.h"
#include "onnx_utils.hpp"
#include "common.hpp"
#include <onnx/optimizer/optimize.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <fstream>
#include <unistd.h> // For ::getopt
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <ctime>
#include <fcntl.h> // For ::open
#include <limits>
void print_usage() {
cout << "ONNX to TensorRT model parser" << endl;
cout << "Usage: onnx2trt onnx_model.pb" << "\n"
<< " [-o engine_file.trt] (output TensorRT engine)" << "\n"
<< " [-t onnx_model.pbtxt] (output ONNX text file without weights)" << "\n"
<< " [-T onnx_model.pbtxt] (output ONNX text file with weights)" << "\n"
<< " [-m onnx_model_out.pb] (output ONNX model)" << "\n"
<< " [-b max_batch_size (default 32)]" << "\n"
<< " [-w max_workspace_size_bytes (default 1 GiB)]" << "\n"
<< " [-d model_data_type_bit_depth] (32 => float32, 16 => float16)" << "\n"
<< " [-O passes] (optimize onnx model. Argument is a semicolon-separated list of passes)" << "\n"
<< " [-p] (list available optimization passes and exit)" << "\n"
<< " [-l] (list layers and their shapes)" << "\n"
<< " [-F] (optimize onnx model in fixed mode)" << "\n"
<< " [-v] (increase verbosity)" << "\n"
<< " [-q] (decrease verbosity)" << "\n"
<< " [-V] (show version information)" << "\n"
<< " [-h] (show help)" << endl;
}
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
std::string engine_filename;
std::string model_filename;
std::string text_filename;
std::string optimization_passes_string;
std::string full_text_filename;
size_t max_batch_size = 32;
size_t max_workspace_size = 1 << 30;
int model_dtype_nbits = 32;
int verbosity = (int)nvinfer1::ILogger::Severity::kWARNING;
bool optimize_model = false;
bool optimize_model_fixed = false;
bool print_optimization_passes_info = false;
bool print_layer_info = false;
int arg = 0;
while( (arg = ::getopt(argc, argv, "o:b:w:t:T:m:d:O:plgFvqVh")) != -1 ) {
switch (arg){
case 'o':
if( optarg ) { engine_filename = optarg; break; }
else { cerr << "ERROR: -o flag requires argument" << endl; return -1; }
case 'm':
if( optarg ) { model_filename = optarg; break; }
else { cerr << "ERROR: -m flag requires argument" << endl; return -1; }
case 't':
if( optarg ) { text_filename = optarg; break; }
else { cerr << "ERROR: -t flag requires argument" << endl; return -1; }
case 'T':
if( optarg ) { full_text_filename = optarg; break; }
else { cerr << "ERROR: -T flag requires argument" << endl; return -1; }
case 'b':
if( optarg ) { max_batch_size = atoll(optarg); break; }
else { cerr << "ERROR: -b flag requires argument" << endl; return -1; }
case 'w':
if( optarg ) { max_workspace_size = atoll(optarg); break; }
else { cerr << "ERROR: -w flag requires argument" << endl; return -1; }
case 'd':
if( optarg ) { model_dtype_nbits = atoi(optarg); break; }
else { cerr << "ERROR: -d flag requires argument" << endl; return -1; }
case 'O':
optimize_model = true;
if( optarg ) { optimization_passes_string = optarg; break; }
else { cerr << "ERROR: -O flag requires argument" << endl; return -1; }
case 'p': print_optimization_passes_info = true; break;
case 'l': print_layer_info = true; break;
case 'F': optimize_model_fixed = true; optimize_model = true; break;
case 'v': ++verbosity; break;
case 'q': --verbosity; break;
case 'V': common::print_version(); return 0;
case 'h': print_usage(); return 0;
}
}
std::vector<std::string> optimizationPassNames;
if(optimize_model || print_optimization_passes_info) {
optimizationPassNames = ::ONNX_NAMESPACE::optimization::GetAvailablePasses();
}
if(print_optimization_passes_info) {
cout << "Available optimization passes are:" << endl;
for( auto it = optimizationPassNames.begin(); it != optimizationPassNames.end(); it++ )
{
cout << " " << it->c_str() << endl;
}
return 0;
}
int num_args = argc - optind;
if( num_args != 1 ) {
print_usage();
return -1;
}
std::string onnx_filename = argv[optind];
nvinfer1::DataType model_dtype;
if( model_dtype_nbits == 32 ) { model_dtype = nvinfer1::DataType::kFLOAT; }
else if( model_dtype_nbits == 16 ) { model_dtype = nvinfer1::DataType::kHALF; }
//else if( model_dtype_nbits == 8 ) { model_dtype = nvinfer1::DataType::kINT8; }
else {
cerr << "ERROR: Invalid model data type bit depth: " << model_dtype_nbits << endl;
return -2;
}
if (!std::ifstream(onnx_filename.c_str())) {
cerr << "Input file not found: " << onnx_filename << endl;
return -3;
}
::ONNX_NAMESPACE::ModelProto _the_onnx_model;
::ONNX_NAMESPACE::ModelProto& onnx_model = _the_onnx_model;
bool is_binary = common::ParseFromFile_WAR(&onnx_model, onnx_filename.c_str());
if( !is_binary && !common::ParseFromTextFile(&onnx_model, onnx_filename.c_str()) ) {
cerr << "Failed to parse ONNX model" << endl;
return -3;
}
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
int64_t opset_version = (onnx_model.opset_import().size() ?
onnx_model.opset_import(0).version() : 0);
cout << "----------------------------------------------------------------" << endl;
cout << "Input filename: " << onnx_filename << endl;
cout << "ONNX IR version: " << common::onnx_ir_version_string(onnx_model.ir_version()) << endl;
cout << "Opset version: " << opset_version << endl;
cout << "Producer name: " << onnx_model.producer_name() << endl;
cout << "Producer version: " << onnx_model.producer_version() << endl;
cout << "Domain: " << onnx_model.domain() << endl;
cout << "Model version: " << onnx_model.model_version() << endl;
cout << "Doc string: " << onnx_model.doc_string() << endl;
cout << "----------------------------------------------------------------" << endl;
}
if( onnx_model.ir_version() > ::ONNX_NAMESPACE::IR_VERSION ) {
cerr << "WARNING: ONNX model has a newer ir_version ("
<< common::onnx_ir_version_string(onnx_model.ir_version())
<< ") than this parser was built against ("
<< common::onnx_ir_version_string(::ONNX_NAMESPACE::IR_VERSION) << ")." << endl;
}
if( !model_filename.empty() ) {
if( optimize_model ) {
std::vector<std::string> passes;
std::string curPass;
std::stringstream passStream(optimization_passes_string);
while( std::getline(passStream, curPass, ';') ) {
if( std::find(optimizationPassNames.begin(), optimizationPassNames.end(), curPass) != optimizationPassNames.end() ) {
passes.push_back(curPass);
}
}
if( !passes.empty() ) {
cout << "Optimizing '" << model_filename << "'" << endl;
::ONNX_NAMESPACE::ModelProto _the_onnx_model_optimized = optimize_model_fixed
? ::ONNX_NAMESPACE::optimization::OptimizeFixed(onnx_model, passes)
: ::ONNX_NAMESPACE::optimization::Optimize(onnx_model, passes);
onnx_model = _the_onnx_model_optimized;
}
}
if( !common::MessageToFile( &onnx_model, model_filename.c_str() ) ) {
cerr << "ERROR: Problem writing ONNX model" << endl;
}
}
if( !text_filename.empty() ) {
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
cout << "Writing ONNX model (without weights) as text to " << text_filename << endl;
}
std::ofstream onnx_text_file(text_filename.c_str());
std::string onnx_text = pretty_print_onnx_to_string(onnx_model);
onnx_text_file.write(onnx_text.c_str(), onnx_text.size());
}
if( !full_text_filename.empty() ) {
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
cout << "Writing ONNX model (with weights) as text to " << full_text_filename << endl;
}
std::string full_onnx_text;
google::protobuf::TextFormat::PrintToString(onnx_model, &full_onnx_text);
std::ofstream full_onnx_text_file(full_text_filename.c_str());
full_onnx_text_file.write(full_onnx_text.c_str(), full_onnx_text.size());
}
const auto explicitBatch = 1U << static_cast<uint32_t>(nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
common::TRT_Logger trt_logger((nvinfer1::ILogger::Severity)verbosity);
auto trt_builder = common::infer_object(nvinfer1::createInferBuilder(trt_logger));
auto trt_network = common::infer_object(trt_builder->createNetworkV2(explicitBatch));
auto trt_parser = common::infer_object(nvonnxparser::createParser(
*trt_network, trt_logger));
// TODO: Fix this for the new API
//if( print_layer_info ) {
// parser->setLayerInfoStream(&std::cout);
//}
(void)print_layer_info;
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
cout << "Parsing model" << endl;
}
{
std::ifstream onnx_file(onnx_filename.c_str(),
std::ios::binary | std::ios::ate);
std::streamsize file_size = onnx_file.tellg();
onnx_file.seekg(0, std::ios::beg);
std::vector<char> onnx_buf(file_size);
if( !onnx_file.read(onnx_buf.data(), onnx_buf.size()) ) {
cerr << "ERROR: Failed to read from file " << onnx_filename << endl;
return -4;
}
if( !trt_parser->parse(onnx_buf.data(), onnx_buf.size()) ) {
int nerror = trt_parser->getNbErrors();
for( int i=0; i<nerror; ++i ) {
nvonnxparser::IParserError const* error = trt_parser->getError(i);
if( error->node() != -1 ) {
::ONNX_NAMESPACE::NodeProto const& node =
onnx_model.graph().node(error->node());
cerr << "While parsing node number " << error->node()
<< " [" << node.op_type();
if( node.output().size() ) {
cerr << " -> \"" << node.output(0) << "\"";
}
cerr << "]:" << endl;
if( verbosity >= (int)nvinfer1::ILogger::Severity::kINFO ) {
cerr << "--- Begin node ---" << endl;
cerr << node << endl;
cerr << "--- End node ---" << endl;
}
}
cerr << "ERROR: "
<< error->file() << ":" << error->line()
<< " In function " << error->func() << ":\n"
<< "[" << static_cast<int>(error->code()) << "] " << error->desc()
<< endl;
}
return -5;
}
}
bool fp16 = trt_builder->platformHasFastFp16();
if( !engine_filename.empty() ) {
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
cout << "Building TensorRT engine, FP16 available:"<< fp16 << endl;
cout << " Max batch size: " << max_batch_size << endl;
cout << " Max workspace size: " << max_workspace_size / (1024. * 1024) << " MiB" << endl;
}
auto builder_config = common::infer_object(trt_builder->createBuilderConfig());
builder_config->setMaxWorkspaceSize(max_workspace_size);
if( fp16 && model_dtype == nvinfer1::DataType::kHALF) {
builder_config->setFlag(nvinfer1::BuilderFlag::kFP16);
} else if( model_dtype == nvinfer1::DataType::kINT8 ) {
// TODO: Int8 support
//trt_builder->setInt8Mode(true);
cerr << "ERROR: Int8 mode not yet supported" << endl;
return -5;
}
auto trt_engine = common::infer_object(trt_builder->buildEngineWithConfig(*trt_network.get(), *builder_config.get()));
auto engine_plan = common::infer_object(trt_engine->serialize());
std::ofstream engine_file(engine_filename.c_str());
if (!engine_file) {
cerr << "Failed to open output file for writing: "
<< engine_filename << endl;
return -6;
}
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
cout << "Writing TensorRT engine to " << engine_filename << endl;
}
engine_file.write((char*)engine_plan->data(), engine_plan->size());
engine_file.close();
}
if( verbosity >= (int)nvinfer1::ILogger::Severity::kWARNING ) {
cout << "All done" << endl;
}
return 0;
}