test_train_inference_python.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_key2=$(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"
  88. mkdir -p ${LOG_PATH}
  89. status_log="${LOG_PATH}/results_python.log"
  90. function func_inference(){
  91. IFS='|'
  92. _python=$1
  93. _script=$2
  94. _model_dir=$3
  95. _log_path=$4
  96. _img_dir=$5
  97. _flag_quant=$6
  98. # inference
  99. for use_gpu in ${use_gpu_list[*]}; do
  100. if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then
  101. for use_mkldnn in ${use_mkldnn_list[*]}; do
  102. if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then
  103. continue
  104. fi
  105. for threads in ${cpu_threads_list[*]}; do
  106. for batch_size in ${batch_size_list[*]}; do
  107. _save_log_path="${_log_path}/python_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_fluid_batchsize_${batch_size}.log"
  108. set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
  109. set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  110. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  111. set_cpu_threads=$(func_set_params "${cpu_threads_key}" "${threads}")
  112. set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  113. set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  114. 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 "
  115. eval $command
  116. last_status=${PIPESTATUS[0]}
  117. eval "cat ${_save_log_path}"
  118. status_check $last_status "${command}" "${status_log}"
  119. done
  120. done
  121. done
  122. elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
  123. for precision in ${precision_list[*]}; do
  124. if [[ ${precision} != "fluid" ]]; then
  125. if [[ ${_flag_quant} = "False" ]] && [[ ${precision} = "trt_int8" ]]; then
  126. continue
  127. fi
  128. if [[ ${_flag_quant} = "True" ]] && [[ ${precision} != "trt_int8" ]]; then
  129. continue
  130. fi
  131. fi
  132. for batch_size in ${batch_size_list[*]}; do
  133. _save_log_path="${_log_path}/python_infer_gpu_precision_${precision}_batchsize_${batch_size}.log"
  134. set_infer_data=$(func_set_params "${image_dir_key}" "${_img_dir}")
  135. set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  136. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  137. set_precision=$(func_set_params "${precision_key}" "${precision}")
  138. set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  139. set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  140. 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 "
  141. eval $command
  142. last_status=${PIPESTATUS[0]}
  143. eval "cat ${_save_log_path}"
  144. status_check $last_status "${command}" "${status_log}"
  145. done
  146. done
  147. else
  148. echo "Does not support hardware other than CPU and GPU Currently!"
  149. fi
  150. done
  151. }
  152. if [ ${MODE} = "whole_infer" ] || [ ${MODE} = "klquant_whole_infer" ]; then
  153. # set CUDA_VISIBLE_DEVICES
  154. GPUID=$3
  155. if [ ${#GPUID} -le 0 ];then
  156. env=" "
  157. else
  158. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  159. fi
  160. eval $env
  161. Count=0
  162. IFS="|"
  163. infer_quant_flag=(${infer_is_quant_list})
  164. for infer_mode in ${infer_mode_list[*]}; do
  165. if [ ${infer_mode} = "null" ]; then
  166. continue
  167. fi
  168. if [ ${MODE} = "klquant_whole_infer" ] && [ ${infer_mode} != "kl_quant" ]; then
  169. continue
  170. fi
  171. if [ ${MODE} = "whole_infer" ] && [ ${infer_mode} = "kl_quant" ]; then
  172. continue
  173. fi
  174. # run export
  175. case ${infer_mode} in
  176. norm) run_export=${norm_export} ;;
  177. pact) run_export=${pact_export} ;;
  178. fpgm) run_export=${fpgm_export} ;;
  179. distill) run_export=${distill_export} ;;
  180. kl_quant) run_export=${kl_quant_export} ;;
  181. *) echo "Undefined infer_mode!"; exit 1;
  182. esac
  183. set_export_weight=$(func_set_params "${export_weight_key}" "${export_weight_value}")
  184. set_save_export_dir=$(func_set_params "${save_export_key}" "${save_export_value}")
  185. set_filename=$(func_set_params "filename" "${model_name}")
  186. export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_save_export_dir} "
  187. echo $export_cmd
  188. eval $export_cmd
  189. status_check $? "${export_cmd}" "${status_log}"
  190. #run inference
  191. save_export_model_dir="${save_export_value}/${model_name}"
  192. is_quant=${infer_quant_flag[Count]}
  193. func_inference "${python}" "${inference_py}" "${save_export_model_dir}" "${LOG_PATH}" "${infer_img_dir}" ${is_quant}
  194. Count=$((${Count} + 1))
  195. done
  196. else
  197. IFS="|"
  198. Count=0
  199. for gpu in ${gpu_list[*]}; do
  200. use_gpu=${train_use_gpu_value}
  201. Count=$((${Count} + 1))
  202. ips=""
  203. if [ ${gpu} = "-1" ];then
  204. env=""
  205. use_gpu=False
  206. elif [ ${#gpu} -le 1 ];then
  207. env="export CUDA_VISIBLE_DEVICES=${gpu}"
  208. eval ${env}
  209. elif [ ${#gpu} -le 15 ];then
  210. IFS=","
  211. array=(${gpu})
  212. env="export CUDA_VISIBLE_DEVICES=${array[0]}"
  213. IFS="|"
  214. else
  215. IFS=";"
  216. array=(${gpu})
  217. ips=${array[0]}
  218. gpu=${array[1]}
  219. IFS="|"
  220. env=" "
  221. fi
  222. for autocast in ${autocast_list[*]}; do
  223. for trainer in ${trainer_list[*]}; do
  224. flag_quant=False
  225. if [ ${trainer} = "${norm_key}" ]; then
  226. run_train=${norm_trainer}
  227. run_export=${norm_export}
  228. elif [ ${trainer} = "${pact_key}" ]; then
  229. run_train=${pact_trainer}
  230. run_export=${pact_export}
  231. flag_quant=True
  232. elif [ ${trainer} = "${fpgm_key}" ]; then
  233. run_train=${fpgm_trainer}
  234. run_export=${fpgm_export}
  235. elif [ ${trainer} = "${distill_key}" ]; then
  236. run_train=${distill_trainer}
  237. run_export=${distill_export}
  238. elif [ ${trainer} = "${trainer_key1}" ]; then
  239. run_train=${trainer_value1}
  240. run_export=${export_value1}
  241. elif [ ${trainer} = "${trainer_key2}" ]; then
  242. run_train=${trainer_value2}
  243. run_export=${export_value2}
  244. else
  245. continue
  246. fi
  247. if [ ${run_train} = "null" ]; then
  248. continue
  249. fi
  250. if [ ${autocast} = "amp" ]; then
  251. set_autocast="--amp"
  252. else
  253. set_autocast=" "
  254. fi
  255. set_epoch=$(func_set_params "${epoch_key}" "${epoch_num}")
  256. set_pretrain=$(func_set_params "${pretrain_model_key}" "${pretrain_model_value}")
  257. set_batchsize=$(func_set_params "${train_batch_key}" "${train_batch_value}")
  258. set_filename=$(func_set_params "filename" "${model_name}")
  259. set_use_gpu=$(func_set_params "${train_use_gpu_key}" "${use_gpu}")
  260. set_train_params1=$(func_set_params "${train_param_key1}" "${train_param_value1}")
  261. save_log="${LOG_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}"
  262. set_save_model=$(func_set_params "${save_model_key}" "${save_log}")
  263. if [ ${#gpu} -le 2 ];then # train with cpu or single gpu
  264. 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_train_params1} ${set_autocast}"
  265. elif [ ${#ips} -le 26 ];then # train with multi-gpu
  266. 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_train_params1} ${set_autocast}"
  267. else # train with multi-machine
  268. cmd="${python} -m paddle.distributed.launch --ips=${ips} --gpus=${gpu} ${set_use_gpu} ${run_train} log_iter=1 ${set_save_model} ${set_epoch} ${set_pretrain} ${set_batchsize} ${set_filename} ${set_train_params1} ${set_autocast}"
  269. fi
  270. # run train
  271. eval $cmd
  272. status_check $? "${cmd}" "${status_log}"
  273. set_eval_trained_weight=$(func_set_params "${export_weight_key}" "${save_log}/${model_name}/${train_model_name}")
  274. # run eval
  275. if [ ${eval_py} != "null" ]; then
  276. set_eval_params1=$(func_set_params "${eval_key1}" "${eval_value1}")
  277. eval_cmd="${python} ${eval_py} ${set_eval_trained_weight} ${set_use_gpu} ${set_eval_params1}"
  278. eval $eval_cmd
  279. status_check $? "${eval_cmd}" "${status_log}"
  280. fi
  281. # run export model
  282. if [ ${run_export} != "null" ]; then
  283. # run export model
  284. set_export_weight=$(func_set_params "${export_weight_key}" "${save_log}/${model_name}/${train_model_name}")
  285. set_save_export_dir=$(func_set_params "${save_export_key}" "${save_log}")
  286. export_cmd="${python} ${run_export} ${set_export_weight} ${set_filename} ${set_save_export_dir} "
  287. eval $export_cmd
  288. status_check $? "${export_cmd}" "${status_log}"
  289. #run inference
  290. save_export_model_dir="${save_export_value}/${model_name}"
  291. eval $env
  292. func_inference "${python}" "${inference_py}" "${save_export_model_dir}" "${LOG_PATH}" "${train_infer_img_dir}" "${flag_quant}"
  293. eval "unset CUDA_VISIBLE_DEVICES"
  294. fi
  295. done # done with: for trainer in ${trainer_list[*]}; do
  296. done # done with: for autocast in ${autocast_list[*]}; do
  297. done # done with: for gpu in ${gpu_list[*]}; do
  298. fi # end if [ ${MODE} = "infer" ]; then