-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathbenchmark.cc
404 lines (378 loc) · 15.5 KB
/
benchmark.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
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
// 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 "fastdeploy/function/functions.h"
#include "flags.h"
#include "macros.h"
#include "option.h"
namespace vision = fastdeploy::vision;
namespace benchmark = fastdeploy::benchmark;
DEFINE_string(shapes, "1,3,224,224",
"Required, set input shape for model."
"default 1,3,224,224");
DEFINE_string(names, "DEFAULT", "Required, set input names for model.");
DEFINE_string(dtypes, "FP32",
"Required, set input dtypes for model."
"default FP32.");
DEFINE_string(trt_shapes, "1,3,224,224:1,3,224,224:1,3,224,224",
"Optional, set min/opt/max shape for trt/paddle_trt."
"default 1,3,224,224:1,3,224,224:1,3,224,224");
DEFINE_int32(batch, 1,
"Optional, set trt max batch size, "
"default 1");
DEFINE_bool(dump, false,
"Optional, whether to dump output tensors, "
"default false.");
DEFINE_bool(info, false,
"Optional, only check the input infos of model."
"default false.");
DEFINE_bool(diff, false,
"Optional, check the diff between two tensors."
"default false.");
DEFINE_string(tensors, "tensor_a.txt:tensor_b.txt",
"Optional, the paths to dumped tensors, "
"default tensor_a.txt:tensor_b.txt");
DEFINE_bool(mem, false,
"Optional, whether to force to collect memory info, "
"default false.");
DEFINE_int32(interval, -1,
"Optional, sampling interval for collect memory info, "
"default false.");
DEFINE_string(model_format, "PADDLE",
"Optional, set specific model format,"
"eg, PADDLE/ONNX/RKNN/TORCHSCRIPT/SOPHGO"
"default PADDLE.");
DEFINE_bool(disable_mkldnn, false,
"Optional, disable mkldnn for paddle backend. "
"default false.");
DEFINE_string(optimized_model_dir, "",
"Optional, set optimized model dir for lite."
"eg: model.opt.nb, "
"default ''");
DEFINE_bool(collect_trt_shape_by_device, false,
"Optional, whether collect trt shape by device. "
"default false.");
DEFINE_double(custom_tensor_value, 1.0,
"Optional, set the value for fd tensor, "
"default 1.0");
DEFINE_bool(collect_trt_shape_by_custom_tensor_value, false,
"Optional, whether collect trt shape by custom tensor value. "
"default false.");
#if defined(ENABLE_BENCHMARK)
static std::vector<int64_t> GetInt64Shape(const std::vector<int>& shape) {
std::vector<int64_t> new_shape;
new_shape.resize(shape.size());
for (int i = 0; i < shape.size(); ++i) {
new_shape[i] = static_cast<int64_t>(shape[i]);
}
return new_shape;
}
static fastdeploy::ModelFormat GetModelFormat(const std::string& model_format) {
if (model_format == "PADDLE") {
return fastdeploy::ModelFormat::PADDLE;
} else if (model_format == "ONNX") {
return fastdeploy::ModelFormat::ONNX;
} else if (model_format == "RKNN") {
return fastdeploy::ModelFormat::RKNN;
} else if (model_format == "TORCHSCRIPT") {
return fastdeploy::ModelFormat::TORCHSCRIPT;
} else if (model_format == "SOPHGO") {
return fastdeploy::ModelFormat::SOPHGO;
} else {
return fastdeploy::ModelFormat::PADDLE;
}
}
static void CheckTensorDiff(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
std::cout << "Check tensor diff ..." << std::endl;
std::vector<std::string> tensor_paths =
benchmark::ResultManager::SplitStr(FLAGS_tensors);
assert(tensor_paths.size() == 2);
fastdeploy::FDTensor tensor_a, tensor_b;
benchmark::ResultManager::LoadFDTensor(&tensor_a, tensor_paths[0]);
benchmark::ResultManager::LoadFDTensor(&tensor_b, tensor_paths[1]);
auto tensor_diff =
benchmark::ResultManager::CalculateDiffStatis(tensor_a, tensor_b);
std::cout << "Tensor diff: mean=" << tensor_diff.data.mean
<< ", max=" << tensor_diff.data.max
<< ", min=" << tensor_diff.data.min << std::endl;
}
static void RuntimeProfiling(int argc, char* argv[]) {
// Init runtime option
auto option = fastdeploy::RuntimeOption();
if (!CreateRuntimeOption(&option, argc, argv, true)) {
return;
}
if (FLAGS_disable_mkldnn) {
option.paddle_infer_option.enable_mkldnn = false;
}
std::unordered_map<std::string, std::string> config_info;
benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path,
&config_info);
UpdateBaseCustomFlags(config_info); // see flags.h
// Init log recorder
std::stringstream ss;
ss.precision(6);
// Memory resource moniter
int sampling_interval = FLAGS_interval >= 1
? FLAGS_interval
: std::stoi(config_info["sampling_interval"]);
benchmark::ResourceUsageMonitor resource_moniter(
sampling_interval, std::stoi(config_info["device_id"]));
// Check model path and model format
std::string model_name, params_name, config_name;
std::string model_file, params_file;
auto model_format = fastdeploy::ModelFormat::PADDLE;
if (FLAGS_model_file != "UNKNOWN") {
// Set model file/param/format via command line
if (FLAGS_model != "") {
model_file = FLAGS_model + sep + FLAGS_model_file;
params_file = FLAGS_model + sep + FLAGS_params_file;
} else {
model_file = FLAGS_model_file;
params_file = FLAGS_params_file;
}
model_format = GetModelFormat(FLAGS_model_format);
if (model_format == fastdeploy::ModelFormat::PADDLE &&
FLAGS_params_file == "") {
if (config_info["backend"] != "lite") {
std::cout << "[ERROR] params_file can not be empty for PADDLE"
<< " format, Please, set your custom params_file manually."
<< std::endl;
return;
} else {
std::cout << "[INFO] Will using the lite light api for: " << model_file
<< std::endl;
}
}
} else {
// Set model file/param/format via model dir (only support
// for Paddle model format now)
if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name,
&model_format, config_info, false)) {
return;
}
model_file = FLAGS_model + sep + model_name;
params_file = FLAGS_model + sep + params_name;
}
option.SetModelPath(model_file, params_file, model_format);
// Set opt model dir
if (config_info["backend"] == "lite") {
if (FLAGS_optimized_model_dir != "") {
option.paddle_lite_option.optimized_model_dir = FLAGS_optimized_model_dir;
} else {
option.paddle_lite_option.optimized_model_dir = FLAGS_model;
}
}
// Get input shapes/names/dtypes
std::vector<std::vector<int32_t>> input_shapes =
benchmark::ResultManager::GetInputShapes(FLAGS_shapes);
std::vector<std::string> input_names =
benchmark::ResultManager::GetInputNames(FLAGS_names);
std::vector<fastdeploy::FDDataType> input_dtypes =
benchmark::ResultManager::GetInputDtypes(FLAGS_dtypes);
// Set tensorrt shapes
if (config_info["backend"] == "paddle_trt") {
option.paddle_infer_option.collect_trt_shape = true;
option.paddle_infer_option.collect_trt_shape_by_device =
FLAGS_collect_trt_shape_by_device;
}
if (config_info["backend"] == "paddle_trt" ||
config_info["backend"] == "trt") {
option.trt_option.max_batch_size = FLAGS_batch;
std::vector<std::vector<int32_t>> trt_shapes =
benchmark::ResultManager::GetInputShapes(FLAGS_trt_shapes);
if (input_names[0] == "DEFAULT") {
std::cout << "Please set the input names for TRT/Paddle-TRT backend!"
<< std::endl;
return;
}
assert(input_names.size() == (trt_shapes.size() / 3));
for (int i = 0; i < input_shapes.size(); ++i) {
option.trt_option.SetShape(input_names[i], trt_shapes[i * 3],
trt_shapes[i * 3 + 1], trt_shapes[i * 3 + 2]);
// Set custom input data for collect trt shapes
if (FLAGS_collect_trt_shape_by_custom_tensor_value) {
int min_shape_num = std::accumulate(trt_shapes[i * 3].begin(),
trt_shapes[i * 3].end(), 1,
std::multiplies<int>());
int opt_shape_num = std::accumulate(trt_shapes[i * 3 + 1].begin(),
trt_shapes[i * 3 + 1].end(), 1,
std::multiplies<int>());
int max_shape_num = std::accumulate(trt_shapes[i * 3 + 2].begin(),
trt_shapes[i * 3 + 2].end(), 1,
std::multiplies<int>());
std::vector<float> min_input_data(min_shape_num, FLAGS_custom_tensor_value);
std::vector<float> opt_input_data(opt_shape_num, FLAGS_custom_tensor_value);
std::vector<float> max_input_data(max_shape_num, FLAGS_custom_tensor_value);
option.trt_option.SetInputData(input_names[i], min_input_data,
opt_input_data, max_input_data);
}
}
}
// Init runtime
fastdeploy::Runtime runtime;
if (!runtime.Init(option)) {
std::cout << "Initial Runtime failed!" << std::endl;
}
// Check default input names
if (input_names[0] == "DEFAULT") {
input_names.clear();
for (int i = 0; i < runtime.NumInputs(); ++i) {
input_names.push_back(runtime.GetInputInfo(i).name);
}
}
assert(runtime.NumInputs() == input_shapes.size());
assert(runtime.NumInputs() == input_names.size());
assert(runtime.NumInputs() == input_dtypes.size());
// Feed inputs, all values set as 1.
std::vector<fastdeploy::FDTensor> inputs(runtime.NumInputs());
for (int i = 0; i < inputs.size(); ++i) {
fastdeploy::function::Full(
FLAGS_custom_tensor_value, GetInt64Shape(input_shapes[i]),
&inputs[i], input_dtypes[i]);
inputs[i].name = input_names[i];
}
// Start memory resource moniter
if (config_info["collect_memory_info"] == "true" || FLAGS_mem) {
resource_moniter.Start();
}
// Run runtime profiling
std::vector<fastdeploy::FDTensor> outputs;
if (!runtime.Infer(inputs, &outputs)) {
std::cerr << "Failed to predict." << std::endl;
ss << "Runtime(ms): Failed" << std::endl;
if (config_info["collect_memory_info"] == "true") {
ss << "cpu_rss_mb: Failed" << std::endl;
ss << "gpu_rss_mb: Failed" << std::endl;
ss << "gpu_util: Failed" << std::endl;
resource_moniter.Stop();
}
benchmark::ResultManager::SaveBenchmarkResult(ss.str(),
config_info["result_path"]);
return;
}
double profile_time = runtime.GetProfileTime() * 1000.0;
std::cout << "Runtime(ms): " << profile_time << "ms." << std::endl;
ss << "Runtime(ms): " << profile_time << "ms." << std::endl;
// Collect memory info
if (config_info["collect_memory_info"] == "true" || FLAGS_mem) {
float cpu_mem = resource_moniter.GetMaxCpuMem();
float gpu_mem = resource_moniter.GetMaxGpuMem();
float gpu_util = resource_moniter.GetMaxGpuUtil();
std::cout << "cpu_rss_mb: " << cpu_mem << "MB." << std::endl;
ss << "cpu_rss_mb: " << cpu_mem << "MB." << std::endl;
std::cout << "gpu_rss_mb: " << gpu_mem << "MB." << std::endl;
ss << "gpu_rss_mb: " << gpu_mem << "MB." << std::endl;
std::cout << "gpu_util: " << gpu_util << std::endl;
ss << "gpu_util: " << gpu_util << "MB." << std::endl;
resource_moniter.Stop();
}
benchmark::ResultManager::SaveBenchmarkResult(ss.str(),
config_info["result_path"]);
// Dump output tensors
if (FLAGS_dump) {
for (int i = 0; i < outputs.size(); ++i) {
auto name_tokens =
benchmark::ResultManager::SplitStr(outputs[i].name, '/');
std::string out_name = name_tokens[0];
for (int j = 1; j < name_tokens.size(); ++j) {
out_name += "_";
out_name += name_tokens[j];
}
std::string out_file = config_info["backend"] + "_" + out_name + ".txt";
benchmark::ResultManager::SaveFDTensor(outputs[i], out_file);
outputs[i].PrintInfo();
std::cout << "Saved: " << out_file << std::endl;
}
}
}
static void showInputInfos(int argc, char* argv[]) {
auto option = fastdeploy::RuntimeOption();
if (!CreateRuntimeOption(&option, argc, argv, true)) {
return;
}
if (FLAGS_disable_mkldnn) {
option.paddle_infer_option.enable_mkldnn = false;
}
std::unordered_map<std::string, std::string> config_info;
benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path,
&config_info);
std::string model_name, params_name, config_name;
std::string model_file, params_file;
auto model_format = fastdeploy::ModelFormat::PADDLE;
if (FLAGS_model_file != "UNKNOWN") {
// Set model file/param/format via command line
if (FLAGS_model != "") {
model_file = FLAGS_model + sep + FLAGS_model_file;
params_file = FLAGS_model + sep + FLAGS_params_file;
} else {
model_file = FLAGS_model_file;
params_file = FLAGS_params_file;
}
model_format = GetModelFormat(FLAGS_model_format);
if (model_format == fastdeploy::ModelFormat::PADDLE &&
FLAGS_params_file == "") {
if (config_info["backend"] != "lite") {
std::cout << "[ERROR] params_file can not be empty for PADDLE"
<< " format, Please, set your custom params_file manually."
<< std::endl;
return;
} else {
std::cout << "[INFO] Will using the lite light api for: " << model_file
<< std::endl;
}
}
} else {
// Set model file/param/format via model dir (only support
// for Paddle model format now)
if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name,
&model_format, config_info, false)) {
return;
}
model_file = FLAGS_model + sep + model_name;
params_file = FLAGS_model + sep + params_name;
}
option.SetModelPath(model_file, params_file, model_format);
// Init runtime
fastdeploy::Runtime runtime;
if (!runtime.Init(option)) {
std::cout << "Initial Runtime failed!" << std::endl;
}
// Show input tensor infos
auto input_infos = runtime.GetInputInfos();
for (int i = 0; i < input_infos.size(); ++i) {
std::cout << input_infos[i] << std::endl;
}
}
#endif
int main(int argc, char* argv[]) {
#if defined(ENABLE_BENCHMARK)
google::SetVersionString("0.0.0");
google::SetUsageMessage(
"./benchmark -[info|diff|check|dump|mem] -model xxx -config_path xxx "
"-[shapes|dtypes|names|tensors] -[model_file|params_file|model_format]");
google::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_diff) {
CheckTensorDiff(argc, argv);
return 0;
} else if (FLAGS_info) {
showInputInfos(argc, argv);
return 0;
} else {
RuntimeProfiling(argc, argv);
return 0;
}
#endif
return 0;
}