build_npu.sh 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/bin/bash
set -ex

function print_usage {
    echo -e "\nUSAGE:"
    echo
    echo "----------------------------------------"
    echo -e "--arm_os=<os> android only yet."
    echo -e "--arm_abi=<abi> armv8, armv7 yet."
10
    echo -e "--android_stl=<shared> shared or static"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    echo -e "--arm_lang=<gcc> "
    echo -e "--ddk_root=<hiai_ddk_root> "
    echo -e "--test_name=<test_name>"
    echo "----------------------------------------"
    echo
}

# for code gen, a source file is generated after a test, 
# but is dependended by some targets in cmake.
# here we fake an empty file to make cmake works.
function prepare_workspace {
    # in build directory
    # 1. Prepare gen_code file
    GEN_CODE_PATH_PREFIX=lite/gen_code
    mkdir -p ./${GEN_CODE_PATH_PREFIX}
    touch ./${GEN_CODE_PATH_PREFIX}/__generated_code__.cc

    # 2.Prepare debug tool
    DEBUG_TOOL_PATH_PREFIX=lite/tools/debug
    mkdir -p ./${DEBUG_TOOL_PATH_PREFIX}
    cp ../${DEBUG_TOOL_PATH_PREFIX}/analysis_tool.py ./${DEBUG_TOOL_PATH_PREFIX}/
}

function prepare_thirdparty {
    readonly THIRDPARTY_TAR=https://paddle-inference-dist.bj.bcebos.com/PaddleLite/third-party-05b862.tar.gz

    readonly workspace=$PWD
    if [ ! -d $workspace/third-party -o -f $workspace/third-party-05b862.tar.gz ]; then
        rm -rf $workspace/third-party

         if [ ! -f $workspace/third-party-05b862.tar.gz ]; then
            wget $THIRDPARTY_TAR
        fi
        tar xzf third-party-05b862.tar.gz
    else
        git submodule update --init --recursive
    fi
}

function cmake_npu {
    prepare_workspace
    # $1: ARM_TARGET_OS in "android" , "armlinux"
    # $2: ARM_TARGET_ARCH_ABI in "armv8", "armv7" ,"armv7hf"
    # $3: ARM_TARGET_LANG in "gcc" "clang"
    # $4: ANDROID_STL_TYPE in "c++_shared" "c++_static"
    # $5: DDK_ROOT path

    # NPU libs need API LEVEL 24 above
    cmake .. \
        -DWITH_GPU=OFF \
        -DWITH_MKL=OFF \
        -DWITH_LITE=ON \
        -DLITE_WITH_CUDA=OFF \
        -DLITE_WITH_X86=OFF \
65
        -DLITE_BUILD_EXTRA=ON \
66 67 68 69
        -DLITE_WITH_ARM=ON \
        -DWITH_ARM_DOTPROD=ON   \
        -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \
        -DWITH_TESTING=ON \
70
        -DLITE_WITH_JAVA=ON \
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        -DLITE_WITH_NPU=ON \
        -DANDROID_API_LEVEL=24 \
        -DARM_TARGET_OS=$1 \
        -DARM_TARGET_ARCH_ABI=$2 \
        -DARM_TARGET_LANG=$3 \
        -DANDROID_STL_TYPE=$4 \
        -DNPU_DDK_ROOT=$5
}

function build_npu {
    # os, abi, lang, stl, ddk_root, test_name
    cur_dir=$(pwd)

    local os=android
    local abi=armv8
    local lang=gcc
    local stl="c++_shared"
    local ddk_root="${cur_dir}/ai_ddk_lib/" 
    local test_name=test_npu_pass
    prepare_thirdparty

92 93
    if [ "x${ARM_OS}" != "x" ]; then
        os=$ARM_OS
94
    fi
95 96
    if [[ "x${ARM_ABI}" != "x" ]]; then
        abi=$ARM_ABI
97
    fi
98 99
    if [[ "x${ARM_LANG}" != "x" ]]; then
        lang=$ARM_LANG
100
    fi
101 102
    if [[ "x${ANDROID_STL}" != "x" ]]; then
        stl=$ANDROID_STL
103
    fi
104 105
    if [[ "x${DDK_ROOT}" != "x" ]]; then
        ddk_root=$DDK_ROOT
106
    fi
107 108
    if [[ $# -ge 1 ]]; then
        test_name=$1
109 110
    fi

111 112 113 114 115 116 117 118
    # the c++ symbol is not recognized by the bundled script
    if [[ "${stl}" == "c++_shared" ]]; then
        stl_dir="cxx_shared"
    fi
    if [[ "${stl}" == "c++_static" ]]; then
        stl_dir="cxx_static"
    fi
    build_dir=$cur_dir/build.lite.npu.${os}.${abi}.${lang}.${stl_dir}
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    mkdir -p $build_dir
    cd $build_dir

    cmake_npu ${os} ${abi} ${lang} ${stl} ${ddk_root}
    make $test_name -j8

    cd -
    echo "Done"
}

function main {
    # Parse command line.
    for i in "$@"; do
        case $i in
            --tests=*)
                TESTS_FILE="${i#*=}"
                shift
                ;;
            --test_name=*)
                TEST_NAME="${i#*=}"
                shift
                ;;
            --arm_os=*)
                ARM_OS="${i#*=}"
                shift
                ;;
            --arm_abi=*)
                ARM_ABI="${i#*=}"
                shift
                ;;
            --arm_lang=*)
                ARM_LANG="${i#*=}"
                shift
                ;;
153 154 155 156 157 158 159 160
            --android_stl=*)
                ANDROID_STL="${i#*=}"
                shift
                ;;
            --ddk_root=*)
                DDK_ROOT="${i#*=}"
                shift
                ;;
161
            build)
162 163 164 165 166
                build_npu $TEST_NAME
                shift
                ;;
            full_publish)
                build_npu publish_inference
167 168 169 170 171 172 173 174 175 176 177 178
                shift
                ;;
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@