forked from mindspore-Ecosystem/mindspore
!31473 [MSLITE] Enable to convert models parallelly.
Merge pull request !31473 from wangshaocong/ci_master
This commit is contained in:
commit
daf2f88a76
|
@ -3,6 +3,13 @@
|
||||||
# Convert models:
|
# Convert models:
|
||||||
function Convert() {
|
function Convert() {
|
||||||
# $1:cfgFileList; $2:inModelPath; $3:outModelPath; $4:logFile; $5:resultFile; $6:failNotReturn;
|
# $1:cfgFileList; $2:inModelPath; $3:outModelPath; $4:logFile; $5:resultFile; $6:failNotReturn;
|
||||||
|
fifo_file="fifo_file.txt"
|
||||||
|
mkfifo ${fifo_file}
|
||||||
|
exec 6<>${fifo_file}
|
||||||
|
rm -f ${fifo_file}
|
||||||
|
max_converter_jobs=6
|
||||||
|
for ((i = 0; i < ${max_converter_jobs}; i++)); do echo; done >&6
|
||||||
|
|
||||||
local cfg_file_list model_info model_name extra_info model_type cfg_file_name model_file weight_file output_file \
|
local cfg_file_list model_info model_name extra_info model_type cfg_file_name model_file weight_file output_file \
|
||||||
quant_type config_file train_model in_dtype out_dtype converter_result cfg_file calib_size
|
quant_type config_file train_model in_dtype out_dtype converter_result cfg_file calib_size
|
||||||
cfg_file_list=$1
|
cfg_file_list=$1
|
||||||
|
@ -11,117 +18,123 @@ function Convert() {
|
||||||
if [[ $line == \#* || $line == "" ]]; then
|
if [[ $line == \#* || $line == "" ]]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
model_info=`echo ${line} | awk -F ' ' '{print $1}'`
|
read -u6
|
||||||
calib_size=`echo ${line} | awk -F ' ' '{print $3}'`
|
{
|
||||||
model_name=`echo ${model_info} | awk -F ';' '{print $1}'`
|
model_info=`echo ${line} | awk -F ' ' '{print $1}'`
|
||||||
input_info=`echo ${model_info} | awk -F ';' '{print $2}'`
|
calib_size=`echo ${line} | awk -F ' ' '{print $3}'`
|
||||||
input_shapes=`echo ${model_info} | awk -F ';' '{print $3}'`
|
model_name=`echo ${model_info} | awk -F ';' '{print $1}'`
|
||||||
extra_info=`echo ${model_info} | awk -F ';' '{print $5}'`
|
input_info=`echo ${model_info} | awk -F ';' '{print $2}'`
|
||||||
input_num=`echo ${input_info} | sed 's/:/;/' | awk -F ';' '{print $1}'`
|
input_shapes=`echo ${model_info} | awk -F ';' '{print $3}'`
|
||||||
input_names=`echo ${input_info} | sed 's/:/;/' | awk -F ';' '{print $2}'`
|
extra_info=`echo ${model_info} | awk -F ';' '{print $5}'`
|
||||||
model_type=${model_name##*.}
|
input_num=`echo ${input_info} | sed 's/:/;/' | awk -F ';' '{print $1}'`
|
||||||
cfg_file_name=${cfg_file##*/}
|
input_names=`echo ${input_info} | sed 's/:/;/' | awk -F ';' '{print $2}'`
|
||||||
quant_config_path="${cfg_file%/*}/quant"
|
model_type=${model_name##*.}
|
||||||
case $model_type in
|
cfg_file_name=${cfg_file##*/}
|
||||||
pb)
|
quant_config_path="${cfg_file%/*}/quant"
|
||||||
model_fmk="TF"
|
case $model_type in
|
||||||
;;
|
pb)
|
||||||
tflite)
|
model_fmk="TF"
|
||||||
model_fmk="TFLITE"
|
;;
|
||||||
;;
|
tflite)
|
||||||
onnx)
|
model_fmk="TFLITE"
|
||||||
model_fmk="ONNX"
|
;;
|
||||||
;;
|
onnx)
|
||||||
mindir)
|
model_fmk="ONNX"
|
||||||
model_fmk="MINDIR"
|
;;
|
||||||
;;
|
mindir)
|
||||||
*)
|
model_fmk="MINDIR"
|
||||||
model_type="caffe"
|
;;
|
||||||
model_fmk="CAFFE"
|
*)
|
||||||
;;
|
model_type="caffe"
|
||||||
esac
|
model_fmk="CAFFE"
|
||||||
# set parameters
|
;;
|
||||||
model_file=$2"/"${model_name}
|
esac
|
||||||
weight_file=""
|
# set parameters
|
||||||
if [[ $model_fmk == "CAFFE" ]]; then
|
model_file=$2"/"${model_name}
|
||||||
model_file=${model_file}".prototxt"
|
weight_file=""
|
||||||
weight_file=${model_file%.*}".caffemodel"
|
if [[ $model_fmk == "CAFFE" ]]; then
|
||||||
fi
|
model_file=${model_file}".prototxt"
|
||||||
output_file=$3"/"${model_name}
|
weight_file=${model_file%.*}".caffemodel"
|
||||||
quant_type=""
|
|
||||||
config_file=""
|
|
||||||
spec_shapes=""
|
|
||||||
train_model="false"
|
|
||||||
in_dtype="DEFAULT"
|
|
||||||
out_dtype="DEFAULT"
|
|
||||||
if [[ ${cfg_file_name} =~ "weightquant" ]]; then
|
|
||||||
# models_weightquant_${suffix}.cfg
|
|
||||||
suffix=${cfg_file_name: 19: -4}
|
|
||||||
quant_type="WeightQuant"
|
|
||||||
output_file=${output_file}"_${suffix}"
|
|
||||||
config_file="${quant_config_path}/weight_quant_${suffix}.cfg"
|
|
||||||
elif [[ ${cfg_file_name} =~ "_train" ]]; then
|
|
||||||
train_model="true"
|
|
||||||
elif [[ ${cfg_file_name} =~ "_ascend" ]]; then
|
|
||||||
model_path=$2
|
|
||||||
option_file="${model_path}/model_option/${model_name}.txt"
|
|
||||||
if [ -f "$option_file" ]; then
|
|
||||||
config_file=${option_file}
|
|
||||||
fi
|
fi
|
||||||
elif [[ ${cfg_file_name} =~ "posttraining" ]]; then
|
output_file=$3"/"${model_name}
|
||||||
quant_type="PostTraining"
|
quant_type=""
|
||||||
output_file=${output_file}"_posttraining"
|
config_file=""
|
||||||
config_file="${quant_config_path}/${model_name}_${cfg_file_name:7:-4}.config"
|
spec_shapes=""
|
||||||
elif [[ ${cfg_file_name} =~ "dynamic_quant" ]]; then
|
train_model="false"
|
||||||
quant_type="DynamicQuant"
|
in_dtype="DEFAULT"
|
||||||
output_file=${output_file}"_dynamic_quant"
|
out_dtype="DEFAULT"
|
||||||
config_file="${quant_config_path}/dynamic_quant.cfg"
|
if [[ ${cfg_file_name} =~ "weightquant" ]]; then
|
||||||
elif [[ ${cfg_file_name} =~ "awaretraining" || ${extra_info} =~ "aware_training" ]]; then
|
# models_weightquant_${suffix}.cfg
|
||||||
in_dtype="FLOAT"
|
suffix=${cfg_file_name: 19: -4}
|
||||||
out_dtype="FLOAT"
|
quant_type="WeightQuant"
|
||||||
fi
|
output_file=${output_file}"_${suffix}"
|
||||||
if [[ ${extra_info} =~ "offline_resize" && ${input_shapes} != "" && ${input_names} != "" ]]; then
|
config_file="${quant_config_path}/weight_quant_${suffix}.cfg"
|
||||||
if [[ ${input_num} == "" ]]; then
|
elif [[ ${cfg_file_name} =~ "_train" ]]; then
|
||||||
input_num=1
|
train_model="true"
|
||||||
|
elif [[ ${cfg_file_name} =~ "_ascend" ]]; then
|
||||||
|
model_path=$2
|
||||||
|
option_file="${model_path}/model_option/${model_name}.txt"
|
||||||
|
if [ -f "$option_file" ]; then
|
||||||
|
config_file=${option_file}
|
||||||
|
fi
|
||||||
|
elif [[ ${cfg_file_name} =~ "posttraining" ]]; then
|
||||||
|
quant_type="PostTraining"
|
||||||
|
output_file=${output_file}"_posttraining"
|
||||||
|
config_file="${quant_config_path}/${model_name}_${cfg_file_name:7:-4}.config"
|
||||||
|
elif [[ ${cfg_file_name} =~ "dynamic_quant" ]]; then
|
||||||
|
quant_type="DynamicQuant"
|
||||||
|
output_file=${output_file}"_dynamic_quant"
|
||||||
|
config_file="${quant_config_path}/dynamic_quant.cfg"
|
||||||
|
elif [[ ${cfg_file_name} =~ "awaretraining" || ${extra_info} =~ "aware_training" ]]; then
|
||||||
|
in_dtype="FLOAT"
|
||||||
|
out_dtype="FLOAT"
|
||||||
fi
|
fi
|
||||||
IFS="," read -r -a name_array <<< ${input_names}
|
if [[ ${extra_info} =~ "offline_resize" && ${input_shapes} != "" && ${input_names} != "" ]]; then
|
||||||
IFS=":" read -r -a shape_array <<< ${input_shapes}
|
if [[ ${input_num} == "" ]]; then
|
||||||
for i in $(seq 0 $((${input_num}-1)))
|
input_num=1
|
||||||
do
|
fi
|
||||||
spec_shapes=${spec_shapes}${name_array[$i]}':'${shape_array[$i]}';'
|
IFS="," read -r -a name_array <<< ${input_names}
|
||||||
done
|
IFS=":" read -r -a shape_array <<< ${input_shapes}
|
||||||
fi
|
for i in $(seq 0 $((${input_num}-1)))
|
||||||
# start running converter
|
do
|
||||||
echo "Convert ${model_name} ${quant_type} ......"
|
spec_shapes=${spec_shapes}${name_array[$i]}':'${shape_array[$i]}';'
|
||||||
echo ${model_name} >> "$4"
|
done
|
||||||
echo './converter_lite --fmk='${model_fmk}' --modelFile='${model_file}' --weightFile='${weight_file}' --outputFile='${output_file}\
|
fi
|
||||||
' --inputDataType='${in_dtype}' --outputDataType='${out_dtype}' --inputShape="'${spec_shapes}'"'\
|
# start running converter
|
||||||
' --configFile='${config_file}' --trainModel='${train_model} >> "$4"
|
echo "Convert ${model_name} ${quant_type} ......"
|
||||||
./converter_lite --fmk=${model_fmk} --modelFile=${model_file} --weightFile=${weight_file} --outputFile=${output_file}\
|
echo ${model_name} >> "$4"
|
||||||
--inputDataType=${in_dtype} --outputDataType=${out_dtype} --inputShape="${spec_shapes}"\
|
echo './converter_lite --fmk='${model_fmk}' --modelFile='${model_file}' --weightFile='${weight_file}' --outputFile='${output_file}\
|
||||||
--configFile=${config_file} --trainModel=${train_model} >> "$4"
|
' --inputDataType='${in_dtype}' --outputDataType='${out_dtype}' --inputShape="'${spec_shapes}'"'\
|
||||||
if [ $? = 0 ]; then
|
' --configFile='${config_file}' --trainModel='${train_model} >> "$4"
|
||||||
converter_result='converter '${model_type}''${quant_type}' '${model_name}' pass';echo ${converter_result} >> $5
|
./converter_lite --fmk=${model_fmk} --modelFile=${model_file} --weightFile=${weight_file} --outputFile=${output_file}\
|
||||||
model_size=`ls ${output_file}.ms -l|awk -F ' ' '{print $5}'`
|
--inputDataType=${in_dtype} --outputDataType=${out_dtype} --inputShape="${spec_shapes}"\
|
||||||
if [[ -n ${calib_size} ]];then
|
--configFile=${config_file} --trainModel=${train_model} >> "$4"
|
||||||
if [ ${model_size} -gt ${calib_size} ]; then
|
if [ $? = 0 ]; then
|
||||||
echo "${output_file}.ms " model size is " ${model_size} " and calib size is " ${calib_size}"
|
converter_result='converter '${model_type}''${quant_type}' '${model_name}' pass';echo ${converter_result} >> $5
|
||||||
converter_result='compare_size '${model_type}''${quant_type}' '${output_file##*/}.ms' failed';echo ${converter_result} >> $5
|
model_size=`ls ${output_file}.ms -l|awk -F ' ' '{print $5}'`
|
||||||
if [[ $6 != "ON" ]]; then
|
if [[ -n ${calib_size} ]];then
|
||||||
return 1
|
if [ ${model_size} -gt ${calib_size} ]; then
|
||||||
|
echo "${output_file}.ms " model size is " ${model_size} " and calib size is " ${calib_size}"
|
||||||
|
converter_result='compare_size '${model_type}''${quant_type}' '${output_file##*/}.ms' failed';echo ${converter_result} >> $5
|
||||||
|
if [[ $6 != "ON" ]]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
converter_result='compare_size '${model_type}''${quant_type}' '${output_file##*/}.ms' pass';echo ${converter_result} >> $5
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
converter_result='compare_size '${model_type}''${quant_type}' '${output_file##*/}.ms' pass';echo ${converter_result} >> $5
|
|
||||||
fi
|
fi
|
||||||
fi
|
else
|
||||||
else
|
converter_result='converter '${model_type}''${quant_type}' '${model_name}' failed';echo ${converter_result} >> $5
|
||||||
converter_result='converter '${model_type}''${quant_type}' '${model_name}' failed';echo ${converter_result} >> $5
|
if [[ $6 != "ON" ]]; then
|
||||||
if [[ $6 != "ON" ]]; then
|
return 1
|
||||||
return 1
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
echo >&6
|
||||||
|
} &
|
||||||
done < ${cfg_file}
|
done < ${cfg_file}
|
||||||
done
|
done
|
||||||
|
wait
|
||||||
|
exec 6>&-
|
||||||
}
|
}
|
||||||
|
|
||||||
function Push_Files() {
|
function Push_Files() {
|
||||||
|
|
Loading…
Reference in New Issue