test.sh 26.8 KB
Newer Older
LDOUBLEV's avatar
LDOUBLEV committed
1
2
#!/bin/bash
FILENAME=$1
MissPenguin's avatar
MissPenguin committed
3
# MODE be one of ['lite_train_infer' 'whole_infer' 'whole_train_infer', 'infer', 'cpp_infer']
LDOUBLEV's avatar
LDOUBLEV committed
4
5
6
7
8
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
36
MODE=$2

dataline=$(cat ${FILENAME})

# parser params
IFS=$'\n'
lines=(${dataline})

function func_parser_key(){
    strs=$1
    IFS=":"
    array=(${strs})
    tmp=${array[0]}
    echo ${tmp}
}
function func_parser_value(){
    strs=$1
    IFS=":"
    array=(${strs})
    tmp=${array[1]}
    echo ${tmp}
}
function func_set_params(){
    key=$1
    value=$2
    if [ ${key} = "null" ];then
        echo " "
    elif [[ ${value} = "null" ]] || [[ ${value} = " " ]] || [ ${#value} -le 0 ];then
        echo " "
    else 
        echo "${key}=${value}"
    fi
}
LDOUBLEV's avatar
LDOUBLEV committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function func_parser_params(){
    strs=$1
    IFS=":"
    array=(${strs})
    key=${array[0]}
    tmp=${array[1]}
    IFS="|"
    res=""
    for _params in ${tmp[*]}; do
        IFS="="
        array=(${_params})
        mode=${array[0]}
        value=${array[1]}
        if [[ ${mode} = ${MODE} ]]; then
            IFS="|"
LDOUBLEV's avatar
LDOUBLEV committed
52
            #echo $(func_set_params "${mode}" "${value}")
LDOUBLEV's avatar
LDOUBLEV committed
53
            echo $value
LDOUBLEV's avatar
LDOUBLEV committed
54
55
56
57
58
59
            break
        fi
        IFS="|"
    done
    echo ${res}
}
LDOUBLEV's avatar
LDOUBLEV committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function status_check(){
    last_status=$1   # the exit code
    run_command=$2
    run_log=$3
    if [ $last_status -eq 0 ]; then
        echo -e "\033[33m Run successfully with command - ${run_command}!  \033[0m" | tee -a ${run_log}
    else
        echo -e "\033[33m Run failed with command - ${run_command}!  \033[0m" | tee -a ${run_log}
    fi
}

IFS=$'\n'
# The training params
model_name=$(func_parser_value "${lines[1]}")
python=$(func_parser_value "${lines[2]}")
gpu_list=$(func_parser_value "${lines[3]}")
LDOUBLEV's avatar
LDOUBLEV committed
76
77
78
79
80
train_use_gpu_key=$(func_parser_key "${lines[4]}")
train_use_gpu_value=$(func_parser_value "${lines[4]}")
autocast_list=$(func_parser_value "${lines[5]}")
autocast_key=$(func_parser_key "${lines[5]}")
epoch_key=$(func_parser_key "${lines[6]}")
LDOUBLEV's avatar
LDOUBLEV committed
81
epoch_num=$(func_parser_params "${lines[6]}")
LDOUBLEV's avatar
LDOUBLEV committed
82
83
save_model_key=$(func_parser_key "${lines[7]}")
train_batch_key=$(func_parser_key "${lines[8]}")
LDOUBLEV's avatar
LDOUBLEV committed
84
train_batch_value=$(func_parser_params "${lines[8]}")
LDOUBLEV's avatar
LDOUBLEV committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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]}")
pact_key=$(func_parser_key "${lines[16]}")
pact_trainer=$(func_parser_value "${lines[16]}")
fpgm_key=$(func_parser_key "${lines[17]}")
fpgm_trainer=$(func_parser_value "${lines[17]}")
distill_key=$(func_parser_key "${lines[18]}")
distill_trainer=$(func_parser_value "${lines[18]}")
trainer_key1=$(func_parser_key "${lines[19]}")
trainer_value1=$(func_parser_value "${lines[19]}")
LDOUBLEV's avatar
LDOUBLEV committed
103
trainer_key2=$(func_parser_key "${lines[20]}")
LDOUBLEV's avatar
LDOUBLEV committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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_key "${lines[28]}")
norm_export=$(func_parser_value "${lines[29]}")
pact_export=$(func_parser_value "${lines[30]}")
fpgm_export=$(func_parser_value "${lines[31]}")
distill_export=$(func_parser_value "${lines[32]}")
export_key1=$(func_parser_key "${lines[33]}")
export_value1=$(func_parser_value "${lines[33]}")
export_key2=$(func_parser_key "${lines[34]}")
export_value2=$(func_parser_value "${lines[34]}")

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
# parser inference model 
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]}")
# parser inference 
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]}")
benchmark_key=$(func_parser_key "${lines[49]}")
benchmark_value=$(func_parser_value "${lines[49]}")
infer_key1=$(func_parser_key "${lines[50]}")
infer_value1=$(func_parser_value "${lines[50]}")
tink2123's avatar
tink2123 committed
147
# parser serving
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
trans_model_py=$(func_parser_value "${lines[67]}")
infer_model_dir_key=$(func_parser_key "${lines[68]}")
infer_model_dir_value=$(func_parser_value "${lines[68]}")
model_filename_key=$(func_parser_key "${lines[69]}")
model_filename_value=$(func_parser_value "${lines[69]}")
params_filename_key=$(func_parser_key "${lines[70]}")
params_filename_value=$(func_parser_value "${lines[70]}")
serving_server_key=$(func_parser_key "${lines[71]}")
serving_server_value=$(func_parser_value "${lines[71]}")
serving_client_key=$(func_parser_key "${lines[72]}")
serving_client_value=$(func_parser_value "${lines[72]}")
serving_dir_value=$(func_parser_value "${lines[73]}")
web_service_py=$(func_parser_value "${lines[74]}")
web_use_gpu_key=$(func_parser_key "${lines[75]}")
web_use_gpu_list=$(func_parser_value "${lines[75]}")
web_use_mkldnn_key=$(func_parser_key "${lines[76]}")
web_use_mkldnn_list=$(func_parser_value "${lines[76]}")
web_cpu_threads_key=$(func_parser_key "${lines[77]}")
web_cpu_threads_list=$(func_parser_value "${lines[77]}")
web_use_trt_key=$(func_parser_key "${lines[78]}")
web_use_trt_list=$(func_parser_value "${lines[78]}")
web_precision_key=$(func_parser_key "${lines[79]}")
web_precision_list=$(func_parser_value "${lines[79]}")
pipeline_py=$(func_parser_value "${lines[80]}")

LDOUBLEV's avatar
LDOUBLEV committed
173

MissPenguin's avatar
refine  
MissPenguin committed
174
175
if [ ${MODE} = "cpp_infer" ]; then
    # parser cpp inference model 
MissPenguin's avatar
refine  
MissPenguin committed
176
177
    cpp_infer_model_dir_list=$(func_parser_value "${lines[53]}")
    cpp_infer_is_quant=$(func_parser_value "${lines[54]}")
MissPenguin's avatar
refine  
MissPenguin committed
178
    # parser cpp inference 
MissPenguin's avatar
refine  
MissPenguin committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    inference_cmd=$(func_parser_value "${lines[55]}")
    cpp_use_gpu_key=$(func_parser_key "${lines[56]}")
    cpp_use_gpu_list=$(func_parser_value "${lines[56]}")
    cpp_use_mkldnn_key=$(func_parser_key "${lines[57]}")
    cpp_use_mkldnn_list=$(func_parser_value "${lines[57]}")
    cpp_cpu_threads_key=$(func_parser_key "${lines[58]}")
    cpp_cpu_threads_list=$(func_parser_value "${lines[58]}")
    cpp_batch_size_key=$(func_parser_key "${lines[59]}")
    cpp_batch_size_list=$(func_parser_value "${lines[59]}")
    cpp_use_trt_key=$(func_parser_key "${lines[60]}")
    cpp_use_trt_list=$(func_parser_value "${lines[60]}")
    cpp_precision_key=$(func_parser_key "${lines[61]}")
    cpp_precision_list=$(func_parser_value "${lines[61]}")
    cpp_infer_model_key=$(func_parser_key "${lines[62]}")
    cpp_image_dir_key=$(func_parser_key "${lines[63]}")
    cpp_infer_img_dir=$(func_parser_value "${lines[63]}")
MissPenguin's avatar
MissPenguin committed
195
196
    cpp_infer_key1=$(func_parser_key "${lines[64]}")
    cpp_infer_value1=$(func_parser_value "${lines[64]}")
MissPenguin's avatar
refine  
MissPenguin committed
197
198
    cpp_benchmark_key=$(func_parser_key "${lines[65]}")
    cpp_benchmark_value=$(func_parser_value "${lines[65]}")
MissPenguin's avatar
refine  
MissPenguin committed
199
fi
MissPenguin's avatar
MissPenguin committed
200
201


LDOUBLEV's avatar
LDOUBLEV committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
LOG_PATH="./tests/output"
mkdir -p ${LOG_PATH}
status_log="${LOG_PATH}/results.log"


function func_inference(){
    IFS='|'
    _python=$1
    _script=$2
    _model_dir=$3
    _log_path=$4
    _img_dir=$5
    _flag_quant=$6
    # inference 
    for use_gpu in ${use_gpu_list[*]}; do
LDOUBLEV's avatar
LDOUBLEV committed
217
        if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
LDOUBLEV's avatar
LDOUBLEV committed
218
            for use_mkldnn in ${use_mkldnn_list[*]}; do
219
220
221
                if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
                    continue
                fi
LDOUBLEV's avatar
LDOUBLEV committed
222
223
224
225
226
                for threads in ${cpu_threads_list[*]}; do
                    for batch_size in ${batch_size_list[*]}; do
                        _save_log_path="${_log_path}/infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_batchsize_${batch_size}.log"
                        set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
                        set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
LDOUBLEV's avatar
LDOUBLEV committed
227
228
229
                        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}")
230
                        set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
Double_V's avatar
Double_V committed
231
                        command="${_python} ${_script} ${use_gpu_key}=${use_gpu} ${use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 "
LDOUBLEV's avatar
LDOUBLEV committed
232
                        eval $command
Double_V's avatar
Double_V committed
233
234
235
                        last_status=${PIPESTATUS[0]}
                        eval "cat ${_save_log_path}"
                        status_check $last_status "${command}" "${status_log}"
LDOUBLEV's avatar
LDOUBLEV committed
236
237
238
                    done
                done
            done
LDOUBLEV's avatar
LDOUBLEV committed
239
        elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
LDOUBLEV's avatar
LDOUBLEV committed
240
241
            for use_trt in ${use_trt_list[*]}; do
                for precision in ${precision_list[*]}; do
242
243
244
                    if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then
                        continue
                    fi 
245
                    if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then
LDOUBLEV's avatar
LDOUBLEV committed
246
247
                        continue
                    fi
248
                    if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [ ${_flag_quant} = "True" ]; then
LDOUBLEV's avatar
LDOUBLEV committed
249
250
251
252
253
254
                        continue
                    fi
                    for batch_size in ${batch_size_list[*]}; do
                        _save_log_path="${_log_path}/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}")
LDOUBLEV's avatar
LDOUBLEV committed
255
256
257
258
                        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}")
LDOUBLEV's avatar
LDOUBLEV committed
259
260
                        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 "
LDOUBLEV's avatar
LDOUBLEV committed
261
                        eval $command
Double_V's avatar
Double_V committed
262
263
264
265
                        last_status=${PIPESTATUS[0]}
                        eval "cat ${_save_log_path}"
                        status_check $last_status "${command}" "${status_log}"
                        
LDOUBLEV's avatar
LDOUBLEV committed
266
267
268
                    done
                done
            done
LDOUBLEV's avatar
LDOUBLEV committed
269
        else
270
            echo "Does not support hardware other than CPU and GPU Currently!"
LDOUBLEV's avatar
LDOUBLEV committed
271
272
273
        fi
    done
}
tink2123's avatar
tink2123 committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
function func_serving(){
    IFS='|'
    _python=$1
    _script=$2
    _model_dir=$3
    # pdserving
    set_dirname=$(func_set_params "${infer_model_dir_key}" "${infer_model_dir_value}")
    set_model_filename=$(func_set_params "${model_filename_key}" "${model_filename_value}")
    set_params_filename=$(func_set_params "${params_filename_key}" "${params_filename_value}")
    set_serving_server=$(func_set_params "${serving_server_key}" "${serving_server_value}")
    set_serving_client=$(func_set_params "${serving_client_key}" "${serving_client_value}")
    trans_model_cmd="${python} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client}"
    eval $trans_model_cmd
    cd ${serving_dir_value}
    echo $PWD
tink2123's avatar
tink2123 committed
289
290
    unset https_proxy
    unset http_proxy
tink2123's avatar
tink2123 committed
291
292
293
294
295
296
297
298
299
300
    for use_gpu in ${web_use_gpu_list[*]}; do
        echo ${ues_gpu}
        if [ ${use_gpu} = "null" ]; then
            for use_mkldnn in ${web_use_mkldnn_list[*]}; do
                if [ ${use_mkldnn} = "False" ]; then
                    continue
                fi
                for threads in ${web_cpu_threads_list[*]}; do
                      _save_log_path="${_log_path}/server_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_batchsize_1.log"
                      set_cpu_threads=$(func_set_params "${web_cpu_threads_key}" "${threads}")
tink2123's avatar
tink2123 committed
301
                      web_service_cmd="${python} ${web_service_py} ${web_use_gpu_key}=${use_gpu} ${web_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} &>${_save_log_path} &"
tink2123's avatar
tink2123 committed
302
                      eval $web_service_cmd
tink2123's avatar
tink2123 committed
303
304
305
306
307
308
309
310
311
312
                      sleep 2s
                      pipeline_cmd="${python} ${pipeline_py}"
                      eval $pipeline_cmd
                      last_status=${PIPESTATUS[0]}
                      eval "cat ${_save_log_path}"
                      status_check $last_status "${pipeline_cmd}" "${status_log}"
                      PID=$!
                      kill $PID
                      sleep 2s
                      ps ux | grep -E 'web_service|pipeline' | awk '{print $2}' | xargs kill -s 9
tink2123's avatar
tink2123 committed
313
314
315
316
317
318
319
320
321
322
323
                done
            done
        elif [ ${use_gpu} = "0" ]; then
            for use_trt in ${web_use_trt_list[*]}; do
                for precision in ${web_precision_list[*]}; do
                    if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then
                        continue
                    fi
                    if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then
                        continue
                    fi
tink2123's avatar
tink2123 committed
324
                    if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [[ ${_flag_quant} = "True" ]]; then
tink2123's avatar
tink2123 committed
325
326
327
328
329
                        continue
                    fi
                    _save_log_path="${_log_path}/infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_1.log"
                    set_tensorrt=$(func_set_params "${web_use_trt_key}" "${use_trt}")
                    set_precision=$(func_set_params "${web_precision_key}" "${precision}")
tink2123's avatar
tink2123 committed
330
                    web_service_cmd="${python} ${web_service_py} ${web_use_gpu_key}=${use_gpu} ${set_tensorrt} ${set_precision} &>${_save_log_path} & "
tink2123's avatar
tink2123 committed
331
                    eval $web_service_cmd
tink2123's avatar
tink2123 committed
332
333
334
335
336
337
338
339
340
341
                    sleep 2s
                    pipeline_cmd="${python} ${pipeline_py}"
                    eval $pipeline_cmd
                    last_status=${PIPESTATUS[0]}
                    eval "cat ${_save_log_path}"
                    status_check $last_status "${pipeline_cmd}" "${status_log}"
                    PID=$!
                    kill $PID
                    sleep 2s
                    ps ux | grep -E 'web_service|pipeline' | awk '{print $2}' | xargs kill -s 9
tink2123's avatar
tink2123 committed
342
343
344
345
346
347
348
                done
            done
        else
            echo "Does not support hardware other than CPU and GPU Currently!"
        fi
    done
}
LDOUBLEV's avatar
LDOUBLEV committed
349

MissPenguin's avatar
MissPenguin committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
function func_cpp_inference(){
    IFS='|'
    _script=$1
    _model_dir=$2
    _log_path=$3
    _img_dir=$4
    _flag_quant=$5
    # inference 
    for use_gpu in ${cpp_use_gpu_list[*]}; do
        if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
            for use_mkldnn in ${cpp_use_mkldnn_list[*]}; do
                if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
                    continue
                fi
                for threads in ${cpp_cpu_threads_list[*]}; do
                    for batch_size in ${cpp_batch_size_list[*]}; do
                        _save_log_path="${_log_path}/cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_batchsize_${batch_size}.log"
                        set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}")
                        set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}")
                        set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}")
                        set_cpu_threads=$(func_set_params "${cpp_cpu_threads_key}" "${threads}")
                        set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}")
MissPenguin's avatar
MissPenguin committed
372
                        set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}")
MissPenguin's avatar
MissPenguin committed
373
                        command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${cpp_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 "
MissPenguin's avatar
MissPenguin committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
                        eval $command
                        last_status=${PIPESTATUS[0]}
                        eval "cat ${_save_log_path}"
                        status_check $last_status "${command}" "${status_log}"
                    done
                done
            done
        elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
            for use_trt in ${cpp_use_trt_list[*]}; do
                for precision in ${cpp_precision_list[*]}; do
                    if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then
                        continue
                    fi 
                    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 ${cpp_batch_size_list[*]}; do
                        _save_log_path="${_log_path}/cpp_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log"
                        set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}")
                        set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}")
                        set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}")
                        set_tensorrt=$(func_set_params "${cpp_use_trt_key}" "${use_trt}")
                        set_precision=$(func_set_params "${cpp_precision_key}" "${precision}")
                        set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}")
MissPenguin's avatar
MissPenguin committed
401
                        set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}")
MissPenguin's avatar
MissPenguin committed
402
                        command="${_script} ${cpp_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 "
MissPenguin's avatar
MissPenguin committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
                        eval $command
                        last_status=${PIPESTATUS[0]}
                        eval "cat ${_save_log_path}"
                        status_check $last_status "${command}" "${status_log}"
                        
                    done
                done
            done
        else
            echo "Does not support hardware other than CPU and GPU Currently!"
        fi
    done
}

LDOUBLEV's avatar
LDOUBLEV committed
417
if [ ${MODE} = "infer" ]; then
LDOUBLEV's avatar
LDOUBLEV committed
418
419
420
421
422
423
    GPUID=$3
    if [ ${#GPUID} -le 0 ];then
        env=" "
    else
        env="export CUDA_VISIBLE_DEVICES=${GPUID}"
    fi
424
425
426
427
428
429
430
431
432
    # 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
LDOUBLEV's avatar
LDOUBLEV committed
433
            save_infer_dir=$(dirname $infer_model)
LDOUBLEV's avatar
LDOUBLEV committed
434
            set_export_weight=$(func_set_params "${export_weight}" "${infer_model}")
LDOUBLEV's avatar
LDOUBLEV committed
435
            set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_dir}")
436
437
438
            export_cmd="${python} ${infer_run_exports[Count]} ${set_export_weight} ${set_save_infer_key}"
            echo ${infer_run_exports[Count]} 
            echo  $export_cmd
439
440
            eval $export_cmd
            status_export=$?
441
            status_check $status_export "${export_cmd}" "${status_log}"
LDOUBLEV's avatar
fix  
LDOUBLEV committed
442
        else
LDOUBLEV's avatar
LDOUBLEV committed
443
            save_infer_dir=${infer_model}
444
445
446
        fi
        #run inference
        is_quant=${infer_quant_flag[Count]}
LDOUBLEV's avatar
LDOUBLEV committed
447
        func_inference "${python}" "${inference_py}" "${save_infer_dir}" "${LOG_PATH}" "${infer_img_dir}" ${is_quant}
448
449
        Count=$(($Count + 1))
    done
LDOUBLEV's avatar
LDOUBLEV committed
450

MissPenguin's avatar
MissPenguin committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
elif [ ${MODE} = "cpp_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_quant_flag=(${cpp_infer_is_quant})
    for infer_model in ${cpp_infer_model_dir_list[*]}; do
        #run inference
        is_quant=${infer_quant_flag[Count]}
        func_cpp_inference "${inference_cmd}" "${infer_model}" "${LOG_PATH}" "${cpp_infer_img_dir}" ${is_quant}
        Count=$(($Count + 1))
    done
469
    
tink2123's avatar
tink2123 committed
470
471
472
473
474
475
476
477
478
479
480
481
482
elif [ ${MODE} = "serving_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="|"
    #run serving
    func_serving "${web_service_cmd}"
MissPenguin's avatar
MissPenguin committed
483

LDOUBLEV's avatar
LDOUBLEV committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
else
    IFS="|"
    export Count=0
    USE_GPU_KEY=(${train_use_gpu_value})
    for gpu in ${gpu_list[*]}; do
        use_gpu=${USE_GPU_KEY[Count]}
        Count=$(($Count + 1))
        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=${array[0]}"
            IFS="|"
        else
            IFS=";"
            array=(${gpu})
            ips=${array[0]}
            gpu=${array[1]}
            IFS="|"
            env=" "
        fi
        for autocast in ${autocast_list[*]}; do 
            for trainer in ${trainer_list[*]}; do 
                flag_quant=False
                if [ ${trainer} = ${pact_key} ]; then
                    run_train=${pact_trainer}
                    run_export=${pact_export}
                    flag_quant=True
                elif [ ${trainer} = "${fpgm_key}" ]; then
                    run_train=${fpgm_trainer}
                    run_export=${fpgm_export}
                elif [ ${trainer} = "${distill_key}" ]; then
                    run_train=${distill_trainer}
                    run_export=${distill_export}
                elif [ ${trainer} = ${trainer_key1} ]; then
                    run_train=${trainer_value1}
                    run_export=${export_value1}
                elif [[ ${trainer} = ${trainer_key2} ]]; then
                    run_train=${trainer_value2}
                    run_export=${export_value2}
                else
                    run_train=${norm_trainer}
                    run_export=${norm_export}
                fi

                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}" "${use_gpu}")
                save_log="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}"
                
                # load pretrain from norm training if current trainer is pact or fpgm trainer
                if [ ${trainer} = ${pact_key} ] || [ ${trainer} = ${fpgm_key} ]; then
                    set_pretrain="${load_norm_train_model}"
                fi

                set_save_model=$(func_set_params "${save_model_key}" "${save_log}")
                if [ ${#gpu} -le 2 ];then  # train with cpu or single gpu
                    cmd="${python} ${run_train} ${set_use_gpu}  ${set_save_model} ${set_epoch} ${set_pretrain} ${set_autocast} ${set_batchsize} ${set_train_params1} "
                elif [ ${#gpu} -le 15 ];then  # train with multi-gpu
                    cmd="${python} -m paddle.distributed.launch --gpus=${gpu} ${run_train} ${set_save_model} ${set_epoch} ${set_pretrain} ${set_autocast} ${set_batchsize} ${set_train_params1}"
                else     # train with multi-machine
                    cmd="${python} -m paddle.distributed.launch --ips=${ips} --gpus=${gpu} ${run_train} ${set_save_model} ${set_pretrain} ${set_epoch} ${set_autocast} ${set_batchsize} ${set_train_params1}"
                fi
                # run train
                eval "unset CUDA_VISIBLE_DEVICES"
                eval $cmd
                status_check $? "${cmd}" "${status_log}"

                set_eval_pretrain=$(func_set_params "${pretrain_model_key}" "${save_log}/${train_model_name}")
                # save norm trained models to set pretrain for pact training and fpgm training 
                if [ ${trainer} = ${trainer_norm} ]; then
                    load_norm_train_model=${set_eval_pretrain}
                fi
                # run eval 
                if [ ${eval_py} != "null" ]; then
                    set_eval_params1=$(func_set_params "${eval_key1}" "${eval_value1}")
                    eval_cmd="${python} ${eval_py} ${set_eval_pretrain} ${set_use_gpu} ${set_eval_params1}" 
                    eval $eval_cmd
                    status_check $? "${eval_cmd}" "${status_log}"
                fi
                # run export model
                if [ ${run_export} != "null" ]; then 
                    # run export model
                    save_infer_path="${save_log}"
LDOUBLEV's avatar
LDOUBLEV committed
579
580
581
                    set_export_weight=$(func_set_params "${export_weight}" "${save_log}/${train_model_name}")
                    set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_path}")
                    export_cmd="${python} ${run_export} ${set_export_weight} ${set_save_infer_key}"
LDOUBLEV's avatar
LDOUBLEV committed
582
583
584
585
586
587
588
589
590
591
592
593
594
                    eval $export_cmd
                    status_check $? "${export_cmd}" "${status_log}"

                    #run inference
                    eval $env
                    save_infer_path="${save_log}"
                    func_inference "${python}" "${inference_py}" "${save_infer_path}" "${LOG_PATH}" "${train_infer_img_dir}" "${flag_quant}"
                    eval "unset CUDA_VISIBLE_DEVICES"
                fi
            done  # done with:    for trainer in ${trainer_list[*]}; do 
        done      # done with:    for autocast in ${autocast_list[*]}; do 
    done          # done with:    for gpu in ${gpu_list[*]}; do
fi  # end if [ ${MODE} = "infer" ]; then