benchmark_train.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/bin/bash
  2. source test_tipc/utils_func.sh
  3. # set env
  4. python=python
  5. export model_branch=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`
  6. export model_commit=$(git log|head -n1|awk '{print $2}')
  7. export str_tmp=$(echo `pip list|grep paddlepaddle-gpu|awk -F ' ' '{print $2}'`)
  8. export frame_version=${str_tmp%%.post*}
  9. export frame_commit=$(echo `${python} -c "import paddle;print(paddle.version.commit)"`)
  10. # run benchmark sh
  11. # Usage:
  12. # bash run_benchmark_train.sh config.txt params
  13. # or
  14. # bash run_benchmark_train.sh config.txt
  15. function func_parser_params(){
  16. strs=$1
  17. IFS="="
  18. array=(${strs})
  19. tmp=${array[1]}
  20. echo ${tmp}
  21. }
  22. function func_sed_params(){
  23. filename=$1
  24. line=$2
  25. param_value=$3
  26. params=`sed -n "${line}p" $filename`
  27. IFS=":"
  28. array=(${params})
  29. key=${array[0]}
  30. new_params="${key}:${param_value}"
  31. IFS=";"
  32. cmd="sed -i '${line}s/.*/${new_params}/' '${filename}'"
  33. eval $cmd
  34. }
  35. function set_gpu_id(){
  36. string=$1
  37. _str=${string:1:6}
  38. IFS="C"
  39. arr=(${_str})
  40. M=${arr[0]}
  41. P=${arr[1]}
  42. gn=`expr $P - 1`
  43. gpu_num=`expr $gn / $M`
  44. seq=`seq -s "," 0 $gpu_num`
  45. echo $seq
  46. }
  47. function get_repo_name(){
  48. IFS=";"
  49. cur_dir=$(pwd)
  50. IFS="/"
  51. arr=(${cur_dir})
  52. echo ${arr[-1]}
  53. }
  54. FILENAME=$1
  55. # copy FILENAME as new
  56. new_filename="./test_tipc/benchmark_train.txt"
  57. cmd=`yes|cp $FILENAME $new_filename`
  58. FILENAME=$new_filename
  59. # MODE must be one of ['benchmark_train']
  60. MODE=$2
  61. PARAMS=$3
  62. # bash test_tipc/benchmark_train.sh test_tipc/configs/det_mv3_db_v2_0/train_benchmark.txt benchmark_train dynamic_bs8_null_DP_N1C1
  63. IFS=$'\n'
  64. # parser params from train_benchmark.txt
  65. dataline=`cat $FILENAME`
  66. # parser params
  67. IFS=$'\n'
  68. lines=(${dataline})
  69. model_name=$(func_parser_value "${lines[1]}")
  70. # 获取benchmark_params所在的行数
  71. line_num=`grep -n "train_benchmark_params" $FILENAME | cut -d ":" -f 1`
  72. # for train log parser
  73. batch_size=$(func_parser_value "${lines[line_num]}")
  74. line_num=`expr $line_num + 1`
  75. fp_items=$(func_parser_value "${lines[line_num]}")
  76. line_num=`expr $line_num + 1`
  77. epoch=$(func_parser_value "${lines[line_num]}")
  78. line_num=`expr $line_num + 1`
  79. profile_option_key=$(func_parser_key "${lines[line_num]}")
  80. profile_option_params=$(func_parser_value "${lines[line_num]}")
  81. profile_option="${profile_option_key}:${profile_option_params}"
  82. line_num=`expr $line_num + 1`
  83. flags_value=$(func_parser_value "${lines[line_num]}")
  84. if [ ${flags_value} != "null" ];then
  85. # set flags
  86. IFS=";"
  87. flags_list=(${flags_value})
  88. for _flag in ${flags_list[*]}; do
  89. cmd="export ${_flag}"
  90. eval $cmd
  91. done
  92. fi
  93. # set log_name
  94. repo_name=$(get_repo_name )
  95. SAVE_LOG=${BENCHMARK_LOG_DIR:-$(pwd)} # */benchmark_log
  96. mkdir -p "${SAVE_LOG}/benchmark_log/"
  97. status_log="${SAVE_LOG}/benchmark_log/results.log"
  98. # The number of lines in which train params can be replaced.
  99. line_python=3
  100. line_gpuid=4
  101. line_precision=6
  102. line_epoch=7
  103. line_batchsize=9
  104. line_profile=13
  105. line_eval_py=24
  106. line_export_py=30
  107. func_sed_params "$FILENAME" "${line_eval_py}" "null"
  108. func_sed_params "$FILENAME" "${line_export_py}" "null"
  109. func_sed_params "$FILENAME" "${line_python}" "${python}"
  110. # if params
  111. if [ ! -n "$PARAMS" ] ;then
  112. # PARAMS input is not a word.
  113. IFS="|"
  114. batch_size_list=(${batch_size})
  115. fp_items_list=(${fp_items})
  116. device_num_list=(N1C4)
  117. run_mode="DP"
  118. else
  119. # parser params from input: modeltype_bs${bs_item}_${fp_item}_${run_mode}_${device_num}
  120. IFS="_"
  121. params_list=(${PARAMS})
  122. model_type=${params_list[0]}
  123. batch_size=${params_list[1]}
  124. batch_size=`echo ${batch_size} | tr -cd "[0-9]" `
  125. precision=${params_list[2]}
  126. run_mode=${params_list[3]}
  127. device_num=${params_list[4]}
  128. IFS=";"
  129. if [ ${precision} = "null" ];then
  130. precision="fp32"
  131. fi
  132. fp_items_list=($precision)
  133. batch_size_list=($batch_size)
  134. device_num_list=($device_num)
  135. fi
  136. IFS="|"
  137. for batch_size in ${batch_size_list[*]}; do
  138. for precision in ${fp_items_list[*]}; do
  139. for device_num in ${device_num_list[*]}; do
  140. # sed batchsize and precision
  141. func_sed_params "$FILENAME" "${line_precision}" "$precision"
  142. func_sed_params "$FILENAME" "${line_batchsize}" "$MODE=$batch_size"
  143. func_sed_params "$FILENAME" "${line_epoch}" "$MODE=$epoch"
  144. gpu_id=$(set_gpu_id $device_num)
  145. if [ ${#gpu_id} -le 1 ];then
  146. log_path="$SAVE_LOG/profiling_log"
  147. mkdir -p $log_path
  148. log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_profiling"
  149. func_sed_params "$FILENAME" "${line_gpuid}" "0" # sed used gpu_id
  150. # set profile_option params
  151. tmp=`sed -i "${line_profile}s/.*/${profile_option}/" "${FILENAME}"`
  152. # run test_train_inference_python.sh
  153. cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
  154. echo $cmd
  155. eval $cmd
  156. eval "cat ${log_path}/${log_name}"
  157. # without profile
  158. log_path="$SAVE_LOG/train_log"
  159. speed_log_path="$SAVE_LOG/index"
  160. mkdir -p $log_path
  161. mkdir -p $speed_log_path
  162. log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_log"
  163. speed_log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_speed"
  164. func_sed_params "$FILENAME" "${line_profile}" "null" # sed profile_id as null
  165. cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
  166. echo $cmd
  167. job_bt=`date '+%Y%m%d%H%M%S'`
  168. eval $cmd
  169. job_et=`date '+%Y%m%d%H%M%S'`
  170. export model_run_time=$((${job_et}-${job_bt}))
  171. eval "cat ${log_path}/${log_name}"
  172. # parser log
  173. _model_name="${model_name}_bs${batch_size}_${precision}_${run_mode}"
  174. cmd="${python} ${BENCHMARK_ROOT}/scripts/analysis.py --filename ${log_path}/${log_name} \
  175. --speed_log_file '${speed_log_path}/${speed_log_name}' \
  176. --model_name ${_model_name} \
  177. --base_batch_size ${batch_size} \
  178. --run_mode ${run_mode} \
  179. --fp_item ${precision} \
  180. --keyword ips: \
  181. --skip_steps 2 \
  182. --device_num ${device_num} \
  183. --speed_unit images/s \
  184. --convergence_key loss: "
  185. echo $cmd
  186. eval $cmd
  187. last_status=${PIPESTATUS[0]}
  188. status_check $last_status "${cmd}" "${status_log}"
  189. else
  190. IFS=";"
  191. unset_env=`unset CUDA_VISIBLE_DEVICES`
  192. log_path="$SAVE_LOG/train_log"
  193. speed_log_path="$SAVE_LOG/index"
  194. mkdir -p $log_path
  195. mkdir -p $speed_log_path
  196. log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_log"
  197. speed_log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_speed"
  198. func_sed_params "$FILENAME" "${line_gpuid}" "$gpu_id" # sed used gpu_id
  199. func_sed_params "$FILENAME" "${line_profile}" "null" # sed --profile_option as null
  200. cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
  201. echo $cmd
  202. job_bt=`date '+%Y%m%d%H%M%S'`
  203. eval $cmd
  204. job_et=`date '+%Y%m%d%H%M%S'`
  205. export model_run_time=$((${job_et}-${job_bt}))
  206. eval "cat ${log_path}/${log_name}"
  207. # parser log
  208. _model_name="${model_name}_bs${batch_size}_${precision}_${run_mode}"
  209. cmd="${python} ${BENCHMARK_ROOT}/scripts/analysis.py --filename ${log_path}/${log_name} \
  210. --speed_log_file '${speed_log_path}/${speed_log_name}' \
  211. --model_name ${_model_name} \
  212. --base_batch_size ${batch_size} \
  213. --run_mode ${run_mode} \
  214. --fp_item ${precision} \
  215. --keyword ips: \
  216. --skip_steps 2 \
  217. --device_num ${device_num} \
  218. --speed_unit images/s \
  219. --convergence_key loss: "
  220. echo $cmd
  221. eval $cmd
  222. last_status=${PIPESTATUS[0]}
  223. status_check $last_status "${cmd}" "${status_log}"
  224. fi
  225. done
  226. done
  227. done