test_train_inference_python.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #!/usr/bin/env bash
  2. source test_tipc/common_func.sh
  3. FILENAME=$1
  4. # $MODE be one of {'lite_train_lite_infer' 'lite_train_whole_infer' 'whole_train_whole_infer', 'whole_infer'}
  5. MODE=$2
  6. dataline=$(awk 'NR>=1{print}' $FILENAME)
  7. # Parse params
  8. IFS=$'\n'
  9. lines=(${dataline})
  10. # Training params
  11. task_name=$(parse_first_value "${lines[1]}")
  12. model_name=$(parse_second_value "${lines[1]}")
  13. python=$(func_parser_value "${lines[2]}")
  14. gpu_list=$(func_parser_value "${lines[3]}")
  15. train_use_gpu_key=$(func_parser_key "${lines[4]}")
  16. train_use_gpu_value=$(func_parser_value "${lines[4]}")
  17. autocast_list=$(func_parser_value "${lines[5]}")
  18. autocast_key=$(func_parser_key "${lines[5]}")
  19. epoch_key=$(func_parser_key "${lines[6]}")
  20. epoch_value=$(func_parser_params "${lines[6]}")
  21. save_model_key=$(func_parser_key "${lines[7]}")
  22. train_batch_key=$(func_parser_key "${lines[8]}")
  23. train_batch_value=$(func_parser_params "${lines[8]}")
  24. pretrain_model_key=$(func_parser_key "${lines[9]}")
  25. pretrain_model_value=$(func_parser_value "${lines[9]}")
  26. train_config_key=$(func_parser_key "${lines[10]}")
  27. train_config_value=$(func_parser_params "${lines[10]}")
  28. train_model_name=$(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. trainer_norm=$(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_py=$(func_parser_value "${lines[23]}")
  45. eval_key1=$(func_parser_key "${lines[24]}")
  46. eval_value1=$(func_parser_value "${lines[24]}")
  47. save_infer_key=$(func_parser_key "${lines[27]}")
  48. export_weight=$(func_parser_key "${lines[28]}")
  49. export_shape_key=$(func_parser_key "${lines[29]}")
  50. export_shape_value=$(func_parser_value "${lines[29]}")
  51. norm_export=$(func_parser_value "${lines[30]}")
  52. pact_export=$(func_parser_value "${lines[31]}")
  53. fpgm_export=$(func_parser_value "${lines[32]}")
  54. distill_export=$(func_parser_value "${lines[33]}")
  55. export_key1=$(func_parser_key "${lines[34]}")
  56. export_value1=$(func_parser_value "${lines[34]}")
  57. export_key2=$(func_parser_key "${lines[35]}")
  58. export_value2=$(func_parser_value "${lines[35]}")
  59. inference_dir=$(func_parser_value "${lines[36]}")
  60. # Params of inference model
  61. infer_model_dir_list=$(func_parser_value "${lines[37]}")
  62. infer_export_list=$(func_parser_value "${lines[38]}")
  63. infer_is_quant=$(func_parser_value "${lines[39]}")
  64. # Inference params
  65. inference_py=$(func_parser_value "${lines[40]}")
  66. use_gpu_key=$(func_parser_key "${lines[41]}")
  67. use_gpu_list=$(func_parser_value "${lines[41]}")
  68. use_mkldnn_key=$(func_parser_key "${lines[42]}")
  69. use_mkldnn_list=$(func_parser_value "${lines[42]}")
  70. cpu_threads_key=$(func_parser_key "${lines[43]}")
  71. cpu_threads_list=$(func_parser_value "${lines[43]}")
  72. batch_size_key=$(func_parser_key "${lines[44]}")
  73. batch_size_list=$(func_parser_value "${lines[44]}")
  74. use_trt_key=$(func_parser_key "${lines[45]}")
  75. use_trt_list=$(func_parser_value "${lines[45]}")
  76. precision_key=$(func_parser_key "${lines[46]}")
  77. precision_list=$(func_parser_value "${lines[46]}")
  78. infer_model_key=$(func_parser_key "${lines[47]}")
  79. infer_config_key=$(func_parser_key "${lines[48]}")
  80. infer_config_value=$(func_parser_value "${lines[48]}")
  81. save_log_key=$(func_parser_key "${lines[49]}")
  82. benchmark_key=$(func_parser_key "${lines[50]}")
  83. benchmark_value=$(func_parser_value "${lines[50]}")
  84. infer_key1=$(func_parser_key "${lines[51]}")
  85. infer_value1=$(func_parser_value "${lines[51]}")
  86. infer_key2=$(func_parser_key "${lines[52]}")
  87. infer_value2=$(func_parser_value "${lines[52]}")
  88. OUT_PATH="./test_tipc/output/${task_name}/${model_name}/${MODE}"
  89. mkdir -p ${OUT_PATH}
  90. status_log="${OUT_PATH}/results_python.log"
  91. echo "------------------------ ${MODE} ------------------------" >> "${status_log}"
  92. # Parse extra args
  93. parse_extra_args "${lines[@]}"
  94. for params in ${extra_args[*]}; do
  95. IFS=':'
  96. arr=(${params})
  97. key=${arr[0]}
  98. value=${arr[1]}
  99. :
  100. done
  101. function func_inference() {
  102. local IFS='|'
  103. local _python=$1
  104. local _script="$2"
  105. local _model_dir="$3"
  106. local _log_path="$4"
  107. local _config="$5"
  108. local set_infer_config=$(func_set_params "${infer_config_key}" "${_config}")
  109. local set_model_dir=$(func_set_params "${infer_model_key}" "${_model_dir}")
  110. local set_benchmark=$(func_set_params "${benchmark_key}" "${benchmark_value}")
  111. local set_infer_params1=$(func_set_params "${infer_key1}" "${infer_value1}")
  112. local set_infer_params2=$(func_set_params "${infer_key2}" "${infer_value2}")
  113. # Do inference
  114. for use_gpu in ${use_gpu_list[*]}; do
  115. local set_device=$(func_set_params "${use_gpu_key}" "${use_gpu}")
  116. if [ ${use_gpu} = 'False' ] || [ ${use_gpu} = 'cpu' ]; then
  117. for use_mkldnn in ${use_mkldnn_list[*]}; do
  118. if [ ${use_mkldnn} = 'False' ]; then
  119. continue
  120. fi
  121. for precision in ${precision_list[*]}; do
  122. if [ ${use_mkldnn} = 'False' ] && [ ${precision} = 'fp16' ]; then
  123. continue
  124. fi # Skip when enable fp16 but disable mkldnn
  125. for threads in ${cpu_threads_list[*]}; do
  126. for batch_size in ${batch_size_list[*]}; do
  127. local _save_log_path="${_log_path}/python_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log"
  128. local infer_value1="${_log_path}/python_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}_results"
  129. local set_mkldnn=$(func_set_params "${use_mkldnn_key}" "${use_mkldnn}")
  130. local set_precision=$(func_set_params "${precision_key}" "${precision}")
  131. local set_cpu_threads=$(func_set_params "${cpu_threads_key}" "${threads}")
  132. local set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  133. local cmd="${_python} ${_script} ${set_config} ${set_device} ${set_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_benchmark} ${set_precision} ${set_infer_params1} ${set_infer_params2}"
  134. echo ${cmd}
  135. run_command "${cmd}" "${_save_log_path}"
  136. last_status=${PIPESTATUS[0]}
  137. status_check ${last_status} "${cmd}" "${status_log}" "${model_name}"
  138. done
  139. done
  140. done
  141. done
  142. elif [ ${use_gpu} = 'True' ] || [ ${use_gpu} = 'gpu' ]; then
  143. for use_trt in ${use_trt_list[*]}; do
  144. for precision in ${precision_list[*]}; do
  145. if [ ${precision} = 'fp16' ] && [ ${use_trt} = 'False' ]; then
  146. continue
  147. fi # Skip when enable fp16 but disable trt
  148. for batch_size in ${batch_size_list[*]}; do
  149. local _save_log_path="${_log_path}/python_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log"
  150. local infer_value1="${_log_path}/python_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}_results"
  151. local set_tensorrt=$(func_set_params "${use_trt_key}" "${use_trt}")
  152. local set_precision=$(func_set_params "${precision_key}" "${precision}")
  153. local set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  154. local cmd="${_python} ${_script} ${set_config} ${set_device} ${set_tensorrt} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_benchmark} ${set_infer_params2}"
  155. echo ${cmd}
  156. run_command "${cmd}" "${_save_log_path}"
  157. last_status=${PIPESTATUS[0]}
  158. status_check $last_status "${cmd}" "${status_log}" "${model_name}"
  159. done
  160. done
  161. done
  162. else
  163. echo "Currently, hardwares other than CPU and GPU are not supported!"
  164. fi
  165. done
  166. }
  167. if [ ${MODE} = 'whole_infer' ]; then
  168. GPUID=$3
  169. if [ ${#GPUID} -le 0 ]; then
  170. env=""
  171. else
  172. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  173. fi
  174. if [ ${infer_model_dir_list} == 'null' ]; then
  175. echo -e "\033[33m No inference model is specified! \033[0m"
  176. exit 1
  177. fi
  178. # Set CUDA_VISIBLE_DEVICES
  179. eval ${env}
  180. export count=0
  181. IFS='|'
  182. infer_run_exports=(${infer_export_list})
  183. for infer_model in ${infer_model_dir_list[*]}; do
  184. # Run export
  185. if [ ${infer_run_exports[count]} != 'null' ]; then
  186. save_infer_dir="${infer_model}/static"
  187. set_export_weight=$(func_set_params "${export_weight}" "${infer_model}")
  188. set_export_shape=$(func_set_params "${export_shape_key}" "${export_shape_value}")
  189. set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_dir}")
  190. export_cmd="${python} ${infer_run_exports[count]} ${set_export_weight} ${set_save_infer_key} ${set_export_shape}"
  191. echo ${infer_run_exports[count]}
  192. eval ${export_cmd}
  193. status_export=$?
  194. status_check ${status_export} "${export_cmd}" "${status_log}" "${model_name}"
  195. else
  196. save_infer_dir=${infer_model}
  197. fi
  198. # Run inference
  199. func_inference "${python}" "${inference_py}" "${save_infer_dir}" "${OUT_PATH}" "${infer_config_value}"
  200. count=$((${count} + 1))
  201. done
  202. else
  203. IFS='|'
  204. export count=0
  205. USE_GPU_KEY=(${train_use_gpu_value})
  206. for gpu in ${gpu_list[*]}; do
  207. train_use_gpu=${USE_GPU_KEY[count]}
  208. count=$((${count} + 1))
  209. ips=""
  210. if [ ${gpu} = '-1' ]; then
  211. env=""
  212. elif [ ${#gpu} -le 1 ]; then
  213. env="export CUDA_VISIBLE_DEVICES=${gpu}"
  214. eval ${env}
  215. elif [ ${#gpu} -le 15 ]; then
  216. IFS=','
  217. array=(${gpu})
  218. env="export CUDA_VISIBLE_DEVICES=${array[0]}"
  219. IFS='|'
  220. else
  221. IFS=';'
  222. array=(${gpu})
  223. ips=${array[0]}
  224. gpu=${array[1]}
  225. IFS='|'
  226. env=""
  227. fi
  228. for autocast in ${autocast_list[*]}; do
  229. if [ ${autocast} = 'amp' ]; then
  230. set_amp_config="Global.use_amp=True Global.scale_loss=1024.0 Global.use_dynamic_loss_scaling=True"
  231. else
  232. set_amp_config=""
  233. fi
  234. for trainer in ${trainer_list[*]}; do
  235. if [ ${trainer} = ${pact_key} ]; then
  236. run_train=${pact_trainer}
  237. run_export=${pact_export}
  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. else
  251. run_train=${norm_trainer}
  252. run_export=${norm_export}
  253. fi
  254. if [ ${run_train} = 'null' ]; then
  255. continue
  256. fi
  257. set_config=$(func_set_params "${train_config_key}" "${train_config_value}")
  258. set_autocast=$(func_set_params "${autocast_key}" "${autocast}")
  259. set_epoch=$(func_set_params "${epoch_key}" "${epoch_value}")
  260. set_pretrain=$(func_set_params "${pretrain_model_key}" "${pretrain_model_value}")
  261. set_batchsize=$(func_set_params "${train_batch_key}" "${train_batch_value}")
  262. set_train_params1=$(func_set_params "${train_param_key1}" "${train_param_value1}")
  263. set_use_gpu=$(func_set_params "${train_use_gpu_key}" "${train_use_gpu}")
  264. # If length of ips >= 15, then it is seen as multi-machine.
  265. # 15 is the min length of ips info for multi-machine: 0.0.0.0,0.0.0.0
  266. if [ ${#ips} -le 15 ]; then
  267. save_dir="${OUT_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}"
  268. nodes=1
  269. else
  270. IFS=','
  271. ips_array=(${ips})
  272. IFS='|'
  273. nodes=${#ips_array[@]}
  274. save_dir="${OUT_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}"
  275. fi
  276. log_path="${OUT_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}.log"
  277. # Load pretrained model from norm training if current trainer is pact or fpgm trainer.
  278. if ([ ${trainer} = ${pact_key} ] || [ ${trainer} = ${fpgm_key} ]) && [ ${nodes} -le 1 ]; then
  279. set_pretrain="${load_norm_train_model}"
  280. fi
  281. set_save_model=$(func_set_params "${save_model_key}" "${save_dir}")
  282. if [ ${#gpu} -le 2 ]; then # Train with cpu or single gpu
  283. cmd="${python} ${run_train} ${set_config} ${set_use_gpu} ${set_save_model} ${set_epoch} ${set_pretrain} ${set_autocast} ${set_batchsize} ${set_train_params1} ${set_amp_config}"
  284. elif [ ${#ips} -le 15 ]; then # Train with multi-gpu
  285. cmd="${python} -m paddle.distributed.launch --gpus=${gpu} ${run_train} ${set_config} ${set_use_gpu} ${set_save_model} ${set_epoch} ${set_pretrain} ${set_autocast} ${set_batchsize} ${set_train_params1} ${set_amp_config}"
  286. else # Train with multi-machine
  287. cmd="${python} -m paddle.distributed.launch --ips=${ips} --gpus=${gpu} ${run_train} ${set_config} ${set_use_gpu} ${set_save_model} ${set_pretrain} ${set_epoch} ${set_autocast} ${set_batchsize} ${set_train_params1} ${set_amp_config}"
  288. fi
  289. echo ${cmd}
  290. # Run train
  291. run_command "${cmd}" "${log_path}"
  292. status_check $? "${cmd}" "${status_log}" "${model_name}"
  293. if [[ "${cmd}" == *'paddle.distributed.launch'* ]]; then
  294. cat log/workerlog.0 >> ${log_path}
  295. fi
  296. set_eval_pretrain=$(func_set_params "${pretrain_model_key}" "${save_dir}/${train_model_name}/model.pdparams")
  297. # Save norm trained models to set pretrain for pact training and fpgm training
  298. if [ ${trainer} = ${trainer_norm} ] && [ ${nodes} -le 1 ]; then
  299. load_norm_train_model=${set_eval_pretrain}
  300. fi
  301. # Run evaluation
  302. if [ ${eval_py} != 'null' ]; then
  303. log_path="${OUT_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_eval.log"
  304. set_eval_params1=$(func_set_params "${eval_key1}" "${eval_value1}")
  305. eval_cmd="${python} ${eval_py} ${set_eval_pretrain} ${set_use_gpu} ${set_eval_params1}"
  306. run_command "${eval_cmd}" "${log_path}"
  307. status_check $? "${eval_cmd}" "${status_log}" "${model_name}"
  308. fi
  309. # Run export model
  310. if [ ${run_export} != 'null' ]; then
  311. log_path="${OUT_PATH}/${trainer}_gpus_${gpu}_autocast_${autocast}_nodes_${nodes}_export.log"
  312. save_infer_path="${save_dir}/static"
  313. set_export_weight=$(func_set_params "${export_weight}" "${save_dir}/${train_model_name}")
  314. set_export_shape=$(func_set_params "${export_shape_key}" "${export_shape_value}")
  315. set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_path}")
  316. export_cmd="${python} ${run_export} ${set_export_weight} ${set_save_infer_key} ${set_export_shape}"
  317. run_command "${export_cmd}" "${log_path}"
  318. status_check $? "${export_cmd}" "${status_log}" "${model_name}"
  319. # Run inference
  320. eval ${env}
  321. if [[ ${inference_dir} != 'null' ]] && [[ ${inference_dir} != '##' ]]; then
  322. infer_model_dir="${save_infer_path}/${inference_dir}"
  323. else
  324. infer_model_dir=${save_infer_path}
  325. fi
  326. func_inference "${python}" "${inference_py}" "${infer_model_dir}" "${OUT_PATH}" "${train_config_value}"
  327. eval "unset CUDA_VISIBLE_DEVICES"
  328. fi
  329. done # Done with: for trainer in ${trainer_list[*]}; do
  330. done # Done with: for autocast in ${autocast_list[*]}; do
  331. done # Done with: for gpu in ${gpu_list[*]}; do
  332. fi # End if [ ${MODE} = 'infer' ]; then