forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.cc
306 lines (286 loc) · 11.5 KB
/
converter.cc
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
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle2onnx/converter.h"
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include "paddle2onnx/mapper/exporter.h"
#include "paddle2onnx/optimizer/convert_fp32_to_fp16.h"
namespace paddle2onnx {
PADDLE2ONNX_DECL bool IsExportable(const char* model, const char* params,
int32_t opset_version,
bool auto_upgrade_opset, bool verbose,
bool enable_onnx_checker,
bool enable_experimental_op,
bool enable_optimize, CustomOp* ops,
int op_count, const char* deploy_backend) {
auto parser = PaddleParser();
if (!parser.Init(model, params)) {
return false;
}
paddle2onnx::ModelExporter me;
std::set<std::string> unsupported_ops;
if (!me.CheckIfOpSupported(parser, &unsupported_ops,
enable_experimental_op)) {
return false;
}
// Add custom operator information
if (ops != nullptr && op_count > 0) {
for (int i = 0; i < op_count; ++i) {
std::string op_name(ops[i].op_name, strlen(ops[i].op_name));
std::string export_op_name(ops[i].export_op_name,
strlen(ops[i].export_op_name));
if (export_op_name == "paddle2onnx_null") {
export_op_name = op_name;
}
me.custom_ops[op_name] = export_op_name;
}
}
if (me.GetMinOpset(parser, false) < 0) {
return false;
}
std::string calibration_str;
std::string onnx_model =
me.Run(parser, opset_version, auto_upgrade_opset, verbose,
enable_onnx_checker, enable_experimental_op, enable_optimize,
deploy_backend, &calibration_str);
if (onnx_model.empty()) {
P2OLogger(verbose) << "The exported ONNX model is invalid!" << std::endl;
return false;
}
if (parser.is_quantized_model && "tensorrt" == std::string(deploy_backend) &&
calibration_str.empty()) {
P2OLogger(verbose) << "Can not generate calibration cache for TensorRT "
"deploy backend when export quantize model."
<< std::endl;
return false;
}
return true;
}
PADDLE2ONNX_DECL bool IsExportable(const void* model_buffer, int model_size,
const void* params_buffer, int params_size,
int32_t opset_version,
bool auto_upgrade_opset, bool verbose,
bool enable_onnx_checker,
bool enable_experimental_op,
bool enable_optimize, CustomOp* ops,
int op_count, const char* deploy_backend) {
auto parser = PaddleParser();
if (!parser.Init(model_buffer, model_size, params_buffer, params_size)) {
return false;
}
paddle2onnx::ModelExporter me;
std::set<std::string> unsupported_ops;
if (!me.CheckIfOpSupported(parser, &unsupported_ops,
enable_experimental_op)) {
return false;
}
// Add custom operator information
if (ops != nullptr && op_count > 0) {
for (int i = 0; i < op_count; ++i) {
std::string op_name(ops[i].op_name, strlen(ops[i].op_name));
std::string export_op_name(ops[i].export_op_name,
strlen(ops[i].export_op_name));
if (export_op_name == "paddle2onnx_null") {
export_op_name = op_name;
}
me.custom_ops[op_name] = export_op_name;
}
}
if (me.GetMinOpset(parser, false) < 0) {
return false;
}
std::string calibration_str;
std::string onnx_model =
me.Run(parser, opset_version, auto_upgrade_opset, verbose,
enable_onnx_checker, enable_experimental_op, enable_optimize,
deploy_backend, &calibration_str);
if (onnx_model.empty()) {
P2OLogger(verbose) << "The exported ONNX model is invalid!" << std::endl;
return false;
}
if (parser.is_quantized_model && "tensorrt" == std::string(deploy_backend) &&
calibration_str.empty()) {
P2OLogger(verbose) << "Can not generate calibration cache for TensorRT "
"deploy backend when export quantize model."
<< std::endl;
return false;
}
return true;
}
PADDLE2ONNX_DECL bool Export(
const char* model, const char* params, char** out, int* out_size,
int32_t opset_version, bool auto_upgrade_opset, bool verbose,
bool enable_onnx_checker, bool enable_experimental_op, bool enable_optimize,
CustomOp* ops, int op_count, const char* deploy_backend,
char** calibration_cache, int* calibration_size, const char* external_file,
bool* save_external, bool export_fp16_model, char** disable_fp16_op_types,
int disable_fp16_op_types_count) {
auto parser = PaddleParser();
P2OLogger(verbose) << "Start to parsing Paddle model..." << std::endl;
if (!parser.Init(model, params)) {
P2OLogger(verbose) << "Paddle model parsing failed." << std::endl;
return false;
}
paddle2onnx::ModelExporter me;
// Add custom operator information
if (ops != nullptr && op_count > 0) {
for (int i = 0; i < op_count; ++i) {
std::string op_name(ops[i].op_name, strlen(ops[i].op_name));
std::string export_op_name(ops[i].export_op_name,
strlen(ops[i].export_op_name));
if (export_op_name == "paddle2onnx_null") {
export_op_name = op_name;
}
me.custom_ops[op_name] = export_op_name;
}
}
// Add disabled fp16 op information
std::vector<std::string> disable_op_types;
if (disable_fp16_op_types != nullptr && disable_fp16_op_types_count > 0) {
for (int i = 0; i < disable_fp16_op_types_count; ++i) {
std::string disable_op_type(disable_fp16_op_types[i],
strlen(disable_fp16_op_types[i]));
disable_op_types.push_back(disable_op_type);
}
}
std::string calibration_str;
std::string result = me.Run(
parser, opset_version, auto_upgrade_opset, verbose, enable_onnx_checker,
enable_experimental_op, enable_optimize, deploy_backend, &calibration_str,
external_file, save_external, export_fp16_model, disable_op_types);
if (result.empty()) {
P2OLogger(verbose) << "The exported ONNX model is invalid!" << std::endl;
return false;
}
if (parser.is_quantized_model && "tensorrt" == std::string(deploy_backend) &&
calibration_str.empty()) {
P2OLogger(verbose) << "Can not generate calibration cache for TensorRT "
"deploy backend when export quantize model."
<< std::endl;
return false;
}
*out_size = result.size();
*out = new char[*out_size]();
memcpy(*out, result.data(), *out_size);
if (calibration_str.size()) {
*calibration_size = calibration_str.size();
*calibration_cache = new char[*calibration_size]();
memcpy(*calibration_cache, calibration_str.data(), *calibration_size);
}
return true;
}
PADDLE2ONNX_DECL bool Export(
const void* model_buffer, int64_t model_size, const void* params_buffer,
int64_t params_size, char** out, int* out_size, int32_t opset_version,
bool auto_upgrade_opset, bool verbose, bool enable_onnx_checker,
bool enable_experimental_op, bool enable_optimize, CustomOp* ops,
int op_count, const char* deploy_backend, char** calibration_cache,
int* calibration_size, const char* external_file, bool* save_external,
bool export_fp16_model, char** disable_fp16_op_types,
int disable_fp16_op_types_count) {
auto parser = PaddleParser();
P2OLogger(verbose) << "Start to parsing Paddle model..." << std::endl;
if (!parser.Init(model_buffer, model_size, params_buffer, params_size)) {
P2OLogger(verbose) << "Paddle model parsing failed." << std::endl;
return false;
}
paddle2onnx::ModelExporter me;
// Add custom operator information
if (ops != nullptr && op_count > 0) {
for (int i = 0; i < op_count; ++i) {
std::string op_name(ops[i].op_name, strlen(ops[i].op_name));
std::string export_op_name(ops[i].export_op_name,
strlen(ops[i].export_op_name));
if (export_op_name == "paddle2onnx_null") {
export_op_name = op_name;
}
me.custom_ops[op_name] = export_op_name;
}
}
// Add disabled fp16 op information
std::vector<std::string> disable_op_types;
if (disable_fp16_op_types != nullptr && disable_fp16_op_types_count > 0) {
for (int i = 0; i < disable_fp16_op_types_count; ++i) {
std::string disable_op_type(disable_fp16_op_types[i],
strlen(disable_fp16_op_types[i]));
disable_op_types.push_back(disable_op_type);
}
}
std::string calibration_str;
std::string result = me.Run(
parser, opset_version, auto_upgrade_opset, verbose, enable_onnx_checker,
enable_experimental_op, enable_optimize, deploy_backend, &calibration_str,
external_file, save_external, export_fp16_model, disable_op_types);
if (result.empty()) {
P2OLogger(verbose) << "The exported ONNX model is invalid!" << std::endl;
return false;
}
if (parser.is_quantized_model && "tensorrt" == std::string(deploy_backend) &&
calibration_str.empty()) {
P2OLogger(verbose) << "Can not generate calibration cache for TensorRT "
"deploy backend when export quantize model."
<< std::endl;
return false;
}
*out_size = result.size();
*out = new char[*out_size]();
memcpy(*out, result.data(), *out_size);
if (calibration_str.size()) {
*calibration_size = calibration_str.size();
*calibration_cache = new char[*calibration_size]();
memcpy(*calibration_cache, calibration_str.data(), *calibration_size);
}
return true;
}
PADDLE2ONNX_DECL bool ConvertFP32ToFP16(const char* onnx_model, int model_size,
char** out_model, int* out_model_size) {
std::string onnx_proto(onnx_model, onnx_model + model_size);
ONNX_NAMESPACE::ModelProto model;
model.ParseFromString(onnx_proto);
P2OLogger(true) << "Convert FP32 ONNX model to FP16." << std::endl;
ConvertFp32ToFp16 convert;
convert.Convert(&model);
// save external data file for big model
std::string external_data_file;
if (model.ByteSizeLong() > INT_MAX) {
external_data_file = "external_data";
}
paddle2onnx::ModelExporter me;
if (external_data_file.size()) {
me.SaveExternalData(model.mutable_graph(), external_data_file);
}
// check model
me.ONNXChecker(model, true);
std::string result;
if (!model.SerializeToString(&result)) {
P2OLogger(true)
<< "Error happenedd while optimizing the exported ONNX model."
<< std::endl;
return false;
}
*out_model_size = result.size();
*out_model = new char[*out_model_size]();
memcpy(*out_model, result.data(), *out_model_size);
return true;
}
ModelTensorInfo::~ModelTensorInfo() {
if (shape != nullptr) {
delete[] shape;
shape = nullptr;
rank = 0;
}
}
} // namespace paddle2onnx