From 2e8115d3c5a9c391fc9c850c65ea5a5f45922e27 Mon Sep 17 00:00:00 2001 From: duanyanhui <45005871+YanhuiDua@users.noreply.github.com> Date: Thu, 29 Sep 2022 10:48:22 +0800 Subject: [PATCH] [TIPC] Add scripts for npu and xpu (#7020) * add scritps for npu and xpu * rm slim_config in config * rm enable lit egine for npu and xpu * add lite_engine_enabled --- deploy/python/infer.py | 11 ++- test_tipc/test_train_inference_python_npu.sh | 71 ++++++++++++++++++ test_tipc/test_train_inference_python_xpu.sh | 79 ++++++++++++++++++++ tools/anchor_cluster.py | 2 + tools/eval.py | 3 + tools/eval_mot.py | 3 + tools/export_model.py | 2 + tools/infer.py | 3 + tools/infer_mot.py | 3 + tools/post_quant.py | 2 + tools/train.py | 4 + 11 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 test_tipc/test_train_inference_python_npu.sh create mode 100644 test_tipc/test_train_inference_python_xpu.sh diff --git a/deploy/python/infer.py b/deploy/python/infer.py index 1af9d0ca3..176c2844b 100644 --- a/deploy/python/infer.py +++ b/deploy/python/infer.py @@ -825,8 +825,13 @@ def load_predictor(model_dir, # optimize graph and fuse op config.switch_ir_optim(True) elif device == 'XPU': - config.enable_lite_engine() + if config.lite_engine_enabled(): + config.enable_lite_engine() config.enable_xpu(10 * 1024 * 1024) + elif device == 'NPU': + if config.lite_engine_enabled(): + config.enable_lite_engine() + config.enable_npu() else: config.disable_gpu() config.set_cpu_math_library_num_threads(cpu_threads) @@ -1026,8 +1031,8 @@ if __name__ == '__main__': FLAGS = parser.parse_args() print_arguments(FLAGS) FLAGS.device = FLAGS.device.upper() - assert FLAGS.device in ['CPU', 'GPU', 'XPU' - ], "device should be CPU, GPU or XPU" + assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU' + ], "device should be CPU, GPU, XPU or NPU" assert not FLAGS.use_gpu, "use_gpu has been deprecated, please use --device" assert not ( diff --git a/test_tipc/test_train_inference_python_npu.sh b/test_tipc/test_train_inference_python_npu.sh new file mode 100644 index 000000000..5b51ac7ac --- /dev/null +++ b/test_tipc/test_train_inference_python_npu.sh @@ -0,0 +1,71 @@ +#!/bin/bash +source test_tipc/utils_func.sh +function readlinkf() { + perl -MCwd -e 'print Cwd::abs_path shift' "$1"; +} +function func_parser_config() { + strs=$1 + IFS=" " + array=(${strs}) + tmp=${array[2]} + echo ${tmp} +} +function func_parser_dir() { + strs=$1 + IFS="/" + array=(${strs}) + len=${#array[*]} + dir="" + count=1 + for arr in ${array[*]}; do + if [ ${len} = "${count}" ]; then + continue; + else + dir="${dir}/${arr}" + count=$((${count} + 1)) + fi + done + echo "${dir}" +} +BASEDIR=$(dirname "$0") +REPO_ROOT_PATH=$(readlinkf ${BASEDIR}/../) +FILENAME=$1 + # change gpu to npu in tipc txt configs + sed -i "s/use_gpu:True/use_npu:True/g" $FILENAME + sed -i "s/--device:gpu|cpu/--device:npu|cpu/g" $FILENAME + sed -i "s/trainer:pact_train/trainer:norm_train/g" $FILENAME + sed -i "s/trainer:fpgm_train/trainer:norm_train/g" $FILENAME + sed -i "s/--slim_config _template_pact/ /g" $FILENAME + sed -i "s/--slim_config _template_fpgm/ /g" $FILENAME + sed -i "s/--slim_config _template_kl_quant/ /g" $FILENAME + sed -i 's/\"gpu\"/\"npu\"/g' test_tipc/test_train_inference_python.sh + + # parser params +dataline=`cat $FILENAME` +IFS=$'\n' +lines=(${dataline}) +# replace training config file +grep -n '.yml' $FILENAME | cut -d ":" -f 1 \ +| while read line_num ; do + train_cmd=$(func_parser_value "${lines[line_num-1]}") + trainer_config=$(func_parser_config ${train_cmd}) + echo ${trainer_config} + sed -i 's/use_gpu/use_npu/g' "$REPO_ROOT_PATH/$trainer_config" + # fine use_gpu in those included yaml + sub_datalinee=`cat $REPO_ROOT_PATH/$trainer_config` + IFS=$'\n' + sub_lines=(${sub_datalinee}) + grep -n '.yml' "$REPO_ROOT_PATH/$trainer_config" | cut -d ":" -f 1 \ + | while read sub_line_num; do + sub_config=${sub_lines[sub_line_num-1]} + dst=${#sub_config}-5 + sub_path=$(func_parser_dir "${trainer_config}") + sub_config_path="${REPO_ROOT_PATH}${sub_path}/${sub_config:3:${dst}}" + echo ${sub_config_path} + sed -i 's/use_gpu/use_npu/g' "$sub_config_path" + done +done +# pass parameters to test_train_inference_python.sh +cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} $2" +echo $cmd +eval $cmd diff --git a/test_tipc/test_train_inference_python_xpu.sh b/test_tipc/test_train_inference_python_xpu.sh new file mode 100644 index 000000000..b020377f1 --- /dev/null +++ b/test_tipc/test_train_inference_python_xpu.sh @@ -0,0 +1,79 @@ +#!/bin/bash + source test_tipc/utils_func.sh + + function readlinkf() { + perl -MCwd -e 'print Cwd::abs_path shift' "$1"; + } + + function func_parser_config() { + strs=$1 + IFS=" " + array=(${strs}) + tmp=${array[2]} + echo ${tmp} + } + + function func_parser_dir() { + strs=$1 + IFS="/" + array=(${strs}) + len=${#array[*]} + dir="" + count=1 + for arr in ${array[*]}; do + if [ ${len} = "${count}" ]; then + continue; + else + dir="${dir}/${arr}" + count=$((${count} + 1)) + fi + done + echo "${dir}" + } + + BASEDIR=$(dirname "$0") + REPO_ROOT_PATH=$(readlinkf ${BASEDIR}/../) + + FILENAME=$1 + + # change gpu to xpu in tipc txt configs + sed -i "s/use_gpu:True/use_xpu:True/g" $FILENAME + sed -i "s/--device:gpu|cpu/--device:xpu|cpu/g" $FILENAME + sed -i "s/trainer:pact_train/trainer:norm_train/g" $FILENAME + sed -i "s/trainer:fpgm_train/trainer:norm_train/g" $FILENAME + sed -i "s/--slim_config _template_pact/ /g" $FILENAME + sed -i "s/--slim_config _template_fpgm/ /g" $FILENAME + sed -i "s/--slim_config _template_kl_quant/ /g" $FILENAME + sed -i 's/\"gpu\"/\"xpu\"/g' test_tipc/test_train_inference_python.sh + + # parser params + dataline=`cat $FILENAME` + IFS=$'\n' + lines=(${dataline}) + + # replace training config file + grep -n '.yml' $FILENAME | cut -d ":" -f 1 \ + | while read line_num ; do + train_cmd=$(func_parser_value "${lines[line_num-1]}") + trainer_config=$(func_parser_config ${train_cmd}) + echo ${trainer_config} + sed -i 's/use_gpu/use_xpu/g' "$REPO_ROOT_PATH/$trainer_config" + # fine use_gpu in those included yaml + sub_datalinee=`cat $REPO_ROOT_PATH/$trainer_config` + IFS=$'\n' + sub_lines=(${sub_datalinee}) + grep -n '.yml' "$REPO_ROOT_PATH/$trainer_config" | cut -d ":" -f 1 \ + | while read sub_line_num; do + sub_config=${sub_lines[sub_line_num-1]} + dst=${#sub_config}-5 + sub_path=$(func_parser_dir "${trainer_config}") + sub_config_path="${REPO_ROOT_PATH}${sub_path}/${sub_config:3:${dst}}" + echo ${sub_config_path} + sed -i 's/use_gpu/use_xpu/g' "$sub_config_path" + done + done + + # pass parameters to test_train_inference_python.sh + cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} $2" + echo $cmd + eval $cmd \ No newline at end of file diff --git a/tools/anchor_cluster.py b/tools/anchor_cluster.py index 5d9702852..adfd869f0 100644 --- a/tools/anchor_cluster.py +++ b/tools/anchor_cluster.py @@ -215,6 +215,8 @@ def main(): merge_config(FLAGS.opt) check_config(cfg) # check if set use_gpu=True in paddlepaddle cpu version + if 'use_gpu' not in cfg: + cfg.use_gpu = False check_gpu(cfg.use_gpu) # check if paddlepaddle version is satisfied check_version('develop') diff --git a/tools/eval.py b/tools/eval.py index 42a2fcafa..38ac7f9ac 100755 --- a/tools/eval.py +++ b/tools/eval.py @@ -168,6 +168,9 @@ def main(): if 'use_xpu' not in cfg: cfg.use_xpu = False + if 'use_gpu' not in cfg: + cfg.use_gpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: diff --git a/tools/eval_mot.py b/tools/eval_mot.py index a9ca51703..49d7d2850 100644 --- a/tools/eval_mot.py +++ b/tools/eval_mot.py @@ -112,6 +112,9 @@ def main(): if 'use_xpu' not in cfg: cfg.use_xpu = False + if 'use_gpu' not in cfg: + cfg.use_gpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: diff --git a/tools/export_model.py b/tools/export_model.py index 1eaac7a76..b239060eb 100644 --- a/tools/export_model.py +++ b/tools/export_model.py @@ -98,6 +98,8 @@ def main(): # FIXME: Temporarily solve the priority problem of FLAGS.opt merge_config(FLAGS.opt) check_config(cfg) + if 'use_gpu' not in cfg: + cfg.use_gpu = False check_gpu(cfg.use_gpu) check_version() diff --git a/tools/infer.py b/tools/infer.py index d9bf3166e..d1e9ed713 100755 --- a/tools/infer.py +++ b/tools/infer.py @@ -201,6 +201,9 @@ def main(): if 'use_xpu' not in cfg: cfg.use_xpu = False + if 'use_gpu' not in cfg: + cfg.use_gpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: diff --git a/tools/infer_mot.py b/tools/infer_mot.py index ef13bff93..8d54a4dcb 100644 --- a/tools/infer_mot.py +++ b/tools/infer_mot.py @@ -124,6 +124,9 @@ def main(): if 'use_xpu' not in cfg: cfg.use_xpu = False + if 'use_gpu' not in cfg: + cfg.use_gpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: diff --git a/tools/post_quant.py b/tools/post_quant.py index fe6f9c79b..0c3404007 100644 --- a/tools/post_quant.py +++ b/tools/post_quant.py @@ -86,6 +86,8 @@ def main(): # FIXME: Temporarily solve the priority problem of FLAGS.opt merge_config(FLAGS.opt) check_config(cfg) + if 'use_gpu' not in cfg: + cfg.use_gpu = False check_gpu(cfg.use_gpu) check_version() diff --git a/tools/train.py b/tools/train.py index 43f883592..289f292c9 100755 --- a/tools/train.py +++ b/tools/train.py @@ -146,6 +146,9 @@ def main(): if 'use_xpu' not in cfg: cfg.use_xpu = False + if 'use_gpu' not in cfg: + cfg.use_gpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: @@ -163,6 +166,7 @@ def main(): check.check_config(cfg) check.check_gpu(cfg.use_gpu) check.check_npu(cfg.use_npu) + check.check_xpu(cfg.use_xpu) check.check_version() run(FLAGS, cfg) -- GitLab