• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Convert models:
4function Convert() {
5  # $1:cfgFileList; $2:inModelPath; $3:outModelPath; $4:logFile; $5:resultFile; $6:failNotReturn;
6  local cfg_file_list model_info model_name extra_info model_type cfg_file_name model_file weight_file output_file \
7        quant_type bit_num config_file train_model in_dtype out_dtype converter_result cfg_file
8  cfg_file_list=$1
9  for cfg_file in ${cfg_file_list[*]}; do
10    while read line; do
11      if [[ $line == \#* || $line == "" ]]; then
12        continue
13      fi
14      model_info=${line%% *}
15      model_name=${model_info%%;*}
16      extra_info=${model_info##*;}
17      model_type=${model_name##*.}
18      cfg_file_name=${cfg_file##*/}
19      quant_config_path="${cfg_file%/*}/quant"
20      case $model_type in
21        pb)
22          model_fmk="TF"
23          ;;
24        tflite)
25          model_fmk="TFLITE"
26          ;;
27        onnx)
28          model_fmk="ONNX"
29          ;;
30        mindir)
31          model_fmk="MINDIR"
32          ;;
33        *)
34          model_type="caffe"
35          model_fmk="CAFFE"
36          ;;
37      esac
38      # set parameters
39      model_file=$2"/"${model_name}
40      weight_file=""
41      if [[ $model_fmk == "CAFFE" ]]; then
42        model_file=${model_file}".prototxt"
43        weight_file=${model_file%.*}".caffemodel"
44      fi
45      output_file=$3"/"${model_name}
46      quant_type=""
47      bit_num=8
48      config_file=""
49      train_model="false"
50      in_dtype="DEFAULT"
51      out_dtype="DEFAULT"
52      if [[ ${cfg_file_name} =~ "weightquant" ]]; then
53        postfix=${cfg_file##*_}
54        bit_num=${postfix:0:1}
55        quant_type="WeightQuant"
56        output_file=${output_file}"_${bit_num}bit"
57        config_file="${quant_config_path}/weight_quant_${bit_num}bit.cfg"
58      elif [[ ${cfg_file_name} =~ "_train" ]]; then
59        train_model="true"
60      elif [[ ${cfg_file_name} =~ "posttraining" ]]; then
61        quant_type="PostTraining"
62        output_file=${output_file}"_posttraining"
63        config_file="${quant_config_path}/${model_name}_posttraining.config"
64      elif [[ ${cfg_file_name} =~ "awaretraining" || ${extra_info} =~ "aware_training" ]]; then
65        in_dtype="FLOAT"
66        out_dtype="FLOAT"
67      fi
68      # start running converter
69      echo ${model_name} >> "$4"
70      echo './converter_lite  --fmk='${model_fmk}' --modelFile='${model_file}' --weightFile='${weight_file}' --outputFile='${output_file}\
71        ' --inputDataType='${in_dtype}' --outputDataType='${out_dtype}' \
72         --configFile='${config_file}' --trainModel='${train_model} >> "$4"
73      ./converter_lite  --fmk=${model_fmk} --modelFile=${model_file} --weightFile=${weight_file} --outputFile=${output_file}\
74        --inputDataType=${in_dtype} --outputDataType=${out_dtype} \
75        --configFile=${config_file} --trainModel=${train_model} >> "$4"
76      if [ $? = 0 ]; then
77          converter_result='converter '${model_type}''${quant_type}' '${model_name}' pass';echo ${converter_result} >> $5
78      else
79          converter_result='converter '${model_type}''${quant_type}' '${model_name}' failed';echo ${converter_result} >> $5
80          if [[ $6 != "ON" ]]; then
81              return 1
82          fi
83      fi
84    done < ${cfg_file}
85  done
86}
87
88function Push_Files() {
89    # $1:packagePath; $2:platform; $3:version; $4:localPath; $5:logFile; $6:deviceID;
90    cd $1 || exit 1
91    tar -zxf mindspore-lite-$3-android-$2.tar.gz || exit 1
92
93    # If build with minddata, copy the minddata related libs
94    cd $4 || exit 1
95    if [ -f $1/mindspore-lite-$3-android-$2/runtime/lib/libminddata-lite.so ]; then
96        cp -a $1/mindspore-lite-$3-android-$2/runtime/lib/libminddata-lite.so $4/libminddata-lite.so || exit 1
97    fi
98    if [ -f $1/mindspore-lite-$3-android-$2/runtime/third_party/hiai_ddk/lib/libhiai.so ]; then
99      cp -a $1/mindspore-lite-$3-android-$2/runtime/third_party/hiai_ddk/lib/libhiai.so $4/libhiai.so || exit 1
100      cp -a $1/mindspore-lite-$3-android-$2/runtime/third_party/hiai_ddk/lib/libhiai_ir.so $4/libhiai_ir.so || exit 1
101      cp -a $1/mindspore-lite-$3-android-$2/runtime/third_party/hiai_ddk/lib/libhiai_ir_build.so $4/libhiai_ir_build.so || exit 1
102    fi
103    if [ -f $1/mindspore-lite-$3-android-$2/runtime/third_party/hiai_ddk/lib/libhiai_hcl_model_runtime.so ]; then
104      cp -a $1/mindspore-lite-$3-android-$2/runtime/third_party/hiai_ddk/lib/libhiai_hcl_model_runtime.so $4/libhiai_hcl_model_runtime.so || exit 1
105    fi
106
107    cp -a $1/mindspore-lite-$3-android-$2/runtime/lib/libmindspore-lite.so $4/libmindspore-lite.so || exit 1
108    cp -a $1/mindspore-lite-$3-android-$2/tools/benchmark/benchmark $4/benchmark || exit 1
109
110    # adb push all needed files to the phone
111    adb -s $6 push $4 /data/local/tmp/ > $5
112
113    arm32_dir=""
114    if [[ $2 == "aarch32" ]]; then
115      arm32_dir="arm32/"
116    fi
117    # run adb ,run session ,check the result:
118    echo 'cd  /data/local/tmp/benchmark_test' > adb_cmd.txt
119    echo 'cp  /data/local/tmp/'$arm32_dir'libc++_shared.so ./' >> adb_cmd.txt
120    echo 'chmod 777 benchmark' >> adb_cmd.txt
121
122    adb -s $6 shell < adb_cmd.txt
123}
124
125# Run converted models:
126function Run_Benchmark() {
127  # $1:cfgFileList; $2:modelPath; $3:dataPath; $4:logFile; $5:resultFile; $6:platform; $7:processor; $8:phoneId; $9:failNotReturn;
128  local cfg_file_list cfg_file_name line_info model_info spec_acc_limit model_name input_num input_shapes spec_threads \
129        extra_info benchmark_mode infix mode model_file input_files output_file data_path threads acc_limit enableFp16 \
130        run_result cfg_file
131  cfg_file_list=$1
132  for cfg_file in ${cfg_file_list[*]}; do
133    cfg_file_name=${cfg_file##*/}
134    while read line; do
135      line_info=${line}
136      if [[ $line_info == \#* || $line_info == "" ]]; then
137        continue
138      fi
139      model_info=`echo ${line_info} | awk -F ' ' '{print $1}'`
140      spec_acc_limit=`echo ${line_info} | awk -F ' ' '{print $2}'`
141      model_name=`echo ${model_info} | awk -F ';' '{print $1}'`
142      input_info=`echo ${model_info} | awk -F ';' '{print $2}'`
143      input_shapes=`echo ${model_info} | awk -F ';' '{print $3}'`
144      spec_threads=`echo ${model_info} | awk -F ';' '{print $4}'`
145      extra_info=`echo ${model_info} | awk -F ';' '{print $5}'`
146      input_num=`echo ${input_info} | sed 's/:/;/' | awk -F ';' '{print $1}'`
147      input_names=`echo ${input_info} | sed 's/:/;/' | awk -F ';' '{print $2}'`
148      if [[ ${model_name##*.} == "caffemodel" ]]; then
149        model_name=${model_name%.*}
150      fi
151      echo "Benchmarking ${model_name} $6 $7 ......"
152      # adjust benchmark mode
153      benchmark_mode="calib"
154      if [[ $6 == "arm64" && $7 == "CPU" && ! ${cfg_file_name} =~ "fp16" ]]; then
155        benchmark_mode="calib+loop"
156      fi
157      # adjust precision mode
158      mode="fp32"
159      if [[ ${cfg_file_name} =~ "fp16" ]]; then
160        mode="fp16"
161      fi
162      # adjust file name
163      infix=""
164      if [[ ${cfg_file_name} =~ "weightquant" ]]; then
165        infix="_${cfg_file##*_}"
166        infix=${infix%.*}
167        benchmark_mode="calib"
168      elif [[ ${cfg_file_name} =~ "_train" ]]; then
169        infix="_train"
170      elif [[ ${cfg_file_name} =~ "_posttraining" ]]; then
171        model_name=${model_name}"_posttraining"
172      elif [[ ${cfg_file_name} =~ "_process_only" ]]; then
173        benchmark_mode="loop"
174      elif [[ ${cfg_file_name} =~ "_compatibility" && ${spec_acc_limit} == "" ]]; then
175        benchmark_mode="loop"
176      fi
177      model_file=$2"/${model_name}${infix}.ms"
178      input_files=""
179      output_file=""
180      data_path=$3"/input_output/"
181      if [[ ${input_num} == "" || ${input_num} == 1 ]]; then
182        input_files=${data_path}'input/'${model_name}'.ms.bin'
183      else
184        for i in $(seq 1 $input_num)
185        do
186          input_files=${input_files}${data_path}'input/'${model_name}'.ms.bin_'$i','
187        done
188      fi
189      output_file=${data_path}'output/'${model_name}'.ms.out'
190      # adjust threads
191      threads="2"
192      if [[ ${spec_threads} != "" ]]; then
193        threads="${spec_threads}"
194      fi
195      # set accuracy limitation
196      acc_limit="0.5"
197      if [[ ${cfg_file_name} =~ "_train" ]]; then
198        acc_limit="1.5"
199      fi
200      if [[ ${spec_acc_limit} != "" ]]; then
201        acc_limit="${spec_acc_limit}"
202      elif [[ $7 == "GPU" ]] && [[ ${mode} == "fp16" || ${cfg_file_name} =~ "_weightquant" ]]; then
203        acc_limit="5"
204      fi
205      # whether enable fp16
206      enableFp16="false"
207      if [[ ${mode} == "fp16" ]]; then
208        enableFp16="true"
209      fi
210      if [[ ${extra_info} =~ "calib_only" ]]; then
211        benchmark_mode="calib"
212      fi
213      # start running benchmark
214      echo "---------------------------------------------------------" >> "$4"
215      if [[ ${benchmark_mode} = "calib" || ${benchmark_mode} = "calib+loop" ]]; then
216        echo "$6 $7 ${mode} run calib: ${model_name}, accuracy limit:${acc_limit}" >> "$4"
217        if [[ $6 == "arm64" || $6 == "arm32" ]]; then
218          echo 'cd  /data/local/tmp/benchmark_test' > adb_run_cmd.txt
219          echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp/benchmark_test' >> adb_run_cmd.txt
220          echo './benchmark --modelFile='${model_file}' --inDataFile='${input_files}' --inputShapes='${input_shapes}' --benchmarkDataFile='${output_file}' --enableFp16='${enableFp16}' --accuracyThreshold='${acc_limit}' --device='$7' --numThreads='${threads} >> adb_run_cmd.txt
221          echo './benchmark --modelFile='${model_file}' --inDataFile='${input_files}' --inputShapes='${input_shapes}' --benchmarkDataFile='${output_file}' --enableFp16='${enableFp16}' --accuracyThreshold='${acc_limit}' --device='$7' --numThreads='${threads}>> $4
222          cat adb_run_cmd.txt >> "$4"
223          adb -s $8 shell < adb_run_cmd.txt >> "$4"
224        else
225          echo 'MSLITE_BENCH_INPUT_NAMES=${input_names} ./benchmark --modelFile='${model_file}' --inDataFile='${input_files}' --inputShapes='${input_shapes}' --benchmarkDataFile='${output_file}' --accuracyThreshold='${acc_limit}' --numThreads='${threads} >> "$4"
226          MSLITE_BENCH_INPUT_NAMES=${input_names} ./benchmark --modelFile=${model_file} --inDataFile=${input_files} --inputShapes=${input_shapes} --benchmarkDataFile=${output_file} --accuracyThreshold=${acc_limit} --numThreads=${threads} >> "$4"
227        fi
228        if [ $? = 0 ]; then
229          run_result="$6_$7_${mode}: ${model_file##*/} pass"; echo ${run_result} >> $5
230        else
231          run_result="$6_$7_${mode}: ${model_file##*/} failed"; echo ${run_result} >> $5
232          if [[ $9 != "ON" ]]; then
233              return 1
234          fi
235        fi
236      fi
237      # run benchmark without clib data recurrently for guarding the repeated graph execution scene
238      if [[ ${benchmark_mode} = "loop" || ${benchmark_mode} = "calib+loop" ]]; then
239        echo "$6 $7 ${mode} run loop: ${model_name}" >> "$4"
240        if [[ ! ${extra_info} =~ "input_dependent" ]]; then
241          input_files=""
242        fi
243        if [[ $6 == "arm64" || $6 == "arm32" ]]; then
244          echo 'cd  /data/local/tmp/benchmark_test' > adb_run_cmd.txt
245          echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp/benchmark_test' >> adb_run_cmd.txt
246          echo './benchmark --inDataFile='${input_files}' --modelFile='${model_file}' --inputShapes='${input_shapes}' --enableFp16='${enableFp16}' --warmUpLoopCount=0 --loopCount=2 --device='$7' --numThreads='${threads} >> adb_run_cmd.txt
247          echo './benchmark --inDataFile='${input_files}' --modelFile='${model_file}' --inputShapes='${input_shapes}' --enableFp16='${enableFp16}' --warmUpLoopCount=0 --loopCount=2 --device='$7' --numThreads='${threads} >> $4
248          cat adb_run_cmd.txt >> "$4"
249          adb -s $8 shell < adb_run_cmd.txt >> "$4"
250        else
251          echo './benchmark --inDataFile='${input_files}' --modelFile='${model_file}' --inputShapes='${input_shapes}' --warmUpLoopCount=0 --loopCount=2 --numThreads='${threads} >> "$4"
252          ./benchmark --inDataFile=${input_files} --modelFile=${model_file} --inputShapes=${input_shapes} --warmUpLoopCount=0 --loopCount=2 --numThreads=${threads} >> "$4"
253        fi
254        if [ $? = 0 ]; then
255            run_result="$6_$7_${mode}_loop: ${model_file##*/} pass"; echo ${run_result} >> $5
256        else
257            run_result="$6_$7_${mode}_loop: ${model_file##*/} failed"; echo ${run_result} >> $5
258            if [[ $9 != "ON" ]]; then
259                return 1
260            fi
261        fi
262      fi
263    done < ${cfg_file}
264  done
265}
266
267# Print start msg before run testcase
268function MS_PRINT_TESTCASE_START_MSG() {
269    echo ""
270    echo -e "-----------------------------------------------------------------------------------------------------------------------------------"
271    echo -e "env                    Testcase                                                                                           Result   "
272    echo -e "---                    --------                                                                                           ------   "
273}
274
275# Print start msg after run testcase
276function MS_PRINT_TESTCASE_END_MSG() {
277    echo -e "-----------------------------------------------------------------------------------------------------------------------------------"
278}
279
280function Print_Converter_Result() {
281    MS_PRINT_TESTCASE_END_MSG
282    while read line; do
283        arr=("${line}")
284        printf "%-15s %-20s %-90s %-7s\n" ${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]}
285    done < $1
286    MS_PRINT_TESTCASE_END_MSG
287}
288
289function Print_Benchmark_Result() {
290    MS_PRINT_TESTCASE_START_MSG
291    while read line; do
292        arr=("${line}")
293        printf "%-20s %-100s %-7s\n" ${arr[0]} ${arr[1]} ${arr[2]}
294    done < $1
295    MS_PRINT_TESTCASE_END_MSG
296}