-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathbenchncnn.cpp
More file actions
304 lines (232 loc) · 7.79 KB
/
Copy pathbenchncnn.cpp
File metadata and controls
304 lines (232 loc) · 7.79 KB
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
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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.
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <float.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <algorithm>
#include <windows.h> // Sleep()
#else
#include <unistd.h> // sleep()
#endif
#include "benchmark.h"
#include "cpu.h"
#include "datareader.h"
#include "net.h"
#include "gpu.h"
class DataReaderFromEmpty : public ncnn::DataReader
{
public:
virtual int scan(const char* format, void* p) const
{
return 0;
}
virtual size_t read(void* buf, size_t size) const
{
memset(buf, 0, size);
return size;
}
};
static int g_warmup_loop_count = 8;
static int g_loop_count = 4;
static bool g_enable_cooling_down = true;
static ncnn::UnlockedPoolAllocator g_blob_pool_allocator;
static ncnn::PoolAllocator g_workspace_pool_allocator;
#if NCNN_VULKAN
static ncnn::VulkanDevice* g_vkdev = 0;
static ncnn::VkAllocator* g_blob_vkallocator = 0;
static ncnn::VkAllocator* g_staging_vkallocator = 0;
#endif // NCNN_VULKAN
void benchmark(const char* comment, const ncnn::Mat& _in, const ncnn::Option& opt)
{
ncnn::Mat in = _in;
in.fill(0.01f);
g_blob_pool_allocator.clear();
g_workspace_pool_allocator.clear();
#if NCNN_VULKAN
if (opt.use_vulkan_compute)
{
g_blob_vkallocator->clear();
g_staging_vkallocator->clear();
}
#endif // NCNN_VULKAN
ncnn::Net net;
net.opt = opt;
#if NCNN_VULKAN
if (net.opt.use_vulkan_compute)
{
net.set_vulkan_device(g_vkdev);
}
#endif // NCNN_VULKAN
char parampath[256];
sprintf(parampath, "%s.param", comment);
net.load_param(parampath);
DataReaderFromEmpty dr;
net.load_model(dr);
if (g_enable_cooling_down)
{
// sleep 10 seconds for cooling down SOC :(
#ifdef _WIN32
Sleep(10 * 1000);
#elif defined(__unix__) || defined(__APPLE__)
sleep(10);
#elif _POSIX_TIMERS
struct timespec ts;
ts.tv_sec = 10;
ts.tv_nsec = 0;
nanosleep(&ts, &ts);
#else
// TODO How to handle it ?
#endif
}
ncnn::Mat out;
// warm up
for (int i = 0; i < g_warmup_loop_count; i++)
{
ncnn::Extractor ex = net.create_extractor();
ex.input("data", in);
ex.extract("output", out);
}
double time_min = DBL_MAX;
double time_max = -DBL_MAX;
double time_avg = 0;
for (int i = 0; i < g_loop_count; i++)
{
double start = ncnn::get_current_time();
{
ncnn::Extractor ex = net.create_extractor();
ex.input("data", in);
ex.extract("output", out);
}
double end = ncnn::get_current_time();
double time = end - start;
time_min = std::min(time_min, time);
time_max = std::max(time_max, time);
time_avg += time;
}
time_avg /= g_loop_count;
fprintf(stderr, "%20s min = %7.2f max = %7.2f avg = %7.2f\n", comment, time_min, time_max, time_avg);
}
int main(int argc, char** argv)
{
int loop_count = 4;
int num_threads = ncnn::get_cpu_count();
int powersave = 0;
int gpu_device = -1;
int cooling_down = 1;
if (argc >= 2)
{
loop_count = atoi(argv[1]);
}
if (argc >= 3)
{
num_threads = atoi(argv[2]);
}
if (argc >= 4)
{
powersave = atoi(argv[3]);
}
if (argc >= 5)
{
gpu_device = atoi(argv[4]);
}
if (argc >= 6)
{
cooling_down = atoi(argv[5]);
}
bool use_vulkan_compute = gpu_device != -1;
g_enable_cooling_down = cooling_down != 0;
g_loop_count = loop_count;
g_blob_pool_allocator.set_size_compare_ratio(0.0f);
g_workspace_pool_allocator.set_size_compare_ratio(0.5f);
#if NCNN_VULKAN
if (use_vulkan_compute)
{
g_warmup_loop_count = 10;
g_vkdev = ncnn::get_gpu_device(gpu_device);
g_blob_vkallocator = new ncnn::VkBlobAllocator(g_vkdev);
g_staging_vkallocator = new ncnn::VkStagingAllocator(g_vkdev);
}
#endif // NCNN_VULKAN
// default option
ncnn::Option opt;
opt.lightmode = true;
opt.num_threads = num_threads;
opt.blob_allocator = &g_blob_pool_allocator;
opt.workspace_allocator = &g_workspace_pool_allocator;
#if NCNN_VULKAN
opt.blob_vkallocator = g_blob_vkallocator;
opt.workspace_vkallocator = g_blob_vkallocator;
opt.staging_vkallocator = g_staging_vkallocator;
#endif // NCNN_VULKAN
opt.use_winograd_convolution = true;
opt.use_sgemm_convolution = true;
opt.use_int8_inference = true;
opt.use_vulkan_compute = use_vulkan_compute;
opt.use_fp16_packed = true;
opt.use_fp16_storage = true;
opt.use_fp16_arithmetic = true;
opt.use_int8_storage = true;
opt.use_int8_arithmetic = true;
opt.use_packing_layout = true;
opt.use_shader_pack8 = false;
opt.use_image_storage = false;
ncnn::set_cpu_powersave(powersave);
ncnn::set_omp_dynamic(0);
ncnn::set_omp_num_threads(num_threads);
fprintf(stderr, "loop_count = %d\n", g_loop_count);
fprintf(stderr, "num_threads = %d\n", num_threads);
fprintf(stderr, "powersave = %d\n", ncnn::get_cpu_powersave());
fprintf(stderr, "gpu_device = %d\n", gpu_device);
fprintf(stderr, "cooling_down = %d\n", (int)g_enable_cooling_down);
// run
benchmark("squeezenet", ncnn::Mat(227, 227, 3), opt);
benchmark("squeezenet_int8", ncnn::Mat(227, 227, 3), opt);
benchmark("mobilenet", ncnn::Mat(224, 224, 3), opt);
benchmark("mobilenet_int8", ncnn::Mat(224, 224, 3), opt);
benchmark("mobilenet_v2", ncnn::Mat(224, 224, 3), opt);
// benchmark("mobilenet_v2_int8", ncnn::Mat(224, 224, 3), opt);
benchmark("mobilenet_v3", ncnn::Mat(224, 224, 3), opt);
benchmark("shufflenet", ncnn::Mat(224, 224, 3), opt);
benchmark("shufflenet_v2", ncnn::Mat(224, 224, 3), opt);
benchmark("mnasnet", ncnn::Mat(224, 224, 3), opt);
benchmark("proxylessnasnet", ncnn::Mat(224, 224, 3), opt);
benchmark("efficientnet_b0", ncnn::Mat(224, 224, 3), opt);
benchmark("regnety_400m", ncnn::Mat(224, 224, 3), opt);
benchmark("blazeface", ncnn::Mat(128, 128, 3), opt);
benchmark("googlenet", ncnn::Mat(224, 224, 3), opt);
benchmark("googlenet_int8", ncnn::Mat(224, 224, 3), opt);
benchmark("resnet18", ncnn::Mat(224, 224, 3), opt);
benchmark("resnet18_int8", ncnn::Mat(224, 224, 3), opt);
benchmark("alexnet", ncnn::Mat(227, 227, 3), opt);
benchmark("vgg16", ncnn::Mat(224, 224, 3), opt);
benchmark("vgg16_int8", ncnn::Mat(224, 224, 3), opt);
benchmark("resnet50", ncnn::Mat(224, 224, 3), opt);
benchmark("resnet50_int8", ncnn::Mat(224, 224, 3), opt);
benchmark("squeezenet_ssd", ncnn::Mat(300, 300, 3), opt);
benchmark("squeezenet_ssd_int8", ncnn::Mat(300, 300, 3), opt);
benchmark("mobilenet_ssd", ncnn::Mat(300, 300, 3), opt);
benchmark("mobilenet_ssd_int8", ncnn::Mat(300, 300, 3), opt);
benchmark("mobilenet_yolo", ncnn::Mat(416, 416, 3), opt);
benchmark("mobilenetv2_yolov3", ncnn::Mat(352, 352, 3), opt);
benchmark("yolov4-tiny", ncnn::Mat(416, 416, 3), opt);
#if NCNN_VULKAN
delete g_blob_vkallocator;
delete g_staging_vkallocator;
#endif // NCNN_VULKAN
return 0;
}