From bca9d4a6e1c9976d17263c4af90c85d102478ace Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 30 May 2019 18:57:26 +0800 Subject: [PATCH] Lite enable armlinux compile and android simulator (#17752) * enable armlinux compile * fix deps and skip some build * update doc and script * add android simulator test=develop --- CMakeLists.txt | 35 +++++-- cmake/cross_compiling/android.cmake | 37 ++++++-- cmake/cross_compiling/armlinux.cmake | 57 +++++++++++ paddle/fluid/lite/arm/math/CMakeLists.txt | 3 + paddle/fluid/lite/core/CMakeLists.txt | 2 +- paddle/fluid/lite/core/context.cc | 10 +- paddle/fluid/lite/core/cpu_info.cc | 12 +-- paddle/fluid/lite/core/cpu_info.h | 6 +- .../fluid/lite/tools/Dockerfile.cross_compile | 61 ------------ paddle/fluid/lite/tools/Dockerfile.mobile | 90 ++++++++++++++++++ paddle/fluid/lite/tools/build.sh | 48 ++++++++-- paddle/fluid/lite/tools/mobile_readme.md | 94 ++++++------------- 12 files changed, 290 insertions(+), 165 deletions(-) create mode 100644 cmake/cross_compiling/armlinux.cmake delete mode 100644 paddle/fluid/lite/tools/Dockerfile.cross_compile create mode 100644 paddle/fluid/lite/tools/Dockerfile.mobile diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e044525d84..93fbc04ca1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,15 +21,32 @@ include(system) if(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) cmake_minimum_required(VERSION 3.10) - # only android yet - set(ANDROID TRUE) - include(cross_compiling/android) + # TODO(TJ): make as function check_default + if(NOT DEFINED ARM_TARGET_OS) + set(ARM_TARGET_OS "android" CACHE STRING "Choose ARM Target OS") + endif() + set(ARM_TARGET_OS_LIST "android" "armlinux") # TODO: "ios" + set_property(CACHE ARM_TARGET_OS PROPERTY STRINGS ${ARM_TARGET_OS_LIST}) + if (NOT ARM_TARGET_OS IN_LIST ARM_TARGET_OS_LIST) + message(FATAL_ERROR "ARM_TARGET_OS must be in one of ${ARM_TARGET_OS_LIST}") + endif() + + if(NOT DEFINED ARM_TARGET_ARCH_ABI) + set(ARM_TARGET_ARCH_ABI "arm64-v8a" CACHE STRING "Choose ARM Target ARCH ABI") + endif() + set(ARM_TARGET_ARCH_ABI_LIST "arm64-v8a" "armeabi-v7a" "armeabi-v7a-softfp" "armeabi-v7a-hf") + set_property(CACHE ARM_TARGET_ARCH_ABI PROPERTY STRINGS ${ARM_TARGET_ARCH_ABI_LIST}) + if (NOT ARM_TARGET_ARCH_ABI IN_LIST ARM_TARGET_ARCH_ABI_LIST) + message(FATAL_ERROR "ARM_TARGET_ARCH_ABI must be in one of ${ARM_TARGET_ARCH_ABI_LIST}") + endif() + + if(NOT DEFINED TARGET_ARCH_ABI) + set(ARCH_ABI "arm64-v8a" CACHE STRING "Choose android platform") + endif() + include(cross_compiling/host) - set(CMAKE_SYSTEM_NAME Android) - set(CMAKE_SYSTEM_VERSION ${ANDROID_API_LEVEL}) - set(CMAKE_ANDROID_ARCH_ABI ${ANDROID_ARCH_ABI}) - set(CMAKE_ANDROID_NDK ${ANDROID_NDK}) - set(CMAKE_ANDROID_STL_TYPE ${ANDROID_STL_TYPE}) + include(cross_compiling/armlinux) + include(cross_compiling/android) endif() project(paddle CXX C) @@ -94,7 +111,7 @@ option(PY_VERSION "Compile PaddlePaddle with python3 support" ${PY_VER option(WITH_FAST_MATH "Make use of fast math library, might affect the precision to some extent" ON) option(WITH_DGC "Use DGC(Deep Gradient Compression) or not" ON) -if(ANDROID OR IOS) +if(ANDROID OR IOS OR ARMLINUX) set(WITH_GPU OFF CACHE STRING "Disable GPU when cross-compiling for Android and iOS" FORCE) set(WITH_DSO OFF CACHE STRING diff --git a/cmake/cross_compiling/android.cmake b/cmake/cross_compiling/android.cmake index dedfad8c7ab..e57f32aae7c 100644 --- a/cmake/cross_compiling/android.cmake +++ b/cmake/cross_compiling/android.cmake @@ -12,11 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -if(NOT ANDROID) +if(NOT ARM_TARGET_OS STREQUAL "android") return() endif() -add_definitions(-DLITE_WITH_ANDROID) +set(ANDROID TRUE) +add_definitions(-DLITE_WITH_LINUX) if(NOT DEFINED ANDROID_NDK) set(ANDROID_NDK $ENV{NDK_ROOT}) @@ -25,9 +26,6 @@ if(NOT DEFINED ANDROID_NDK) endif() endif() -if(NOT DEFINED ANDROID_ARCH_ABI) - set(ANDROID_ARCH_ABI "arm64-v8a" CACHE STRING "Choose android platform") -endif() if(NOT DEFINED ANDROID_API_LEVEL) set(ANDROID_API_LEVEL "22") @@ -37,22 +35,45 @@ if(NOT DEFINED ANDROID_STL_TYPE) set(ANDROID_STL_TYPE "c++_static" CACHE STRING "stl type") endif() +# TODO(TJ): enable me +if(ARM_TARGET_ARCH_ABI STREQUAL "armeabi-v7a-hf") + message(FATAL_ERROR "Not supported building android armeabi-v7a-hf yet") +endif() + +set(ANDROID_ARCH_ABI ${ARM_TARGET_ARCH_ABI} CACHE STRING "Choose Android Arch ABI") + +if(ANDROID_ARCH_ABI STREQUAL "armeabi-v7a-softfp") + set(ANDROID_ARCH_ABI "armeabi-v7a") +endif() + set(ANDROID_ARCH_ABI_LIST "arm64-v8a" "armeabi-v7a" "armeabi-v6" "armeabi" - "mips" "mips64" "x86" "x86_64") + "mips" "mips64" "x86" "x86_64" "armeabi-v7a-hf") set_property(CACHE ANDROID_ARCH_ABI PROPERTY STRINGS ${ANDROID_ARCH_ABI_LIST}) -if (NOT ANDROID_ARCH_ABI IN_LIST ANDROID_ARCH_ABI_LIST) +if(NOT ANDROID_ARCH_ABI IN_LIST ANDROID_ARCH_ABI_LIST) message(FATAL_ERROR "ANDROID_ARCH_ABI must be in one of ${ANDROID_ARCH_ABI_LIST}") endif() if(ANDROID_ARCH_ABI STREQUAL "armeabi-v7a") + message(STATUS "armeabi-v7a default use softfp") set(CMAKE_ANDROID_ARM_NEON ON) message(STATUS "NEON is enabled on arm-v7a with softfp") endif() +if(ANDROID_ARCH_ABI STREQUAL "armeabi-v7a-hf") + set(ANDROID_ARCH_ABI "armeabi-v7a") + set(CMAKE_CXX_FLAGS "-std=c++11 -march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}" ) + set(CMAKE_C_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_C_FLAGS}" ) + message(STATUS "NEON is enabled on arm-v7a with hard float") +endif() + set(ANDROID_STL_TYPE_LITS "gnustl_static" "c++_static") set_property(CACHE ANDROID_STL_TYPE PROPERTY STRINGS ${ANDROID_STL_TYPE_LITS}) if (NOT ANDROID_STL_TYPE IN_LIST ANDROID_STL_TYPE_LITS) message(FATAL_ERROR "ANDROID_STL_TYPE must be in one of ${ANDROID_STL_TYPE_LITS}") endif() -set(ANDROID_PIE TRUE) +set(CMAKE_SYSTEM_NAME Android) +set(CMAKE_SYSTEM_VERSION ${ANDROID_API_LEVEL}) +set(CMAKE_ANDROID_ARCH_ABI ${ANDROID_ARCH_ABI}) +set(CMAKE_ANDROID_NDK ${ANDROID_NDK}) +set(CMAKE_ANDROID_STL_TYPE ${ANDROID_STL_TYPE}) diff --git a/cmake/cross_compiling/armlinux.cmake b/cmake/cross_compiling/armlinux.cmake new file mode 100644 index 00000000000..1d752075cca --- /dev/null +++ b/cmake/cross_compiling/armlinux.cmake @@ -0,0 +1,57 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if(NOT ARM_TARGET_OS STREQUAL "armlinux") + return() +endif() + +set(ARMLINUX TRUE) +add_definitions(-DLITE_WITH_LINUX) +set(CMAKE_SYSTEM_NAME Linux) + +if(ARM_TARGET_ARCH_ABI STREQUAL "arm64-v8a") + set(CMAKE_SYSTEM_PROCESSOR aarch64) + set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc") + set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++") + + set(CMAKE_CXX_FLAGS "-march=armv8-a ${CMAKE_CXX_FLAGS}") + set(CMAKE_C_FLAGS "-march=armv8-a ${CMAKE_C_FLAGS}") + message(STATUS "NEON is enabled on arm64-v8a") +endif() + +if(ARM_TARGET_ARCH_ABI STREQUAL "armeabi-v7a" + OR ARM_TARGET_ARCH_ABI STREQUAL "armeabi-v7a-hf") + message(FATAL_ERROR "Not supported building arm linux arm-v7 yet") +endif() + +# TODO(TJ): make sure v7 works +if(ARM_TARGET_ARCH_ABI STREQUAL "armeabi-v7a") + set(CMAKE_SYSTEM_PROCESSOR arm) + set(CMAKE_C_COMPILER "arm-linux-gnueabi-gcc") + set(CMAKE_CXX_COMPILER "arm-linux-gnueabi-g++") + + set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=softfp -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}") + set(CMAKE_C_FLAGS "-march=armv7-a -mfloat-abi=softfp -mfpu=neon-vfpv4 ${CMAKE_C_FLAGS}") + message(STATUS "NEON is enabled on arm-v7a with softfp") +endif() + +if(ARM_TARGET_ARCH_ABI STREQUAL "armeabi-v7a-hf") + set(CMAKE_SYSTEM_PROCESSOR arm) + set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc") + set(CMAKE_CXX_COMPILER "arm-linux-gnueabihf-g++") + + set(CMAKE_CXX_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_CXX_FLAGS}") + set(CMAKE_C_FLAGS "-march=armv7-a -mfloat-abi=hard -mfpu=neon-vfpv4 ${CMAKE_C_FLAGS}" ) + message(STATUS "NEON is enabled on arm-v7a with hard float") +endif() diff --git a/paddle/fluid/lite/arm/math/CMakeLists.txt b/paddle/fluid/lite/arm/math/CMakeLists.txt index 0418ebc9dca..02a72ad8a73 100644 --- a/paddle/fluid/lite/arm/math/CMakeLists.txt +++ b/paddle/fluid/lite/arm/math/CMakeLists.txt @@ -1,3 +1,6 @@ +if(NOT (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK AND LITE_WITH_ARM)) + return() +endif() if(NOT (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK AND LITE_WITH_ARM)) return() diff --git a/paddle/fluid/lite/core/CMakeLists.txt b/paddle/fluid/lite/core/CMakeLists.txt index ddeae5834af..4ccb2062ae1 100644 --- a/paddle/fluid/lite/core/CMakeLists.txt +++ b/paddle/fluid/lite/core/CMakeLists.txt @@ -19,7 +19,7 @@ endif() proto_library(framework_proto_lite SRCS framework.proto) -cc_library(kernel_lite SRCS kernel.cc DEPS type_system target_wrapper_lite any_lite op_params_lite) +cc_library(kernel_lite SRCS kernel.cc DEPS type_system target_wrapper_lite any_lite op_params_lite framework_proto_lite) cc_library(variable_lite SRCS variable.cc) cc_library(op_registry_lite SRCS op_registry.cc DEPS framework_proto_lite) cc_library(scope_lite SRCS scope.cc DEPS ${tensor_lite}) diff --git a/paddle/fluid/lite/core/context.cc b/paddle/fluid/lite/core/context.cc index c2dfe2aba95..2c36c60159c 100644 --- a/paddle/fluid/lite/core/context.cc +++ b/paddle/fluid/lite/core/context.cc @@ -15,7 +15,7 @@ #include "paddle/fluid/lite/core/context.h" #include "paddle/fluid/lite/core/cpu_info.h" -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX #include #include #endif @@ -87,7 +87,7 @@ void ARMContext::BindDev() { #ifdef USE_OPENMP int num_threads = active_ids_.size(); omp_set_num_threads(num_threads); -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX std::vector ssarets; for (int j = 0; j < num_threads; ++j) { ssarets.push_back(0); @@ -102,9 +102,9 @@ void ARMContext::BindDev() { return; } } -#endif // LITE_WITH_ANDROID +#endif // LITE_WITH_LINUX #else // USE_OPENMP -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX std::vector cpuid1; cpuid1.push_back(active_ids_[0]); int ssaret = set_sched_affinity(cpuid1); @@ -112,7 +112,7 @@ void ARMContext::BindDev() { printf("set cpu affinity failed, cpuID: %d\n", active_ids_[0]); return; } -#endif // LITE_WITH_ANDROID +#endif // LITE_WITH_LINUX #endif // USE_OPENMP } diff --git a/paddle/fluid/lite/core/cpu_info.cc b/paddle/fluid/lite/core/cpu_info.cc index 8c2c427777d..df80f1c8576 100644 --- a/paddle/fluid/lite/core/cpu_info.cc +++ b/paddle/fluid/lite/core/cpu_info.cc @@ -26,7 +26,7 @@ void DeviceInfo::InitInternal(DeviceInfo* dev) { dev->max_memory_ = arm_get_meminfo(); // get max freq -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX std::vector max_freq(dev->compute_core_num_); for (int i = 0; i < dev->compute_core_num_; ++i) { max_freq[i] = get_max_freq_khz(i) / 1000; @@ -311,7 +311,7 @@ bool get_cpu_info_from_name(DeviceInfo* cpu_info, std::string hardware_name) { } size_t arm_get_meminfo() { -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX // get cpu count from /proc/cpuinfo FILE* fp = fopen("/proc/meminfo", "rb"); if (!fp) { @@ -339,7 +339,7 @@ size_t arm_get_meminfo() { } int arm_get_cpucount() { -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX // get cpu count from /sys/devices/system/cpu/cpunum/uevent int max_cpu_count = 20; int count = 0; @@ -371,7 +371,7 @@ int arm_get_cpucount() { } void arm_get_cpu_arch(std::vector* archs) { -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX archs->clear(); //! get CPU ARCH FILE* fp = fopen("/proc/cpuinfo", "rb"); @@ -436,7 +436,7 @@ void arm_get_cpu_arch(std::vector* archs) { #endif } -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX void set_default_cache(DeviceInfo* dev) { int cpu_count = arm_get_cpucount(); @@ -621,7 +621,7 @@ int set_sched_affinity(const std::vector& cpuids) { return 0; } -#endif // LITE_WITH_ANDROID +#endif // LITE_WITH_LINUX #endif // LITE_WITH_ARM diff --git a/paddle/fluid/lite/core/cpu_info.h b/paddle/fluid/lite/core/cpu_info.h index bf603024bc2..385954e6d8e 100644 --- a/paddle/fluid/lite/core/cpu_info.h +++ b/paddle/fluid/lite/core/cpu_info.h @@ -18,7 +18,7 @@ #include #include "paddle/fluid/lite/utils/cp_logging.h" -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX #include #include #endif @@ -103,7 +103,7 @@ void arm_get_cpu_arch(std::vector* archs); bool get_cpu_info_from_name(DeviceInfo* cpu_info, std::string hardware_name); -#ifdef LITE_WITH_ANDROID +#ifdef LITE_WITH_LINUX void set_default_cache(DeviceInfo* dev); @@ -117,7 +117,7 @@ int arm_sort_cpuid_by_max_frequency(int cpu_count, std::vector* cpuids, int check_online(const std::vector& core_ids); int set_sched_affinity(const std::vector& cpuids); -#endif // LITE_WITH_ANDROID +#endif // LITE_WITH_LINUX #endif // LITE_WITH_ARM diff --git a/paddle/fluid/lite/tools/Dockerfile.cross_compile b/paddle/fluid/lite/tools/Dockerfile.cross_compile deleted file mode 100644 index b5b233b6c17..00000000000 --- a/paddle/fluid/lite/tools/Dockerfile.cross_compile +++ /dev/null @@ -1,61 +0,0 @@ -# A image for building paddle cross compile -FROM ubuntu:16.04 -MAINTAINER PaddlePaddle Authors - -# change name to cross compile -ARG UBUNTU_MIRROR -RUN /bin/bash -c '\ -if [[ -n ${UBUNTU_MIRROR} ]]; then \ -sed -i 's#http://archive.ubuntu.com/ubuntu#${UBUNTU_MIRROR}#g' /etc/apt/sources.list; fi' - -RUN apt-get update && apt-get upgrade -y -RUN apt-get install -y --no-install-recommends \ - clang-format-3.8 \ - cmake-curses-gui \ - curl \ - fish \ - gawk \ - gcc \ - g++ \ - git \ - graphviz \ - less \ - make \ - patch \ - python \ - python-pip \ - python-setuptools \ - unzip \ - vim \ - wget - -RUN apt-get autoremove -y && apt-get clean -RUN ln -s clang-format-3.8 /usr/bin/clang-format -RUN pip install --upgrade pip -RUN pip install pre-commit wheel - -# for cmake 3.10 -RUN curl -O https://mms-res.cdn.bcebos.com/cmake-3.10.3-Linux-x86_64.tar.gz && \ - tar xzf cmake-3.10.3-Linux-x86_64.tar.gz && \ - mv cmake-3.10.3-Linux-x86_64 /opt/cmake-3.10 && \ - mv /usr/bin/cmake /usr/bin/cmake.bak && ln -s /opt/cmake-3.10/bin/cmake /usr/bin/cmake && \ - mv /usr/bin/ccmake /usr/bin/ccmake.bak && ln -s /opt/cmake-3.10/bin/ccmake /usr/bin/ccmake -RUN cd /opt && unzip /tmp/android-ndk-r17c-linux-x86_64.zip - -# for arm linux -RUN apt-get install -y --no-install-recommends \ - g++-arm-linux-gnueabi \ - gcc-arm-linux-gnueabi \ - g++-arm-linux-gnueabihf \ - gcc-arm-linux-gnueabihf \ - gcc-aarch64-linux-gnu \ - g++-aarch64-linux-gnu - - -# for android -RUN cd /tmp && curl -O https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip -ENV NDK_ROOT /opt/android-ndk-r17c - -# clean -RUN rm -rf /tmp/android-ndk-r17c-linux-x86_64.zip /cmake-3.10.3-Linux-x86_64.tar.gz - diff --git a/paddle/fluid/lite/tools/Dockerfile.mobile b/paddle/fluid/lite/tools/Dockerfile.mobile new file mode 100644 index 00000000000..363e8d75393 --- /dev/null +++ b/paddle/fluid/lite/tools/Dockerfile.mobile @@ -0,0 +1,90 @@ +# A image for paddle lite mobile cross compile and simulator on android + +FROM ubuntu:16.04 +MAINTAINER PaddlePaddle Authors + +RUN echo '\ +deb main restricted universe multiverse\n\ +deb -updates main restricted universe multiverse\n\ +deb -backports main restricted universe multiverse\n\ +deb -security main restricted universe multiverse\n'\ +> /etc/apt/sources.list +RUN sed -ie 's||http://mirrors.tuna.tsinghua.edu.cn/ubuntu/|' /etc/apt/sources.list +RUN sed -ie 's||xenial|' /etc/apt/sources.list + +RUN apt-get update && apt-get upgrade -y +RUN apt-get install -y --no-install-recommends \ + clang-format-3.8 \ + cmake-curses-gui \ + curl \ + fish \ + gawk \ + gcc \ + g++ \ + git \ + graphviz \ + less \ + make \ + patch \ + python \ + python-pip \ + python-setuptools \ + unzip \ + vim \ + wget + +# for android simulator +RUN apt-get install -y --no-install-recommends \ + libc6-i386 \ + lib32stdc++6 \ + redir \ + iptables \ + openjdk-8-jre + +# for cmake 3.10 +RUN curl -O https://mms-res.cdn.bcebos.com/cmake-3.10.3-Linux-x86_64.tar.gz && \ + tar xzf cmake-3.10.3-Linux-x86_64.tar.gz && \ + mv cmake-3.10.3-Linux-x86_64 /opt/cmake-3.10 && \ + mv /usr/bin/cmake /usr/bin/cmake.bak && ln -s /opt/cmake-3.10/bin/cmake /usr/bin/cmake && \ + mv /usr/bin/ccmake /usr/bin/ccmake.bak && ln -s /opt/cmake-3.10/bin/ccmake /usr/bin/ccmake + +# for arm linux compile +RUN apt-get install -y --no-install-recommends \ + g++-arm-linux-gnueabi \ + gcc-arm-linux-gnueabi \ + g++-arm-linux-gnueabihf \ + gcc-arm-linux-gnueabihf \ + gcc-aarch64-linux-gnu \ + g++-aarch64-linux-gnu + +# for android ndk17c +RUN cd /tmp && curl -O https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip +ENV NDK_ROOT /opt/android-ndk-r17c +RUN cd /opt && unzip /tmp/android-ndk-r17c-linux-x86_64.zip + +# for android simulator +RUN wget "https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip" && \ + unzip sdk-tools-linux-4333796.zip -d /opt/android_sdk && \ + mkdir /root/.android && touch /root/.android/repositories.cfg && \ + echo y | sdkmanager "platform-tools" "emulator" && \ + echo y | sdkmanager "platforms;android-24" && \ + echo y | sdkmanager "system-images;android-24;google_apis;arm64-v8a" "system-images;android-24;google_apis;armeabi-v7a" +ENV ANDROID_HOME /opt/android_sdk +ENV PATH $PATH:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools/bin + +# this will install the ndk19c and only use clang to compile, then can replace the ndk17c +# echo y | sdkmanager "ndk;19.2.5345600" + +# Expose android port +EXPOSE 5555 +EXPOSE 5557 +# VNC port +EXPOSE 5900 + +# clean +RUN rm -rf /tmp/android-ndk-r17c-linux-x86_64.zip /cmake-3.10.3-Linux-x86_64.tar.gz +RUN apt-get autoremove -y && apt-get clean +RUN ln -s clang-format-3.8 /usr/bin/clang-format +RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip +RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple wheel +RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pre-commit \ No newline at end of file diff --git a/paddle/fluid/lite/tools/build.sh b/paddle/fluid/lite/tools/build.sh index bd0cb7ed375..a2b0ade09fa 100755 --- a/paddle/fluid/lite/tools/build.sh +++ b/paddle/fluid/lite/tools/build.sh @@ -13,15 +13,45 @@ function cmake_gpu { } function cmake_arm { - cmake .. \ - -DWITH_GPU=OFF \ - -DWITH_LITE=ON \ - -DLITE_WITH_X86=OFF \ - -DLITE_WITH_CUDA=OFF \ - -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \ - -DWITH_TESTING=ON \ - -DWITH_MKL=OFF \ - -DWITH_MKLDNN=OFF + # ARM_TARGET_OS="android" , "armlinux" + # ARM_TARGET_ARCH_ABI = "arm64-v8a", "armeabi-v7a" ,"armeabi-v7a-hf" + for os in "android" "armlinux" ; do + for abi in "arm64-v8a" "armeabi-v7a" "armeabi-v7a-hf" ; do + if [[ ${abi} == "armeabi-v7a-hf" ]]; then + echo "armeabi-v7a-hf is not supported on both android and armlinux" + continue + fi + + if [[ ${os} == "armlinux" && ${abi} == "armeabi-v7a" ]]; then + echo "armeabi-v7a is not supported on armlinux yet" + continue + fi + + echo "Build for ${os} ${abi}" + + build_dir=build.lite.${os}.${abi} + mkdir -p $build_dir + cd $build_dir + + cmake .. \ + -DWITH_GPU=OFF \ + -DWITH_LITE=ON \ + -DLITE_WITH_CUDA=OFF \ + -DLITE_WITH_X86=OFF \ + -DLITE_WITH_ARM=ON \ + -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \ + -DWITH_TESTING=ON \ + -DWITH_MKL=OFF \ + -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} + + make test_fc_compute_arm -j + make test_softmax_compute_arm -j + make cxx_api_lite_bin -j + cd - + + done + done + } function build { diff --git a/paddle/fluid/lite/tools/mobile_readme.md b/paddle/fluid/lite/tools/mobile_readme.md index b9b07baaa2d..a375ac11bd5 100644 --- a/paddle/fluid/lite/tools/mobile_readme.md +++ b/paddle/fluid/lite/tools/mobile_readme.md @@ -1,106 +1,75 @@ -# Paddle-lite-mobile交叉编译指导 +# Paddle-lite-mobile开发指南 -Paddle-lite-mobile开发环境目前推荐在Docker容器里,在容器里进行交叉编译安卓版本的Native C/C++代码,然后将可执行程序`adb push`到安卓手机上进行调试。 -## Android +## 交叉编译 -TBD +Paddle-lite-mobile 推荐在我们的Docker环境下交叉编译,减少环境配置上的不必要问题。 ### 1. 拉取代码创建容器 ```shell -$ git clone --recursive https://github.com/PaddlePaddle/Paddle.git +$ git clone https://github.com/PaddlePaddle/Paddle.git $ git checkout incubate/lite ``` -先根据仓库下的`Dockerfile.cross_compile`文件生成对应的环境镜像。 +编译docker环境: +`docker build --file paddle/fluid/lite/tools/Dockerfile.mobile --tag paddle-lite-mobile:latest . ` -```shell -$ cd -$ mkdir android-docker -$ cp Dockerfile.android ./android-docker/Dockerfile -$ cd android-docker -$ docker build -t paddle/paddle-lite-mobile . -``` +### 主要cmake选项 + +- `ARM_TARGET_OS` 代表目标操作系统, 目前支持 "android" "armlinux", 模型是Android +- `ARM_TARGET_ARCH_ABI` 代表ARCH, 目前支持 "arm64-v8a" "armeabi-v7a"。 模型是arm64-v8a -完成后,可以看到: -```shell -$ docker images -REPOSITORY TAG IMAGE ID CREATED SIZE -paddle/paddle-lite-mobile latest 9c2000469891 5 hours ago 3.88GB -``` +### 编译 -基于`paddle/paddle-lite`镜像创建容器,并在容器内外建立目录映射关系: +基于`paddle-lite-mobile`镜像创建容器,并在容器内外建立目录映射关系: ```shell -$ ddocker run -v : -tdi paddle/paddle-lite-mobile -# 启动成功会显示container_id +$ docker run -it --name --net=host --privileged -v : paddle-lite-mobile bash ``` -进入容器并切换到Paddle仓库目录: -```shell -$ docker exec -it bash -$ cd -``` -### 2. 交叉编译Paddle-lite-mobile的Native C/C++程序 +参考build.sh下的 cmake arm编译需要的平台。 -创建名为`make_paddle_lite_mobile.sh`的文件: +参考示例: ```shell -$ touch make_paddle_lite_mobile.sh -$ chmod +x make_paddle_lite_mobile.sh -``` - -打开`make_paddle_lite_mobile.sh`文件然后将以下内容复制到该文件中,保存并退出: -```shell -#!/usr/bin/env bash - -# build mobile -mkdir build -cd build - -# cross-compile native cpp +#!/bin/bash cmake .. \ - -DWITH_GPU=OFF \ - -DWITH_MKL=OFF \ - -DWITH_LITE=ON \ - -DLITE_WITH_X86=OFF \ - -DLITE_WITH_ARM=ON \ - -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \ - -DLITE_WITH_CUDA=OFF \ - -DWITH_TESTING=ON + -DWITH_GPU=OFF \ + -DWITH_LITE=ON \ + -DLITE_WITH_CUDA=OFF \ + -DLITE_WITH_X86=OFF \ + -DLITE_WITH_ARM=ON \ + -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \ + -DWITH_TESTING=ON \ + -DWITH_MKL=OFF \ + -DARM_TARGET_OS="android" -DARM_TARGET_ARCH_ABI="arm64-v8a" # fc层单测 make test_fc_compute_arm -j -# 小模型单测 -#make cxx_api_lite_bin ``` +### 在Android上执行 -### 3. 上传编译文件到手机上 +#### 1. 在模拟器上执行 +#### 2. 上传编译文件到手机上 键盘上`crtl+q+p`同时摁下,切换到容器外(容器还在后台运行),将刚刚编译出的程序`adb push`到手机上。USB线连接手机,确保`adb devices`可以找到手机设备。 ```shell $ cd -$ adb push ./build/paddle/fluid/lite/api/test_cxx_api_lite /data/local/tmp/ +$ adb push ./build/paddle/fluid/lite/kernels/arm/test_fc_compute_arm /data/local/tmp/ # 进入手机 $ adb shell # 若多台手机设备先用命令adb devices查看目标手机的序列码 $ cd /data/local/tmp # 执行编译的程序 -$ ./test_cxx_api_lite +$ ./test_fc_compute_arm ``` -## ARM Linux -# 在模拟器中运行 +### 在ARM LINUX下执行 TBD -## Android - -## ARM Linux - - # Q&A @@ -128,4 +97,3 @@ $ adb devices List of devices attached 5cb00b6 device ``` -# -- GitLab