目录
一. MNN编译安装 (Android编译)
二. 测试benchmark (Benchmark工具)
1. 编译选项
cmake_minimum_required(VERSION 2.8)
project(MNN)
# complier options
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
enable_language(ASM)
# set(CMAKE_C_COMPILER gcc)
# set(CMAKE_CXX_COMPILER g++)
option(MNN_USE_CPP11 "Enable MNN use c++11" ON)
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
option(MNN_BUILD_HARD "Build -mfloat-abi=hard or not" OFF)
option(MNN_BUILD_SHARED_LIBS "MNN build shared or static lib" ON)
option(MNN_FORBID_MULTI_THREAD "Disable Multi Thread" OFF)
option(MNN_OPENMP "Enable Multiple Thread Linux|Android" ON)
option(MNN_USE_THREAD_POOL "Use Multiple Thread by Self ThreadPool" ON)
option(MNN_SUPPORT_TRAIN "Enable Train Ops" OFF)
option(MNN_BUILD_DEMO "Build demo/exec or not" ON)
option(MNN_BUILD_QUANTOOLS "Build Quantized Tools or not" ON)
option(MNN_EVALUATION "Build Evaluation Tools or not" ON)
if (MNN_USE_THREAD_POOL)
set(MNN_OPENMP OFF)
add_definitions(-DMNN_USE_THREAD_POOL)
endif()
if(MNN_FORBID_MULTI_THREAD)
add_definitions(-DMNN_FORBIT_MULTI_THREADS)
endif()
# debug options
option(MNN_DEBUG "Enable MNN DEBUG" OFF)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(MNN_DEBUG ON)
endif()
option(MNN_DEBUG_MEMORY "MNN Debug Memory Access" ON)
option(MNN_DEBUG_TENSOR_SIZE "Enable Tensor Size" ON)
option(MNN_GPU_TRACE "Enable MNN Gpu Debug" ON)
if(MNN_DEBUG_MEMORY)
add_definitions(-DMNN_DEBUG_MEMORY)
endif()
if(MNN_DEBUG_TENSOR_SIZE)
add_definitions(-DMNN_DEBUG_TENSOR_SIZE)
endif()
if(MNN_GPU_TRACE)
add_definitions(-DMNN_GPU_FORCE_FINISH)
endif()
# backend options
option(MNN_METAL "Enable Metal" OFF)
option(MNN_OPENCL "Enable OpenCL" ON)
option(MNN_OPENGL "Enable OpenGL" ON)
option(MNN_VULKAN "Enable Vulkan" ON)
option(MNN_ARM82 "Enable ARM82" OFF)
# codegen register ops
if (MNN_METAL)
add_definitions(-DMNN_CODEGEN_REGISTER)
endif()
# target options
option(MNN_BUILD_BENCHMARK "Build benchmark or not" ON)
option(MNN_BUILD_TEST "Build tests or not" ON)
option(MNN_BUILD_FOR_ANDROID_COMMAND "Build from command" OFF)
option(MNN_BUILD_FOR_IOS "Build for ios" OFF)
set (MNN_HIDDEN FALSE)
if (NOT MNN_BUILD_TEST)
if (NOT MNN_DEBUG)
set (MNN_HIDDEN TRUE)
endif()
endif()
include(cmake/macros.cmake)
message(STATUS ">>>>>>>>>>>>>")
message(STATUS "MNN BUILD INFO:")
message(STATUS "\tSystem: ${CMAKE_SYSTEM_NAME}")
message(STATUS "\tProcessor: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "\tDEBUG: ${MNN_DEBUG}")
message(STATUS "\tMetal: ${MNN_METAL}")
message(STATUS "\tOpenCL: ${MNN_OPENCL}")
message(STATUS "\tOpenGL: ${MNN_OPENGL}")
message(STATUS "\tVulkan: ${MNN_VULKAN}")
message(STATUS "\tOpenMP: ${MNN_OPENMP}")
message(STATUS "\tHideen: ${MNN_HIDDEN}")
.
.
.
2. 本地编译
1. 在https://developer.android.com/ndk/downloads/下载安装NDK,建议使用最新稳定版本
2. 在 .bashrc 或者 .bash_profile 中设置 NDK 环境变量,例如:export ANDROID_NDK=/Users/username/path/to/android-ndk-r17c
3. cd /path/to/MNN
4. ./schema/generate.sh
5. ./tools/script/get_model.sh(可选,模型仅demo工程需要)
6. cd project/android
7. 编译armv7动态库:mkdir build_32 && cd build_32 && ../build_32.sh
8. 编译armv8动态库:mkdir build_64 && cd build_64 && ../build_64.sh
修改build_64.sh:
#!/bin/bash
cmake ../../../ \
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DMNN_DEBUG=true \
-DANDROID_ABI="arm64-v8a" \
-DANDROID_STL=c++_static \
-DANDROID_NATIVE_API_LEVEL=android-28 \
-DMNN_DEBUG=true \
-DMNN_BUILD_FOR_ANDROID_COMMAND=true \
-DNATIVE_LIBRARY_OUTPUT=. $1 $2
make -j4
测试benchmark之前,将Android设备和本地机器相连,然后执行
cd benchmark
./bench_android.sh
# --------------------------------- output -------------------------------- #
zzw@zzw-HP-ProDesk:~/AndroidStudioProjects/MNN/benchmark$ ./bench_android.sh
-- >>>>>>>>>>>>>
-- MNN BUILD INFO:
-- System: Android
-- Processor: aarch64
-- DEBUG: OFF
-- Metal: OFF
-- OpenCL: ON
-- OpenGL: OFF
-- Vulkan: ON
-- OpenMP: ON
-- Hideen: FALSE
-- [*] searching *.c
-- [*] searching *.cc
-- [*] searching *.cpp
-- [*] searching *.s *.S
-- [*] linking MNN with Vulkan done
-- [*] linking MNN with OpenCL done
-- [*] linking MNN with ARM 82 done
-- [*] Checking OpenMP
-- /home/zzw/AndroidStudioProjects/MNN/benchmark/build/benchmark
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zzw/AndroidStudioProjects/MNN/benchmark/build
*** done ***
[ 0%] Built target MNN_SCHEMA
[ 0%] Building C object CMakeFiles/MNN.dir/source/core/MNNMemoryUtils.c.o
[ 0%] Building CXX object CMakeFiles/MNN.dir/source/core/AutoTime.cpp.o
[ 0%] Building CXX object CMakeFiles/MNN.dir/source/core/Backend.cpp.o
[ 0%] Building CXX object CMakeFiles/MNN.dir/source/core/BackendFactory.cpp.o
[ 0%] Building CXX object CMakeFiles/MNN.dir/source/core/Interpreter.cpp.o
[ 0%] Building CXX object CMakeFiles/MNN.dir/source/core/Execution.cpp.o
.
.
.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zzw/AndroidStudioProjects/MNN/benchmark/build
*** done ***
[ 0%] Built target MNN_SCHEMA
[ 76%] Built target MNN
[ 86%] Built target MNN_CL
[100%] Built target MNN_Vulkan
[100%] Built target benchmark.out
*** done ***
[ 0%] Built target MNN_SCHEMA
[ 75%] Built target MNN
[ 85%] Built target MNN_CL
[ 98%] Built target MNN_Vulkan
[100%] Built target timeProfile.out
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
4081 KB/s (337628 bytes in 0.080s)
5340 KB/s (485404 bytes in 0.088s)
5761 KB/s (919340 bytes in 0.155s)
5537 KB/s (218892 bytes in 0.038s)
5724 KB/s (218892 bytes in 0.037s)
18 KB/s (1650 bytes in 0.088s)
脚本执行完成在benchmark目录下得到测试结果benchmark.txt:
processor : 0
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
.
.
processor : 5
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd08
CPU revision : 2
Serial : e4262d1d202f3b06
Build Flags: ABI=arm64-v8a OpenMP=ON Vulkan=ON OpenCL=ON
Invalide device for support vulkan
Invalide device for support vulkan
MNN benchmark
Forward type: **CPU** thread=4** precision=2
--------> Benchmarking... loop = 10
[ - ] inception-v3.mnn max = 768.601ms min = 767.298ms avg = 767.927ms
[ - ] mobilenet-v1-1.0.mnn max = 82.820ms min = 82.369ms avg = 82.608ms
[ - ] MobileNetV2_224.mnn max = 54.484ms min = 54.169ms avg = 54.309ms
[ - ] resnet-v2-50.mnn max = 436.100ms min = 434.881ms avg = 435.331ms
[ - ] SqueezeNetV1.0.mnn max = 123.373ms min = 108.306ms avg = 110.039ms
Invalide device for support vulkan
Invalide device for support vulkan
MNN benchmark
Forward type: **Vulkan** thread=4** precision=2
--------> Benchmarking... loop = 10
Can't Find type=7 backend, use 0 instead
[ - ] inception-v3.mnn max = 746.681ms min = 725.569ms avg = 733.729ms
Can't Find type=7 backend, use 0 instead
[ - ] mobilenet-v1-1.0.mnn max = 82.463ms min = 82.039ms avg = 82.289ms
Can't Find type=7 backend, use 0 instead
[ - ] MobileNetV2_224.mnn max = 53.785ms min = 53.194ms avg = 53.435ms
Can't Find type=7 backend, use 0 instead
[ - ] resnet-v2-50.mnn max = 407.557ms min = 406.926ms avg = 407.134ms
Can't Find type=7 backend, use 0 instead
[ - ] SqueezeNetV1.0.mnn max = 103.184ms min = 102.817ms avg = 103.054ms
注意: open /data/local/tmp/benchmark_models failed: No such file or directory
需要将测试的模型传到测试设备上,使用命令:
adb shell
cd data/local/tmp
mkdir benchmark_models
cd MNN/benchmark/models
adb push inception-v3.mnn data/local/tmp/benchmark_models
adb push mobilenet-v1-1.0.mnn data/local/tmp/benchmark_models
adb push MobileNetV2_224.mnn data/local/tmp/benchmark_models
adb push resnet-v2-50.mnn data/local/tmp/benchmark_models
adb push SqueezeNetV1.0.mnn data/local/tmp/benchmark_models
接着执行./bench_android.sh就能在benchmark.txt得到模型测试时间.