test_ptq_inference_python.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. source test_tipc/utils_func.sh
  3. FILENAME=$1
  4. MODE="whole_infer"
  5. # parser model_name
  6. dataline=$(cat ${FILENAME})
  7. IFS=$'\n'
  8. lines=(${dataline})
  9. model_name=$(func_parser_value "${lines[1]}")
  10. echo "ppdet ptq: ${model_name}"
  11. python=$(func_parser_value "${lines[2]}")
  12. filename_key=$(func_parser_key "${lines[3]}")
  13. # parser export params
  14. save_export_key=$(func_parser_key "${lines[5]}")
  15. save_export_value=$(func_parser_value "${lines[5]}")
  16. export_weight_key=$(func_parser_key "${lines[6]}")
  17. export_weight_value=$(func_parser_value "${lines[6]}")
  18. kl_quant_export=$(func_parser_value "${lines[7]}")
  19. export_param1_key=$(func_parser_key "${lines[8]}")
  20. export_param1_value=$(func_parser_value "${lines[8]}")
  21. # parser infer params
  22. inference_py=$(func_parser_value "${lines[10]}")
  23. device_key=$(func_parser_key "${lines[11]}")
  24. device_list=$(func_parser_value "${lines[11]}")
  25. use_mkldnn_key=$(func_parser_key "${lines[12]}")
  26. use_mkldnn_list=$(func_parser_value "${lines[12]}")
  27. cpu_threads_key=$(func_parser_key "${lines[13]}")
  28. cpu_threads_list=$(func_parser_value "${lines[13]}")
  29. batch_size_key=$(func_parser_key "${lines[14]}")
  30. batch_size_list=$(func_parser_value "${lines[14]}")
  31. run_mode_key=$(func_parser_key "${lines[15]}")
  32. run_mode_list=$(func_parser_value "${lines[15]}")
  33. model_dir_key=$(func_parser_key "${lines[16]}")
  34. image_dir_key=$(func_parser_key "${lines[17]}")
  35. image_dir_value=$(func_parser_value "${lines[17]}")
  36. run_benchmark_key=$(func_parser_key "${lines[18]}")
  37. run_benchmark_value=$(func_parser_value "${lines[18]}")
  38. infer_param1_key=$(func_parser_key "${lines[19]}")
  39. infer_param1_value=$(func_parser_value "${lines[19]}")
  40. LOG_PATH="./test_tipc/output/${model_name}/${MODE}"
  41. mkdir -p ${LOG_PATH}
  42. status_log="${LOG_PATH}/results_ptq_python.log"
  43. function func_ptq_inference(){
  44. IFS='|'
  45. _python=$1
  46. _log_path=$2
  47. _script=$3
  48. _set_model_dir=$4
  49. set_image_dir=$(func_set_params "${image_dir_key}" "${image_dir_value}")
  50. set_run_benchmark=$(func_set_params "${run_benchmark_key}" "${run_benchmark_value}")
  51. set_infer_param1=$(func_set_params "${infer_param1_key}" "${infer_param1_value}")
  52. # inference
  53. for device in ${device_list[*]}; do
  54. set_device=$(func_set_params "${device_key}" "${device}")
  55. if [ ${device} = "cpu" ]; then
  56. for use_mkldnn in ${use_mkldnn_list[*]}; do
  57. set_use_mkldnn=$(func_set_params "${use_mkldnn_key}" "${use_mkldnn}")
  58. for threads in ${cpu_threads_list[*]}; do
  59. set_cpu_threads=$(func_set_params "${cpu_threads_key}" "${threads}")
  60. for batch_size in ${batch_size_list[*]}; do
  61. _save_log_path="${_log_path}/python_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_mode_paddle_batchsize_${batch_size}.log"
  62. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  63. command="${_python} ${_script} ${set_device} ${set_use_mkldnn} ${set_cpu_threads} ${_set_model_dir} ${set_batchsize} ${set_image_dir} ${set_run_benchmark} ${set_infer_param1} > ${_save_log_path} 2>&1 "
  64. eval $command
  65. last_status=${PIPESTATUS[0]}
  66. eval "cat ${_save_log_path}"
  67. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  68. done
  69. done
  70. done
  71. elif [ ${device} = "gpu" ]; then
  72. for run_mode in ${run_mode_list[*]}; do
  73. if [[ ${run_mode} = "paddle" ]] || [[ ${run_mode} = "trt_int8" ]]; then
  74. for batch_size in ${batch_size_list[*]}; do
  75. _save_log_path="${_log_path}/python_infer_gpu_mode_${run_mode}_batchsize_${batch_size}.log"
  76. set_batchsize=$(func_set_params "${batch_size_key}" "${batch_size}")
  77. set_run_mode=$(func_set_params "${run_mode_key}" "${run_mode}")
  78. command="${_python} ${_script} ${set_device} ${set_run_mode} ${_set_model_dir} ${set_batchsize} ${set_image_dir} ${set_run_benchmark} ${set_infer_param1} > ${_save_log_path} 2>&1 "
  79. eval $command
  80. last_status=${PIPESTATUS[0]}
  81. eval "cat ${_save_log_path}"
  82. status_check $last_status "${command}" "${status_log}" "${model_name}" "${_save_log_path}"
  83. done
  84. fi
  85. done
  86. else
  87. echo "Does not support hardware other than CPU and GPU Currently!"
  88. fi
  89. done
  90. }
  91. IFS="|"
  92. # run ptq
  93. set_export_weight=$(func_set_params "${export_weight_key}" "${export_weight_value}")
  94. set_save_export_dir=$(func_set_params "${save_export_key}" "${save_export_value}")
  95. set_filename=$(func_set_params "${filename_key}" "${model_name}")
  96. export_log_path="${LOG_PATH}/export.log"
  97. ptq_cmd="${python} ${kl_quant_export} ${set_export_weight} ${set_filename} ${set_save_export_dir}"
  98. echo $ptq_cmd
  99. eval "${ptq_cmd} > ${export_log_path} 2>&1"
  100. status_export=$?
  101. cat ${export_log_path}
  102. status_check $status_export "${ptq_cmd}" "${status_log}" "${model_name}" "${export_log_path}"
  103. #run inference
  104. set_export_model_dir=$(func_set_params "${model_dir_key}" "${save_export_value}/${model_name}")
  105. func_ptq_inference "${python}" "${LOG_PATH}" "${inference_py}" "${set_export_model_dir}"