forked from huawei-noah/bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
188 lines (169 loc) · 7.67 KB
/
benchmark.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
// Copyright (C) 2019. Huawei Technologies Co., Ltd. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <iostream>
#include <getopt.h>
#include "inference.hpp"
#include "data_loader.hpp"
char *modelPath = (char *)"";
std::string inputData = "";
char *affinityPolicyName = (char *)"CPU_AFFINITY_HIGH_PERFORMANCE";
char *algorithmMapPath = (char *)"";
int loopTime = 1;
int warmUp = 10;
void print_benchmark_usage()
{
std::cout << "benchmark usage: (<> must be filled in with exact value; [] is optional)\n"
"./benchmark -m <boltModelPath> -i [inputDataPath] -a [affinityPolicyName] -p "
"[algorithmMapPath] -l [loopTime]\n"
"\nParameter description:\n"
"1. -m <boltModelPath>: The path where .bolt is stored.\n"
"2. -i [inputDataPath]: The input data absolute path. If not input the option, "
"benchmark will run with fake data.\n"
"3. -a [affinityPolicyName]: The affinity policy. If not input the option, "
"affinityPolicyName is CPU_AFFINITY_HIGH_PERFORMANCE.Or you can only choose one "
"of {CPU_AFFINITY_HIGH_PERFORMANCE, CPU_AFFINITY_LOW_POWER, GPU}.\n"
"4. -p [algorithmMapPath]: The algorithm configration path.\n"
"5. -l [loopTime]: The running loopTimes.\n"
"6. -w [warmUp]: WarmUp times. The default value is 10.\n"
"Example: ./benchmark -m /local/models/resnet50_f16.bolt"
<< std::endl;
}
void parse_options(int argc, char *argv[])
{
std::cout << "\nPlease enter this command './benchmark --help' to get more usage "
"information.\n";
std::vector<std::string> lineArgs(argv, argv + argc);
for (std::string arg : lineArgs) {
if (arg == "--help" || arg == "-help" || arg == "--h" || arg == "-h") {
print_benchmark_usage();
exit(-1);
}
}
int option;
const char *optionstring = "m:i:a:p:l:w:";
while ((option = getopt(argc, argv, optionstring)) != -1) {
switch (option) {
case 'm':
std::cout << "option is -m <boltModelPath>, value is: " << optarg << std::endl;
modelPath = optarg;
break;
case 'i':
std::cout << "option is -i [inputDataPath], value is: " << optarg << std::endl;
inputData = std::string(optarg);
break;
case 'a':
std::cout << "option is -a [affinityPolicyName], value is: " << optarg << std::endl;
affinityPolicyName = optarg;
break;
case 'p':
std::cout << "option is -p [algorithmMapPath], value is: " << optarg << std::endl;
algorithmMapPath = optarg;
break;
case 'l':
std::cout << "option is -l [loopTime], value is: " << optarg << std::endl;
loopTime = atoi(optarg);
break;
case 'w':
std::cout << "option is -w [warmUp], value is: " << optarg << std::endl;
warmUp = atoi(optarg);
break;
default:
std::cout << "Input option gets error, please check the params meticulously.\n";
print_benchmark_usage();
exit(-1);
}
}
}
std::map<std::string, std::shared_ptr<U8>> create_tensors_from_path(
std::string dataPath, std::shared_ptr<CNN> pipeline)
{
std::vector<std::string> inputNames = pipeline->get_model_input_tensor_names();
std::map<std::string, std::shared_ptr<Tensor>> inMap = pipeline->get_inputs();
std::vector<DataType> sourceDataTypes;
std::vector<TensorDesc> inputDescs;
for (int i = 0; i < (int)(inputNames.size()); i++) {
std::string curName = inputNames[i];
TensorDesc curDesc = (*(inMap[curName])).get_desc();
std::cout << "Input Tensor Dimension: " << tensorDesc2Str(curDesc) << std::endl;
sourceDataTypes.push_back(curDesc.dt);
inputDescs.push_back(curDesc);
}
std::vector<Tensor> input;
if (string_end_with(inputData, ".txt")) {
input = load_txt(inputData, inputDescs);
} else {
input = load_bin(inputData, sourceDataTypes, inputDescs);
}
std::map<std::string, std::shared_ptr<U8>> model_tensors_input;
for (U32 index = 0; index < inputNames.size(); index++) {
model_tensors_input[inputNames[index]] =
((CpuMemory *)input[index].get_memory())->get_shared_ptr();
}
return model_tensors_input;
}
void print_result(std::map<std::string, std::shared_ptr<Tensor>> outMap)
{
std::cout << "\n\nBenchmark Result:\n";
int outputIndex = 0;
for (auto iter : outMap) {
Tensor result = *(iter.second);
std::cout << "Output Tensor" << outputIndex++ << " : " << iter.first << "\n"
<< result.string(8) << "\n\n";
}
}
std::map<std::string, std::shared_ptr<Tensor>> get_output(
std::shared_ptr<CNN> pipeline, std::string affinity)
{
std::map<std::string, std::shared_ptr<Tensor>> outMap = pipeline->get_outputs();
if (affinity == "GPU") {
#ifdef _USE_MALI
for (auto iter : outMap) {
Tensor result = *(iter.second);
auto mem = (OclMemory *)result.get_memory();
mem->get_mapped_ptr();
}
#else
UNI_WARNING_LOG("this binary not support GPU, please recompile project with GPU "
"compile options\n");
#endif
}
return outMap;
}
int main(int argc, char *argv[])
{
UNI_TIME_INIT
parse_options(argc, argv);
// 1: set up the pipeline
auto pipeline = createPipeline(affinityPolicyName, modelPath, algorithmMapPath);
// 2: create input data and feed the pipeline with it
auto model_tensors_input = create_tensors_from_path(inputData, pipeline);
std::map<std::string, std::shared_ptr<Tensor>> outMap;
// 3: warm up and run
for (int i = 0; i < warmUp; i++) {
pipeline->set_input_tensors_value(model_tensors_input);
pipeline->run();
outMap = get_output(pipeline, affinityPolicyName);
}
double timeBegin = ut_time_ms();
for (int i = 0; i < loopTime; i++) {
pipeline->set_input_tensors_value(model_tensors_input);
pipeline->run();
outMap = get_output(pipeline, affinityPolicyName);
}
double timeEnd = ut_time_ms();
double totalTime = (timeEnd - timeBegin);
// 4: process result
print_result(outMap);
UNI_TIME_STATISTICS
UNI_CI_LOG("total_time:%fms(loops=%d)\n", 1.0 * totalTime, loopTime);
UNI_CI_LOG("avg_time:%fms/data\n", 1.0 * totalTime / loopTime);
pipeline->saveAlgorithmMapToText(algorithmMapPath);
return 0;
}