benchmark_train.sh 9.1 KB

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