Skip to content

Commit 987537b

Browse files
authored
Merge pull request #32 from MegEngine/update-internal
Update internal
2 parents 8e8a2f8 + 16fa9ee commit 987537b

File tree

115 files changed

+7572
-367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+7572
-367
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ MegCC supports Arm64/ArmV7/X86/BareMatal backend. You may want to check [support
3333
* Download release compiler suit from [release page](https://github.com/MegEngine/MegCC/releases)
3434
* Compiler from source, please fellow the [compiler doc](compiler/README.md)
3535
* Build the release tar, please fellow the [release doc](doc/how-to-release.md)
36+
* Get benchmark of different model please reference [benchmark](benchmark/README.md)
3637

3738
#### How to use MegCC
3839

benchmark/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
model/benchmark_*
2+
model/generated_models
3+
config
4+
output

benchmark/CMakeLists.txt

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
cmake_minimum_required(VERSION 3.15.2)
2+
set(CMAKE_EXPORT_COMPILE_COMMANDS
3+
ON
4+
CACHE INTERNAL "")
5+
6+
project(Benchmarker)
7+
8+
option(ENABLE_MEGENGINE_FRAMEWORK "build benchmark for megengine" OFF)
9+
configure_file(src/build_config.h.in
10+
${CMAKE_CURRENT_BINARY_DIR}/genfiles/build_config.h)
11+
# set megcc lib
12+
if(NOT DEFINED RUNTIME_KERNEL_DIR)
13+
message(FATAL_ERROR "build MegCC runtime kernel dir RUNTIME_KERNEL_DIR is empty, use -DRUNTIME_KERNEL_DIR=your_model_kernel_dir to set")
14+
else()
15+
message(STATUS "build MegCC runtime with kernel dir ${RUNTIME_KERNEL_DIR}")
16+
endif()
17+
18+
add_library(TinyNN STATIC IMPORTED)
19+
set_target_properties(
20+
TinyNN PROPERTIES IMPORTED_LOCATION
21+
"${RUNTIME_KERNEL_DIR}/runtime/install/lib/libTinyNN.a")
22+
if(ENABLE_MEGENGINE_FRAMEWORK)
23+
message(STATUS "build benchmark with megengine ${ENABLE_MEGENGINE_FRAMEWORK}")
24+
option(X86_BACKEND "Build bechmarker with X86 megengine lib" ON)
25+
# set megengine lib
26+
if(NOT DEFINED MEGENGINE_INSTALL_DIR)
27+
message(FATAL_ERROR "MEGENGINE_INSTALL_DIR is empty use -DMEGENGINE_INSTALL_DIR=your_megengine_install_dir to set")
28+
else()
29+
message(STATUS "MEGENGINE_INSTALL_DIR is ${MEGENGINE_INSTALL_DIR}")
30+
endif()
31+
add_library(mgb_imported INTERFACE IMPORTED)
32+
33+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
34+
set(MGE_INSTALL_LIBS ${MEGENGINE_INSTALL_DIR}/lite/lib/aarch64/liblite_static_all_in_one.a)
35+
target_link_libraries(mgb_imported INTERFACE ${MGE_INSTALL_LIBS})
36+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
37+
set(MGE_INSTALL_LIBS ${MEGENGINE_INSTALL_DIR}/lite/lib/armv7/liblite_static_all_in_one.a)
38+
target_link_libraries(mgb_imported INTERFACE ${MGE_INSTALL_LIBS})
39+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
40+
set(MGE_INSTALL_LIBS ${MEGENGINE_INSTALL_DIR}/lite/lib/riscv64/liblite_static_all_in_one.a)
41+
target_link_libraries(mgb_imported INTERFACE ${MGE_INSTALL_LIBS})
42+
else()
43+
if(X86_BACKEND)
44+
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
45+
set(MKL_LIBS
46+
${PROJECT_SOURCE_DIR}/../third_party/MegEngine/third_party/mkl/x86_64/lib/libmkl_core.a
47+
${PROJECT_SOURCE_DIR}/../third_party/MegEngine/third_party/mkl/x86_64/lib/libmkl_sequential.a
48+
${PROJECT_SOURCE_DIR}/../third_party/MegEngine/third_party/mkl/x86_64/lib/libmkl_intel_ilp64.a
49+
)
50+
set(MGE_INSTALL_LIBS ${MEGENGINE_INSTALL_DIR}/lite/lib/x86_64/liblite_static_all_in_one.a)
51+
target_compile_definitions(mgb_imported INTERFACE -DMKL_ILP64)
52+
# WARNING: i386 is not test locally
53+
elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
54+
set(MKL_LIBS
55+
${PROJECT_SOURCE_DIR}/../third_party/MegEngine/third_party/mkl/i386/lib/libmkl_core.a
56+
${PROJECT_SOURCE_DIR}/../third_party/MegEngine/third_party/mkl/x86_64/lib/libmkl_sequential.a
57+
${PROJECT_SOURCE_DIR}/../third_party/MegEngine/third_party/mkl/x86_64/lib/libmkl_intel_32.a
58+
)
59+
set(MGE_INSTALL_LIBS ${MEGENGINE_INSTALL_DIR}/lite/lib/i386/liblite_static_all_in_one.a)
60+
endif()
61+
set(MKL_DNN_LIBS
62+
${MEGENGINE_INSTALL_DIR}/lib/libdnnl.a
63+
${MEGENGINE_INSTALL_DIR}/lib/libmkldnn.a
64+
)
65+
66+
if(UNIX AND NOT APPLE)
67+
target_link_libraries(mgb_imported INTERFACE ${MGE_INSTALL_LIBS} ${MKL_DNN_LIBS} -Wl,--start-group -ldl ${MKL_LIBS} -Wl,--end-group)
68+
else()
69+
target_link_libraries(mgb_imported INTERFACE ${MGE_INSTALL_LIBS} ${MKL_DNN_LIBS} ${MKL_LIBS})
70+
endif()
71+
else()
72+
set(MGE_INSTALL_LIBS ${MEGENGINE_INSTALL_DIR}/lib/libmegengine.a ${MEGENGINE_INSTALL_DIR}/lib/libflatbuffers.a)
73+
target_link_libraries(mgb_imported INTERFACE ${MGE_INSTALL_LIBS})
74+
endif()
75+
76+
endif()
77+
78+
target_include_directories(mgb_imported INTERFACE ${MEGENGINE_INSTALL_DIR}/include)
79+
endif()
80+
# benchmarker config
81+
file(GLOB_RECURSE SOURCES main.cpp src/*.cpp src/*.h)
82+
add_executable(benchmarker ${SOURCES})
83+
target_include_directories(
84+
benchmarker PUBLIC $<BUILD_INTERFACE:${RUNTIME_KERNEL_DIR}/runtime/install/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/genfiles>)
85+
if(ENABLE_MEGENGINE_FRAMEWORK)
86+
target_link_libraries(benchmarker -pthread TinyNN mgb_imported)
87+
else()
88+
target_link_libraries(benchmarker -pthread TinyNN)
89+
endif()
90+
message(STATUS "${CMAKE_TOOLCHAIN_FILE}")
91+
if(CMAKE_TOOLCHAIN_FILE)
92+
if(ANDROID)
93+
target_link_libraries(benchmarker log)
94+
endif()
95+
endif()
96+
97+
install(TARGETS benchmarker LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX})

benchmark/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# How to use megcc benchmark
2+
3+
## introduction
4+
megcc benchmark is a easy tool to get the benchmark result of different model in megcc
5+
the file struction is shown bellow:
6+
```
7+
├── clean.sh
8+
├── CMakeLists.txt
9+
├── main.cpp
10+
├── model
11+
│ ├── model_arm.json
12+
│ ├── model_riscv.json
13+
│ ├── model_x86.json
14+
│ └── request.txt
15+
├── python
16+
│ ├── example.py
17+
│ ├── format.sh
18+
│ └── src
19+
│ ├── benchmark.py
20+
│ └── models.py
21+
├── README.md
22+
├── src
23+
│ ├── benchmark.h
24+
│ ├── build_config.h.in
25+
│ ├── CCbenchmark.cpp
26+
│ ├── CCbenchmark.h
27+
│ ├── MGEbenchmark.cpp
28+
│ └── MGEbenchmark.h
29+
└── tools
30+
├── cc_analysis.py
31+
└── inference_visual.py
32+
```
33+
34+
in src, it is a c++ application to run benchmark result on different platform.
35+
in python, the model convertion, other related preparing work and the benchmarker example is given
36+
the tools contains some usable scripts to analysis benchmark results
37+
## supported model
38+
mobilenetv2, resnet18, efficientnetb0 shufflenetv2 vgg16
39+
## request
40+
```bash
41+
mgeconvert > v.1.0.2
42+
onnx==1.11.0
43+
torch==1.10.0
44+
# or
45+
git clone https://github.com/MegEngine/mgeconvert.git
46+
cd mgeconvert
47+
git checkout master
48+
python3 -m pip install . --user --install-option="--targets=onnx"
49+
50+
```
51+
the mgeconvert can be install by following command:
52+
```bash
53+
git clone https://github.com/MegEngine/mgeconvert.git
54+
cd mgeconvert
55+
git checkout master
56+
python3 -m pip install . --user --install-option="--targets=onnx"
57+
58+
```
59+
## get model and run benchmark example
60+
``` bash
61+
cd megcc/benchmark
62+
export MEGCC_MGB_TO_TINYNN_PATH=<your_mgb_to_tinynn_path>
63+
python3 python/example.py
64+
```
65+
if you want to run in other platform, please reference the example add your new run_platform_xxx function in BenchmarkRunner,
66+
the example given a ssh remote device test template
67+
68+
## analysis megcc log
69+
70+
the `output` directory is generated by `example.py`
71+
72+
### visualize the inference result of different model
73+
```bash
74+
python3 benchmark/tools/inference_visual.py benchmark/output -o figure_dir
75+
```
76+
77+
### visualize the profile result of different kernel in different model
78+
```bash
79+
python3 benchmark/tools/cc_analysis.py benchmark/output -o figure_dir
80+
```

benchmark/clean.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# /bin/bash -e
2+
set -x
3+
rm -rf ./build* ./output ./config ./model/benchmark* ./model/generate*

benchmark/main.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* \file benchmark/main.cpp
3+
*
4+
* This file is part of MegCC, a deep learning compiler developed by Megvii.
5+
*
6+
* \copyright Copyright (c) 2021-2022 Megvii Inc. All rights reserved.
7+
*/
8+
#include <cmath>
9+
#include <cstdio>
10+
#include <memory>
11+
#include "src/CCbenchmark.h"
12+
#include "src/MGEbenchmark.h"
13+
14+
using namespace megcc;
15+
using namespace Benchmark;
16+
int main(int argc, char** argv) {
17+
if (argc < 2 && argc > 4) {
18+
fprintf(stderr, "cmdline error, please run with:\n");
19+
fprintf(stderr, "benchmarker <input_model> [options] ... \n");
20+
fprintf(stderr,
21+
"tips:\n\t you can use --profile and --mge to profile model "
22+
"and enable megengine framework (\"megcc\" is default)\n");
23+
return -1;
24+
}
25+
int log_level = 3;
26+
std::string framework = "megcc";
27+
std::string model_path = argv[1];
28+
int idx = 2;
29+
while (idx < argc) {
30+
std::string args = argv[idx];
31+
if (args == "--profile") {
32+
log_level = 0;
33+
} else if (args == "--mge") {
34+
framework = "mge";
35+
} else {
36+
fprintf(stderr, "invalid option: %s\n", argv[idx]);
37+
}
38+
++idx;
39+
}
40+
std::vector<std::shared_ptr<Benchmarker>> benchmarkers;
41+
if (framework == "megcc") {
42+
benchmarkers.push_back(
43+
std::make_shared<CCBenchmarker>(model_path, log_level));
44+
}
45+
#if ENABLE_MEGENGINE_FRAMEWORK
46+
else if (framework == "mge") {
47+
benchmarkers.push_back(
48+
std::make_shared<MGEBenchmarker>(model_path, log_level));
49+
}
50+
#endif
51+
else {
52+
fprintf(stderr,
53+
"unsupport framework: %s, megcc, mge(export "
54+
"ENABLE_MEGENGINE_FRAMEWORK=ON) is supported\n",
55+
framework.c_str());
56+
}
57+
58+
for (size_t i = 0; i < benchmarkers.size(); ++i) {
59+
benchmarkers[i]->load_model();
60+
benchmarkers[i]->profile();
61+
}
62+
63+
return 0;
64+
}

benchmark/model/model_arm.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"dump_dir": "./benchmark_kernel_arm/",
3+
"models": [
4+
{
5+
"model_name": "mobilenetv2",
6+
"model_path": "./generated_models/mobilenetv2.mge",
7+
"input_shape_str": "data=(1,3,224,224)",
8+
"enable_nchw44": true
9+
},
10+
{
11+
"model_name": "resnet18",
12+
"model_path": "./generated_models/resnet18.mge",
13+
"input_shape_str": "data=(1,3,224,224)",
14+
"enable_nchw44": true
15+
},
16+
{
17+
"model_name": "resnet50",
18+
"model_path": "./generated_models/resnet50.mge",
19+
"input_shape_str": "data=(1,3,224,224)",
20+
"enable_nchw44": true
21+
},
22+
{
23+
"model_name": "efficientnetb0",
24+
"model_path": "./generated_models/efficientnetb0.mge",
25+
"input_shape_str": "data=(1,3,256,256)",
26+
"enable_nchw44": true
27+
},
28+
{
29+
"model_name": "shufflenetv2",
30+
"model_path": "./generated_models/shufflenetv2.mge",
31+
"input_shape_str": "data=(1,3,224,224)",
32+
"enable_nchw44": true
33+
},
34+
{
35+
"model_name": "vgg11",
36+
"model_path": "./generated_models/vgg11.mge",
37+
"input_shape_str": "data=(1,3,224,224)",
38+
"enable_nchw44": true
39+
},
40+
{
41+
"model_name": "vgg16",
42+
"model_path": "./generated_models/vgg16.mge",
43+
"input_shape_str": "data=(1,3,224,224)",
44+
"enable_nchw44": true
45+
}
46+
]
47+
}

benchmark/model/model_riscv.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"dump_dir": "./benchmark_kernel_riscv/",
3+
"models": [
4+
{
5+
"model_name": "mobilenetv2",
6+
"model_path": "./generated_models/mobilenetv2.mge",
7+
"input_shape_str": "data=(1,3,224,224)"
8+
},
9+
{
10+
"model_name": "resnet18",
11+
"model_path": "./generated_models/resnet18.mge",
12+
"input_shape_str": "data=(1,3,224,224)"
13+
},
14+
{
15+
"model_name": "resnet50",
16+
"model_path": "./generated_models/resnet50.mge",
17+
"input_shape_str": "data=(1,3,224,224)"
18+
},
19+
{
20+
"model_name": "efficientnetb0",
21+
"model_path": "./generated_models/efficientnetb0.mge",
22+
"input_shape_str": "data=(1,3,256,256)"
23+
},
24+
{
25+
"model_name": "shufflenetv2",
26+
"model_path": "./generated_models/shufflenetv2.mge",
27+
"input_shape_str": "data=(1,3,224,224)"
28+
},
29+
{
30+
"model_name": "vgg11",
31+
"model_path": "./generated_models/vgg11.mge",
32+
"input_shape_str": "data=(1,3,224,224)"
33+
},
34+
{
35+
"model_name": "vgg16",
36+
"model_path": "./generated_models/vgg16.mge",
37+
"input_shape_str": "data=(1,3,224,224)"
38+
}
39+
]
40+
}

benchmark/model/model_x86.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"dump_dir": "./benchmark_kernel_x86/",
3+
"models": [
4+
{
5+
"model_name": "mobilenetv2",
6+
"model_path": "./generated_models/mobilenetv2.mge",
7+
"input_shape_str": "data=(1,3,224,224)"
8+
},
9+
{
10+
"model_name": "resnet18",
11+
"model_path": "./generated_models/resnet18.mge",
12+
"input_shape_str": "data=(1,3,224,224)"
13+
},
14+
{
15+
"model_name": "resnet50",
16+
"model_path": "./generated_models/resnet50.mge",
17+
"input_shape_str": "data=(1,3,224,224)"
18+
},
19+
{
20+
"model_name": "efficientnetb0",
21+
"model_path": "./generated_models/efficientnetb0.mge",
22+
"input_shape_str": "data=(1,3,256,256)"
23+
},
24+
{
25+
"model_name": "shufflenetv2",
26+
"model_path": "./generated_models/shufflenetv2.mge",
27+
"input_shape_str": "data=(1,3,224,224)"
28+
},
29+
{
30+
"model_name": "vgg11",
31+
"model_path": "./generated_models/vgg11.mge",
32+
"input_shape_str": "data=(1,3,224,224)"
33+
},
34+
{
35+
"model_name": "vgg16",
36+
"model_path": "./generated_models/vgg16.mge",
37+
"input_shape_str": "data=(1,3,224,224)"
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)