提交 576ca249 编写于 作者: L Liangliang He

Add support for CMake

Refactor cmake build flags
Add arm linux toolchains
Add macOS+iOS build support
Support windows
Add library install
Refactor gitlab CI
上级 2d650b67
BasedOnStyle: google
MaxEmptyLinesToKeep: 3
DerivePointerAlignment: false
PointerAlignment: Right
BinPackParameters: false
stages:
- cpplint
- pycodestyle
- docs
- platform_compatible_tests
- build_libraries
- ndk_versions_compatible_tests
- ops_test_disable_neon
- ops_test
- api_test
- model_tests
- quantization_tests
- build_android_demo
- ops_benchmark
- extra_tests
- so_size_check
- dynamic_link_test
- python3_test
- linting
- basic-build
- smoke-test
- build
- test
- extra
cpplint:
stage: cpplint
stage: linting
script:
- curl -o cpplint.py https://raw.githubusercontent.com/google/styleguide/gh-pages/cpplint/cpplint.py
- python cpplint.py --linelength=80 --counting=detailed $(find mace -name "*.h" -or -name "*.cc")
- sh tools/cpplint.sh
pycodestyle:
stage: pycodestyle
pylint:
stage: linting
script:
- pycodestyle $(find -name "*.py")
docs:
stage: docs
build_docs:
stage: basic-build
script:
- cd docs
- make html
......@@ -46,205 +34,108 @@ docs:
paths:
- docs/_build
platform_compatible_tests:
stage: platform_compatible_tests
script:
- bazel build mace/core:core --define openmp=false
- bazel build --config arm_linux_gnueabihf --define openmp=false --define opencl=true --define neon=true //mace/libmace:libmace.so
- bazel build --config aarch64_linux_gnu --define openmp=false --define opencl=true --define neon=true //mace/libmace:libmace.so
build_libraries:
stage: build_libraries
script:
- bash tools/build-standalone-lib.sh
ndk_versions_compatible_tests:
stage: ndk_versions_compatible_tests
script:
- DEFAULT_NDK_PATH=$ANDROID_NDK_HOME
- prefix_path=${DEFAULT_NDK_PATH%android-ndk-*}
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- >
for ndk in android-ndk-r15c android-ndk-r16 android-ndk-r17b;
do
new_ndk_path=${prefix_path}${ndk};
if [ "$new_ndk_path" != "$DEFAULT_NDK_PATH" ]; then
export ANDROID_NDK_HOME=$new_ndk_path;
export PATH=$ANDROID_NDK_HOME:$PATH;
echo "ndk path: $ANDROID_NDK_HOME";
python tools/bazel_adb_run.py --target="//mace/ops:ops_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a --target_socs=$TARGET_SOCS --enable_neon=false --args="--gtest_filter=ActivationOpTest*" || exit 1;
python tools/bazel_adb_run.py --target="//mace/ops:ops_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a --target_socs=$TARGET_SOCS --args="--gtest_filter=ActivationOpTest*" || exit 1;
python tools/bazel_adb_run.py --target="//mace/ops:ops_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=arm64-v8a --target_socs=$TARGET_SOCS --args="--gtest_filter=ActivationOpTest*" || exit 1;
fi
done
- export ANDROID_NDK_HOME=$DEFAULT_NDK_PATH
- export PATH=$ANDROID_NDK_HOME:$PATH
ops_test_disable_neon:
stage: ops_test_disable_neon
script:
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- python tools/bazel_adb_run.py --target="//mace/ops:ops_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS --enable_neon=false || exit 1;
ops_test:
stage: ops_test
script:
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- python tools/bazel_adb_run.py --target="//mace/ops:ops_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS || exit 1;
api_test:
stage: api_test
script:
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- python tools/bazel_adb_run.py --target="//mace/test:mace_api_test" --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a --target_socs=$TARGET_SOCS || exit 1;
- python tools/bazel_adb_run.py --target="//mace/test:mace_api_mt_test" --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a --target_socs=$TARGET_SOCS || exit 1;
- python tools/bazel_adb_run.py --target="//mace/test:mace_api_exception_test" --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a --target_socs=$TARGET_SOCS || exit 1;
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- python tools/bazel_adb_run.py --target="//mace/test:mace_api_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=arm64 --target_socs=$TARGET_SOCS || exit 1;
- python tools/bazel_adb_run.py --target="//mace/test:mace_api_mt_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=arm64 --target_socs=$TARGET_SOCS || exit 1;
- python tools/bazel_adb_run.py --target="//mace/test:mace_api_exception_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=arm64 --target_socs=$TARGET_SOCS || exit 1;
model_tests:
stage: model_tests
script:
- pwd
- rm -rf mace-models
- rm -rf generic-mobile-devices
- GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@github.com:XiaoMi/mace-models.git
- CONF_FILE=mace-models/mobilenet-v1/mobilenet-v1.yml
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=file --model_data_format=file --cl_mem_type=buffer || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --device_yml=${DEVICE_CONF_FILE} --round=1 --target_abis=armeabi-v7a,arm64 --validate --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --device_yml=${DEVICE_CONF_FILE} --example --target_abis=armeabi-v7a,arm64 --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1;
- CONF_FILE=mace-models/mobilenet-v2/mobilenet-v2-host.yml
- >
python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=1 --validate --model_graph_format=file --model_data_format=file --address_sanitizer || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --example --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py benchmark --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=5 --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=code --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=1 --validate --model_graph_format=code --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --example --round=1 --validate --model_graph_format=code --model_data_format=file || exit 1;
python tools/converter.py benchmark --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=5 --model_graph_format=code --model_data_format=file || exit 1;
- rm -rf mace-models
quantization_tests:
stage: quantization_tests
script:
- pwd
- rm -rf mace-models
- GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@github.com:XiaoMi/mace-models.git
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
for CONF_FILE in mace-models/mobilenet-v1/mobilenet-v1-quantize-friendly.yml mace-models/mobilenet-v1/mobilenet-v1-quantize-retrain-for-check-only.yml mace-models/mobilenet-v1/mobilenet-v1-quantize-retrain-dsp.yml;
do
python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --device_yml=${DEVICE_CONF_FILE} --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --device_yml=${DEVICE_CONF_FILE} --example --round=1 --validate --layers=0 --model_graph_format=file --model_data_format=file || exit 1;
done
- rm -rf mace-models
build_android-armeabi-v7a:
stage: basic-build
script:
- sh tools/build-android-armeabi-v7a-full.sh
build_android-arm64-v8:
stage: basic-build
script:
- sh tools/build-android-arm64-v8a-full.sh
code_footprint_check:
stage: basic-build
script:
- echo check shared library size
cc_test_smoketest:
stage: smoke-test
script:
- echo tests
cc_benchmark_smoketest:
stage: smoke-test
script:
- echo benchmark
build_arm-linux-gnueabihf:
stage: build
script:
- sh tools/build-arm-linux-gnueabihf-full.sh
only:
- triggers
build_aarch64-linux-gnu:
stage: build
script:
- sh tools/build-aarch64-linux-gnu-full.sh
only:
- triggers
build_host:
stage: build
script:
- sh tools/build-host.sh
only:
- triggers
build_android_demo:
stage: build_android_demo
stage: build
script:
- echo build android demo
only:
- triggers
cc_test:
stage: test
script:
- echo cc_test
only:
- triggers
cc_benchmark:
stage: test
script:
- echo cc_benchmark
only:
- triggers
quantization_test:
stage: test
script:
- echo quantization_test
only:
- triggers
model_test:
stage: test
script:
- echo model_test
only:
- triggers
ndk_compatibility_check:
stage: extra
script:
- pushd mace/examples/android/ && bash build.sh static && bash build.sh dynamic && popd
- echo ndk_compatibility_check
only:
- triggers
ops_benchmark:
stage: ops_benchmark
python_compatibility_check:
stage: extra
script:
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- python tools/bazel_adb_run.py --target="//mace/ops:ops_benchmark" --run_target=True --stdout_processor=ops_benchmark_stdout_processor --target_abis=armeabi-v7a,arm64-v8a --target_socs=$TARGET_SOCS --args="--filter=.*SIGMOID.*" || exit 1;
- echo python3 check
only:
- triggers
extra_tests:
stage: extra_tests
script:
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- python tools/bazel_adb_run.py --target="//mace/utils:utils_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS || exit 1;
- python tools/bazel_adb_run.py --target="//mace/port:port_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS || exit 1;
so_size_check:
stage: so_size_check
script:
- DYNAMIC_LIB_PATH="bazel-bin/mace/libmace/libmace.so"
- bazel build -s --config android --config optimization mace/libmace:libmace_dynamic --define neon=true --define openmp=false --define opencl=false --define quantize=false --cpu=armeabi-v7a
- CURRENT_LIBMACE_SO_SIZE=`ls -l $DYNAMIC_LIB_PATH --block-size=K -s | cut -f 1 -d "K"`
- TARGET_MACE_WORK_DIR=`mktemp -d`
- pushd $TARGET_MACE_WORK_DIR
- GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@github.com:XiaoMi/mace.git
- pushd mace
- bazel build -s --config android --config optimization mace/libmace:libmace_dynamic --define neon=true --define openmp=false --define opencl=false --define quantize=false --cpu=armeabi-v7a
- TARGET_LIBMACE_SO_SIZE=`ls -l $DYNAMIC_LIB_PATH --block-size=K -s | cut -f 1 -d "K"`
- popd
- popd
- rm -rf $TARGET_MACE_WORK_DIR
- echo "The libmace.so size grows from $TARGET_LIBMACE_SO_SIZE(KB) to $CURRENT_LIBMACE_SO_SIZE(KB)"
- if [ $TARGET_LIBMACE_SO_SIZE -lt `expr \( $CURRENT_LIBMACE_SO_SIZE - $TARGET_LIBMACE_SO_SIZE \) \* 10` ]; then echo "The libmace.so size grows larger than 10%"; exit 1; fi
dynamic_link_test:
stage: dynamic_link_test
script:
- pwd
- rm -rf mace-models
- rm -rf generic-mobile-devices
- GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@github.com:XiaoMi/mace-models.git
- CONF_FILE=mace-models/mobilenet-v1/mobilenet-v1.yml
- >
if ping -c 1 v9.git.n.xiaomi.com 1>/dev/null 2>&1; then
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git
DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=file --model_data_format=file --cl_mem_type=buffer || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --device_yml=${DEVICE_CONF_FILE} --example --mace_lib_type=dynamic --target_abis=armeabi-v7a,arm64 --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1;
- rm -rf mace-models
python3_test:
stage: python3_test
script:
- pyenv local 3.6.3
- rm -rf mace-models
- GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@github.com:XiaoMi/mace-models.git
- CONF_FILE=mace-models/mobilenet-v1/mobilenet-v1.yml
- if [ -z "$TARGET_SOCS" ]; then TARGET_SOCS=random; fi
- >
python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=file --model_data_format=file || exit 1;
python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1;
- python tools/bazel_adb_run.py --target="//mace/ops:ops_test" --run_target=False --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a --target_socs=$TARGET_SOCS --validate || exit 1;
- pyenv local 2.7.14
stage: extra
script:
- ops_test_disable_neon
- ops_test
- api_test
- extra_tests
- dynamic_link_test
only:
- triggers
# new CUDA support requires 3.8 for Linux/Mac, and 3.9 for Windows
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
message("CMAKE_VERSION: ${CMAKE_VERSION}")
project(mace C CXX)
option(MACE_ENABLE_NEON "whether to enable NEON support" OFF)
option(MACE_ENABLE_QUANTIZE "whether to enable NEON int8 support" OFF)
option(MACE_ENABLE_OPENCL "whether to enable OpenCL support" OFF)
option(MACE_ENABLE_CUDA "whether to enable CUDA support" OFF)
option(MACE_ENABLE_HEXAGON_DSP "whether to enable Hexagon DSP support" OFF)
option(MACE_ENABLE_HEXAGON_HTA "whether to enable Hexagon HTA support" OFF)
option(MACE_ENABLE_TESTS "whether to build c++ unit tests" OFF)
option(MACE_ENABLE_BENCHMARK "whether to build c++ micro benchmarks" OFF)
option(MACE_ENABLE_EXAMPLES "whether to build examples" OFF)
option(MACE_ENABLE_OPT_SIZE "whether to build with optimized binary size" ON)
option(MACE_ENABLE_OBFUSCATE "whether to build with code obfuscation" ON)
message("CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
# TODO make these flags well defined and organized
# TODO enable sanitizer
set(MACE_CC_FLAGS "${MACE_CC_FLAGS} -fPIC")
if(MACE_ENABLE_OPT_SIZE)
if(NOT APPLE)
set(MACE_LINKER_FLAGS "${MACE_LINKER_FLAGS} -Wl,--strip-all -Wl,--gc-sections")
set(MACE_CC_FLAGS "${MACE_CC_FLAGS} -ffunction-sections -fdata-sections")
endif()
set(MACE_CC_FLAGS "${MACE_CC_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden")
set(MACE_CODE_CC_FLAGS "${MACE_CODE_CC_FLAGS} -fno-rtti -fno-exceptions -DGOOGLE_PROTOBUF_NO_RTTI -DPROTOBUF_USE_EXCEPTIONS=0")
endif()
# flags apply only to mace code (third_party excluded)
set(MACE_CODE_CC_FLAGS "${MACE_CODE_CC_FLAGS} -Wall -Werror")
set(MACE_CODE_CC_FLAGS "${MACE_CODE_CC_FLAGS} -std=c++11 -D_GLIBCXX_USE_C99_MATH_TR1")
if(IOS)
# TODO correct the code
set(MACE_CODE_CC_FLAGS "${MACE_CODE_CC_FLAGS} -Wno-error=shorten-64-to-32")
endif()
if(MACE_ENABLE_NEON)
add_definitions(-DMACE_ENABLE_NEON)
if(ANDROID_ABI STREQUAL "armeabi-v7a")
# Enable NEON fp16 support
string(REPLACE "-mfpu=neon " "-mfpu=neon-fp16 " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
endif(ANDROID_ABI STREQUAL "armeabi-v7a")
endif()
if(MACE_ENABLE_QUANTIZE)
add_definitions(-DMACE_ENABLE_QUANTIZE)
add_definitions(-DGEMMLOWP_USE_MACE_THREAD_POOL)
add_definitions(-DMACE_DEPTHWISE_U8_USE_MULTI_THREAD)
endif()
if(MACE_ENABLE_OPENCL)
if(IOS)
message(FATAL_ERROR "OpenCL is not supported for iOS")
endif()
add_definitions(-DMACE_ENABLE_OPENCL)
endif()
if(MACE_ENABLE_CUDA)
# new CUDA support requires 3.8 for Linux/Mac, and 3.9 for Windows
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
enable_language(CUDA)
endif()
if((MACE_ENABLE_HEXAGON_DSP OR MACE_ENABLE_HEXAGON_HTA))
if(ANDROID_ABI STREQUAL "arm64-v8a")
# Use gold linker to avoid linking check of libcdsprpc.so
set(MACE_LINKER_FLAGS "${MACE_LINKER_FLAGS} -fuse-ld=gold")
endif(ANDROID_ABI STREQUAL "arm64-v8a")
endif(MACE_ENABLE_HEXAGON_DSP OR MACE_ENABLE_HEXAGON_HTA)
if(MACE_ENABLE_HEXAGON_DSP)
if(NOT ANDROID)
message(FATAL_ERROR "Hexagon DSP is only supported on Android")
endif(NOT ANDROID)
# TODO => -DMACE_ENABLE_HEXAGON_DSP
add_definitions(-DMACE_ENABLE_HEXAGON)
endif()
if(MACE_ENABLE_HEXAGON_HTA)
if(NOT ANDROID)
message(FATAL_ERROR "Hexagon HTA is only supported on Android")
endif(NOT ANDROID)
add_definitions(-DMACE_ENABLE_HEXAGON_HTA)
endif()
if(MACE_ENABLE_OBFUSCATE)
add_definitions(-DMACE_OBFUSCATE_LITERALS)
endif()
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MACE_CODE_CC_FLAGS} ${MACE_CC_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MACE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MACE_LINKER_FLAGS}")
endif()
include(${PROJECT_SOURCE_DIR}/third_party/third_party.cmake)
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}/include")
include_directories("${PROJECT_BINARY_DIR}") # proto
add_subdirectory(include)
add_subdirectory(mace)
if(MACE_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()
if(MACE_ENABLE_TESTS OR MACE_ENABLE_BENCHMARK)
add_subdirectory(test)
endif()
......@@ -61,7 +61,6 @@ new_http_archive(
strip_prefix = "half-code-356-trunk",
urls = [
"https://cnbj1.fds.api.xiaomi.com/mace/third-party/half/half-code-356-trunk.zip",
"https://sourceforge.net/code-snapshots/svn/h/ha/half/code/half-code-356-trunk.zip",
],
)
......
# Tested with gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu
# https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER "${CROSSTOOL_ROOT}/bin/aarch64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "${CROSSTOOL_ROOT}/bin/aarch64-linux-gnu-g++")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_CXX_FLAGS "-march=armv8-a ${CMAKE_CXX_FLAGS}")
# Tested with gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi
# https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "${CROSSTOOL_ROOT}/bin/arm-linux-gnueabi-gcc")
set(CMAKE_CXX_COMPILER "${CROSSTOOL_ROOT}/bin/arm-linux-gnueabi-g++")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=softfp -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}")
# Tested with gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf
# https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "${CROSSTOOL_ROOT}/bin/arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER "${CROSSTOOL_ROOT}/bin/arm-linux-gnueabihf-g++")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}")
此差异已折叠。
......@@ -104,7 +104,7 @@ RUN cd /opt/ && \
unzip -q android-ndk-r15c-linux-x86_64.zip && \
rm -f android-ndk-r15c-linux-x86_64.zip
ENV ANDROID_NDK_VERSION r15c
ENV ANDROID_NDK_VERSION r19c
ENV ANDROID_NDK_HOME /opt/android-ndk-${ANDROID_NDK_VERSION}
# add to PATH
......
......@@ -73,14 +73,45 @@ RUN wget https://cmake.org/files/v3.11/cmake-3.11.3-Linux-x86_64.tar.gz -P /tmp/
update-alternatives --install /usr/bin/cmake cmake /usr/local/bin/cmake 1 --force && \
rm -f /tmp/cmake-3.11.3-Linux-x86_64.tar.gz
# mace-dev-lite image already included NDK r15c
## Download other NDK r16
# mace-dev-lite image already included NDK r19c
## Download other NDK r15c
RUN cd /opt/ && \
wget -q https://dl.google.com/android/repository/android-ndk-r16-linux-x86_64.zip && \
unzip -q android-ndk-r16-linux-x86_64.zip && \
rm -f android-ndk-r16-linux-x86_64.zip
wget -q https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip && \
unzip -q android-ndk-r15c-linux-x86_64.zip && \
rm -f android-ndk-r15c-linux-x86_64.zip
## Download other NDK r16b
RUN cd /opt/ && \
wget -q https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip && \
unzip -q android-ndk-r16b-linux-x86_64.zip && \
rm -f android-ndk-r16b-linux-x86_64.zip
## Download other NDK r17b
RUN cd /opt/ && \
wget -q https://dl.google.com/android/repository/android-ndk-r17b-linux-x86_64.zip && \
unzip -q android-ndk-r17b-linux-x86_64.zip && \
rm -f android-ndk-r17b-linux-x86_64.zip
## Download other NDK r18b
RUN cd /opt/ && \
wget -q https://dl.google.com/android/repository/android-ndk-r18b-linux-x86_64.zip && \
unzip -q android-ndk-r18b-linux-x86_64.zip && \
rm -f android-ndk-r18b-linux-x86_64.zip
# Mirror of https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/arm-linux-gnueabihf/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
RUN cd /opt && \
wget -q https://cnbj1.fds.api.xiaomi.com/mace/third-party/gcc-linaro/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz && \
tar xf gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz && \
rm -rf gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
# Mirror of https://cnbj1.fds.api.xiaomi.com/mace/third-party/gcc-linaro/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
RUN cd /opt && \
wget -q https://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
tar xf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz && \
rm -rf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
ENV CROSS_TOOLCHAIN_PARENT /opt
ENV ANDROID_NDK_VERSION r19c
ENV ANDROID_NDK_HOME /opt/android-ndk-${ANDROID_NDK_VERSION}
ENV LINARO_ARM_LINUX_GNUEABIHF /opt/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf
ENV LINARO_AARCH64_LINUX_GNU /opt/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu
file(GLOB MACE_EXAMPLE_SRCS
cli/example.cc
)
add_executable(mace_example ${MACE_EXAMPLE_SRCS})
target_link_libraries(mace_example PUBLIC
mace_static
gflags
)
install(TARGETS mace_example RUNTIME DESTINATION bin)
install(DIRECTORY mace DESTINATION include)
......@@ -16,11 +16,18 @@
#define MACE_PORT_ENV_H_
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#ifdef _WIN32
#include <malloc.h>
#endif
#include <sys/stat.h>
#include "mace/public/mace.h"
namespace mace {
......@@ -38,6 +45,7 @@ class LogWriter;
class Env {
public:
virtual int64_t NowMicros() = 0;
virtual MaceStatus AdviseFree(void *addr, size_t length);
virtual MaceStatus GetCPUMaxFreq(std::vector<float> *max_freqs);
virtual MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids);
virtual FileSystem *GetFileSystem() = 0;
......@@ -58,6 +66,10 @@ inline int64_t NowMicros() {
return port::Env::Default()->NowMicros();
}
inline MaceStatus AdviseFree(void *addr, size_t length) {
return port::Env::Default()->AdviseFree(addr, length);
}
inline MaceStatus GetCPUMaxFreq(std::vector<float> *max_freqs) {
return port::Env::Default()->GetCPUMaxFreq(max_freqs);
}
......@@ -70,6 +82,59 @@ inline port::FileSystem *GetFileSystem() {
return port::Env::Default()->GetFileSystem();
}
inline MaceStatus Memalign(void **memptr, size_t alignment, size_t size) {
#ifdef _WIN32
*memptr = _aligned_malloc(size, alignment);
if (*memptr == nullptr) {
return MaceStatus::MACE_OUT_OF_RESOURCES;
} else {
return MaceStatus::MACE_SUCCESS;
}
#else
#if defined(__ANDROID__) || defined(__hexagon__)
*memptr = memalign(alignment, size);
if (*memptr == nullptr) {
return MaceStatus::MACE_OUT_OF_RESOURCES;
} else {
return MaceStatus::MACE_SUCCESS;
}
#else
int error = posix_memalign(memptr, alignment, size);
if (error != 0) {
if (*memptr != nullptr) {
free(*memptr);
*memptr = nullptr;
}
return MaceStatus::MACE_OUT_OF_RESOURCES;
} else {
return MaceStatus::MACE_SUCCESS;
}
#endif
#endif
}
inline MaceStatus GetEnv(const char *name, std::string *value) {
#ifdef _WIN32
char *val;
size_t len;
errno_t error = _dupenv_s(&val, &len, name);
if (error != 0) {
return MaceStatus::MACE_RUNTIME_ERROR;
} else {
*value = std::string(val);
free(val);
return MaceStatus::MACE_SUCCESS;
}
#else
char *val = getenv(name);
*value = std::string(val);
return MaceStatus::MACE_SUCCESS;
#endif
}
#if defined(_WIN32) && !defined(S_ISREG)
#define S_ISREG(m) (((m) & 0170000) == (0100000))
#endif
} // namespace mace
#endif // MACE_PORT_ENV_H_
......@@ -19,6 +19,7 @@
#include <memory>
#include "mace/public/mace.h"
#include "mace/utils/macros.h"
namespace mace {
namespace port {
......@@ -29,6 +30,8 @@ class ReadOnlyMemoryRegion {
virtual ~ReadOnlyMemoryRegion() = default;
virtual const void *data() const = 0;
virtual uint64_t length() const = 0;
private:
MACE_DISABLE_COPY_AND_ASSIGN(ReadOnlyMemoryRegion);
};
class ReadOnlyBufferMemoryRegion : public ReadOnlyMemoryRegion {
......@@ -44,12 +47,25 @@ class ReadOnlyBufferMemoryRegion : public ReadOnlyMemoryRegion {
uint64_t length_;
};
class WritableFile {
public:
WritableFile() {}
virtual ~WritableFile();
virtual MaceStatus Append(const char *data, size_t length) = 0;
virtual MaceStatus Close() = 0;
virtual MaceStatus Flush() = 0;
private:
MACE_DISABLE_COPY_AND_ASSIGN(WritableFile);
};
class FileSystem {
public:
FileSystem() = default;
virtual ~FileSystem() = default;
virtual MaceStatus NewReadOnlyMemoryRegionFromFile(const char *fname,
std::unique_ptr<ReadOnlyMemoryRegion>* result) = 0;
virtual MaceStatus NewWritableFile(const char *fname,
std::unique_ptr<WritableFile>* result);
};
} // namespace port
......
......@@ -25,8 +25,20 @@
#include <vector>
#ifndef MACE_API
#ifdef _MSC_VER
#define MACE_API
#else
#define MACE_API __attribute__((visibility("default")))
#endif
#endif
#ifndef MACE_DEPRECATED
#ifdef _MSC_VER
#define MACE_DEPRECATED
#else
#define MACE_DEPRECATED __attribute__((deprecated))
#endif
#endif
namespace mace {
......@@ -435,7 +447,8 @@ MACE_API MaceStatus CreateMaceEngineFromProto(
const std::vector<std::string> &input_nodes,
const std::vector<std::string> &output_nodes,
const MaceEngineConfig &config,
std::shared_ptr<MaceEngine> *engine) __attribute__((deprecated));
std::shared_ptr<MaceEngine> *engine) MACE_DEPRECATED;
} // namespace mace
......
......@@ -75,6 +75,15 @@ T &&CheckNotNull(const char *file, int line, const char *exprtext, T &&t) {
#define MACE_NOT_IMPLEMENTED MACE_CHECK(false, "not implemented")
#define MACE_CHECK_SUCCESS(stmt) \
{ \
MaceStatus status = (stmt); \
if (status != MaceStatus::MACE_SUCCESS) { \
LOG(FATAL) << #stmt << " failed with error: " \
<< status.information(); \
} \
}
#define MACE_RETURN_IF_ERROR(stmt) \
{ \
MaceStatus status = (stmt); \
......
add_subdirectory(codegen)
add_subdirectory(core)
add_subdirectory(libmace)
add_subdirectory(ops)
add_subdirectory(port)
add_subdirectory(proto)
add_subdirectory(tools)
add_subdirectory(utils)
# Generate MACE version source
set(MACE_VERSION_GEN_TOOL "${CMAKE_CURRENT_SOURCE_DIR}/tools/gen_version_source.sh")
set(MACE_VERSION_SRC "${CMAKE_CURRENT_BINARY_DIR}/version/version.cc")
add_custom_command(
OUTPUT ${MACE_VERSION_SRC}
COMMAND ${MACE_VERSION_GEN_TOOL} ${MACE_VERSION_SRC}
DEPENDS ${MACE_VERSION_GEN_TOOL}
COMMENT "Generating version source file" )
add_custom_target(version_src DEPENDS ${MACE_VERSION_SRC}
COMMENT "Checking if re-generation is required" )
add_library(generated_version ${MACE_VERSION_SRC})
add_dependencies(generated_version version_src)
# Generate OpenCL kernel source
set(MACE_OPENCL_KERNELS_GEN_TOOL "${PROJECT_SOURCE_DIR}/mace/python/tools/encrypt_opencl_codegen.py")
set(MACE_OPENCL_KERNELS_DIR "${PROJECT_SOURCE_DIR}/mace/ops/opencl/cl")
set(MACE_OPENCL_KERNELS_SRC "${CMAKE_CURRENT_BINARY_DIR}/opencl/encrypt_opencl_kernel.cc")
add_custom_command(
OUTPUT ${MACE_OPENCL_KERNELS_SRC}
COMMAND python ${MACE_OPENCL_KERNELS_GEN_TOOL} --output_path=${MACE_OPENCL_KERNELS_SRC} --cl_kernel_dir=${MACE_OPENCL_KERNELS_DIR}
DEPENDS ${MACE_OPENCL_KERNELS_GEN_TOOL}
COMMENT "Generating OpenCL kernel source file" )
add_custom_target(opencl_kernel_src DEPENDS ${MACE_OPENCL_KERNELS_SRC}
COMMENT "Checking if re-generation is required" )
add_library(generated_opencl_kernel ${MACE_OPENCL_KERNELS_SRC})
add_dependencies(generated_opencl_kernel opencl_kernel_src)
install(TARGETS generated_version ARCHIVE DESTINATION lib)
install(TARGETS generated_opencl_kernel ARCHIVE DESTINATION lib)
......@@ -13,29 +13,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
MACE_SOURCE_DIR=$(dirname $(dirname $(dirname $(dirname $0))))
OUTPUT_FILENAME=$1
if [[ -z "${OUTPUT_FILENAME}}" ]]; then
if [[ -z "${OUTPUT_FILENAME}" ]]; then
echo "Usage: $0 <filename>"
exit 1
fi
OUTPUT_DIR=$(dirname $OUTPUT_FILENAME)
if [ -d $OUTPUT_DIR ]; then
rm -rf $OUTPUT_DIR
fi
mkdir -p $OUTPUT_DIR
mkdir -p $(dirname $OUTPUT_FILENAME)
GIT_VERSION=$(git --git-dir=${MACE_SOURCE_DIR}/.git --work-tree=${MACE_SOURCE_DIR} describe --long --tags)
GIT_VERSION=$(git describe --tags --long)
if [[ $? != 0 ]]; then
GIT_VERSION=unknown
else
GIT_VERSION=${GIT_VERSION}
fi
echo $GIT_VERSION
echo write version $GIT_VERSION to ${OUTPUT_FILENAME}
cat <<EOF > ${OUTPUT_FILENAME}
// Copyright 2018 The MACE Authors. All Rights Reserved.
......@@ -55,7 +48,9 @@ cat <<EOF > ${OUTPUT_FILENAME}
// This is a generated file. DO NOT EDIT!
namespace mace {
#ifndef _MSC_VER
__attribute__((visibility("default")))
#endif
const char *MaceVersion() { return "MACEVER-${GIT_VERSION}" + 8; }
} // namespace mace
EOF
......
set(CORE_SRCS
allocator.cc
arg_helper.cc
device.cc
device_context.cc
kv_storage.cc
memory_optimizer.cc
net.cc
net_def_adapter.cc
net_optimizer.cc
op_context.cc
operator.cc
quantize.cc
runtime_failure_mock.cc
types.cc
workspace.cc
runtime/cpu/cpu_runtime.cc
)
if(MACE_ENABLE_OPENCL)
set(CORE_SRCS ${CORE_SRCS}
runtime/opencl/gpu_device.cc
runtime/opencl/gpu_runtime.cc
runtime/opencl/opencl_allocator.cc
runtime/opencl/opencl_runtime.cc
runtime/opencl/opencl_util.cc
runtime/opencl/opencl_wrapper.cc
runtime/opencl/scratch_image.cc
)
set(EXTRA_LINK_LIBS ${EXTRA_LINK_LIBS} generated_opencl_kernel dl)
endif(MACE_ENABLE_OPENCL)
if(MACE_ENABLE_HEXAGON_DSP)
set(CORE_SRCS ${CORE_SRCS} runtime/hexagon/hexagon_dsp_wrapper.cc)
set(EXTRA_LINK_LIBS ${EXTRA_LINK_LIBS} hexagon_controller)
endif(MACE_ENABLE_HEXAGON_DSP)
if(MACE_ENABLE_HEXAGON_HTA)
set(CORE_SRCS ${CORE_SRCS} runtime/hexagon/hexagon_hta_wrapper.cc)
set(EXTRA_LINK_LIBS ${EXTRA_LINK_LIBS} hta_controller hta_hexagon_runtime npu)
endif(MACE_ENABLE_HEXAGON_HTA)
add_library(core STATIC ${CORE_SRCS})
target_link_libraries(core PRIVATE
proto
utils
port
generated_version
${EXTRA_LINK_LIBS}
)
add_dependencies(core half)
if(MACE_ENABLE_OPENCL)
add_dependencies(core opencl_clhpp)
endif(MACE_ENABLE_OPENCL)
if(MACE_ENABLE_QUANTIZE)
add_dependencies(core gemmlowp)
endif(MACE_ENABLE_QUANTIZE)
install(TARGETS core ARCHIVE DESTINATION lib)
......@@ -14,10 +14,6 @@
#include "mace/core/allocator.h"
#include <unistd.h>
#include <sys/mman.h>
#include <memory>
namespace mace {
Allocator *GetCPUAllocator() {
......@@ -25,22 +21,4 @@ Allocator *GetCPUAllocator() {
return &allocator;
}
void AdviseFree(void *addr, size_t length) {
int page_size = sysconf(_SC_PAGESIZE);
void *addr_aligned =
reinterpret_cast<void *>(
(reinterpret_cast<uintptr_t>(addr) + page_size - 1)
& (~(page_size - 1)));
uintptr_t delta =
reinterpret_cast<uintptr_t>(addr_aligned)
- reinterpret_cast<uintptr_t>(addr);
if (length >= delta + page_size) {
size_t len_aligned = (length - delta) & (~(page_size - 1));
int ret = madvise(addr_aligned, len_aligned, MADV_DONTNEED);
if (ret != 0) {
LOG(ERROR) << "Advise free failed: " << strerror(errno);
}
}
}
} // namespace mace
......@@ -74,32 +74,9 @@ class CPUAllocator : public Allocator {
return MaceStatus::MACE_OUT_OF_RESOURCES;
}
void *data = nullptr;
#if defined(__ANDROID__) || defined(__hexagon__)
data = memalign(kMaceAlignment, nbytes);
if (data == NULL) {
LOG(WARNING) << "Allocate CPU Buffer with "
<< nbytes << " bytes failed because of"
<< strerror(errno);
*result = nullptr;
return MaceStatus::MACE_OUT_OF_RESOURCES;
}
#else
int ret = posix_memalign(&data, kMaceAlignment, nbytes);
if (ret != 0) {
LOG(WARNING) << "Allocate CPU Buffer with "
<< nbytes << " bytes failed because of"
<< strerror(errno);
if (data != NULL) {
free(data);
}
*result = nullptr;
return MaceStatus::MACE_OUT_OF_RESOURCES;
}
#endif
MACE_RETURN_IF_ERROR(Memalign(result, kMaceAlignment, nbytes));
// TODO(heliangliang) This should be avoided sometimes
memset(data, 0, nbytes);
*result = data;
memset(*result, 0, nbytes);
return MaceStatus::MACE_SUCCESS;
}
......@@ -143,8 +120,6 @@ class CPUAllocator : public Allocator {
// Global CPU allocator used for CPU/GPU/DSP
Allocator *GetCPUAllocator();
void AdviseFree(void *addr, size_t length);
} // namespace mace
#endif // MACE_CORE_ALLOCATOR_H_
......@@ -16,6 +16,8 @@
#include <sys/stat.h>
#include "mace/port/env.h"
namespace mace {
namespace {
......
......@@ -12,11 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <climits>
#include <algorithm>
#include <cstring>
......@@ -26,6 +21,8 @@
#include "mace/core/kv_storage.h"
#include "mace/utils/macros.h"
#include "mace/utils/logging.h"
#include "mace/port/env.h"
#include "mace/port/file_system.h"
namespace mace {
......@@ -98,69 +95,24 @@ FileStorage::FileStorage(const std::string &file_path):
loaded_(false), data_changed_(false), file_path_(file_path) {}
int FileStorage::Load() {
struct stat st;
if (stat(file_path_.c_str(), &st) == -1) {
if (errno == ENOENT) {
VLOG(1) << "File " << file_path_
<< " does not exist";
return 0;
} else {
LOG(WARNING) << "Stat file " << file_path_
<< " failed, error code: " << strerror(errno);
return -1;
}
}
utils::WriteLock lock(&data_mutex_);
if (loaded_) {
return 0;
}
int fd = open(file_path_.c_str(), O_RDONLY);
if (fd < 0) {
if (errno == ENOENT) {
LOG(INFO) << "File " << file_path_
<< " does not exist";
return 0;
} else {
LOG(WARNING) << "open file " << file_path_
<< " failed, error code: " << strerror(errno);
return -1;
}
}
size_t file_size = st.st_size;
unsigned char *file_data =
static_cast<unsigned char *>(mmap(nullptr, file_size, PROT_READ,
MAP_PRIVATE, fd, 0));
int res = 0;
if (file_data == MAP_FAILED) {
LOG(WARNING) << "mmap file " << file_path_
<< " failed, error code: " << strerror(errno);
res = close(fd);
if (res != 0) {
LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << strerror(errno);
}
std::unique_ptr<mace::port::ReadOnlyMemoryRegion> kv_data =
make_unique<mace::port::ReadOnlyBufferMemoryRegion>();
auto fs = GetFileSystem();
auto status = fs->NewReadOnlyMemoryRegionFromFile(
file_path_.c_str(), &kv_data);
if (status != MaceStatus::MACE_SUCCESS) {
LOG(ERROR) << "Failed to read kv store file: " << file_path_;
return -1;
} else {
ParseKVData(static_cast<const unsigned char *>(kv_data->data()),
kv_data->length(), &data_);
}
ParseKVData(file_data, file_size, &data_);
res = munmap(file_data, file_size);
if (res != 0) {
LOG(WARNING) << "munmap file " << file_path_
<< " failed, error code: " << strerror(errno);
res = close(fd);
if (res != 0) {
LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << strerror(errno);
}
return -1;
}
res = close(fd);
if (res != 0) {
LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << strerror(errno);
return -1;
}
loaded_ = true;
return 0;
}
......@@ -196,8 +148,10 @@ const std::vector<unsigned char> *FileStorage::Find(const std::string &key) {
int FileStorage::Flush() {
utils::WriteLock lock(&data_mutex_);
if (!data_changed_) return 0;
int fd = open(file_path_.c_str(), O_WRONLY | O_CREAT, 0600);
if (fd < 0) {
auto fs = GetFileSystem();
std::unique_ptr<port::WritableFile> file;
MaceStatus s = fs->NewWritableFile(file_path_.c_str(), &file);
if (s != MaceStatus::MACE_SUCCESS) {
LOG(WARNING) << "open file " << file_path_
<< " failed, error code: " << strerror(errno);
return -1;
......@@ -209,8 +163,8 @@ int FileStorage::Flush() {
for (auto &kv : data_) {
data_size += 2 * int_size + kv.first.size() + kv.second.size();
}
std::unique_ptr<unsigned char[]> buffer(new unsigned char[data_size]);
unsigned char *buffer_ptr = &buffer[0];
std::unique_ptr<char[]> buffer = make_unique<char[]>(data_size);
char *buffer_ptr = buffer.get();
int64_t num_of_data = data_.size();
memcpy(buffer_ptr, &num_of_data, sizeof(int64_t));
......@@ -230,32 +184,16 @@ int FileStorage::Flush() {
memcpy(buffer_ptr, kv.second.data(), kv.second.size());
buffer_ptr += kv.second.size();
}
int res = 0;
buffer_ptr = &buffer[0];
int64_t remain_size = data_size;
while (remain_size > 0) {
size_t buffer_size = std::min<int64_t>(remain_size, SSIZE_MAX);
res = write(fd, buffer_ptr, buffer_size);
if (res == -1) {
LOG(WARNING) << "write file " << file_path_
<< " failed, error code: " << strerror(errno);
res = close(fd);
if (res != 0) {
LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << strerror(errno);
}
return -1;
}
remain_size -= buffer_size;
buffer_ptr += buffer_size;
s = file->Append(buffer.get(), data_size);
if (s != MaceStatus::MACE_SUCCESS) {
file->Close();
return -1;
}
res = close(fd);
if (res != 0) {
LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << strerror(errno);
s = file->Close();
if (s != MaceStatus::MACE_SUCCESS) {
return -1;
}
data_changed_ = false;
return 0;
}
......
......@@ -32,7 +32,7 @@
namespace mace {
HexagonHTAWrapper::HexagonHTAWrapper(Device *device)
: device_(device), quantize_util_(&device->cpu_runtime()->thread_pool()) {
: quantize_util_(&device->cpu_runtime()->thread_pool()) {
}
int HexagonHTAWrapper::GetVersion() {
......
......@@ -50,7 +50,6 @@ class HexagonHTAWrapper : public HexagonControlWrapper {
void SetDebugLevel(int level) override;
private:
Device *device_;
QuantizeUtil<uint8_t> quantize_util_;
MACE_DISABLE_COPY_AND_ASSIGN(HexagonHTAWrapper);
};
......
......@@ -37,7 +37,7 @@
#pragma GCC diagnostic ignored "-Wignored-attributes"
#endif // MACE_OS_MAC
#include "include/CL/cl2.hpp"
#include "CL/cl2.hpp"
#ifdef MACE_OS_MAC
#pragma GCC diagnostic pop
......
......@@ -147,8 +147,8 @@ void *OpenCLAllocator::MapImage(void *buffer,
VLOG(3) << "Map OpenCL Image";
MACE_CHECK(image_shape.size() == 2) << "Just support map 2d image";
auto cl_image = static_cast<cl::Image2D *>(buffer);
std::array<size_t, 3> origin = {0, 0, 0};
std::array<size_t, 3> region = {image_shape[0], image_shape[1], 1};
std::array<size_t, 3> origin = {{0, 0, 0}};
std::array<size_t, 3> region = {{image_shape[0], image_shape[1], 1}};
mapped_image_pitch->resize(2);
cl_int error;
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册