test_train_inference_python.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #!/bin/bash
  2. source test_tipc/utils_func.sh
  3. FILENAME=$1
  4. # MODE be one of ['lite_train_lite_infer' 'lite_train_whole_infer'
  5. # 'whole_train_whole_infer', 'whole_infer', 'klquant_whole_infer']
  6. MODE=$2
  7. # parse params
  8. dataline=$(cat ${FILENAME})
  9. IFS=$'\n'
  10. lines=(${dataline})
  11. # The training params
  12. model_name=$(func_parser_value "${lines[1]}")
  13. echo "ppdet python_infer: ${model_name}"
  14. python=$(func_parser_value "${lines[2]}")
  15. gpu_list=$(func_parser_value "${lines[3]}")
  16. train_use_gpu_key=$(func_parser_key "${lines[4]}")
  17. train_use_gpu_value=$(func_parser_value "${lines[4]}")
  18. autocast_list=$(func_parser_value "${lines[5]}")
  19. autocast_key=$(func_parser_key "${lines[5]}")
  20. epoch_key=$(func_parser_key "${lines[6]}")
  21. epoch_num=$(func_parser_params "${lines[6]}")
  22. save_model_key=$(func_parser_key "${lines[7]}")
  23. train_batch_key=$(func_parser_key "${lines[8]}")
  24. train_batch_value=$(func_parser_params "${lines[8]}")
  25. pretrain_model_key=$(func_parser_key "${lines[9]}")
  26. pretrain_model_value=$(func_parser_value "${lines[9]}")
  27. train_model_name=$(func_parser_value "${lines[10]}")
  28. train_infer_img_dir=$(func_parser_value "${lines[11]}")
  29. train_param_key1=$(func_parser_key "${lines[12]}")
  30. train_param_value1=$(func_parser_value "${lines[12]}")
  31. trainer_list=$(func_parser_value "${lines[14]}")
  32. norm_key=$(func_parser_key "${lines[15]}")
  33. norm_trainer=$(func_parser_value "${lines[15]}")
  34. pact_key=$(func_parser_key "${lines[16]}")
  35. pact_trainer=$(func_parser_value "${lines[16]}")
  36. fpgm_key=$(func_parser_key "${lines[17]}")
  37. fpgm_trainer=$(func_parser_value "${lines[17]}")
  38. distill_key=$(func_parser_key "${lines[18]}")
  39. distill_trainer=$(func_parser_value "${lines[18]}")
  40. trainer_key1=$(func_parser_key "${lines[19]}")
  41. trainer_value1=$(func_parser_value "${lines[19]}")
  42. trainer_key2=$(func_parser_key "${lines[20]}")
  43. trainer_value2=$(func_parser_value "${lines[20]}")
  44. # eval params
  45. eval_py=$(func_parser_value "${lines[23]}")
  46. eval_key1=$(func_parser_key "${lines[24]}")
  47. eval_value1=$(func_parser_value "${lines[24]}")
  48. # export params
  49. save_export_key=$(func_parser_key "${lines[27]}")
  50. save_export_value=$(func_parser_value "${lines[27]}")
  51. export_weight_key=$(func_parser_key "${lines[28]}")
  52. export_weight_value=$(func_parser_value "${lines[28]}")
  53. norm_export=$(func_parser_value "${lines[29]}")
  54. pact_export=$(func_parser_value "${lines[30]}")
  55. fpgm_export=$(func_parser_value "${lines[31]}")
  56. distill_export=$(func_parser_value "${lines[32]}")
  57. export_key1=$(func_parser_key "${lines[33]}")
  58. export_value1=$(func_parser_value "${lines[33]}")
  59. export_onnx_key=$(func_parser_key "${lines[34]}")
  60. export_value2=$(func_parser_value "${lines[34]}")
  61. kl_quant_export=$(func_parser_value "${lines[35]}")
  62. # parser inference model
  63. infer_mode_list=$(func_parser_value "${lines[37]}")
  64. infer_is_quant_list=$(func_parser_value "${lines[38]}")
  65. # parser inference
  66. inference_py=$(func_parser_value "${lines[39]}")
  67. use_gpu_key=$(func_parser_key "${lines[40]}")
  68. use_gpu_list=$(func_parser_value "${lines[40]}")
  69. use_mkldnn_key=$(func_parser_key "${lines[41]}")
  70. use_mkldnn_list=$(func_parser_value "${lines[41]}")
  71. cpu_threads_key=$(func_parser_key "${lines[42]}")
  72. cpu_threads_list=$(func_parser_value "${lines[42]}")
  73. batch_size_key=$(func_parser_key "${lines[43]}")
  74. batch_size_list=$(func_parser_value "${lines[43]}")
  75. use_trt_key=$(func_parser_key "${lines[44]}")
  76. use_trt_list=$(func_parser_value "${lines[44]}")
  77. precision_key=$(func_parser_key "${lines[45]}")
  78. precision_list=$(func_parser_value "${lines[45]}")
  79. infer_model_key=$(func_parser_key "${lines[46]}")
  80. image_dir_key=$(func_parser_key "${lines[47]}")
  81. infer_img_dir=$(func_parser_value "${lines[47]}")
  82. save_log_key=$(func_parser_key "${lines[48]}")
  83. benchmark_key=$(func_parser_key "${lines[49]}")
  84. benchmark_value=$(func_parser_value "${lines[49]}")
  85. infer_key1=$(func_parser_key "${lines[50]}")
  86. infer_value1=$(func_parser_value "${lines[50]}")
  87. LOG_PATH="./test_tipc/output/${model_name}/${MODE}"
  88. mkdir -p ${LOG_PATH}
  89. status_log="${LOG_PATH}/results_python.log"
  90. line_num=`grep -n -w "to_static_train_benchmark_params" $FILENAME | cut -d ":" -f 1`
  91. to_static_key=$(func_parser_key "${lines[line_num]}")
  92. to_static_trainer=$(func_parser_value "${lines[line_num]}")
  93. function func_inference(){
  94. IFS='|'
  95. _python=$1
  96. _script=$2
  97. _model_dir=$3
  98. _log_path=$4
  99. _img_dir=$5
  100. _flag_quant=$6
  101. _gpu=$7
  102. # inference
  103. for use_gpu in ${use_gpu_list[*]}; do
  104. if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
  105. for use_mkldnn in ${use_mkldnn_list[*]}; do
  106. if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
  107. continue
  108. fi
  109. for threads in ${cpu_threads_list[*]}; do
  110. for batch_size in ${batch_size_list[*]}; do
  111. _save_log_path="${_log_path}/python_infer_cpu_gpus_${gpu}_usemkldnn_${use_mkldnn}_threads_${threads}_mode_paddle_batchsize_${batch_size}.log"
  112. set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
  113. set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  114. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  115. set_cpu_threads=$(func_set_params "${cpu_threads_key}" "${threads}")
  116. set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  117. set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  118. 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 "
  119. eval $command
  120. last_status=${PIPESTATUS[0]}
  121. eval "cat ${_save_log_path}"
  122. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  123. done
  124. done
  125. done
  126. elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
  127. for precision in ${precision_list[*]}; do
  128. if [[ ${precision} != "paddle" ]]; then
  129. if [[ ${_flag_quant} = "False" ]] && [[ ${precision} = "trt_int8" ]]; then
  130. continue
  131. fi
  132. if [[ ${_flag_quant} = "True" ]] && [[ ${precision} != "trt_int8" ]]; then
  133. continue
  134. fi
  135. fi
  136. for batch_size in ${batch_size_list[*]}; do
  137. _save_log_path="${_log_path}/python_infer_gpu_gpus_${gpu}_mode_${precision}_batchsize_${batch_size}.log"
  138. set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
  139. set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  140. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  141. set_precision=$(func_set_params "${precision_key}" "${precision}")
  142. set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  143. set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  144. command="${_python} ${_script} ${use_gpu_key}=${use_gpu} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 "
  145. eval $command
  146. last_status=${PIPESTATUS[0]}
  147. eval "cat ${_save_log_path}"
  148. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  149. done
  150. done
  151. else
  152. echo "Does not support hardware other than CPU and GPU Currently!"
  153. fi
  154. done
  155. }
  156. if [ ${MODE} = "whole_infer" ] || [ ${MODE} = "klquant_whole_infer" ]; then
  157. # set CUDA_VISIBLE_DEVICES
  158. GPUID=$3
  159. if [ ${#GPUID} -le 0 ];then
  160. env=" "
  161. else
  162. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  163. fi
  164. eval $env
  165. Count=0
  166. gpu=0
  167. IFS="|"
  168. infer_quant_flag=(${infer_is_quant_list})
  169. for infer_mode in ${infer_mode_list[*]}; do
  170. if [ ${infer_mode} = "null" ]; then
  171. continue
  172. fi
  173. if [ ${MODE} = "klquant_whole_infer" ] && [ ${infer_mode} != "kl_quant" ]; then
  174. continue
  175. fi
  176. if [ ${MODE} = "whole_infer" ] && [ ${infer_mode} = "kl_quant" ]; then
  177. continue
  178. fi
  179. # run export
  180. case ${infer_mode} in
  181. norm) run_export=${norm_export} ;;
  182. pact) run_export=${pact_export} ;;
  183. fpgm) run_export=${fpgm_export} ;;
  184. distill) run_export=${distill_export} ;;
  185. kl_quant) run_export=${kl_quant_export} ;;
  186. *) echo "Undefined infer_mode!"; exit 1;
  187. esac
  188. set_export_weight=$(func_set_params "${export_weight_key}" "${export_weight_value}")
  189. set_save_export_dir=$(func_set_params "${save_export_key}" "${save_export_value}")
  190. set_filename=$(func_set_params "filename" "${model_name}")
  191. export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_save_export_dir} "
  192. echo $export_cmd
  193. eval $export_cmd
  194. status_check $? "${export_cmd}" "${status_log}" "${model_name}"
  195. #run inference
  196. save_export_model_dir="${save_export_value}/${model_name}"
  197. is_quant=${infer_quant_flag[Count]}
  198. func_inference "${python}" "${inference_py}" "${save_export_model_dir}" "${LOG_PATH}" "${infer_img_dir}" ${is_quant} "{gpu}"
  199. Count=$((${Count} + 1))
  200. done
  201. else
  202. IFS="|"
  203. Count=0
  204. for gpu in ${gpu_list[*]}; do
  205. use_gpu=${train_use_gpu_value}
  206. Count=$((${Count} + 1))
  207. ips=""
  208. if [ ${gpu} = "-1" ];then
  209. env=""
  210. use_gpu=False
  211. elif [ ${#gpu} -le 1 ];then
  212. env="export CUDA_VISIBLE_DEVICES=${gpu}"
  213. eval ${env}
  214. elif [ ${#gpu} -le 15 ];then
  215. IFS=","
  216. array=(${gpu})
  217. env="export CUDA_VISIBLE_DEVICES=${array[0]}"
  218. IFS="|"
  219. else
  220. IFS=";"
  221. array=(${gpu})
  222. ips=${array[0]}
  223. gpu=${array[1]}
  224. IFS="|"
  225. env=" "
  226. fi
  227. for autocast in ${autocast_list[*]}; do
  228. for trainer in ${trainer_list[*]}; do
  229. flag_quant=False
  230. set_to_static=""
  231. if [ ${trainer} = "${norm_key}" ]; then
  232. run_train=${norm_trainer}
  233. run_export=${norm_export}
  234. elif [ ${trainer} = "${pact_key}" ]; then
  235. run_train=${pact_trainer}
  236. run_export=${pact_export}
  237. flag_quant=True
  238. elif [ ${trainer} = "${fpgm_key}" ]; then
  239. run_train=${fpgm_trainer}
  240. run_export=${fpgm_export}
  241. elif [ ${trainer} = "${distill_key}" ]; then
  242. run_train=${distill_trainer}
  243. run_export=${distill_export}
  244. elif [ ${trainer} = "${trainer_key1}" ]; then
  245. run_train=${trainer_value1}
  246. run_export=${export_value1}
  247. elif [ ${trainer} = "${trainer_key2}" ]; then
  248. run_train=${trainer_value2}
  249. run_export=${export_value2}
  250. elif [ ${trainer} = "${to_static_key}" ]; then
  251. run_train=${norm_trainer}
  252. run_export=${norm_export}
  253. set_to_static=${to_static_trainer}
  254. else
  255. continue
  256. fi
  257. if [ ${run_train} = "null" ]; then
  258. continue
  259. fi
  260. set_epoch=$(func_set_params "${epoch_key}" "${epoch_num}")
  261. set_pretrain=$(func_set_params "${pretrain_model_key}" "${pretrain_model_value}")
  262. set_batchsize=$(func_set_params "${train_batch_key}" "${train_batch_value}")
  263. set_filename=$(func_set_params "filename" "${model_name}")
  264. set_use_gpu=$(func_set_params "${train_use_gpu_key}" "${use_gpu}")
  265. set_train_params1=$(func_set_params "${train_param_key1}" "${train_param_value1}")
  266. save_log="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}"
  267. if [ ${autocast} = "amp" ] || [ ${autocast} = "fp16" ]; then
  268. set_autocast="--amp"
  269. set_amp_level="amp_level=O2"
  270. else
  271. set_autocast=" "
  272. set_amp_level=" "
  273. fi
  274. if [ ${MODE} = "benchmark_train" ]; then
  275. set_shuffle="TrainReader.shuffle=False"
  276. set_enable_ce="--enable_ce=True"
  277. else
  278. set_shuffle=" "
  279. set_enable_ce=" "
  280. fi
  281. set_save_model=$(func_set_params "${save_model_key}" "${save_log}")
  282. nodes="1"
  283. if [ ${#gpu} -le 2 ];then # train with cpu or single gpu
  284. cmd="${python} ${run_train} LearningRate.base_lr=0.0001 log_iter=1 ${set_use_gpu} ${set_save_model} ${set_epoch} ${set_pretrain} ${set_batchsize} ${set_filename} ${set_shuffle} ${set_amp_level} ${set_enable_ce} ${set_autocast} ${set_to_static} ${set_train_params1}"
  285. elif [ ${#ips} -le 15 ];then # train with multi-gpu
  286. cmd="${python} -m paddle.distributed.launch --gpus=${gpu} ${run_train} log_iter=1 ${set_use_gpu} ${set_save_model} ${set_epoch} ${set_pretrain} ${set_batchsize} ${set_filename} ${set_shuffle} ${set_amp_level} ${set_enable_ce} ${set_autocast} ${set_to_static} ${set_train_params1}"
  287. else # train with multi-machine
  288. IFS=","
  289. ips_array=(${ips})
  290. nodes=${#ips_array[@]}
  291. save_log="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}"
  292. IFS="|"
  293. set_save_model=$(func_set_params "${save_model_key}" "${save_log}")
  294. cmd="${python} -m paddle.distributed.launch --ips=${ips} --gpus=${gpu} ${run_train} log_iter=1 ${set_use_gpu} ${set_save_model} ${set_epoch} ${set_pretrain} ${set_batchsize} ${set_filename} ${set_shuffle} ${set_amp_level} ${set_enable_ce} ${set_autocast} ${set_to_static} ${set_train_params1}"
  295. fi
  296. # run train
  297. train_log_path="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}.log"
  298. eval "${cmd} > ${train_log_path} 2>&1"
  299. last_status=$?
  300. cat ${train_log_path}
  301. status_check $last_status "${cmd}" "${status_log}" "${model_name}" "${train_log_path}"
  302. set_eval_trained_weight=$(func_set_params "${export_weight_key}" "${save_log}/${model_name}/${train_model_name}")
  303. # run eval
  304. if [ ${eval_py} != "null" ]; then
  305. set_eval_params1=$(func_set_params "${eval_key1}" "${eval_value1}")
  306. eval_log_path="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_eval.log"
  307. eval_cmd="${python} ${eval_py} ${set_eval_trained_weight} ${set_use_gpu} ${set_eval_params1}"
  308. eval "${eval_cmd} > ${eval_log_path} 2>&1"
  309. last_status=$?
  310. cat ${eval_log_path}
  311. status_check $last_status "${eval_cmd}" "${status_log}" "${model_name}" "${eval_log_path}"
  312. fi
  313. # run export model
  314. if [ ${run_export} != "null" ]; then
  315. save_export_model_dir="${save_log}/${model_name}"
  316. set_export_weight=$(func_set_params "${export_weight_key}" "${save_log}/${model_name}/${train_model_name}")
  317. set_save_export_dir=$(func_set_params "${save_export_key}" "${save_log}")
  318. if [ ${export_onnx_key} = "export_onnx" ]; then
  319. # run export onnx model for rcnn
  320. export_log_path_onnx=${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_onnx_export.log
  321. export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} export_onnx=True ${set_save_export_dir} >${export_log_path_onnx} 2>&1"
  322. eval $export_cmd
  323. status_check $? "${export_cmd}" "${status_log}" "${model_name}" "${export_log_path_onnx}"
  324. # copy model for inference benchmark
  325. eval "cp ${save_export_model_dir}/* ${save_log}/"
  326. fi
  327. # run export model
  328. export_log_path="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_export.log"
  329. export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_save_export_dir} "
  330. eval "${export_cmd} > ${export_log_path} 2>&1"
  331. last_status=$?
  332. cat ${export_log_path}
  333. status_check $last_status "${export_cmd}" "${status_log}" "${model_name}" "${export_log_path}"
  334. #run inference
  335. if [ ${export_onnx_key} != "export_onnx" ]; then
  336. # copy model for inference benchmark
  337. eval "cp ${save_export_model_dir}/* ${save_log}/"
  338. fi
  339. eval $env
  340. func_inference "${python}" "${inference_py}" "${save_export_model_dir}" "${LOG_PATH}" "${train_infer_img_dir}" "${flag_quant}" "{gpu}"
  341. eval "unset CUDA_VISIBLE_DEVICES"
  342. fi
  343. done # done with: for trainer in ${trainer_list[*]}; do
  344. done # done with: for autocast in ${autocast_list[*]}; do
  345. done # done with: for gpu in ${gpu_list[*]}; do
  346. fi # end if [ ${MODE} = "infer" ]; then