66// Copyright © 2018, Alibaba Group Holding Limited
77//
88
9- #include < dirent.h>
109#include < errno.h>
1110#include < float.h>
1211#include < math.h>
1312#include < stdio.h>
1413#include < stdlib.h>
1514#include < string.h>
16- #include < sys/stat.h>
17- #include < sys/time.h>
18- #include < sys/types.h>
1915#include < cstring>
2016#include < fstream>
2117#include < iostream>
2218#include < vector>
19+ #if defined(_MSC_VER)
20+ #include < Windows.h>
21+ #undef min
22+ #undef max
23+ #else
24+ #include < sys/time.h>
25+ #include < sys/stat.h>
26+ #include < sys/types.h>
27+ #include < dirent.h>
28+ #endif
2329
2430#include " Backend.hpp"
2531#include " Interpreter.hpp"
@@ -36,14 +42,33 @@ struct Model {
3642 std::string model_file;
3743};
3844
45+ #if !defined(_MSC_VER)
3946inline bool file_exist (const char * file) {
4047 struct stat buffer;
4148 return stat (file, &buffer) == 0 ;
4249}
50+ #endif
4351
4452std::vector<Model> findModelFiles (const char * dir) {
4553 std::vector<Model> models;
46-
54+ #if defined(_MSC_VER)
55+ WIN32_FIND_DATA ffd;
56+ HANDLE hFind = INVALID_HANDLE_VALUE;
57+ hFind = FindFirstFile (dir, &ffd);
58+ if (INVALID_HANDLE_VALUE == hFind) {
59+ std::cout << " open " << dir << " failed: " << strerror (errno) << std::endl;
60+ return models;
61+ }
62+ do {
63+ Model m;
64+ m.name = ffd.cFileName ;
65+ m.model_file = std::string (dir) + " \\ " + m.name ;
66+ if (INVALID_FILE_ATTRIBUTES != GetFileAttributes (m.model_file .c_str ()) || GetLastError () != ERROR_FILE_NOT_FOUND) {
67+ models.push_back (std::move (m));
68+ }
69+ } while (FindNextFile (hFind, &ffd) != 0 );
70+ FindClose (hFind);
71+ #else
4772 DIR* root;
4873 if ((root = opendir (dir)) == NULL ) {
4974 std::cout << " open " << dir << " failed: " << strerror (errno) << std::endl;
@@ -62,6 +87,7 @@ std::vector<Model> findModelFiles(const char* dir) {
6287 }
6388 }
6489 closedir (root);
90+ #endif
6591 return models;
6692}
6793
@@ -72,13 +98,31 @@ void setInputData(MNN::Tensor* tensor) {
7298 }
7399}
74100
101+ static inline uint64_t getTimeInUs () {
102+ uint64_t time;
103+ #if defined(_MSC_VER)
104+ LARGE_INTEGER now, freq;
105+ QueryPerformanceCounter (&now);
106+ QueryPerformanceFrequency (&freq);
107+ uint64_t sec = now.QuadPart / freq.QuadPart ;
108+ uint64_t usec = (now.QuadPart % freq.QuadPart ) * 1000000 / freq.QuadPart ;
109+ time = sec * 1000000 + usec;
110+ #else
111+ struct timeval tv;
112+ gettimeofday (&tv, nullptr );
113+ time = static_cast <uint64_t >(tv.tv_sec ) * 1000000 + tv.tv_usec ;
114+ #endif
115+ return time;
116+ }
117+
75118std::vector<float > doBench (Model& model, int loop, int forward = MNN_FORWARD_CPU, bool only_inference = true ,
76119 int numberThread = 4 , int precision = 2 ) {
77120 auto revertor = std::unique_ptr<Revert>(new Revert (model.model_file .c_str ()));
78121 revertor->initialize ();
79122 auto modelBuffer = revertor->getBuffer ();
80123 const auto bufferSize = revertor->getBufferSize ();
81124 auto net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromBuffer (modelBuffer, bufferSize));
125+ revertor.reset ();
82126 MNN::ScheduleConfig config;
83127 config.numThread = numberThread;
84128 config.type = static_cast <MNNForwardType>(forward);
@@ -88,6 +132,7 @@ std::vector<float> doBench(Model& model, int loop, int forward = MNN_FORWARD_CPU
88132
89133 std::vector<float > costs;
90134 MNN::Session* session = net->createSession (config);
135+ net->releaseModel ();
91136 MNN::Tensor* input = net->getSessionInput (session, NULL );
92137
93138 // if the model has not the input dimension, umcomment the below code to set the input dims
@@ -109,16 +154,14 @@ std::vector<float> doBench(Model& model, int loop, int forward = MNN_FORWARD_CPU
109154 }
110155
111156 for (int round = 0 ; round < loop; round++) {
112- struct timeval time_begin, time_end;
113- gettimeofday (&time_begin, NULL );
157+ auto timeBegin = getTimeInUs ();
114158
115159 input->copyFromHostTensor (givenTensor.get ());
116160 net->runSession (session);
117161 outputTensor->copyToHostTensor (expectTensor.get ());
118162
119- gettimeofday (&time_end, NULL );
120- costs.push_back ((time_end.tv_sec - time_begin.tv_sec ) * 1000.0 +
121- (time_end.tv_usec - time_begin.tv_usec ) / 1000.0 );
163+ auto timeEnd = getTimeInUs ();
164+ costs.push_back ((timeEnd - timeBegin) / 1000.0 );
122165 }
123166 return costs;
124167}
0 commit comments