build.sh 8.0 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4
#!/bin/bash
set -ex

TESTS_FILE="./lite_tests.txt"
5
LIBS_FILE="./lite_libs.txt"
T
tensor-tang 已提交
6 7

readonly common_flags="-DWITH_LITE=ON -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=OFF -DWITH_PYTHON=OFF -DWITH_TESTING=ON -DLITE_WITH_ARM=OFF"
8 9 10 11 12 13 14 15

# 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_for_codegen {
    # in build directory
    mkdir -p ./paddle/fluid/lite/gen_code
    touch ./paddle/fluid/lite/gen_code/__generated_code__.cc
}
T
tensor-tang 已提交
16
function cmake_x86 {
17
    prepare_for_codegen
T
tensor-tang 已提交
18 19 20 21
    cmake ..  -DWITH_GPU=OFF -DWITH_MKLDNN=OFF -DLITE_WITH_X86=ON ${common_flags}
}

function cmake_x86_for_CI {
22
    prepare_for_codegen
T
tensor-tang 已提交
23 24 25 26
    cmake ..  -DWITH_GPU=OFF -DWITH_MKLDNN=OFF -DLITE_WITH_X86=ON ${common_flags} -DLITE_WITH_PROFILE=ON
}

function cmake_gpu {
27
    prepare_for_codegen
T
tensor-tang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    cmake .. " -DWITH_GPU=ON {common_flags} -DLITE_WITH_GPU=ON"
}

function cmake_arm {
    # $1: ARM_TARGET_OS in "android" , "armlinux"
    # $2: ARM_TARGET_ARCH_ABI in "arm64-v8a", "armeabi-v7a" ,"armeabi-v7a-hf"
    cmake .. \
        -DWITH_GPU=OFF \
        -DWITH_MKL=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 \
        -DARM_TARGET_OS=$1 -DARM_TARGET_ARCH_ABI=$2
}

T
tensor-tang 已提交
46 47 48 49
function build_single {
        make $1 -j$(expr $(nproc) - 2)
}

T
tensor-tang 已提交
50 51 52
function build {
    file=$1
    for _test in $(cat $file); do
T
tensor-tang 已提交
53
        build_single $_test
T
tensor-tang 已提交
54 55 56 57 58 59 60
    done
}

# It will eagerly test all lite related unittests.
function test_lite {
    local file=$1
    echo "file: ${file}"
61

T
tensor-tang 已提交
62
    for _test in $(cat $file); do
63 64 65
        # We move the build phase here to make the 'gen_code' test compiles after the
        # corresponding test is executed and the C++ code generates.
        make $_test -j$(expr $(nproc) - 2)
T
tensor-tang 已提交
66 67 68 69 70 71 72 73 74 75
        ctest -R $_test -V
    done
}

# Build the code and run lite server tests. This is executed in the CI system.
function build_test_server {
    mkdir -p ./build
    cd ./build
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/paddle/build/third_party/install/mklml/lib"
    cmake_x86_for_CI
76
    # compile the tests and execute them.
T
tensor-tang 已提交
77
    test_lite $TESTS_FILE
78 79
    # build the remaining libraries to check compiling error.
    build $LIBS_FILE
T
tensor-tang 已提交
80 81
}

T
tensor-tang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
# test_arm_android <some_test_name> <adb_port_number>
function test_arm_android {
    test_name=$1
    port=$2
    if [[ "${test_name}x" == "x" ]]; then
        echo "test_name can not be empty"
        exit 1
    fi
    if [[ "${port}x" == "x" ]]; then
        echo "Port can not be empty"
        exit 1
    fi

    echo "test name: ${test_name}"
    adb_work_dir="/data/local/tmp"
    skip_list="test_model_parser_lite" # add more with space
    [[ $skip_list =~ (^|[[:space:]])$test_name($|[[:space:]]) ]] && continue || echo 'skip $test_name'
    testpath=$(find ./paddle/fluid -name ${test_name})
    adb -s emulator-${port} push ${testpath} ${adb_work_dir}
    adb -s emulator-${port} shell chmod +x "${adb_work_dir}/${test_name}"
    adb -s emulator-${port} shell "./${adb_work_dir}/${test_name}"
}

# Build the code and run lite arm tests. This is executed in the CI system.
T
tensor-tang 已提交
106
function build_test_arm {
T
tensor-tang 已提交
107 108 109
    port_armv8=5554
    port_armv7=5556

T
tensor-tang 已提交
110
    adb kill-server
T
tensor-tang 已提交
111 112 113 114 115 116 117 118 119 120 121
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    # start android arm64-v8a armeabi-v7a emulators first
    echo n | avdmanager create avd -f -n paddle-armv8 -k "system-images;android-24;google_apis;arm64-v8a"
    echo -ne '\n' | ${ANDROID_HOME}/emulator/emulator -avd paddle-armv8 -noaudio -no-window -gpu off -verbose -port ${port_armv8} &
    sleep 1m
    echo n | avdmanager create avd -f -n paddle-armv7 -k "system-images;android-24;google_apis;armeabi-v7a"
    echo -ne '\n' | ${ANDROID_HOME}/emulator/emulator -avd paddle-armv7 -noaudio -no-window -gpu off -verbose -port ${port_armv7} &
    sleep 1m

    for os in "android" "armlinux" ; do
        for abi in "arm64-v8a" "armeabi-v7a" "armeabi-v7a-hf" ; do
T
tensor-tang 已提交
122
            # TODO(TJ): enable compile on v7-hf on andorid and all v7 on armlinux
T
tensor-tang 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            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

            build_dir=build.lite.${os}.${abi}
            mkdir -p $build_dir
            cd $build_dir
            cmake_arm ${os} ${abi}
            build $TESTS_FILE

T
tensor-tang 已提交
139 140
            # armlinux need in another docker
            # TODO(TJ): enable test with armlinux
T
tensor-tang 已提交
141 142 143 144 145 146
            if [[ ${os} == "android" ]]; then
                adb_abi=${abi}
                if [[ ${adb_abi} == "armeabi-v7a-hf" ]]; then
                    adb_abi="armeabi-v7a"
                fi
                if [[ ${adb_abi} == "armeabi-v7a" ]]; then
T
tensor-tang 已提交
147 148
                    # skip all armv7 tests
                    # TODO(TJ): enable test with armv7
T
tensor-tang 已提交
149 150
                    continue
                fi
T
tensor-tang 已提交
151 152 153 154 155 156 157 158 159 160 161 162
                local port=
                if [[ ${adb_abi} == "armeabi-v7a" ]]; then
                    port=${port_armv7}
                fi

                if [[ ${adb_abi} == "arm64-v8a" ]]; then
                    port=${port_armv8}
                fi
                echo "test file: ${TESTS_FILE}"
                for _test in $(cat $TESTS_FILE); do
                    test_arm_android $_test $port
                done
T
tensor-tang 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
            fi
            cd -
        done
    done
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    echo "Done"
}

############################# MAIN #################################
function print_usage {
    echo -e "\nUSAGE:"
    echo
    echo "----------------------------------------"
    echo -e "cmake_x86: run cmake with X86 mode"
    echo -e "cmake_cuda: run cmake with CUDA mode"
T
tensor-tang 已提交
178
    echo -e "--arm_os=<os> --arm_abi=<abi> cmake_arm: run cmake with ARM mode"
T
tensor-tang 已提交
179 180
    echo
    echo -e "build: compile the tests"
T
tensor-tang 已提交
181
    echo -e "--test_name=<test_name> build_single: compile single test"
T
tensor-tang 已提交
182 183
    echo
    echo -e "test_server: run server tests"
T
tensor-tang 已提交
184
    echo -e "--test_name=<test_name> --adb_port_number=<adb_port_number> test_arm_android: run arm test"
T
tensor-tang 已提交
185 186 187 188 189 190 191 192 193 194 195 196
    echo "----------------------------------------"
    echo
}

function main {
    # Parse command line.
    for i in "$@"; do
        case $i in
            --tests=*)
                TESTS_FILE="${i#*=}"
                shift
                ;;
T
tensor-tang 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            --test_name=*)
                TEST_NAME="${i#*=}"
                shift
                ;;
            --arm_os=*)
                ARM_OS="${i#*=}"
                shift
                ;;
            --arm_abi=*)
                ARM_ABI="${i#*=}"
                shift
                ;;
            --arm_port=*)
                ARM_PORT="${i#*=}"
                shift
                ;;
T
tensor-tang 已提交
213 214
            build)
                build $TESTS_FILE
215
                build $LIBS_FILE
T
tensor-tang 已提交
216 217
                shift
                ;;
T
tensor-tang 已提交
218 219 220 221
            build_single)
                build_single $TEST_NAME
                shift
                ;;
T
tensor-tang 已提交
222 223 224 225 226 227 228 229 230
            cmake_x86)
                cmake_x86
                shift
                ;;
            cmake_cuda)
                cmake_cuda
                shift
                ;;
            cmake_arm)
T
tensor-tang 已提交
231
                cmake_arm $ARM_OS $ARM_ABI
T
tensor-tang 已提交
232 233 234 235 236 237
                shift
                ;;
            test_server)
                test_lite $TESTS_FILE
                shift
                ;;
T
tensor-tang 已提交
238 239
            test_arm_android)
                test_arm_android $TEST_NAME $ARM_PORT
T
tensor-tang 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                shift
                ;;
            build_test_server)
                build_test_server
                shift
                ;;
            build_test_arm)
                build_test_arm
                shift
                ;;
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@