Skip to content

Commit

Permalink
beta 0.1.1.6
Browse files Browse the repository at this point in the history
- add support for windows
- fix bugs in converting dropout
- fix bugs in post treat
  • Loading branch information
liqing committed Jun 10, 2019
1 parent 2170047 commit ff405a3
Show file tree
Hide file tree
Showing 31 changed files with 408 additions and 141 deletions.
54 changes: 37 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ enable_language(ASM)

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

if(MNN_USE_CPP11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
if (NOT MSVC)
if(MNN_USE_CPP11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()
endif()

# build options
Expand Down Expand Up @@ -127,16 +129,26 @@ endif()

if(MNN_DEBUG)
add_definitions(-DMNN_DEBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -g")
if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG /DEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG /DEBUG")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -g")
endif()
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC")
if(MNN_BUILD_FOR_ANDROID_COMMAND)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s -Wl,--gc-sections")
if (MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC")
if(MNN_BUILD_FOR_ANDROID_COMMAND)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s -Wl,--gc-sections")
endif()
endif()
endif()
if (MNN_HIDDEN)
if ((MNN_HIDDEN) AND (NOT MSVC))
add_definitions(-fvisibility=hidden)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
endif()
Expand Down Expand Up @@ -339,7 +351,9 @@ if(MNN_BUILD_FOR_IOS OR MNN_METAL)
endif()

if(WIN32)
target_compile_definitions(MNN PRIVATE "-D_CRT_SECURE_NO_WARNINGS")
target_compile_definitions(MNN PRIVATE "-DBUILDING_DLL")
target_compile_definitions(MNN PUBLIC "-D_CRT_SECURE_NO_WARNINGS")
target_compile_options(MNN PUBLIC "/wd4244" "/wd4146" "/wd4018" "/wd4267" "/wd4996" "/wd4081")
endif()

if(SYSTEM.Android AND NOT MNN_BUILD_FOR_ANDROID_COMMAND)
Expand Down Expand Up @@ -375,9 +389,15 @@ if(MNN_BUILD_BENCHMARK)
endif()

# schema generator
add_custom_target( MNN_SCHEMA ALL
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/schema/generate.sh -lazy
)
if(WIN32)
add_custom_target( MNN_SCHEMA ALL
COMMAND powershell ${CMAKE_CURRENT_SOURCE_DIR}/schema/generate.ps1 -lazy
)
else()
add_custom_target( MNN_SCHEMA ALL
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/schema/generate.sh -lazy
)
endif()
add_dependencies(MNN MNN_SCHEMA)

install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/MNNDefine.h DESTINATION include)
Expand Down
63 changes: 53 additions & 10 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
// Copyright © 2018, Alibaba Group Holding Limited
//

#include <dirent.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.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 "Backend.hpp"
#include "Interpreter.hpp"
Expand All @@ -36,14 +42,33 @@ struct Model {
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;
hFind = FindFirstFile(dir, &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;
Expand All @@ -62,6 +87,7 @@ std::vector<Model> findModelFiles(const char* dir) {
}
}
closedir(root);
#endif
return models;
}

Expand All @@ -72,13 +98,31 @@ void setInputData(MNN::Tensor* tensor) {
}
}

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 forward = MNN_FORWARD_CPU, bool only_inference = true,
int numberThread = 4, int precision = 2) {
auto revertor = std::unique_ptr<Revert>(new Revert(model.model_file.c_str()));
revertor->initialize();
auto modelBuffer = revertor->getBuffer();
const auto bufferSize = revertor->getBufferSize();
auto net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromBuffer(modelBuffer, bufferSize));
revertor.reset();
MNN::ScheduleConfig config;
config.numThread = numberThread;
config.type = static_cast<MNNForwardType>(forward);
Expand All @@ -88,6 +132,7 @@ std::vector<float> doBench(Model& model, int loop, int forward = MNN_FORWARD_CPU

std::vector<float> costs;
MNN::Session* session = net->createSession(config);
net->releaseModel();
MNN::Tensor* input = net->getSessionInput(session, NULL);

// if the model has not the input dimension, umcomment the below code to set the input dims
Expand All @@ -109,16 +154,14 @@ std::vector<float> doBench(Model& model, int loop, int forward = MNN_FORWARD_CPU
}

for (int round = 0; round < loop; round++) {
struct timeval time_begin, time_end;
gettimeofday(&time_begin, NULL);
auto timeBegin = getTimeInUs();

input->copyFromHostTensor(givenTensor.get());
net->runSession(session);
outputTensor->copyToHostTensor(expectTensor.get());

gettimeofday(&time_end, NULL);
costs.push_back((time_end.tv_sec - time_begin.tv_sec) * 1000.0 +
(time_end.tv_usec - time_begin.tv_usec) / 1000.0);
auto timeEnd = getTimeInUs();
costs.push_back((timeEnd - timeBegin) / 1000.0);
}
return costs;
}
Expand Down
11 changes: 11 additions & 0 deletions doc/Install_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [编译选项](#编译选项)
- [Linux|arm|aarch64|Darwin](#Linux|arm|aarch64|Darwin)
- [Windows 10 (x64)](#Windows)
- [Android](#Android)
- [iOS](#iOS)

Expand Down Expand Up @@ -68,6 +69,16 @@ cmake ..
make -j4
```

## Windows 10 (x64)
1. 安装 Microsoft Visual Studio 2019, cmake(建议使用3.10或以上版本),powershell
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结构程序的虚拟环境
3. 编译构建MNN
```powershell
cd /path/to/MNN
mkdir build && cd build
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
nmake
```

## Android

Expand Down
12 changes: 12 additions & 0 deletions doc/Install_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [Build-Option](#Build-Option)
- [Linux|arm|aarch64|Darwin](#Linux|arm|aarch64|Darwin)
- [Windows 10 (x64)](#Windows)
- [Android](#Android)
- [iOS](#iOS)

Expand Down Expand Up @@ -68,6 +69,17 @@ cmake ..
make -j4
```

## Windows 10 (x64)
1. Install "Microsoft Visual Studio 2019", cmake (version >= 3.10 is recommended),powershell
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"
3. compile and build MNN
```powershell
cd /path/to/MNN
mkdir build && cd build
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
nmake
```

## Android

1. Install cmake (version >=3.10 is recommended), protobuf (version >= 3.0 is required) and gcc (version >= 4.9 is required)
Expand Down
3 changes: 3 additions & 0 deletions include/ErrorCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace MNN {
enum ErrorCode {
#ifdef NO_ERROR
#undef NO_ERROR
#endif //NO_ERROR
NO_ERROR = 0,
OUT_OF_MEMORY = 1,
NOT_SUPPORT = 2,
Expand Down
8 changes: 8 additions & 0 deletions include/MNNDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
#define FUNC_PRINT(x) MNN_PRINT(#x "=%d in %s, %d \n", x, __func__, __LINE__);
#define FUNC_PRINT_ALL(x, type) MNN_PRINT(#x "=" #type " %" #type " in %s, %d \n", x, __func__, __LINE__);

#if defined(_MSC_VER)
#ifdef BUILDING_DLL
#define MNN_PUBLIC __declspec(dllexport)
#else
#define MNN_PUBLIC __declspec(dllimport)
#endif
#else
#define MNN_PUBLIC __attribute__((visibility("default")))
#endif

#endif /* MNNDefine_h */
63 changes: 0 additions & 63 deletions include/Rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,43 +449,6 @@ struct MNN_PUBLIC Rect {
this->inset(-dx, -dy);
}

/** Returns true if Rect intersects r, and sets Rect to intersection.
Returns false if Rect does not intersect r, and leaves Rect unchanged.
Returns false if either r or Rect is empty, leaving Rect unchanged.
@param r limit of result
@return true if r and Rect have area in common
*/
bool intersect(const Rect& r);

/** Constructs Rect to intersect from (left, top, right, bottom). Does not sort
construction.
Returns true if Rect intersects construction, and sets Rect to intersection.
Returns false if Rect does not intersect construction, and leaves Rect unchanged.
Returns false if either construction or Rect is empty, leaving Rect unchanged.
@param left x-axis minimum of constructed Rect
@param top y-axis minimum of constructed Rect
@param right x-axis maximum of constructed Rect
@param bottom y-axis maximum of constructed Rect
@return true if construction and Rect have area in common
*/
bool intersect(float left, float top, float right, float bottom);

/** Returns true if a intersects b, and sets Rect to intersection.
Returns false if a does not intersect b, and leaves Rect unchanged.
Returns false if either a or b is empty, leaving Rect unchanged.
@param a Rect to intersect
@param b Rect to intersect
@return true if a and b have area in common
*/
bool intersect(const Rect& a, const Rect& b);

private:
static bool Intersects(float al, float at, float ar, float ab, float bl, float bt, float br, float bb) {
float L = std::max(al, bl);
Expand Down Expand Up @@ -533,32 +496,6 @@ struct MNN_PUBLIC Rect {
return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
}

/** Constructs Rect to intersect from (left, top, right, bottom). Does not sort
construction.
Sets Rect to the union of itself and the construction.
Has no effect if construction is empty. Otherwise, if Rect is empty, sets
Rect to construction.
@param left x-axis minimum of constructed Rect
@param top y-axis minimum of constructed Rect
@param right x-axis maximum of constructed Rect
@param bottom y-axis maximum of constructed Rect
*/
void join(float left, float top, float right, float bottom);

/** Sets Rect to the union of itself and r.
Has no effect if r is empty. Otherwise, if Rect is empty, sets
Rect to r.
@param r expansion Rect
*/
void join(const Rect& r) {
this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
}

/** Sets Rect to the union of itself and r.
Asserts if r is empty and SK_DEBUG is defined.
Expand Down
Loading

0 comments on commit ff405a3

Please sign in to comment.