build.sh 14.5 KB
Newer Older
Y
Yan Chunwei 已提交
1
#!/bin/bash
2
set -ex
Y
Yan Chunwei 已提交
3

4
TESTS_FILE="./lite_tests.txt"
Y
Yan Chunwei 已提交
5
LIBS_FILE="./lite_libs.txt"
Y
Yan Chunwei 已提交
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

C
Chunwei 已提交
9 10
NUM_CORES_FOR_COMPILE=8

11 12 13 14 15 16 17
# 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
}
S
up  
superjomn 已提交
18 19 20 21 22

function check_need_ci {
    git log -1 --oneline | grep "test=develop" || exit -1
}

Y
Yan Chunwei 已提交
23
function cmake_x86 {
24
    prepare_for_codegen
25
    cmake ..  -DWITH_GPU=OFF -DWITH_MKLDNN=OFF -DLITE_WITH_X86=ON ${common_flags}
Y
Yan Chunwei 已提交
26 27
}

Z
Zhen Wang 已提交
28 29 30 31 32 33 34 35 36 37 38 39
function cmake_opencl {
    cmake .. \
        -DLITE_WITH_OPENCL=ON \
        -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
Z
Zhen Wang 已提交
40 41 42
}


C
Chunwei 已提交
43
# This method is only called in CI.
Y
Yan Chunwei 已提交
44
function cmake_x86_for_CI {
C
Chunwei 已提交
45
    prepare_for_codegen # fake an empty __generated_code__.cc to pass cmake.
Y
Yan Chunwei 已提交
46
    cmake ..  -DWITH_GPU=OFF -DWITH_MKLDNN=OFF -DLITE_WITH_X86=ON ${common_flags} -DLITE_WITH_PROFILE=ON
C
Chunwei 已提交
47 48 49 50 51 52 53

    # Compile and execute the gen_code related test, so it will generate some code, and make the compilation reasonable.
    make test_gen_code_lite -j$NUM_CORES_FOR_COMPILE
    make test_cxx_api_lite -j$NUM_CORES_FOR_COMPILE
    ctest -R test_cxx_api_lite
    ctest -R test_gen_code_lite
    make test_generated_code -j$NUM_CORES_FOR_COMPILE
Y
Yan Chunwei 已提交
54 55
}

Y
Yan Chunwei 已提交
56
function cmake_gpu {
57
    prepare_for_codegen
Y
Yan Chunwei 已提交
58 59 60
    cmake .. " -DWITH_GPU=ON {common_flags} -DLITE_WITH_GPU=ON"
}

S
up  
superjomn 已提交
61 62
function check_style {
    export PATH=/usr/bin:$PATH
S
update  
superjomn 已提交
63
    #pre-commit install
S
up  
superjomn 已提交
64 65 66 67 68 69 70 71
    clang-format --version

    if ! pre-commit run -a ; then
        git diff
        exit 1
    fi
}

T
tensor-tang 已提交
72
function build_single {
73
    #make $1 -j$(expr $(nproc) - 2)
C
Chunwei 已提交
74
    make $1 -j$NUM_CORES_FOR_COMPILE
T
tensor-tang 已提交
75 76
}

Y
Yan Chunwei 已提交
77
function build {
78
    make lite_compile_deps -j$NUM_CORES_FOR_COMPILE
Y
Yan Chunwei 已提交
79
}
Y
Yan Chunwei 已提交
80 81 82 83 84

# It will eagerly test all lite related unittests.
function test_lite {
    local file=$1
    echo "file: ${file}"
Y
Yan Chunwei 已提交
85

Y
Yan Chunwei 已提交
86 87 88 89 90
    for _test in $(cat $file); do
        ctest -R $_test -V
    done
}

91 92 93 94 95
# 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"
Y
Yan Chunwei 已提交
96
    cmake_x86_for_CI
C
Chunwei 已提交
97
    build
98 99 100
    test_lite $TESTS_FILE
}

T
tensor-tang 已提交
101 102
# test_arm_android <some_test_name> <adb_port_number>
function test_arm_android {
C
Chunwei 已提交
103 104
    local test_name=$1
    local port=$2
T
tensor-tang 已提交
105 106 107 108 109 110 111 112 113 114 115
    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"
S
sangoly 已提交
116

C
Chunwei 已提交
117
    skip_list=("test_model_parser_lite" "test_mobilenetv1_lite" "test_mobilenetv2_lite" "test_resnet50_lite" "test_inceptionv4_lite" "test_light_api")
S
sangoly 已提交
118 119 120 121
    for skip_name in ${skip_list[@]} ; do
        [[ $skip_name =~ (^|[[:space:]])$test_name($|[[:space:]]) ]] && echo "skip $test_name" && return
    done

C
Chunwei 已提交
122 123 124 125 126 127 128
    local testpath=$(find ./paddle/fluid -name ${test_name})

    # if [[ "$test_name" == "test_light_api" ]]; then
    #     local model_path=$(find . -name "lite_naive_model")
    #     arm_push_necessary_file $port $model_path $adb_work_dir
    # fi

T
tensor-tang 已提交
129 130 131 132 133
    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}"
}

S
sangoly 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
function test_arm_model {
    local test_name=$1
    local port=$2
    local model_dir=$3

    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
    if [[ "${model_dir}x" == "x" ]]; then
        echo "Model dir can not be empty"
        exit 1
    fi

    echo "test name: ${test_name}"
    adb_work_dir="/data/local/tmp"

    testpath=$(find ./paddle/fluid -name ${test_name})
    adb -s emulator-${port} push ${model_dir} ${adb_work_dir}
    adb -s emulator-${port} push ${testpath} ${adb_work_dir}
    adb -s emulator-${port} shell chmod +x "${adb_work_dir}/${test_name}"
C
Chunwei 已提交
159
    local adb_model_path="${adb_work_dir}/`basename ${model_dir}`"
160
    adb -s emulator-${port} shell "${adb_work_dir}/${test_name} --model_dir=$adb_model_path"
T
tensor-tang 已提交
161

S
sangoly 已提交
162 163
}

T
tensor-tang 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
function cmake_arm {
    # $1: ARM_TARGET_OS in "android" , "armlinux"
    # $2: ARM_TARGET_ARCH_ABI in "armv8", "armv7" ,"armv7hf"
    # $3: ARM_TARGET_LANG in "gcc" "clang"
    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 -DARM_TARGET_LANG=$3
}

# $1: ARM_TARGET_OS in "android" , "armlinux"
# $2: ARM_TARGET_ARCH_ABI in "armv8", "armv7" ,"armv7hf"
# $3: ARM_TARGET_LANG in "gcc" "clang"
function build_arm {
    os=$1
    abi=$2
    lang=$3

C
Chunwei 已提交
188
    cur_dir=$(pwd)
T
tensor-tang 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    if [[ ${os} == "armlinux" ]]; then
        # TODO(hongming): enable compile armv7 and armv7hf on armlinux, and clang compile
        if [[ ${lang} == "clang" ]]; then
            echo "clang is not enabled on armlinux yet"
            return 0
        fi
        if [[ ${abi} == "armv7hf" ]]; then
            echo "armv7hf is not supported on armlinux yet"
            return 0
        fi
        if [[ ${abi} == "armv7" ]]; then
            echo "armv7 is not supported on armlinux yet"
            return 0
        fi
    fi

    if [[ ${os} == "android" && ${abi} == "armv7hf" ]]; then
        echo "android do not need armv7hf"
        return 0
    fi

    build_dir=$cur_dir/build.lite.${os}.${abi}.${lang}
    mkdir -p $build_dir
    cd $build_dir

    cmake_arm ${os} ${abi} ${lang}
    build $TESTS_FILE
}

# $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 test port
# Note: test must be in build dir
function test_arm {
    os=$1
    abi=$2
    lang=$3
    port=$4
C
Chunwei 已提交
228

T
tensor-tang 已提交
229 230 231 232 233 234 235 236 237 238
    if [[ ${os} == "armlinux" ]]; then
        # TODO(hongming): enable test armlinux on armv8, armv7 and armv7hf
        echo "Skip test arm linux yet. armlinux must in another docker"
        return 0
    fi

    if [[ ${os} == "android" && ${abi} == "armv7hf" ]]; then
        echo "android do not need armv7hf"
        return 0
    fi
239
    
T
tensor-tang 已提交
240 241 242 243 244 245
    # TODO(yuanshuai): enable armv7 on android
    if [[ ${abi} == "armv7" ]]; then
        echo "skip android v7 test yet"
        return 0
    fi

C
Chunwei 已提交
246

T
tensor-tang 已提交
247 248 249
    echo "test file: ${TESTS_FILE}"
    for _test in $(cat $TESTS_FILE); do
        test_arm_android $_test $port
T
tensor-tang 已提交
250
    done
T
tensor-tang 已提交
251
}
T
tensor-tang 已提交
252

C
Chunwei 已提交
253 254 255
function prepare_emulator {
    local port_armv8=$1
    local port_armv7=$2
T
tensor-tang 已提交
256 257 258 259 260 261 262 263 264 265

    adb kill-server
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    # start android armv8 and armv7 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
C
Chunwei 已提交
266 267
}

C
Chunwei 已提交
268 269 270 271 272 273 274 275
function arm_push_necessary_file {
    local port=$1
    local testpath=$2
    local adb_work_dir=$3

    adb -s emulator-${port} push ${testpath} ${adb_work_dir}
}

C
Chunwei 已提交
276 277 278 279 280 281 282 283 284 285

# We split the arm unittest into several sub-tasks to parallel and reduce the overall CI timetime.
# sub-task1
function build_test_arm_subtask_android {
    ########################################################################
    # job 1-4 must be in one runner
    port_armv8=5554
    port_armv7=5556

    prepare_emulator $port_armv8 $port_armv7
T
tensor-tang 已提交
286

T
tensor-tang 已提交
287 288 289 290 291 292
    # job 1
    build_arm "android" "armv8" "gcc"
    test_arm "android" "armv8" "gcc" ${port_armv8}
    cd -

    # job 2
C
Chunwei 已提交
293 294 295
    #build_arm "android" "armv8" "clang"
    #test_arm "android" "armv8" "clang" ${port_armv8}
    #cd -
T
tensor-tang 已提交
296

T
tensor-tang 已提交
297 298 299 300 301 302
    # job 3
    build_arm "android" "armv7" "gcc"
    test_arm "android" "armv7" "gcc" ${port_armv7}
    cd -

    # job 4
C
Chunwei 已提交
303 304 305
    #build_arm "android" "armv7" "clang"
    #test_arm "android" "armv7" "clang" ${port_armv7}
    #cd -
T
tensor-tang 已提交
306

T
tensor-tang 已提交
307 308
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    echo "Done"
C
Chunwei 已提交
309 310 311 312
}

# sub-task2
function build_test_arm_subtask_armlinux {
T
tensor-tang 已提交
313
    ########################################################################
C
Chunwei 已提交
314 315 316 317 318
    # job 1-4 must be in one runner
    port_armv8=5554
    port_armv7=5556

    prepare_emulator $port_armv8 $port_armv7
T
tensor-tang 已提交
319

C
Chunwei 已提交
320 321
    cur=$PWD

T
tensor-tang 已提交
322
    # job 5
C
Chunwei 已提交
323 324 325
    build_arm "armlinux" "armv8" "gcc" $port_armv8
    test_arm "armlinux" "armv8" "gcc" $port_armv8
    cd $cur
T
tensor-tang 已提交
326 327

    # job 6
C
Chunwei 已提交
328 329 330
    build_arm "armlinux" "armv7" "gcc" $port_armv8
    test_arm "armlinux" "armv7" "gcc" $port_armv8
    cd $cur
T
tensor-tang 已提交
331 332

    # job 7
C
Chunwei 已提交
333 334 335
    build_arm "armlinux" "armv7hf" "gcc" $port_armv8
    test_arm "armlinux" "armv7hf" "gcc" $port_armv8
    cd $cur
T
tensor-tang 已提交
336

C
Chunwei 已提交
337 338 339 340
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    echo "Done"
}

341 342
# sub-task-model
function build_test_arm_subtask_model {
C
Chunwei 已提交
343 344 345 346 347 348 349
    local port_armv8=5554
    local port_armv7=5556
    # We just test following single one environment to limit the CI time.
    local os=android
    local abi=armv8
    local lang=gcc

350 351 352
    local test_name=$1
    local model_name=$2

C
Chunwei 已提交
353 354 355 356 357
    cur_dir=$(pwd)
    build_dir=$cur_dir/build.lite.${os}.${abi}.${lang}
    mkdir -p $build_dir
    cd $build_dir
    cmake_arm $os $abi $lang
358
    make $test_name -j$NUM_CORES_FOR_COMPILE
C
Chunwei 已提交
359 360 361 362

    prepare_emulator $port_armv8 $port_armv7

    # just test the model on armv8
363
    test_arm_model $test_name $port_armv8 "./third_party/install/$model_name"
C
Chunwei 已提交
364 365

    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
T
tensor-tang 已提交
366
    echo "Done"
T
tensor-tang 已提交
367 368
}

C
Chunwei 已提交
369 370 371 372 373 374 375 376 377 378 379 380
# Build the code and run lite arm tests. This is executed in the CI system.
function build_test_arm {
    ########################################################################
    # job 1-4 must be in one runner
    port_armv8=5554
    port_armv7=5556

    build_test_arm_subtask_android
    build_test_arm_subtask_armlinux
}


Y
Yan Chunwei 已提交
381 382 383 384 385 386 387
############################# 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 已提交
388
    echo -e "--arm_os=<os> --arm_abi=<abi> cmake_arm: run cmake with ARM mode"
Y
Yan Chunwei 已提交
389 390
    echo
    echo -e "build: compile the tests"
T
tensor-tang 已提交
391
    echo -e "--test_name=<test_name> build_single: compile single test"
Y
Yan Chunwei 已提交
392 393
    echo
    echo -e "test_server: run server tests"
T
tensor-tang 已提交
394
    echo -e "--test_name=<test_name> --adb_port_number=<adb_port_number> test_arm_android: run arm test"
Y
Yan Chunwei 已提交
395 396 397 398 399 400 401 402 403 404 405 406
    echo "----------------------------------------"
    echo
}

function main {
    # Parse command line.
    for i in "$@"; do
        case $i in
            --tests=*)
                TESTS_FILE="${i#*=}"
                shift
                ;;
T
tensor-tang 已提交
407 408 409 410 411 412 413 414 415 416 417 418
            --test_name=*)
                TEST_NAME="${i#*=}"
                shift
                ;;
            --arm_os=*)
                ARM_OS="${i#*=}"
                shift
                ;;
            --arm_abi=*)
                ARM_ABI="${i#*=}"
                shift
                ;;
T
tensor-tang 已提交
419 420 421 422
            --arm_lang=*)
                ARM_LANG="${i#*=}"
                shift
                ;;
T
tensor-tang 已提交
423 424 425 426
            --arm_port=*)
                ARM_PORT="${i#*=}"
                shift
                ;;
Y
Yan Chunwei 已提交
427 428 429 430 431
            build)
                build $TESTS_FILE
                build $LIBS_FILE
                shift
                ;;
T
tensor-tang 已提交
432 433 434 435
            build_single)
                build_single $TEST_NAME
                shift
                ;;
Y
Yan Chunwei 已提交
436 437 438 439
            cmake_x86)
                cmake_x86
                shift
                ;;
Z
Zhen Wang 已提交
440 441
            cmake_opencl)
                cmake_opencl
Z
Zhen Wang 已提交
442 443
                shift
                ;;
Y
Yan Chunwei 已提交
444 445 446 447 448
            cmake_cuda)
                cmake_cuda
                shift
                ;;
            cmake_arm)
T
tensor-tang 已提交
449 450 451 452 453
                cmake_arm $ARM_OS $ARM_ABI $ARM_LANG
                shift
                ;;
            build_arm)
                build_arm $ARM_OS $ARM_ABI $ARM_LANG
Y
Yan Chunwei 已提交
454 455 456 457 458 459
                shift
                ;;
            test_server)
                test_lite $TESTS_FILE
                shift
                ;;
T
tensor-tang 已提交
460 461 462 463
            test_arm)
                build_arm $ARM_OS $ARM_ABI $ARM_LANG $ARM_PORT
                shift
                ;;
T
tensor-tang 已提交
464 465
            test_arm_android)
                test_arm_android $TEST_NAME $ARM_PORT
Y
Yan Chunwei 已提交
466 467
                shift
                ;;
468 469 470 471
            build_test_server)
                build_test_server
                shift
                ;;
T
tensor-tang 已提交
472 473 474 475
            build_test_arm)
                build_test_arm
                shift
                ;;
C
Chunwei 已提交
476 477 478 479 480 481 482 483
            build_test_arm_subtask_android)
                build_test_arm_subtask_android
                shift
                ;;
            build_test_arm_subtask_armlinux)
                build_test_arm_subtask_armlinux
                shift
                ;;
484 485 486 487 488 489 490 491 492 493 494 495 496 497
            build_test_arm_model_mobilenetv1)
                build_test_arm_subtask_model test_mobilenetv1_lite mobilenet_v1
                shift
                ;;
            build_test_arm_model_mobilenetv2)
                build_test_arm_subtask_model test_mobilenetv2_lite mobilenet_v2
                shift
                ;;
            build_test_arm_model_resnet50)
                build_test_arm_subtask_model test_resnet50_lite resnet50
                shift
                ;;
            build_test_arm_model_inceptionv4)
                build_test_arm_subtask_model test_inceptionv4_lite inception_v4
C
Chunwei 已提交
498 499
                shift
                ;;
S
up  
superjomn 已提交
500 501 502 503
            check_style)
                check_style
                shift
                ;;
S
up  
superjomn 已提交
504 505 506 507
            check_need_ci)
                check_need_ci
                shift
                ;;
Y
Yan Chunwei 已提交
508 509 510 511 512 513 514 515 516 517
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@