Arm NN 的交叉編譯筆記

Note of Cross Compilation on Arm NN

Posted by imprld01 on Saturday, June 25, 2022

目錄


框架概念 (Concept of Arm NN ML Software)

交叉編譯 (Cross Compilation)

在ubuntu常會使用 gccg++ 指令編譯 c 程式,但是對於嵌入式系統或是開發板而言,並不能直接使用這兩個編譯器生成的執行檔,這是因為底層硬體架構不同,支援的機器語言基本上也會不相同。嵌入式系統或開發板的開發商,沒意外都會提供一套工具鏈 (Toolchain),用來編譯生成適用於該平台的執行檔。而這樣子的工具鏈其實也是由一般常見的 gccg++ 指令編譯生成,所以交叉編譯器 (Cross Compiler) 顧名思義是運行在平台A,但它編譯出的程式卻是運行於平台B。

簡單而言,目標平台 (Target Plateform) 如果跟編譯器運行的平台不一樣,這樣就是交叉編譯 (Cross Compilation)。

Arm NN的交叉編譯筆記 (Note for Cross Compilation on Arm NN)

  • 如果交叉編譯的平台不是特別平台,不用自行替換toolchain的話,可以參考官方dockerfile

    FROM ubuntu:18.04
    ENV TERM linux
    ENV DEBIAN_FRONTEND noninteractive
    
    # Forward system proxy setting
    # ARG proxy
    # ENV http_proxy $proxy
    # ENV https_proxy $proxy
    
    # Basic apt update
    RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends locales ca-certificates &&  rm -rf /var/lib/apt/lists/*
    
    # Set the locale to en_US.UTF-8, because the Yocto build fails without any locale set.
    RUN locale-gen en_US.UTF-8 && update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
    ENV LANG en_US.UTF-8
    ENV LC_ALL en_US.UTF-8
    
    # Again, off the certificare
    RUN echo "check_certificate = off" >> ~/.wgetrc
    RUN echo "[global] \n\
    trusted-host = pypi.python.org \n \
    \t               pypi.org \n \
    \t              files.pythonhosted.org" >> /etc/pip.conf
    
    # Get basic packages
    RUN apt-get update && apt-get install -y \
        apparmor \
        aufs-tools \
        automake \
        bash-completion \
        btrfs-tools \
        build-essential \
        cmake \
        createrepo \
        curl \
        dpkg-sig \
        g++ \
        gcc \
        git \
        iptables \
        jq \
        libapparmor-dev \
        libc6-dev \
        libcap-dev \
        libsystemd-dev \
        libyaml-dev \
        mercurial \
        net-tools \
        parallel \
        pkg-config \
        python-dev \
        python-mock \
        python-pip \
        python-setuptools \
        python-websocket \
        golang-go \
        iproute2 \
        iputils-ping \
        vim-common \
        vim \
        wget \
        libtool \
        unzip \
        scons \
        curl \
        autoconf \
        libtool \
        build-essential \
        libssl-dev \
        g++ && rm -rf /var/lib/apt/lists/*
    
    # Install Cross-compiling ToolChain
    RUN apt-get update && apt-get install -y crossbuild-essential-arm64 crossbuild-essential-armhf
    
    # Install Cmake 3.19
    RUN cd $HOME && \
        wget -O cmake-3.19.0.tar.gz https://cmake.org/files/v3.19/cmake-3.19.0.tar.gz && \
        tar -xzf cmake-3.19.0.tar.gz && \
        cd $HOME/cmake-3.19.0 && \
        ./bootstrap && \
        make && \
        make install
    
    # Build and install Google's Protobuf library
    # Download and Extract
    RUN mkdir -p $HOME/google && \
        cd $HOME/google && \
        wget https://github.com/protocolbuffers/protobuf/releases/download/v3.12.0/protobuf-all-3.12.0.tar.gz && \
        tar -zxvf protobuf-all-3.12.0.tar.gz
    
    # Build a native (x86_64) version
    RUN cd $HOME/google/protobuf-3.12.0 && \
        mkdir x86_build && cd x86_build && \
        ../configure --prefix=$HOME/armnn-devenv/google/x86_64_pb_install && \
        make install -j$(nproc)
    
    # Build the arm64 version of the protobuf libraries
    RUN cd $HOME/google/protobuf-3.12.0 && \
        mkdir arm64_build && cd arm64_build && \
        export CC=aarch64-linux-gnu-gcc && \
        export CXX=aarch64-linux-gnu-g++ && \
        ../configure --host=aarch64-linux \
        --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
        --with-protoc=$HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc && \
        make install -j$(nproc)
    
    # Build the arm32 version of the protobuf libraries
    RUN cd $HOME/google/protobuf-3.12.0 && \
        mkdir arm32_build && cd arm32_build && \
        export CC=arm-linux-gnueabihf-gcc && \
        export CXX=arm-linux-gnueabihf-g++ && \
        ../configure --host=arm-linux \
        --prefix=$HOME/armnn-devenv/google/arm32_pb_install \
        --with-protoc=$HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc && \
        make install -j$(nproc)
    
    # Dep Error - Bug ARMNN
    RUN apt-get update && apt-get install -y \
         python-numpy
    
    # Setup Env
    # ENV PATH=$HOME/armnn-devenv/google/x86_64_pb_install/bin/:$PATH
    # ENV LD_LIBRARY_PATH=$HOME/armnn-devenv/google/x86_64_pb_install/lib/:$LD_LIBRARY_PATH
    
    # Download ArmNN 64-bit
    RUN cd $HOME/armnn-devenv && git clone "https://review.mlplatform.org/ml/armnn" && \
        cd armnn/ && git checkout master
    
    # Download ArmNN 32-bit
    RUN cd $HOME/armnn-devenv && git clone "https://review.mlplatform.org/ml/armnn" armnn-32 && \
        cd armnn-32/ && git checkout master
    
    # Build 64-bit Compute Library
    RUN cd $HOME/armnn-devenv/ && git clone https://review.mlplatform.org/ml/ComputeLibrary && \
        cd ComputeLibrary && \
        git checkout $($HOME/armnn-devenv/armnn/scripts/get_compute_library.sh -p) && \
        scons Werror=0 arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j$(nproc)
    
    # Build 32-bit Compute Library
    RUN cd $HOME/armnn-devenv/ && mkdir ComputeLibrary_32 && git clone https://review.mlplatform.org/ml/ComputeLibrary ComputeLibrary_32 && \
        cd ComputeLibrary_32 &&\
        git checkout $($HOME/armnn-devenv/armnn/scripts/get_compute_library.sh -p) && \
        scons Werror=0 arch=armv7a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j$(nproc)
    
    # Download Tensorflow (Checkout latest tested version of TF using get_tensorflow.sh)
    RUN cd $HOME/armnn-devenv && \
        git clone https://github.com/tensorflow/tensorflow.git && \
        cd tensorflow && \
        git checkout $($HOME/armnn-devenv/armnn/scripts/get_tensorflow.sh -p)
    
    # Build 64-bit TF Lite
    RUN cd $HOME/armnn-devenv && \
        curl -LO https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz && \
        mkdir tflite-toolchains &&\
        tar xvf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz -C tflite-toolchains && \
        mkdir -p tflite/build && \
        cd tflite/build && \
        ARMCC_PREFIX=$HOME/armnn-devenv/tflite-toolchains/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu- && \
        ARMCC_FLAGS="-funsafe-math-optimizations" && \
        cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
              -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
              -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
              -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON  -DCMAKE_SYSTEM_NAME=Linux \
              -DTFLITE_ENABLE_XNNPACK=OFF \
              -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
              $HOME/armnn-devenv/tensorflow/tensorflow/lite/ && \
        cmake --build .
    
    # Build 32-bit TF Lite
    RUN cd $HOME/armnn-devenv && \
        curl -LO https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz && \
        mkdir tflite-toolchains-32 && \
        tar xf gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz -C tflite-toolchains-32 && \
        mkdir -p tflite-32/build && \
        cd tflite-32/build && \
        ARMCC_PREFIX=$HOME/armnn-devenv/tflite-toolchains-32/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf- && \
        ARMCC_FLAGS="-funsafe-math-optimizations" && \
        cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
              -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
              -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
              -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON  -DCMAKE_SYSTEM_NAME=Linux \
              -DTFLITE_ENABLE_XNNPACK=OFF \
              -DCMAKE_SYSTEM_PROCESSOR=armv7a \
              $HOME/armnn-devenv/tensorflow/tensorflow/lite/ && \
        cmake --build .
    
    # Download Flatbuffers
    RUN cd $HOME/armnn-devenv && \
        wget -O flatbuffers-1.12.0.tar.gz https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz && \
        tar xf flatbuffers-1.12.0.tar.gz && \
        cd flatbuffers-1.12.0 && \
        rm -f CMakeCache.txt
    
    # Build native x86_64 version of Flatbuffers
    RUN cd $HOME/armnn-devenv && cd flatbuffers-1.12.0 && \
        mkdir build && \
        cd build && \
        cmake .. -DFLATBUFFERS_BUILD_FLATC=1 \
             -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers \
             -DFLATBUFFERS_BUILD_TESTS=0 && \
        make all install -j$(nproc)
    
    # Build arm64 version of Flatbuffers
    RUN cd $HOME/armnn-devenv && cd flatbuffers-1.12.0 && \
        mkdir build-arm64 && \
        cd build-arm64 && \
        CXXFLAGS="-fPIC" cmake .. -DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc \
             -DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++ \
             -DFLATBUFFERS_BUILD_FLATC=1 \
             -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers-arm64 \
             -DFLATBUFFERS_BUILD_TESTS=0 && \
        make all install -j$(nproc)
    
    # Build arm32 version of Flatbuffers
    RUN cd $HOME/armnn-devenv && cd flatbuffers-1.12.0  && \
        mkdir build-arm32 && \
        cd build-arm32 && \
        CXXFLAGS="-fPIC" cmake .. -DCMAKE_C_COMPILER=/usr/bin/arm-linux-gnueabihf-gcc \
             -DCMAKE_CXX_COMPILER=/usr/bin/arm-linux-gnueabihf-g++ \
             -DFLATBUFFERS_BUILD_FLATC=1 \
             -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers-arm32 \
             -DFLATBUFFERS_BUILD_TESTS=0 && \
        make all install -j$(nproc)
    
    # Build onnx
    RUN cd $HOME/armnn-devenv && git clone https://github.com/onnx/onnx.git && \
        cd onnx && \
        git fetch https://github.com/onnx/onnx.git 553df22c67bee5f0fe6599cff60f1afc6748c635 && git checkout FETCH_HEAD && \
        LD_LIBRARY_PATH=$HOME/armnn-devenv/google/x86_64_pb_install/lib:$LD_LIBRARY_PATH \
        $HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc \
        onnx/onnx.proto --proto_path=. --proto_path=../google/x86_64_pb_install/include --cpp_out $HOME/armnn-devenv/onnx
    
    # Generate TF Lite Schema 64-bit
    RUN cd $HOME/armnn-devenv && \
        cd tflite && \
        cp ../tensorflow/tensorflow/lite/schema/schema.fbs . && \
        ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs
    
    # Generate TF Lite Schema 32-bit
    RUN cd $HOME/armnn-devenv && \
        cd tflite-32 && \
        cp ../tensorflow/tensorflow/lite/schema/schema.fbs . && \
        ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs
    
    # Build 64-bit ArmNN
    RUN cd $HOME/armnn-devenv && \
        cd armnn && mkdir build && cd build && \
        export CXX=aarch64-linux-gnu-g++ && \
        export CC=aarch64-linux-gnu-gcc && \
        cmake .. \
        -DCMAKE_CXX_FLAGS=-w \
        -DBUILD_TESTS=1 \
        -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary \
        -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build/ \
        -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
        -DONNX_GENERATED_SOURCES=$HOME/armnn-devenv/onnx \
        -DBUILD_ONNX_PARSER=1 \
        -DBUILD_TF_LITE_PARSER=1 \
        -DBUILD_ARMNN_TFLITE_DELEGATE=1 \
        -DTENSORFLOW_ROOT=$HOME/armnn-devenv/tensorflow \
        -DTFLITE_LIB_ROOT=$HOME/armnn-devenv/tflite/build \
        -DTF_LITE_SCHEMA_INCLUDE_PATH=$HOME/armnn-devenv/tflite \
        -DFLATBUFFERS_ROOT=$HOME/armnn-devenv/flatbuffers-arm64 \
        -DFLATC_DIR=$HOME/armnn-devenv/flatbuffers-1.12.0/build \
        -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install \
        -DPROTOBUF_LIBRARY_DEBUG=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0 \
        -DPROTOBUF_LIBRARY_RELEASE=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0 && \
        make -j$(nproc)
    
    # Build 32-bit ArmNN
    RUN cd $HOME/armnn-devenv && \
        cd armnn-32 && mkdir build && cd build && \
        export CXX=arm-linux-gnueabihf-g++ && \
        export CC=arm-linux-gnueabihf-gcc && \
        cmake .. \
        -DCMAKE_CXX_FLAGS=-w \
        -DBUILD_TESTS=1 \
        -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary_32 \
        -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary_32/build/ \
        -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
        -DONNX_GENERATED_SOURCES=$HOME/armnn-devenv/onnx \
        -DBUILD_ONNX_PARSER=1 \
        -DBUILD_TF_LITE_PARSER=1 \
        -DBUILD_ARMNN_TFLITE_DELEGATE=1 \
        -DTENSORFLOW_ROOT=$HOME/armnn-devenv/tensorflow \
        -DTFLITE_LIB_ROOT=$HOME/armnn-devenv/tflite-32/build \
        -DTF_LITE_SCHEMA_INCLUDE_PATH=$HOME/armnn-devenv/tflite-32 \
        -DFLATBUFFERS_ROOT=$HOME/armnn-devenv/flatbuffers-arm32 \
        -DFLATC_DIR=$HOME/armnn-devenv/flatbuffers-1.12.0/build \
        -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install \
        -DPROTOBUF_LIBRARY_DEBUG=$HOME/armnn-devenv/google/arm32_pb_install/lib/libprotobuf.so.23.0.0 \
        -DPROTOBUF_LIBRARY_RELEASE=$HOME/armnn-devenv/google/arm32_pb_install/lib/libprotobuf.so.23.0.0 \
        -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON && \
        make -j$(nproc)
    
  • 如果需要自行替換toolchain,可以參考下面步驟:

  1. Dockerfile

    FROM ubuntu:18.04
    ENV TERM linux
    ENV DEBIAN_FRONTEND noninteractive
    
    # Forward system proxy setting
    # ARG proxy
    # ENV http_proxy $proxy
    # ENV https_proxy $proxy
    
    # Basic apt update
    RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends locales ca-certificates &&  rm -rf /var/lib/apt/lists/*
    
    # Set the locale to en_US.UTF-8, because the Yocto build fails without any locale set.
    RUN locale-gen en_US.UTF-8 && update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
    ENV LANG en_US.UTF-8
    ENV LC_ALL en_US.UTF-8
    
    # Again, off the certificare
    RUN echo "check_certificate = off" >> ~/.wgetrc
    RUN echo "[global] \n\
    trusted-host = pypi.python.org \n \
    \t               pypi.org \n \
    \t              files.pythonhosted.org" >> /etc/pip.conf
    
    # Get basic packages
    RUN apt-get update && apt-get install -y \
        apparmor \
        aufs-tools \
        automake \
        bash-completion \
        btrfs-tools \
        build-essential \
        cmake \
        createrepo \
        curl \
        dpkg-sig \
        g++ \
        gcc \
        git \
        iptables \
        jq \
        libapparmor-dev \
        libc6-dev \
        libcap-dev \
        libsystemd-dev \
        libyaml-dev \
        mercurial \
        net-tools \
        parallel \
        pkg-config \
        python-dev \
        python-mock \
        python-pip \
        python-setuptools \
        python-websocket \
        golang-go \
        iproute2 \
        iputils-ping \
        vim-common \
        vim \
        wget \
        libtool \
        unzip \
        scons \
        curl \
        autoconf \
        libtool \
        build-essential \
        libssl-dev \
        g++ && rm -rf /var/lib/apt/lists/*
    
    # Install Cross-compiling ToolChain
    RUN apt-get update && apt-get install -y crossbuild-essential-arm64 crossbuild-essential-armhf
    
    # Install Cmake 3.19
    RUN cd $HOME && \
        wget -O cmake-3.19.0.tar.gz https://cmake.org/files/v3.19/cmake-3.19.0.tar.gz && \
        tar -xzf cmake-3.19.0.tar.gz && \
        cd $HOME/cmake-3.19.0 && \
        ./bootstrap && \
        make && \
        make install
    
    sudo docker build --rm -t imprld01/armnn:v0 . --no-cache
    
    sudo docker run --rm --gpus all --net host -it --shm-size=8g \
                    -v /armnn_scripts/:/armnn_scripts/ \
                    -v /toolchain/:/toolchain/ \
                    imprld01/armnn:v0 bash
    
  2. Download Google Protobuf Library amd Build it

    #!/bin/bash
    
    # Build and install Google's Protobuf library
    # Download and Extract
    mkdir -p $HOME/google && \
        cd $HOME/google && \
        wget https://github.com/protocolbuffers/protobuf/releases/download/v3.12.0/protobuf-all-3.12.0.tar.gz && \
        tar -zxvf protobuf-all-3.12.0.tar.gz
    
    # Build a native (x86_64) version
    cd $HOME/google/protobuf-3.12.0 && \
        mkdir x86_build && cd x86_build && \
        ../configure --prefix=$HOME/armnn-devenv/google/x86_64_pb_install && \
        make install -j$(nproc)
    
    #!/bin/bash
    
    # Build the arm32 version of the protobuf libraries
    cd $HOME/google/protobuf-3.12.0 && \
        mkdir arm32_build_myarm && cd arm32_build_myarm && \
        export LD_LIBRARY_PATH=/toolchain/sysroots/unknown-linux/usr/lib/arm-unknown-linux-gnueabi/ && \
        export CC=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-gcc && \
        export CFLAGS="--sysroot=/toolchain/sysroots/unknown-linux-gnueabi" && \
        export CFLAGS_FOR_BUILD="-g -fPIC -fno-short-enums -fno-short-enums -lstdc++" && \
        export CXX=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-g++ && \
        export CXXFLAGS="--sysroot=/toolchain/sysroots/unknown-linux-gnueabi" && \
        export CXXFLAGS_FOR_BUILD="-g -fPIC -fno-short-enums -fno-short-enums -lstdc++" && \
        ../configure --host=arm-unknown-linux \
        --prefix=$HOME/armnn-devenv/google/arm32_pb_install_myarm \
        --with-protoc=$HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc && \
        make install -j$(nproc)
    
  3. Download ArmNN

    #!/bin/bash
    
    # Dep Error - Bug ARMNN
    apt-get update && apt-get install -y \
         python-numpy
    
    # Download ArmNN 32-bit
    cd $HOME/armnn-devenv && git clone "https://review.mlplatform.org/ml/armnn" armnn-32-myarm && \
        cd armnn-32-myarm/ && git checkout master
    
  4. Download Compute Library and Build it

    #!/bin/bash
    
    # Build 32-bit Compute Library
    cd $HOME/armnn-devenv/ && mkdir ComputeLibrary_myarm && git clone https://review.mlplatform.org/ml/ComputeLibrary ComputeLibrary_myarm && \
        cd ComputeLibrary_myarm && \
        git checkout $($HOME/armnn-devenv/armnn/scripts/get_compute_library.sh -p) && \
        export LD_LIBRARY_PATH=/toolchain/sysroots/unknown-linux/usr/lib/arm-unknown-linux-gnueabi/ && \
        scons Werror=0 arch=armv7a neon=1 opencl=1 embed_kernels=1 cppthreads=1 openmp=0 compiler_prefix="/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-" extra_cxx_flags="-fPIC -mfloat-abi=softfp --sysroot=/toolchain/sysroots/unknown-linux-gnueabi/" extra_link_flags="-fPIC --sysroot=/toolchain/sysroots/unknown-linux-gnueabi/" -j$(nproc)
    
  5. Download Tensorflow and Build TFLite

    #!/bin/bash
    
    # Download Tensorflow (Checkout latest tested version of TF using get_tensorflow.sh)
    cd $HOME/armnn-devenv && \
        git clone https://github.com/tensorflow/tensorflow.git && \
        cd tensorflow && \
        git checkout $($HOME/armnn-devenv/armnn/scripts/get_tensorflow.sh -p)
    
    # Build 32-bit TF Lite
    cd $HOME/armnn-devenv && \
        mkdir -p tflite-myarm/build && \
        cd tflite-myarm/build && \
        ARMCC_PREFIX=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi- && \
        ARMCC_FLAGS="-funsafe-math-optimizations -fPIC --sysroot=/toolchain/sysroots/unknown-linux-gnueabi/" && \
        cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
              -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
              -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
              -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON  -DCMAKE_SYSTEM_NAME=Linux \
              -DTFLITE_ENABLE_XNNPACK=OFF \
              -DCMAKE_SYSTEM_PROCESSOR=armv7a \
              $HOME/armnn-devenv/tensorflow/tensorflow/lite/ && \
        cmake --build .
    
  6. Download Flatbuffers and Build it

    #!/bin/bash
    
    # Download Flatbuffers
    cd $HOME/armnn-devenv && \
        wget -O flatbuffers-1.12.0.tar.gz https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz && \
        tar xf flatbuffers-1.12.0.tar.gz && \
        cd flatbuffers-1.12.0 && \
        rm -f CMakeCache.txt
    
    # Build native x86_64 version of Flatbuffers
    cd $HOME/armnn-devenv && cd flatbuffers-1.12.0 && \
        mkdir build && \
        cd build && \
        cmake .. -DFLATBUFFERS_BUILD_FLATC=1 \
             -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers \
             -DFLATBUFFERS_BUILD_TESTS=0 && \
        make all install -j$(nproc)
    

    修改flatbuffers-1.12.0下面的CMakeLists.txt,加上:

    set(CMAKE_C_COMPILER_FORCED TRUE)
    set(CMAKE_CXX_COMPILER_FORCED TRUE)
    
    # Build arm32 version of Flatbuffers
    cd $HOME/armnn-devenv && cd flatbuffers-1.12.0  && \
        cp /root/armnn_envs_x86_64/flatbuffers-1.12.0/CMakeLists.txt . && \
        mkdir build-arm32-myarm && \
        cd build-arm32-myarm && \
        CXXFLAGS="-fPIC --sysroot=/toolchain/sysroots/unknown-linux-gnueabi" cmake .. -DCMAKE_C_COMPILER=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-gcc \
             -DCMAKE_CXX_COMPILER=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-g++ \
             -DFLATBUFFERS_BUILD_FLATC=1 \
             -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers-arm32-myarm \
             -DFLATBUFFERS_BUILD_TESTS=0 && \
        make all install -j$(nproc)
    
  7. Download ONNX and Build it

    #!/bin/bash
    
    # Build onnx
    cd $HOME/armnn-devenv && git clone https://github.com/onnx/onnx.git && \
        cd onnx && \
        git fetch https://github.com/onnx/onnx.git 553df22c67bee5f0fe6599cff60f1afc6748c635 && git checkout FETCH_HEAD && \
        LD_LIBRARY_PATH=$HOME/armnn-devenv/google/x86_64_pb_install/lib:$LD_LIBRARY_PATH \
        $HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc \
        onnx/onnx.proto --proto_path=. --proto_path=../google/x86_64_pb_install/include --cpp_out $HOME/armnn-devenv/onnx
    
  8. Generate TFLite Schema

    #!/bin/bash
    
    # Generate TF Lite Schema 32-bit
    cd $HOME/armnn-devenv && \
        cd tflite-myarm && \
        cp ../tensorflow/tensorflow/lite/schema/schema.fbs . && \
        ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs
    
  9. Build Arm NN

    修改armnn-32-myarm下面的CMakeLists.txt,加上:

    set(CMAKE_C_COMPILER_FORCED TRUE)
    set(CMAKE_CXX_COMPILER_FORCED TRUE)
    
    #!/bin/bash
    
    # Build 32-bit ArmNN
    cd $HOME/armnn-devenv && \
        cd armnn-32-myarm && \
        cp /root/armnn_envs_x86_64/armnn-32-myarm/CMakeLists.txt . && \
        mkdir build && cd build && \
        export LD_LIBRARY_PATH=/toolchain/sysroots/unknown-linux/usr/lib/arm-unknown-linux-gnueabi/ && \
        export CXX=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-g++ && \
        export CC=/toolchain/sysroots/unknown-linux/usr/bin/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi-gcc && \
        cmake .. \
        -DCMAKE_CXX_FLAGS="-w -fPIC -pthread -latomic --sysroot=/toolchain/sysroots/unknown-linux-gnueabi" \
        -DBUILD_TESTS=1 \
        -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary_myarm \
        -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary_myarm/build/ \
        -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
        -DONNX_GENERATED_SOURCES=$HOME/armnn-devenv/onnx \
        -DBUILD_ONNX_PARSER=1 \
        -DBUILD_TF_LITE_PARSER=1 \
        -DBUILD_ARMNN_TFLITE_DELEGATE=1 \
        -DTENSORFLOW_ROOT=$HOME/armnn-devenv/tensorflow \
        -DTFLITE_LIB_ROOT=$HOME/armnn-devenv/tflite-myarm/build \
        -DTF_LITE_SCHEMA_INCLUDE_PATH=$HOME/armnn-devenv/tflite-myarm \
        -DFLATBUFFERS_ROOT=$HOME/armnn-devenv/flatbuffers-arm32-myarm \
        -DFLATC_DIR=$HOME/armnn-devenv/flatbuffers-1.12.0/build \
        -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install \
        -DPROTOBUF_LIBRARY_DEBUG=$HOME/armnn-devenv/google/arm32_pb_install_myarm/lib/libprotobuf.so.23.0.0 \
        -DPROTOBUF_LIBRARY_RELEASE=$HOME/armnn-devenv/google/arm32_pb_install_myarm/lib/libprotobuf.so.23.0.0 \
        -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON && \
        make -j$(nproc)
    

Further Reading

  1. Note of Compilation of Arm NN by Android NDK - 使用 Android NDK 編譯 Arm NN

Reference

  1. https://github.com/ARM-software/armnn
  2. 交叉编译ARM平台、移植ArmNN_yiyayiya557的博客-CSDN博客_arm nn
  3. 为Tensorflow、Tensorflow lite配置Arm NN SDK编译环境_我是木林森呀的博客-CSDN博客_armnn tensorflow
  4. Configuring the Arm NN SDK build environment for Caffe
  5. 在imx8qm上开心把玩Machine Learning_AIA-XP的博客-CSDN博客_imx8qm
  6. ATU Book i.MX8系列 - ArmNN 使用介紹
  7. armnn/BuildGuideCrossCompilation.md at branches/armnn_20_02 · ARM-software/armnn

comments powered by Disqus