Skip to content

Commit ff405a3

Browse files
author
liqing
committed
beta 0.1.1.6
- add support for windows - fix bugs in converting dropout - fix bugs in post treat
1 parent 2170047 commit ff405a3

31 files changed

+408
-141
lines changed

CMakeLists.txt

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ enable_language(ASM)
1111

1212
option(MNN_USE_CPP11 "Enable MNN use c++11" ON)
1313

14-
if(MNN_USE_CPP11)
15-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
16-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
17-
else()
18-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
19-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
14+
if (NOT MSVC)
15+
if(MNN_USE_CPP11)
16+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
17+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
18+
else()
19+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
20+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
21+
endif()
2022
endif()
2123

2224
# build options
@@ -127,16 +129,26 @@ endif()
127129

128130
if(MNN_DEBUG)
129131
add_definitions(-DMNN_DEBUG)
130-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -g")
131-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -g")
132+
if(MSVC)
133+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG /DEBUG")
134+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG /DEBUG")
135+
else()
136+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -g")
137+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -g")
138+
endif()
132139
else()
133-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -fPIC")
134-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC")
135-
if(MNN_BUILD_FOR_ANDROID_COMMAND)
136-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s -Wl,--gc-sections")
140+
if (MSVC)
141+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /O2")
142+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2")
143+
else()
144+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -fPIC")
145+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC")
146+
if(MNN_BUILD_FOR_ANDROID_COMMAND)
147+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s -Wl,--gc-sections")
148+
endif()
137149
endif()
138150
endif()
139-
if (MNN_HIDDEN)
151+
if ((MNN_HIDDEN) AND (NOT MSVC))
140152
add_definitions(-fvisibility=hidden)
141153
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
142154
endif()
@@ -339,7 +351,9 @@ if(MNN_BUILD_FOR_IOS OR MNN_METAL)
339351
endif()
340352

341353
if(WIN32)
342-
target_compile_definitions(MNN PRIVATE "-D_CRT_SECURE_NO_WARNINGS")
354+
target_compile_definitions(MNN PRIVATE "-DBUILDING_DLL")
355+
target_compile_definitions(MNN PUBLIC "-D_CRT_SECURE_NO_WARNINGS")
356+
target_compile_options(MNN PUBLIC "/wd4244" "/wd4146" "/wd4018" "/wd4267" "/wd4996" "/wd4081")
343357
endif()
344358

345359
if(SYSTEM.Android AND NOT MNN_BUILD_FOR_ANDROID_COMMAND)
@@ -375,9 +389,15 @@ if(MNN_BUILD_BENCHMARK)
375389
endif()
376390

377391
# schema generator
378-
add_custom_target( MNN_SCHEMA ALL
379-
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/schema/generate.sh -lazy
380-
)
392+
if(WIN32)
393+
add_custom_target( MNN_SCHEMA ALL
394+
COMMAND powershell ${CMAKE_CURRENT_SOURCE_DIR}/schema/generate.ps1 -lazy
395+
)
396+
else()
397+
add_custom_target( MNN_SCHEMA ALL
398+
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/schema/generate.sh -lazy
399+
)
400+
endif()
381401
add_dependencies(MNN MNN_SCHEMA)
382402

383403
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/MNNDefine.h DESTINATION include)

benchmark/benchmark.cpp

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,26 @@
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)
3946
inline bool file_exist(const char* file) {
4047
struct stat buffer;
4148
return stat(file, &buffer) == 0;
4249
}
50+
#endif
4351

4452
std::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+
75118
std::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
}

doc/Install_CN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [编译选项](#编译选项)
66
- [Linux|arm|aarch64|Darwin](#Linux|arm|aarch64|Darwin)
7+
- [Windows 10 (x64)](#Windows)
78
- [Android](#Android)
89
- [iOS](#iOS)
910

@@ -68,6 +69,16 @@ cmake ..
6869
make -j4
6970
```
7071

72+
## Windows 10 (x64)
73+
1. 安装 Microsoft Visual Studio 2019, cmake(建议使用3.10或以上版本),powershell
74+
2. 在设置中找到x64 Native Tools Command Prompt for VS 2019并单击,或者win+R然后输入cmd /k "Path\to\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat",打开VS编译构建原生x64结构程序的虚拟环境
75+
3. 编译构建MNN
76+
```powershell
77+
cd /path/to/MNN
78+
mkdir build && cd build
79+
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
80+
nmake
81+
```
7182

7283
## Android
7384

doc/Install_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [Build-Option](#Build-Option)
66
- [Linux|arm|aarch64|Darwin](#Linux|arm|aarch64|Darwin)
7+
- [Windows 10 (x64)](#Windows)
78
- [Android](#Android)
89
- [iOS](#iOS)
910

@@ -68,6 +69,17 @@ cmake ..
6869
make -j4
6970
```
7071

72+
## Windows 10 (x64)
73+
1. Install "Microsoft Visual Studio 2019", cmake (version >= 3.10 is recommended),powershell
74+
2. Find and click "x64 Native Tools Command Prompt for VS 2019" in Setting,or win+R and input: cmd /k "Path\to\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
75+
3. compile and build MNN
76+
```powershell
77+
cd /path/to/MNN
78+
mkdir build && cd build
79+
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
80+
nmake
81+
```
82+
7183
## Android
7284

7385
1. Install cmake (version >=3.10 is recommended), protobuf (version >= 3.0 is required) and gcc (version >= 4.9 is required)

include/ErrorCode.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace MNN {
1313
enum ErrorCode {
14+
#ifdef NO_ERROR
15+
#undef NO_ERROR
16+
#endif //NO_ERROR
1417
NO_ERROR = 0,
1518
OUT_OF_MEMORY = 1,
1619
NOT_SUPPORT = 2,

include/MNNDefine.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
#define FUNC_PRINT(x) MNN_PRINT(#x "=%d in %s, %d \n", x, __func__, __LINE__);
5151
#define FUNC_PRINT_ALL(x, type) MNN_PRINT(#x "=" #type " %" #type " in %s, %d \n", x, __func__, __LINE__);
5252

53+
#if defined(_MSC_VER)
54+
#ifdef BUILDING_DLL
55+
#define MNN_PUBLIC __declspec(dllexport)
56+
#else
57+
#define MNN_PUBLIC __declspec(dllimport)
58+
#endif
59+
#else
5360
#define MNN_PUBLIC __attribute__((visibility("default")))
61+
#endif
5462

5563
#endif /* MNNDefine_h */

include/Rect.h

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -449,43 +449,6 @@ struct MNN_PUBLIC Rect {
449449
this->inset(-dx, -dy);
450450
}
451451

452-
/** Returns true if Rect intersects r, and sets Rect to intersection.
453-
Returns false if Rect does not intersect r, and leaves Rect unchanged.
454-
455-
Returns false if either r or Rect is empty, leaving Rect unchanged.
456-
457-
@param r limit of result
458-
@return true if r and Rect have area in common
459-
*/
460-
bool intersect(const Rect& r);
461-
462-
/** Constructs Rect to intersect from (left, top, right, bottom). Does not sort
463-
construction.
464-
465-
Returns true if Rect intersects construction, and sets Rect to intersection.
466-
Returns false if Rect does not intersect construction, and leaves Rect unchanged.
467-
468-
Returns false if either construction or Rect is empty, leaving Rect unchanged.
469-
470-
@param left x-axis minimum of constructed Rect
471-
@param top y-axis minimum of constructed Rect
472-
@param right x-axis maximum of constructed Rect
473-
@param bottom y-axis maximum of constructed Rect
474-
@return true if construction and Rect have area in common
475-
*/
476-
bool intersect(float left, float top, float right, float bottom);
477-
478-
/** Returns true if a intersects b, and sets Rect to intersection.
479-
Returns false if a does not intersect b, and leaves Rect unchanged.
480-
481-
Returns false if either a or b is empty, leaving Rect unchanged.
482-
483-
@param a Rect to intersect
484-
@param b Rect to intersect
485-
@return true if a and b have area in common
486-
*/
487-
bool intersect(const Rect& a, const Rect& b);
488-
489452
private:
490453
static bool Intersects(float al, float at, float ar, float ab, float bl, float bt, float br, float bb) {
491454
float L = std::max(al, bl);
@@ -533,32 +496,6 @@ struct MNN_PUBLIC Rect {
533496
return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
534497
}
535498

536-
/** Constructs Rect to intersect from (left, top, right, bottom). Does not sort
537-
construction.
538-
539-
Sets Rect to the union of itself and the construction.
540-
541-
Has no effect if construction is empty. Otherwise, if Rect is empty, sets
542-
Rect to construction.
543-
544-
@param left x-axis minimum of constructed Rect
545-
@param top y-axis minimum of constructed Rect
546-
@param right x-axis maximum of constructed Rect
547-
@param bottom y-axis maximum of constructed Rect
548-
*/
549-
void join(float left, float top, float right, float bottom);
550-
551-
/** Sets Rect to the union of itself and r.
552-
553-
Has no effect if r is empty. Otherwise, if Rect is empty, sets
554-
Rect to r.
555-
556-
@param r expansion Rect
557-
*/
558-
void join(const Rect& r) {
559-
this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
560-
}
561-
562499
/** Sets Rect to the union of itself and r.
563500
564501
Asserts if r is empty and SK_DEBUG is defined.

0 commit comments

Comments
 (0)