forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
439 lines (397 loc) · 13.5 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//
// benchmark.cpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
#if defined(_MSC_VER)
#include <Windows.h>
#undef min
#undef max
#else
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif
#include "core/Backend.hpp"
#include <MNN/Interpreter.hpp>
#include <MNN/MNNDefine.h>
#include <MNN/Tensor.hpp>
#include "revertMNNModel.hpp"
/**
TODOs:
1. dynamically get CPU related info.
2. iOS support
*/
struct Model {
std::string name;
std::string model_file;
};
#if !defined(_MSC_VER)
inline bool file_exist(const char* file) {
struct stat buffer;
return stat(file, &buffer) == 0;
}
#endif
std::vector<Model> findModelFiles(const char* dir) {
std::vector<Model> models;
#if defined(_MSC_VER)
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
std::string mnn_model_pattern = std::string(dir) + "\\*.mnn";
hFind = FindFirstFile(mnn_model_pattern.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind) {
std::cout << "open " << dir << " failed: " << strerror(errno) << std::endl;
return models;
}
do {
Model m;
m.name = ffd.cFileName;
m.model_file = std::string(dir) + "\\" + m.name;
if(INVALID_FILE_ATTRIBUTES != GetFileAttributes(m.model_file.c_str()) && GetLastError() != ERROR_FILE_NOT_FOUND) {
models.push_back(std::move(m));
}
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
#else
DIR* root;
if ((root = opendir(dir)) == NULL) {
std::cout << "open " << dir << " failed: " << strerror(errno) << std::endl;
return models;
}
struct dirent* ent;
while ((ent = readdir(root)) != NULL) {
Model m;
if (ent->d_name[0] != '.') {
m.name = ent->d_name;
m.model_file = std::string(dir) + "/" + m.name;
if (file_exist(m.model_file.c_str())) {
models.push_back(std::move(m));
}
}
}
closedir(root);
#endif
return models;
}
void setInputData(MNN::Tensor* tensor) {
float* data = tensor->host<float>();
Revert::fillRandValue(data, tensor->elementSize());
}
static inline uint64_t getTimeInUs() {
uint64_t time;
#if defined(_MSC_VER)
LARGE_INTEGER now, freq;
QueryPerformanceCounter(&now);
QueryPerformanceFrequency(&freq);
uint64_t sec = now.QuadPart / freq.QuadPart;
uint64_t usec = (now.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart;
time = sec * 1000000 + usec;
#else
struct timeval tv;
gettimeofday(&tv, nullptr);
time = static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
#endif
return time;
}
std::vector<float> doBench(Model& model, int loop, int warmup = 10, int forward = MNN_FORWARD_CPU, bool only_inference = true,
int numberThread = 4, int precision = 2, float sparsity = 0.0f, int sparseBlockOC = 1, bool testQuantModel=false) {
auto revertor = std::unique_ptr<Revert>(new Revert(model.model_file.c_str()));
if (testQuantModel) {
printf("Auto set sparsity=0 when test quantized model in benchmark...\n");
revertor->initialize(0, sparseBlockOC, false, true);
} else {
revertor->initialize(sparsity, sparseBlockOC);
}
auto modelBuffer = revertor->getBuffer();
const auto bufferSize = revertor->getBufferSize();
auto net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromBuffer(modelBuffer, bufferSize));
revertor.reset();
net->setSessionMode(MNN::Interpreter::Session_Release);
MNN::ScheduleConfig config;
config.numThread = numberThread;
config.type = static_cast<MNNForwardType>(forward);
MNN::BackendConfig backendConfig;
backendConfig.precision = (MNN::BackendConfig::PrecisionMode)precision;
backendConfig.power = MNN::BackendConfig::Power_High;
config.backendConfig = &backendConfig;
std::vector<float> costs;
MNN::Session* session = net->createSession(config);
MNN::Tensor* input = net->getSessionInput(session, NULL);
// if the model has not the input dimension, umcomment the below code to set the input dims
// std::vector<int> dims{1, 3, 224, 224};
// net->resizeTensor(input, dims);
// net->resizeSession(session);
net->releaseModel();
const MNN::Backend* inBackend = net->getBackend(session, input);
std::shared_ptr<MNN::Tensor> givenTensor(MNN::Tensor::createHostTensorFromDevice(input, false));
auto outputTensor = net->getSessionOutput(session, NULL);
std::shared_ptr<MNN::Tensor> expectTensor(MNN::Tensor::createHostTensorFromDevice(outputTensor, false));
// Warming up...
for (int i = 0; i < warmup; ++i) {
void* host = input->map(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType());
input->unmap(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType(), host);
net->runSession(session);
host = outputTensor->map(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType());
outputTensor->unmap(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType(), host);
}
for (int round = 0; round < loop; round++) {
auto timeBegin = getTimeInUs();
void* host = input->map(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType());
input->unmap(MNN::Tensor::MAP_TENSOR_WRITE, input->getDimensionType(), host);
net->runSession(session);
host = outputTensor->map(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType());
outputTensor->unmap(MNN::Tensor::MAP_TENSOR_READ, outputTensor->getDimensionType(), host);
auto timeEnd = getTimeInUs();
costs.push_back((timeEnd - timeBegin) / 1000.0);
}
return costs;
}
void displayStats(const std::string& name, const std::vector<float>& costs) {
float max = 0, min = FLT_MAX, sum = 0, avg;
for (auto v : costs) {
max = fmax(max, v);
min = fmin(min, v);
sum += v;
//printf("[ - ] cost:%f ms\n", v);
}
avg = costs.size() > 0 ? sum / costs.size() : 0;
printf("[ - ] %-24s max = %8.3f ms min = %8.3f ms avg = %8.3f ms\n", name.c_str(), max, avg == 0 ? 0 : min, avg);
}
static inline std::string forwardType(MNNForwardType type) {
switch (type) {
case MNN_FORWARD_CPU:
return "CPU";
case MNN_FORWARD_VULKAN:
return "Vulkan";
case MNN_FORWARD_OPENCL:
return "OpenCL";
case MNN_FORWARD_METAL:
return "Metal";
default:
break;
}
return "N/A";
}
#ifdef __ANDROID__
#include <errno.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#define BUFFER_SIZE 1024
static uint32_t getNumberOfCPU() {
FILE* fp = fopen("/proc/cpuinfo", "rb");
if (!fp) {
return 1;
}
uint32_t number = 0;
char buffer[BUFFER_SIZE];
while (!feof(fp)) {
char* str = fgets(buffer, BUFFER_SIZE, fp);
if (!str) {
break;
}
if (memcmp(buffer, "processor", 9) == 0) {
number++;
}
}
fclose(fp);
if (number < 1) {
number = 1;
}
return number;
}
static int getCPUMaxFreqKHz(int cpuID) {
char path[256];
sprintf(path, "/sys/devices/system/cpu/cpufreq/stats/cpu%d/time_in_state", cpuID);
FILE* fp = fopen(path, "rb");
if (!fp) {
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpuID);
fp = fopen(path, "rb");
if (!fp) {
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpuID);
fp = fopen(path, "rb");
if (!fp) {
return -1;
}
int maxfrequency = -1;
fscanf(fp, "%d", &maxfrequency);
fclose(fp);
return maxfrequency;
}
}
int maxfrequency = 0;
while (!feof(fp)) {
int frequency = 0;
int history = fscanf(fp, "%d %*d", &frequency);
if (history != 1) {
break;
}
if (frequency > maxfrequency) {
maxfrequency = frequency;
}
}
fclose(fp);
return maxfrequency;
}
static int sortCPUIDByMaxFrequency(std::vector<int>& cpuIDs, int* littleClusterOffset) {
const int cpuNumbers = cpuIDs.size();
*littleClusterOffset = 0;
if (cpuNumbers == 0) {
return 0;
}
std::vector<int> cpusFrequency;
cpusFrequency.resize(cpuNumbers);
for (int i = 0; i < cpuNumbers; ++i) {
int frequency = getCPUMaxFreqKHz(i);
cpuIDs[i] = i;
cpusFrequency[i] = frequency;
// MNN_PRINT("cpu fre: %d, %d\n", i, frequency);
}
for (int i = 0; i < cpuNumbers; ++i) {
for (int j = i + 1; j < cpuNumbers; ++j) {
if (cpusFrequency[i] < cpusFrequency[j]) {
// id
int temp = cpuIDs[i];
cpuIDs[i] = cpuIDs[j];
cpuIDs[j] = temp;
// frequency
temp = cpusFrequency[i];
cpusFrequency[i] = cpusFrequency[j];
cpusFrequency[j] = temp;
}
}
}
int midMaxFrequency = (cpusFrequency.front() + cpusFrequency.back()) / 2;
if (midMaxFrequency == cpusFrequency.back()) {
return 0;
}
for (int i = 0; i < cpuNumbers; ++i) {
if (cpusFrequency[i] < midMaxFrequency) {
*littleClusterOffset = i;
break;
}
}
return 0;
}
//#define CPU_SETSIZE 1024
#define __NCPUBITS (8 * sizeof (unsigned long))
#endif
void set_cpu_affinity()
{
#ifdef __ANDROID__
int cpu_core_num = sysconf(_SC_NPROCESSORS_CONF);
//LOG_MCNN_CL_INF("cpu core num = %d\n", cpu_core_num);
int cpu_id = 0;
cpu_set_t mask;
CPU_ZERO(&mask);
auto numberOfCPUs = getNumberOfCPU();
static std::vector<int> sortedCPUIDs;
static int littleClusterOffset = 0;
if (sortedCPUIDs.empty()) {
sortedCPUIDs.resize(numberOfCPUs);
for (int i = 0; i < numberOfCPUs; ++i) {
sortedCPUIDs[i] = i;
}
sortCPUIDByMaxFrequency(sortedCPUIDs, &littleClusterOffset);
}
printf("max core:");
for (cpu_id = 0; cpu_id < littleClusterOffset; cpu_id++)
{
printf("%d ", sortedCPUIDs[cpu_id]);
CPU_SET(sortedCPUIDs[cpu_id], &mask);
}
printf("\n");
int sys_call_res = syscall(__NR_sched_setaffinity, gettid(), sizeof(mask), &mask);
//LOG_MCNN_CL_INF("sys call res = %d\n", sys_call_res);
if (sys_call_res)
{
printf("set_cpu_affinity errno = %d\n", (int)errno);
}
#endif
}
#if TARGET_OS_IPHONE
void iosBenchAll(const char* modelPath) {
std::cout << "MNN benchmark" << std::endl;
int loop = 20;
int warmup = 10;
MNNForwardType forward = MNN_FORWARD_CPU;
forward = MNN_FORWARD_NN;
int numberThread = 4;
int precision = 2;
std::cout << "Forward type: **" << forwardType(forward) << "** thread=" << numberThread << "** precision=" <<precision << std::endl;
std::vector<Model> models = findModelFiles(modelPath);
std::cout << "--------> Benchmarking... loop = " << loop << ", warmup = " << warmup << std::endl;
for (auto& m : models) {
std::vector<float> costs = doBench(m, loop, warmup, forward, false, numberThread, precision);
displayStats(m.name, costs);
}
}
#else
int main(int argc, const char* argv[]) {
std::cout << "MNN benchmark" << std::endl;
int loop = 10;
int warmup = 10;
MNNForwardType forward = MNN_FORWARD_CPU;
int testQuantizedModel = 0;
int numberThread = 4;
int precision = 2;
float sparsity = 0.0f;
int sparseBlockOC = 1;
if (argc <= 2) {
std::cout << "Usage: " << argv[0] << " models_folder [loop_count] [warmup] [forwardtype] [numberThread] [precision] [weightSparsity] [testQuantizedModel]" << std::endl;
return 1;
}
if (argc >= 3) {
loop = atoi(argv[2]);
}
if (argc >= 4) {
warmup = atoi(argv[3]);
}
if (argc >= 5) {
forward = static_cast<MNNForwardType>(atoi(argv[4]));
}
if (argc >= 6) {
numberThread = atoi(argv[5]);
}
if (argc >= 7) {
precision = atoi(argv[6]);
}
if (argc >= 8) {
sparsity = atof(argv[7]);
}
if(argc >= 9) {
sparseBlockOC = atoi(argv[8]);
}
if(argc >= 10) {
testQuantizedModel = atoi(argv[9]);
}
std::cout << "Forward type: **" << forwardType(forward) << "** thread=" << numberThread << "** precision=" <<precision << "** sparsity=" <<sparsity << "** sparseBlockOC=" << sparseBlockOC << "** testQuantizedModel=" << testQuantizedModel << std::endl;
std::vector<Model> models = findModelFiles(argv[1]);
std::cout << "--------> Benchmarking... loop = " << argv[2] << ", warmup = " << warmup << std::endl;
/* not called yet */
// set_cpu_affinity();
for (auto& m : models) {
printf("Float model test...\n");
std::vector<float> costs = doBench(m, loop, warmup, forward, false, numberThread, precision, sparsity, sparseBlockOC, false);
displayStats(m.name, costs);
if (testQuantizedModel) {
printf("Quantized model test...\n");
costs = doBench(m, loop, warmup, forward, false, numberThread, precision, sparsity, sparseBlockOC, true);
displayStats(m.name, costs);
}
}
}
#endif