test_serving_infer_python.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. source test_tipc/common_func.sh
  3. function func_parser_model_config(){
  4. strs=$1
  5. IFS="/"
  6. array=(${strs})
  7. tmp=${array[-1]}
  8. echo ${tmp}
  9. }
  10. FILENAME=$1
  11. dataline=$(awk 'NR==1, NR==23{print}' $FILENAME)
  12. MODE=$2
  13. # parser params
  14. IFS=$'\n'
  15. lines=(${dataline})
  16. # parser serving
  17. model_name=$(func_parser_value "${lines[1]}")
  18. python_list=$(func_parser_value "${lines[2]}")
  19. trans_model_py=$(func_parser_value "${lines[3]}")
  20. det_infer_model_dir_key=$(func_parser_key "${lines[4]}")
  21. det_infer_model_dir_value=$(func_parser_value "${lines[4]}")
  22. model_filename_key=$(func_parser_key "${lines[5]}")
  23. model_filename_value=$(func_parser_value "${lines[5]}")
  24. params_filename_key=$(func_parser_key "${lines[6]}")
  25. params_filename_value=$(func_parser_value "${lines[6]}")
  26. det_serving_server_key=$(func_parser_key "${lines[7]}")
  27. det_serving_server_value=$(func_parser_value "${lines[7]}")
  28. det_serving_client_key=$(func_parser_key "${lines[8]}")
  29. det_serving_client_value=$(func_parser_value "${lines[8]}")
  30. rec_infer_model_dir_key=$(func_parser_key "${lines[9]}")
  31. rec_infer_model_dir_value=$(func_parser_value "${lines[9]}")
  32. rec_serving_server_key=$(func_parser_key "${lines[10]}")
  33. rec_serving_server_value=$(func_parser_value "${lines[10]}")
  34. rec_serving_client_key=$(func_parser_key "${lines[11]}")
  35. rec_serving_client_value=$(func_parser_value "${lines[11]}")
  36. serving_dir_value=$(func_parser_value "${lines[12]}")
  37. web_service_py=$(func_parser_value "${lines[13]}")
  38. web_use_gpu_key=$(func_parser_key "${lines[14]}")
  39. web_use_gpu_list=$(func_parser_value "${lines[14]}")
  40. web_use_mkldnn_key=$(func_parser_key "${lines[15]}")
  41. web_use_mkldnn_list=$(func_parser_value "${lines[15]}")
  42. web_cpu_threads_key=$(func_parser_key "${lines[16]}")
  43. web_cpu_threads_list=$(func_parser_value "${lines[16]}")
  44. web_use_trt_key=$(func_parser_key "${lines[17]}")
  45. web_use_trt_list=$(func_parser_value "${lines[17]}")
  46. web_precision_key=$(func_parser_key "${lines[18]}")
  47. web_precision_list=$(func_parser_value "${lines[18]}")
  48. det_server_key=$(func_parser_key "${lines[19]}")
  49. det_server_value=$(func_parser_model_config "${lines[7]}")
  50. det_client_value=$(func_parser_model_config "${lines[8]}")
  51. rec_server_key=$(func_parser_key "${lines[20]}")
  52. rec_server_value=$(func_parser_model_config "${lines[10]}")
  53. rec_client_value=$(func_parser_model_config "${lines[11]}")
  54. pipeline_py=$(func_parser_value "${lines[21]}")
  55. image_dir_key=$(func_parser_key "${lines[22]}")
  56. image_dir_value=$(func_parser_value "${lines[22]}")
  57. LOG_PATH="$(pwd)/test_tipc/output/${model_name}/${MODE}/python"
  58. mkdir -p ${LOG_PATH}
  59. status_log="${LOG_PATH}/results_python_serving.log"
  60. function func_serving(){
  61. IFS='|'
  62. _python=$1
  63. _script=$2
  64. _model_dir=$3
  65. # pdserving
  66. set_model_filename=$(func_set_params "${model_filename_key}" "${model_filename_value}")
  67. set_params_filename=$(func_set_params "${params_filename_key}" "${params_filename_value}")
  68. if [ ${model_name} = "ch_PP-OCRv2" ] || [ ${model_name} = "ch_PP-OCRv3" ] || [ ${model_name} = "ch_ppocr_mobile_v2_0" ] || [ ${model_name} = "ch_ppocr_server_v2_0" ]; then
  69. # trans det
  70. set_dirname=$(func_set_params "--dirname" "${det_infer_model_dir_value}")
  71. set_serving_server=$(func_set_params "--serving_server" "${det_serving_server_value}")
  72. set_serving_client=$(func_set_params "--serving_client" "${det_serving_client_value}")
  73. python_list=(${python_list})
  74. trans_det_log="${LOG_PATH}/python_trans_model_det.log"
  75. trans_model_cmd="${python_list[0]} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client} > ${trans_det_log} 2>&1 "
  76. eval $trans_model_cmd
  77. # trans rec
  78. set_dirname=$(func_set_params "--dirname" "${rec_infer_model_dir_value}")
  79. set_serving_server=$(func_set_params "--serving_server" "${rec_serving_server_value}")
  80. set_serving_client=$(func_set_params "--serving_client" "${rec_serving_client_value}")
  81. python_list=(${python_list})
  82. trans_rec_log="${LOG_PATH}/python_trans_model_rec.log"
  83. trans_model_cmd="${python_list[0]} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client} > ${trans_rec_log} 2>&1 "
  84. eval $trans_model_cmd
  85. elif [[ ${model_name} =~ "det" ]]; then
  86. # trans det
  87. set_dirname=$(func_set_params "--dirname" "${det_infer_model_dir_value}")
  88. set_serving_server=$(func_set_params "--serving_server" "${det_serving_server_value}")
  89. set_serving_client=$(func_set_params "--serving_client" "${det_serving_client_value}")
  90. python_list=(${python_list})
  91. trans_det_log="${LOG_PATH}/python_trans_model_det.log"
  92. trans_model_cmd="${python_list[0]} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client} > ${trans_det_log} 2>&1 "
  93. eval $trans_model_cmd
  94. elif [[ ${model_name} =~ "rec" ]]; then
  95. # trans rec
  96. set_dirname=$(func_set_params "--dirname" "${rec_infer_model_dir_value}")
  97. set_serving_server=$(func_set_params "--serving_server" "${rec_serving_server_value}")
  98. set_serving_client=$(func_set_params "--serving_client" "${rec_serving_client_value}")
  99. python_list=(${python_list})
  100. trans_rec_log="${LOG_PATH}/python_trans_model_rec.log"
  101. trans_model_cmd="${python_list[0]} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client} > ${trans_rec_log} 2>&1 "
  102. eval $trans_model_cmd
  103. fi
  104. set_image_dir=$(func_set_params "${image_dir_key}" "${image_dir_value}")
  105. python_list=(${python_list})
  106. cd ${serving_dir_value}
  107. python=${python_list[0]}
  108. # python serving
  109. for use_gpu in ${web_use_gpu_list[*]}; do
  110. if [ ${use_gpu} = "null" ]; then
  111. for use_mkldnn in ${web_use_mkldnn_list[*]}; do
  112. for threads in ${web_cpu_threads_list[*]}; do
  113. set_cpu_threads=$(func_set_params "${web_cpu_threads_key}" "${threads}")
  114. server_log_path="${LOG_PATH}/python_server_cpu_usemkldnn_${use_mkldnn}_threads_${threads}.log"
  115. if [ ${model_name} = "ch_PP-OCRv2" ] || [ ${model_name} = "ch_PP-OCRv3" ] || [ ${model_name} = "ch_ppocr_mobile_v2_0" ] || [ ${model_name} = "ch_ppocr_server_v2_0" ]; then
  116. set_det_model_config=$(func_set_params "${det_server_key}" "${det_server_value}")
  117. set_rec_model_config=$(func_set_params "${rec_server_key}" "${rec_server_value}")
  118. web_service_cmd="nohup ${python} ${web_service_py} ${web_use_gpu_key}="" ${web_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_det_model_config} ${set_rec_model_config} > ${server_log_path} 2>&1 &"
  119. eval $web_service_cmd
  120. last_status=${PIPESTATUS[0]}
  121. status_check $last_status "${web_service_cmd}" "${status_log}" "${model_name}" "${server_log_path}"
  122. elif [[ ${model_name} =~ "det" ]]; then
  123. set_det_model_config=$(func_set_params "${det_server_key}" "${det_server_value}")
  124. web_service_cmd="nohup ${python} ${web_service_py} ${web_use_gpu_key}="" ${web_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_det_model_config} > ${server_log_path} 2>&1 &"
  125. eval $web_service_cmd
  126. last_status=${PIPESTATUS[0]}
  127. status_check $last_status "${web_service_cmd}" "${status_log}" "${model_name}" "${server_log_path}"
  128. elif [[ ${model_name} =~ "rec" ]]; then
  129. set_rec_model_config=$(func_set_params "${rec_server_key}" "${rec_server_value}")
  130. web_service_cmd="nohup ${python} ${web_service_py} ${web_use_gpu_key}="" ${web_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_rec_model_config} > ${server_log_path} 2>&1 &"
  131. eval $web_service_cmd
  132. last_status=${PIPESTATUS[0]}
  133. status_check $last_status "${web_service_cmd}" "${status_log}" "${model_name}" "${server_log_path}"
  134. fi
  135. sleep 2s
  136. for pipeline in ${pipeline_py[*]}; do
  137. _save_log_path="${LOG_PATH}/python_client_cpu_${pipeline%_client*}_usemkldnn_${use_mkldnn}_threads_${threads}_batchsize_1.log"
  138. pipeline_cmd="${python} ${pipeline} ${set_image_dir} > ${_save_log_path} 2>&1 "
  139. eval $pipeline_cmd
  140. last_status=${PIPESTATUS[0]}
  141. eval "cat ${_save_log_path}"
  142. status_check $last_status "${pipeline_cmd}" "${status_log}" "${model_name}" "${_save_log_path}"
  143. sleep 2s
  144. done
  145. ps ux | grep -E 'web_service' | awk '{print $2}' | xargs kill -s 9
  146. done
  147. done
  148. elif [ ${use_gpu} = "gpu" ]; then
  149. for use_trt in ${web_use_trt_list[*]}; do
  150. for precision in ${web_precision_list[*]}; do
  151. server_log_path="${LOG_PATH}/python_server_gpu_usetrt_${use_trt}_precision_${precision}.log"
  152. if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then
  153. continue
  154. fi
  155. if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then
  156. continue
  157. fi
  158. if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [[ ${_flag_quant} = "True" ]]; then
  159. continue
  160. fi
  161. set_tensorrt=$(func_set_params "${web_use_trt_key}" "${use_trt}")
  162. if [ ${use_trt} = True ]; then
  163. device_type=2
  164. fi
  165. set_precision=$(func_set_params "${web_precision_key}" "${precision}")
  166. if [ ${model_name} = "ch_PP-OCRv2" ] || [ ${model_name} = "ch_PP-OCRv3" ] || [ ${model_name} = "ch_ppocr_mobile_v2_0" ] || [ ${model_name} = "ch_ppocr_server_v2_0" ]; then
  167. set_det_model_config=$(func_set_params "${det_server_key}" "${det_server_value}")
  168. set_rec_model_config=$(func_set_params "${rec_server_key}" "${rec_server_value}")
  169. web_service_cmd="nohup ${python} ${web_service_py} ${set_tensorrt} ${set_precision} ${set_det_model_config} ${set_rec_model_config} > ${server_log_path} 2>&1 &"
  170. eval $web_service_cmd
  171. last_status=${PIPESTATUS[0]}
  172. status_check $last_status "${web_service_cmd}" "${status_log}" "${model_name}" "${server_log_path}"
  173. elif [[ ${model_name} =~ "det" ]]; then
  174. set_det_model_config=$(func_set_params "${det_server_key}" "${det_server_value}")
  175. web_service_cmd="nohup ${python} ${web_service_py} ${set_tensorrt} ${set_precision} ${set_det_model_config} > ${server_log_path} 2>&1 &"
  176. eval $web_service_cmd
  177. last_status=${PIPESTATUS[0]}
  178. status_check $last_status "${web_service_cmd}" "${status_log}" "${model_name}" "${server_log_path}"
  179. elif [[ ${model_name} =~ "rec" ]]; then
  180. set_rec_model_config=$(func_set_params "${rec_server_key}" "${rec_server_value}")
  181. web_service_cmd="nohup ${python} ${web_service_py} ${set_tensorrt} ${set_precision} ${set_rec_model_config} > ${server_log_path} 2>&1 &"
  182. eval $web_service_cmd
  183. last_status=${PIPESTATUS[0]}
  184. status_check $last_status "${web_service_cmd}" "${status_log}" "${model_name}" "${server_log_path}"
  185. fi
  186. sleep 2s
  187. for pipeline in ${pipeline_py[*]}; do
  188. _save_log_path="${LOG_PATH}/python_client_gpu_${pipeline%_client*}_usetrt_${use_trt}_precision_${precision}_batchsize_1.log"
  189. pipeline_cmd="${python} ${pipeline} ${set_image_dir}> ${_save_log_path} 2>&1"
  190. eval $pipeline_cmd
  191. last_status=${PIPESTATUS[0]}
  192. eval "cat ${_save_log_path}"
  193. status_check $last_status "${pipeline_cmd}" "${status_log}" "${model_name}" "${_save_log_path}"
  194. sleep 2s
  195. done
  196. ps ux | grep -E 'web_service' | awk '{print $2}' | xargs kill -s 9
  197. done
  198. done
  199. else
  200. echo "Does not support hardware other than CPU and GPU Currently!"
  201. fi
  202. done
  203. }
  204. #set cuda device
  205. GPUID=$3
  206. if [ ${#GPUID} -le 0 ];then
  207. env="export CUDA_VISIBLE_DEVICES=0"
  208. else
  209. env="export CUDA_VISIBLE_DEVICES=${GPUID}"
  210. fi
  211. eval $env
  212. echo $env
  213. echo "################### run test ###################"
  214. export Count=0
  215. IFS="|"
  216. func_serving "${web_service_cmd}"