run_unittests.sh 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2020 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.

set -e
set +x
NIGHTLY_MODE=$1
18
PRECISION_TEST=$2
19
WITH_GPU=$3
20

21
export PADDLE_ROOT="$(cd "$PWD/../" && pwd )"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
if [ ${NIGHTLY_MODE:-OFF} == "ON" ]; then
    nightly_label=""
else
    nightly_label="(RUN_TYPE=NIGHTLY|RUN_TYPE=DIST:NIGHTLY|RUN_TYPE=EXCLUSIVE:NIGHTLY)"
    echo "========================================="
    echo "Unittests with nightly labels  are only run at night"
    echo "========================================="
fi

if disable_ut_quickly=$(python ${PADDLE_ROOT}/tools/get_quick_disable_lt.py); then
    echo "========================================="
    echo "The following unittests have been disabled:"
    echo ${disable_ut_quickly}
    echo "========================================="
else
    disable_ut_quickly=''
fi

Y
YUNSHEN XIE 已提交
40
# check added ut
41 42 43 44 45 46 47
if [ ${WITH_GPU:-OFF} == "ON" ];then
    set +e
    cp $PADDLE_ROOT/tools/check_added_ut.sh $PADDLE_ROOT/tools/check_added_ut_win.sh
    bash $PADDLE_ROOT/tools/check_added_ut_win.sh
    rm -rf $PADDLE_ROOT/tools/check_added_ut_win.sh
    set -e
fi
Y
YUNSHEN XIE 已提交
48

49
# /*==================Fixed Disabled Windows GPU MKL unittests==============================*/
50
# TODO: fix these unittest that is bound to fail
51 52 53
disable_wingpu_test="^test_model$|\
^test_dataloader_early_reset$|\
^test_add_reader_dependency$|\
54 55
^test_decoupled_py_reader$|\
^test_generator_dataloader$|\
56
^test_parallel_dygraph_sync_batch_norm$|\
57
^test_py_reader_using_executor$|\
58 59 60
^test_parallel_executor_seresnext_base_gpu$|\
^test_parallel_executor_seresnext_with_fuse_all_reduce_gpu$|\
^test_parallel_executor_seresnext_with_reduce_gpu$|\
61
^test_program_prune_backward$|\
62 63 64
^test_decoupled_py_reader_data_check$|\
^test_fleet_base_single$|\
^test_multiprocess_dataloader_iterable_dataset_dynamic$|\
65 66 67
^test_parallel_executor_feed_persistable_var$|\
^test_parallel_executor_inference_feed_partial_data$|\
^test_parallel_ssa_graph_inference_feed_partial_data$|\
68 69 70 71
^test_py_reader_combination$|\
^test_py_reader_pin_memory$|\
^test_py_reader_push_pop$|\
^test_reader_reset$|\
72
^test_imperative_se_resnext$|\
73
^test_sync_batch_norm_op$|\
74
^test_imperative_static_runner_while$|\
75 76 77
^test_dataloader_keep_order$|\
^test_dataloader_unkeep_order$|\
^test_multiprocess_dataloader_iterable_dataset_static$|\
78 79
^test_fuse_bn_act_pass$|\
^test_fuse_bn_add_act_pass$|\
S
Sing_chan 已提交
80 81 82 83 84
^test_gather_op$|\
^test_activation_op$|\
^test_norm_nn_grad$|\
^test_bilinear_interp_op$|\
^test_imperative_optimizer_v2$|\
85 86
^disable_wingpu_test$"

87 88
# /*============================================================================*/

89 90
# /*==================Fixed Disabled Windows CPU OPENBLAS unittests==============================*/
# TODO: fix these unittest that is bound to fail
91
disable_wincpu_test="^jit_kernel_test$|\
92 93 94 95 96 97 98 99 100 101 102 103
^test_analyzer_transformer$|\
^test_vision_models$|\
^test_dygraph_multi_forward$|\
^test_imperative_transformer_sorted_gradient$|\
^test_program_prune_backward$|\
^test_imperative_resnet$|\
^test_imperative_resnet_sorted_gradient$|\
^test_imperative_se_resnext$|\
^test_imperative_static_runner_mnist$|\
^test_bmn$|\
^test_mobile_net$|\
^test_resnet_v2$|\
104
^test_build_strategy$|\
105
^test_se_resnet$|\
106
^disable_wincpu_test$"
107

108
# these unittest that cost long time, diabled temporarily, Maybe moved to the night
S
Sing_chan 已提交
109
long_time_test="^test_gru_op$|\
110 111 112 113 114 115 116 117 118 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 153 154
^decorator_test$|\
^test_dataset_imdb$|\
^test_datasets$|\
^test_pretrained_model$|\
^test_gather_op$|\
^test_gather_nd_op$|\
^test_sequence_conv$|\
^test_space_to_depth_op$|\
^test_activation_nn_grad$|\
^test_activation_op$|\
^test_bicubic_interp_v2_op$|\
^test_bilinear_interp_v2_op$|\
^test_conv_nn_grad$|\
^test_crop_tensor_op$|\
^test_cross_entropy2_op$|\
^test_cross_op$|\
^test_elementwise_div_op$|\
^test_elementwise_nn_grad$|\
^test_fused_elemwise_activation_op$|\
^test_group_norm_op$|\
^test_gru_unit_op$|\
^test_imperative_lod_tensor_to_selected_rows$|\
^test_imperative_optimizer$|\
^test_imperative_selected_rows_to_lod_tensor$|\
^test_layer_norm_op$|\
^test_multiclass_nms_op$|\
^test_nearest_interp_v2_op$|\
^test_nn_grad$|\
^test_norm_nn_grad$|\
^test_normal$|\
^test_pool3d_op$|\
^test_pool2d_op$|\
^test_softmax_with_cross_entropy_op$|\
^test_static_save_load$|\
^test_trilinear_interp_op$|\
^test_trilinear_interp_v2_op$|\
^test_bilinear_interp_op$|\
^test_nearest_interp_op$|\
^test_sequence_conv$|\
^test_sgd_op$|\
^test_transformer$|\
^test_lstmp_op$|\
^test_conv2d_transpose_op$|\
^test_imperative_auto_mixed_precision$|\
^test_imperative_optimizer_v2$|\
155
^test_strided_slice_op$"
156

157 158
if [ ${WITH_GPU:-OFF} == "ON" ];then
    export CUDA_VISIBLE_DEVICES=0
159

160 161 162 163
    UT_list=$(ctest -N | awk -F ': ' '{print $2}' | sed '/^$/d' | sed '$d')
    num=$(ctest -N | awk -F ': ' '{print $2}' | sed '/^$/d' | sed '$d' | wc -l)
    echo "Windows 1 card TestCases count is $num"
    if [ ${PRECISION_TEST:-OFF} == "ON" ]; then
164
        python ${PADDLE_ROOT}/tools/get_pr_ut.py || echo "Failed to obtain ut_list !"
165 166 167 168
        if [[ -f "ut_list" ]]; then
            echo "PREC length: "`wc -l ut_list`
            precision_cases=`cat ut_list`
        fi
169 170
    fi

171 172
    set +e
    if [ ${PRECISION_TEST:-OFF} == "ON" ] && [[ "$precision_cases" != "" ]];then
173
        UT_list_res=$(python ${PADDLE_ROOT}/tools/windows/get_prec_ut_list.py "$UT_list" )
174
        UT_list_prec=$(echo "${UT_list_res}" | grep -v 'PRECISION_TEST')
175
        echo "${UT_list_res}" | grep 'PRECISION_TEST'
176 177 178
        UT_list=$UT_list_prec
    fi
    set -e
179

180 181 182 183 184 185
    output=$(python ${PADDLE_ROOT}/tools/parallel_UT_rule.py "${UT_list}")
    cpu_parallel_job=$(echo $output | cut -d ";" -f 1)
    tetrad_parallel_job=$(echo $output | cut -d ";" -f 2)
    two_parallel_job=$(echo $output | cut -d ";" -f 3)
    non_parallel_job=$(echo $output | cut -d ";" -f 4)
fi
186 187 188 189

failed_test_lists=''
tmp_dir=`mktemp -d`
function collect_failed_tests() {
190
    set +e
191 192 193 194 195 196 197 198 199 200 201
    for file in `ls $tmp_dir`; do
        grep -q 'The following tests FAILED:' $tmp_dir/$file
        exit_code=$?
        if [ $exit_code -ne 0 ]; then
            failuretest=''
        else
            failuretest=`grep -A 10000 'The following tests FAILED:' $tmp_dir/$file | sed 's/The following tests FAILED://g'|sed '/^$/d'`
            failed_test_lists="${failed_test_lists}
            ${failuretest}"
        fi
    done
202
    set -e
203 204
}

205 206
function run_unittest_cpu() {
    tmpfile=$tmp_dir/$RANDOM
207
    (ctest -E "$disable_ut_quickly|$disable_wincpu_test" -LE "${nightly_label}" --output-on-failure -C Release -j 8 | tee $tmpfile) &
208 209 210 211
    wait;
}

function run_unittest_gpu() {
212 213
    test_case=$1
    parallel_job=$2
214
    parallel_level_base=${CTEST_PARALLEL_LEVEL:-1}
215
    if [ "$2" == "" ]; then
216
        parallel_job=$parallel_level_base
217
    else
218
        parallel_job=`expr $2 \* $parallel_level_base`
219 220 221 222 223 224
    fi
    echo "************************************************************************"
    echo "********These unittests run $parallel_job job each time with 1 GPU**********"
    echo "************************************************************************"
    export CUDA_VISIBLE_DEVICES=0
    tmpfile=$tmp_dir/$RANDOM
225
    (ctest -R "$test_case" -E "$disable_ut_quickly|$disable_wingpu_test|$long_time_test" -LE "${nightly_label}" --output-on-failure -C Release -j $parallel_job | tee $tmpfile ) &
226 227 228
    wait;
}

229 230 231 232 233 234 235
function unittests_retry(){
    is_retry_execuate=0
    wintest_error=1
    retry_time=3
    exec_times=0
    exec_retry_threshold=10
    retry_unittests=$(echo "${failed_test_lists}" | grep -oEi "\-.+\(" | sed 's/(//' | sed 's/- //' )
236
    need_retry_ut_counts=$(echo "$retry_unittests" |awk -F ' ' '{print }'| sed '/^$/d' | wc -l)
237
    retry_unittests_regular=$(echo "$retry_unittests" |awk -F ' ' '{print }' | awk 'BEGIN{ all_str=""}{if (all_str==""){all_str=$1}else{all_str=all_str"$|^"$1}} END{print "^"all_str"$"}')
238
    tmpfile=$tmp_dir/$RANDOM
239 240 241 242 243 244 245 246 247 248

    if [ $need_retry_ut_counts -lt $exec_retry_threshold ];then
            retry_unittests_record=''
            while ( [ $exec_times -lt $retry_time ] )
                do
                    retry_unittests_record="$retry_unittests_record$failed_test_lists"
                    if ( [[ "$exec_times" == "0" ]] );then
                        cur_order='first'
                    elif ( [[ "$exec_times" == "1" ]] );then
                        cur_order='second'
249 250 251 252 253 254
                        if [[ "$failed_test_lists" == "" ]]; then
                            break
                        else
                            retry_unittests=$(echo "${failed_test_lists}" | grep -oEi "\-.+\(" | sed 's/(//' | sed 's/- //' )
                            retry_unittests_regular=$(echo "$retry_unittests" |awk -F ' ' '{print }' | awk 'BEGIN{ all_str=""}{if (all_str==""){all_str=$1}else{all_str=all_str"$|^"$1}} END{print "^"all_str"$"}')
                        fi
255
                    elif ( [[ "$exec_times" == "2" ]] );then
256 257 258 259 260 261 262 263 264 265
                        cur_order='third'
                    fi
                    echo "========================================="
                    echo "This is the ${cur_order} time to re-run"
                    echo "========================================="
                    echo "The following unittest will be re-run:"
                    echo "${retry_unittests}"
                    echo "========================================="
                    rm -f $tmp_dir/*
                    failed_test_lists=''
266
                    (ctest -R "($retry_unittests_regular)" --output-on-failure -C Release -j 1 | tee $tmpfile ) &
267
                    wait;
268 269 270 271 272 273 274 275 276 277 278 279
                    collect_failed_tests
                    exec_times=$(echo $exec_times | awk '{print $0+1}')
                done
    else
        # There are more than 10 failed unit tests, so no unit test retry
        is_retry_execuate=1
    fi
    rm -f $tmp_dir/*
}

function show_ut_retry_result() {
    if [[ "$is_retry_execuate" != "0" ]];then
280
        failed_test_lists_ult=`echo "${failed_test_lists}"`
281 282 283 284 285 286 287 288 289 290 291 292
        echo "========================================="
        echo "There are more than 10 failed unit tests, so no unit test retry!!!"
        echo "========================================="
        echo "${failed_test_lists_ult}"
        exit 8;
    else
        retry_unittests_ut_name=$(echo "$retry_unittests_record" | grep -oEi "\-.+\(" | sed 's/(//' | sed 's/- //' )
        retry_unittests_record_judge=$(echo ${retry_unittests_ut_name}| tr ' ' '\n' | sort | uniq -c | awk '{if ($1 >=3) {print $2}}')
        if [ -z "${retry_unittests_record_judge}" ];then
            echo "========================================"
            echo "There are failed tests, which have been successful after re-run:"
            echo "========================================"
293
            echo "The following tests have been re-run:"
294 295 296 297 298 299 300 301 302 303 304 305 306 307
            echo "${retry_unittests_record}"
        else
            failed_ut_re=$(echo "${retry_unittests_record_judge}" | awk 'BEGIN{ all_str=""}{if (all_str==""){all_str=$1}else{all_str=all_str"|"$1}} END{print all_str}')
            echo "========================================"
            echo "There are failed tests, which have been executed re-run,but success rate is less than 50%:"
            echo "Summary Failed Tests... "
            echo "========================================"
            echo "The following tests FAILED: "
            echo "${retry_unittests_record}" | grep -E "$failed_ut_re"
            exit 8;
        fi
    fi
}

308
set +e
309

310
export FLAGS_call_stack_level=2
311
if [ "${WITH_GPU:-OFF}" == "ON" ];then
Y
YUNSHEN XIE 已提交
312 313 314
    if [ -f "$PADDLE_ROOT/added_ut" ];then
        added_uts=^$(awk BEGIN{RS=EOF}'{gsub(/\n/,"$|^");print}' $PADDLE_ROOT/added_ut)$
        ctest -R "(${added_uts})" --output-on-failure -C Release --repeat-until-fail 3;added_ut_error=$?
315
        rm -f $PADDLE_ROOT/added_ut
Y
YUNSHEN XIE 已提交
316 317 318 319 320 321 322
        if [ "$added_ut_error" != 0 ];then
            echo "========================================"
            echo "Added UT should pass three additional executions"
            echo "========================================"
            exit 8;
        fi
    fi
323
    run_unittest_gpu $cpu_parallel_job 10
324 325 326 327 328 329
    run_unittest_gpu $tetrad_parallel_job 4
    run_unittest_gpu $two_parallel_job 2
    run_unittest_gpu $non_parallel_job
else
    run_unittest_cpu
fi
330 331 332 333
collect_failed_tests
set -e
rm -f $tmp_dir/*
if [[ "$failed_test_lists" != "" ]]; then
334 335
    unittests_retry
    show_ut_retry_result
336
fi