test_train_inference_python.sh 14.7 KB
Newer Older
L
lzzyzlbb 已提交
1 2 3 4 5 6 7
#!/bin/bash
source test_tipc/common_func.sh

FILENAME=$1
# MODE be one of ['lite_train_lite_infer' 'lite_train_whole_infer' 'whole_train_whole_infer', 'whole_infer']
MODE=$2

W
wangna11BD 已提交
8
dataline=$(cat ${FILENAME})
L
lzzyzlbb 已提交
9 10 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

# parser params
IFS=$'\n'
lines=(${dataline})
# The training params
model_name=$(func_parser_value "${lines[1]}")
python=$(func_parser_value "${lines[2]}")
gpu_list=$(func_parser_value "${lines[3]}")

autocast_list=$(func_parser_value "${lines[5]}")
epoch_key=$(func_parser_key "${lines[6]}")
epoch_num=$(func_parser_params "${lines[6]}")
save_model_key=$(func_parser_key "${lines[7]}")
train_batch_key=$(func_parser_key "${lines[8]}")
train_batch_value=$(func_parser_params "${lines[8]}")
pretrain_model_key=$(func_parser_key "${lines[9]}")
pretrain_model_value=$(func_parser_value "${lines[9]}")
train_model_name=$(func_parser_value "${lines[10]}")
train_infer_img_dir=$(func_parser_value "${lines[11]}")
train_param_key1=$(func_parser_key "${lines[12]}")
train_param_value1=$(func_parser_value "${lines[12]}")

trainer_list=$(func_parser_value "${lines[14]}")

trainer_norm=$(func_parser_key "${lines[15]}")
norm_trainer=$(func_parser_value "${lines[15]}")

W
wangna11BD 已提交
36 37
trainer_key1=$(func_parser_key "${lines[19]}")
trainer_value1=$(func_parser_value "${lines[19]}")
L
lzzyzlbb 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
trainer_key2=$(func_parser_key "${lines[20]}")
trainer_value2=$(func_parser_value "${lines[20]}")

eval_py=$(func_parser_value "${lines[23]}")
eval_key1=$(func_parser_key "${lines[24]}")
eval_value1=$(func_parser_value "${lines[24]}")

save_infer_key=$(func_parser_key "${lines[27]}")
export_weight=$(func_parser_value "${lines[28]}")
norm_export=$(func_parser_value "${lines[29]}")

inference_dir=$(func_parser_value "${lines[35]}")

51
# parser inference model
L
lzzyzlbb 已提交
52 53 54
infer_model_dir_list=$(func_parser_value "${lines[36]}")
infer_export_list=$(func_parser_value "${lines[37]}")
infer_is_quant=$(func_parser_value "${lines[38]}")
55
# parser inference
L
lzzyzlbb 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
inference_py=$(func_parser_value "${lines[39]}")
use_gpu_key=$(func_parser_key "${lines[40]}")
use_gpu_list=$(func_parser_value "${lines[40]}")
use_mkldnn_key=$(func_parser_key "${lines[41]}")
use_mkldnn_list=$(func_parser_value "${lines[41]}")
cpu_threads_key=$(func_parser_key "${lines[42]}")
cpu_threads_list=$(func_parser_value "${lines[42]}")
batch_size_key=$(func_parser_key "${lines[43]}")
batch_size_list=$(func_parser_value "${lines[43]}")
use_trt_key=$(func_parser_key "${lines[44]}")
use_trt_list=$(func_parser_value "${lines[44]}")
precision_key=$(func_parser_key "${lines[45]}")
precision_list=$(func_parser_value "${lines[45]}")
infer_model_key=$(func_parser_key "${lines[46]}")
image_dir_key=$(func_parser_key "${lines[47]}")
infer_img_dir=$(func_parser_value "${lines[47]}")
save_log_key=$(func_parser_key "${lines[48]}")
infer_key1=$(func_parser_key "${lines[50]}")
infer_value1=$(func_parser_value "${lines[50]}")

W
wangna11BD 已提交
76 77 78 79
line_num=`grep -n -w "to_static_train_benchmark_params" $FILENAME  | cut -d ":" -f 1`
to_static_key=$(func_parser_key "${lines[line_num]}")
to_static_trainer=$(func_parser_value "${lines[line_num]}")

80
LOG_PATH="./test_tipc/output/${model_name}/${MODE}"
L
lzzyzlbb 已提交
81 82 83 84 85 86 87 88 89 90 91
mkdir -p ${LOG_PATH}
status_log="${LOG_PATH}/results_python.log"

function func_inference(){
    IFS='|'
    _python=$1
    _script=$2
    _model_dir=$3
    _log_path=$4
    _img_dir=$5
    _flag_quant=$6
92
    # inference
L
lzzyzlbb 已提交
93 94 95 96 97 98 99 100 101 102
    for use_gpu in ${use_gpu_list[*]}; do
        if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
            for use_mkldnn in ${use_mkldnn_list[*]}; do
                if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
                    continue
                fi
                for threads in ${cpu_threads_list[*]}; do
                    for batch_size in ${batch_size_list[*]}; do
                        for precision in ${precision_list[*]}; do
                            set_precision=$(func_set_params "${precision_key}" "${precision}")
103

L
lzzyzlbb 已提交
104 105 106 107 108 109 110 111 112 113 114
                            _save_log_path="${_log_path}/python_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log"
                            set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
                            set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
                            set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
                            set_cpu_threads=$(func_set_params "${cpu_threads_key}" "${threads}")
                            set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
                            set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
                            command="${_python} ${_script} ${use_gpu_key}=${use_gpu} ${set_model_dir} > ${_save_log_path} 2>&1 "
                            eval $command
                            last_status=${PIPESTATUS[0]}
                            eval "cat ${_save_log_path}"
B
Birdylx 已提交
115
                            status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
L
lzzyzlbb 已提交
116 117 118 119 120 121 122 123 124
                        done
                    done
                done
            done
        elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
            for use_trt in ${use_trt_list[*]}; do
                for precision in ${precision_list[*]}; do
                    if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then
                        continue
125
                    fi
L
lzzyzlbb 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                    if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then
                        continue
                    fi
                    if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [ ${_flag_quant} = "True" ]; then
                        continue
                    fi
                    for batch_size in ${batch_size_list[*]}; do
                        _save_log_path="${_log_path}/python_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log"
                        set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
                        set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
                        set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
                        set_tensorrt=$(func_set_params "${use_trt_key}" "${use_trt}")
                        set_precision=$(func_set_params "${precision_key}" "${precision}")
                        set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
                        set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
                        command="${_python} ${_script} ${use_gpu_key}=${use_gpu} ${set_tensorrt} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 "
                        eval $command
                        last_status=${PIPESTATUS[0]}
                        eval "cat ${_save_log_path}"
B
Birdylx 已提交
145
                        status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
146

L
lzzyzlbb 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                    done
                done
            done
        else
            echo "Does not support hardware other than CPU and GPU Currently!"
        fi
    done
}

if [ ${MODE} = "whole_infer" ]; then
    GPUID=$3
    if [ ${#GPUID} -le 0 ];then
        env=" "
    else
        env="export CUDA_VISIBLE_DEVICES=${GPUID}"
    fi
    # set CUDA_VISIBLE_DEVICES
    eval $env
    export Count=0
    IFS="|"
    infer_run_exports=(${infer_export_list})
    infer_quant_flag=(${infer_is_quant})
    for infer_model in ${infer_model_dir_list[*]}; do
        # run export
        if [ ${infer_run_exports[Count]} != "null" ];then
            save_infer_dir=$(dirname $infer_model)
            set_export_weight=$(func_set_params "${export_weight}" "${infer_model}")
            set_save_infer_key="${save_infer_key} ${save_infer_dir}"
B
Birdylx 已提交
175 176
            export_log_path="${LOG_PATH}_export_${Count}.log"
            export_cmd="${python} ${infer_run_exports[Count]} ${set_export_weight} ${set_save_infer_key} > ${export_log_path} 2>&1"
177
            echo ${infer_run_exports[Count]}
L
lzzyzlbb 已提交
178 179 180
            echo  $export_cmd
            eval $export_cmd
            status_export=$?
B
Birdylx 已提交
181
            status_check $status_export "${export_cmd}" "${status_log}" "${model_name}" "${export_log_path}"
L
lzzyzlbb 已提交
182 183 184 185 186 187 188 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
        else
            save_infer_dir=${infer_model}
        fi
        #run inference
        func_inference "${python}" "${inference_py}" "${save_infer_dir}" "${LOG_PATH}" "${infer_img_dir}"
        Count=$(($Count + 1))
    done
else
    IFS="|"
    export Count=0
    USE_GPU_KEY=(${train_use_gpu_value})
    for gpu in ${gpu_list[*]}; do
        train_use_gpu=${USE_GPU_KEY[Count]}
        Count=$(($Count + 1))
        ips=""
        if [ ${gpu} = "-1" ];then
            env=""
        elif [ ${#gpu} -le 1 ];then
            env="export CUDA_VISIBLE_DEVICES=${gpu}"
            eval ${env}
        elif [ ${#gpu} -le 15 ];then
            IFS=","
            array=(${gpu})
            env="export CUDA_VISIBLE_DEVICES=${gpu}"
            IFS="|"
        else
            IFS=";"
            array=(${gpu})
            ips=${array[0]}
            gpu=${array[1]}
            IFS="|"
            env=" "
        fi
215 216 217
        for autocast in ${autocast_list[*]}; do
            if [ ${autocast} = "fp16" ]; then
                set_amp_config="--amp"
B
Birdylx 已提交
218
                set_amp_level="--amp_level=O2"
L
lzzyzlbb 已提交
219 220
            else
                set_amp_config=" "
B
Birdylx 已提交
221
                set_amp_level=" "
222 223
            fi
            for trainer in ${trainer_list[*]}; do
L
lzzyzlbb 已提交
224
                flag_quant=False
225 226 227 228 229 230 231 232 233 234
                # In case of @to_static, we re-used norm_traier,
                # but append "-o Global.to_static=True" for config
                # to trigger "apply_to_static" logic in 'engine.py'
                if [ ${trainer} = "${to_static_key}" ]; then
                    run_train="${norm_trainer}  ${to_static_trainer}"
                    run_export=${norm_export}
                else
                    run_train=${norm_trainer}
                    run_export=${norm_export}
                fi
235

L
lzzyzlbb 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                if [ ${run_train} = "null" ]; then
                    continue
                fi
                set_autocast=$(func_set_params "${autocast_key}" "${autocast}")
                set_epoch=$(func_set_params "${epoch_key}" "${epoch_num}")
                set_pretrain=$(func_set_params "${pretrain_model_key}" "${pretrain_model_value}")
                set_batchsize=$(func_set_params "${train_batch_key}" "${train_batch_value}")
                set_train_params1=$(func_set_params "${train_param_key1}" "${train_param_value1}")
                set_use_gpu=$(func_set_params "${train_use_gpu_key}" "${train_use_gpu}")
                if [ ${#ips} -le 26 ];then
                    save_log="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}"
                    nodes=1
                else
                    IFS=","
                    ips_array=(${ips})
                    IFS="|"
                    nodes=${#ips_array[@]}
                    save_log="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}"
                fi
                set_save_model=$(func_set_params "${save_model_key}" "${save_log}")
                if [ ${#gpu} -le 2 ];then  # train with cpu or single gpu
B
Birdylx 已提交
257
                    cmd="${python} ${run_train} ${set_use_gpu}  ${set_save_model} ${set_train_params1} ${set_epoch} ${set_pretrain} ${set_batchsize} ${set_amp_config} ${set_amp_level}"
L
lzzyzlbb 已提交
258
                elif [ ${#ips} -le 26 ];then  # train with multi-gpu
B
Birdylx 已提交
259
                    cmd="${python} -m paddle.distributed.launch --gpus=${gpu} ${run_train} ${set_use_gpu} ${set_save_model} ${set_train_params1} ${set_epoch} ${set_pretrain} ${set_batchsize} ${set_amp_config} ${set_amp_level}"
L
lzzyzlbb 已提交
260
                else     # train with multi-machine
B
Birdylx 已提交
261
                    cmd="${python} -m paddle.distributed.launch --ips=${ips} --gpus=${gpu} ${run_train} ${set_use_gpu} ${set_save_model} ${set_train_params1} ${set_pretrain} ${set_epoch} ${set_batchsize} ${set_amp_config} ${set_amp_level}"
L
lzzyzlbb 已提交
262 263
                fi
                # run train
L
lzzyzlbb 已提交
264
                export FLAGS_cudnn_deterministic=True
L
lzzyzlbb 已提交
265 266
                eval $cmd
                echo $cmd
267 268 269
                log_name=${train_model_name/checkpoint.pdparams/.txt}
                train_log_path=$( echo "${save_log}/${log_name}")
                eval "cat ${train_log_path} >> ${save_log}.log"
B
Birdylx 已提交
270
                status_check $? "${cmd}" "${status_log}" "${model_name}" "${save_log}.log"
L
lzzyzlbb 已提交
271 272

                set_eval_pretrain=$(func_set_params "${pretrain_model_key}" "${save_log}/${train_model_name}")
273 274 275
                # save norm trained models to set pretrain for pact training and fpgm training

                # run eval
L
lzzyzlbb 已提交
276 277
                if [ ${eval_py} != "null" ]; then
                    set_eval_params1=$(func_set_params "${eval_key1}" "${eval_value1}")
B
Birdylx 已提交
278 279
                    eval_log_path="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_eval.log"
                    eval_cmd="${python} ${eval_py} ${set_eval_pretrain} ${set_use_gpu} ${set_eval_params1} > ${eval_log_path} 2>&1"
L
lzzyzlbb 已提交
280
                    eval $eval_cmd
B
Birdylx 已提交
281
                    status_check $? "${eval_cmd}" "${status_log}" "${model_name}" "${eval_log_path}"
L
lzzyzlbb 已提交
282 283
                fi
                # run export model
284
                if [ ${run_export} != "null" ]; then
L
lzzyzlbb 已提交
285 286 287 288 289
                    # run export model
                    save_infer_path="${save_log}"
                    set_export_weight="${save_log}/${train_model_name}"
                    set_export_weight_path=$( echo ${set_export_weight})
                    set_save_infer_key="${save_infer_key} ${save_infer_path}"
B
Birdylx 已提交
290 291
                    export_log_path="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_export.log"
                    export_cmd="${python} ${run_export}  ${set_export_weight_path} ${set_save_infer_key} > ${export_log_path} 2>&1"
L
lzzyzlbb 已提交
292
                    eval "$export_cmd"
B
Birdylx 已提交
293
                    status_check $? "${export_cmd}" "${status_log}" "${model_name}" "${export_log_path}"
294

L
lzzyzlbb 已提交
295 296 297 298 299 300 301 302 303
                    #run inference
                    eval $env
                    save_infer_path="${save_log}"
                    if [ ${inference_dir} != "null" ] && [ ${inference_dir} != '##' ]; then
                        infer_model_dir="${save_infer_path}/${inference_dir}"
                    else
                        infer_model_dir=${save_infer_path}
                    fi
                    func_inference "${python}" "${inference_py}" "${infer_model_dir}" "${LOG_PATH}" "${train_infer_img_dir}" "${flag_quant}"
304

L
lzzyzlbb 已提交
305 306
                    eval "unset CUDA_VISIBLE_DEVICES"
                fi
307 308
            done  # done with:    for trainer in ${trainer_list[*]}; do
        done      # done with:    for autocast in ${autocast_list[*]}; do
L
lzzyzlbb 已提交
309 310
    done          # done with:    for gpu in ${gpu_list[*]}; do
fi  # end if [ ${MODE} = "infer" ]; then