Skip to content

Commit

Permalink
[TIPC] Add scripts for npu and xpu, test=develop (#3377)
Browse files Browse the repository at this point in the history
* add scripts for xpu and npu

* add npu/xpu args

* add script for xpu

* add npu/xpu args to predict.py

* fix codestyle ci bug

* add copyright

* fix copyright_checker
  • Loading branch information
YanhuiDua authored Sep 29, 2022
1 parent be4b6c2 commit 3abb358
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/language_model/gpt-3/dygraph/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def parse_args(MODEL_CLASSES):
parser.add_argument("--device",
type=str,
default="gpu",
choices=["cpu", "gpu", "xpu"],
choices=["cpu", "gpu", "xpu", "npu"],
help="select cpu, gpu, xpu devices.")
parser.add_argument("--lr_decay_style",
type=str,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

Expand Down Expand Up @@ -28,7 +42,7 @@ def parse_args():
parser.add_argument("--device",
default="gpu",
type=str,
choices=["gpu", "xpu", "cpu"],
choices=["gpu", "xpu", "cpu", "npu"],
help="Device to use during inference. ")
parser.add_argument("--use_mkl",
default=False,
Expand Down Expand Up @@ -131,7 +145,9 @@ def create_predictor(cls,
if args.device == "gpu":
config.enable_use_gpu(100, 0)
elif args.device == "xpu":
config.enable_xpu(100)
config.enable_xpu()
elif args.device == "npu":
config.enable_npu()
else:
# CPU
config.disable_gpu()
Expand Down
24 changes: 24 additions & 0 deletions examples/machine_translation/transformer/predict.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import yaml
import logging
Expand Down Expand Up @@ -60,6 +74,11 @@ def parse_args():
type=str,
help="The eos token. It should be provided when use custom vocab_file. "
)
parser.add_argument("--device",
default="gpu",
choices=["gpu", "cpu", "xpu", "npu"],
help="Device selected for inference.")

args = parser.parse_args()
return args

Expand All @@ -83,6 +102,10 @@ def post_process_seq(seq, bos_idx, eos_idx, output_bos=False, output_eos=False):
def do_predict(args):
if args.device == "gpu":
place = "gpu"
elif args.device == "xpu":
place = "xpu"
elif args.device == "npu":
place = "npu"
else:
place = "cpu"

Expand Down Expand Up @@ -157,6 +180,7 @@ def do_predict(args):
args.unk_token = ARGS.unk_token
args.bos_token = ARGS.bos_token
args.eos_token = ARGS.eos_token
args.device = ARGS.device
pprint(args)

do_predict(args)
13 changes: 13 additions & 0 deletions examples/machine_translation/transformer/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def parse_args():
type=str,
choices=['true', 'false', 'True', 'False'],
help="Whether to use amp to train Transformer. ")
parser.add_argument("--device",
default="gpu",
choices=["gpu", "cpu", "xpu", "npu"],
help="Device selected for inference.")
parser.add_argument(
"--amp_level",
default=None,
Expand All @@ -126,6 +130,14 @@ def do_train(args):
if args.device == "gpu":
rank = dist.get_rank()
trainer_count = dist.get_world_size()
elif args.device == "npu":
rank = dist.get_rank()
trainer_count = dist.get_world_size()
paddle.set_device("npu")
elif args.device == "xpu":
rank = dist.get_rank()
trainer_count = dist.get_world_size()
paddle.set_device("xpu")
else:
rank = 0
trainer_count = 1
Expand Down Expand Up @@ -401,6 +413,7 @@ def do_train(args):
args.bos_token = ARGS.bos_token
args.eos_token = ARGS.eos_token
args.to_static = ARGS.to_static
args.device = ARGS.device
pprint(args)

args.profiler_options = ARGS.profiler_options
Expand Down
2 changes: 1 addition & 1 deletion model_zoo/bert/run_pretrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def parse_args():
parser.add_argument("--device",
type=str,
default="gpu",
choices=["cpu", "gpu", "xpu"],
choices=["cpu", "gpu", "xpu", "npu"],
help="Device for selecting for the training.")
parser.add_argument("--use_amp",
type=distutils.util.strtobool,
Expand Down
2 changes: 1 addition & 1 deletion model_zoo/gpt/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def parse_args(MODEL_CLASSES):
parser.add_argument("--device",
type=str,
default="gpu",
choices=["cpu", "gpu", "xpu"],
choices=["cpu", "gpu", "xpu", "npu"],
help="select cpu, gpu, xpu devices.")
parser.add_argument("--lr_decay_style",
type=str,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_tipc/bigru_crf/deploy/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
parser.add_argument("--data_dir", type=str, default=None, help="The folder where the dataset is located.")
parser.add_argument("--batch_size", type=int, default=2, help="The number of sequences contained in a mini-batch.")
parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.")
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu"] ,help="The device to select to train the model, is must be cpu/gpu.")
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "npu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu.")
parser.add_argument("--benchmark", type=eval, default=False, help="To log some information about environment and running.")
parser.add_argument("--save_log_path", type=str, default="./log_output/", help="The file path to save log.")
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
Expand Down Expand Up @@ -202,7 +202,10 @@ def __init__(self,
config.set_cpu_math_library_num_threads(args.cpu_threads)
elif device == "xpu":
# set XPU configs accordingly
config.enable_xpu(100)
config.enable_xpu()
elif device == "npu":
# set NPU configs accordingly
config.enable_npu()
config.switch_use_feed_fetch_ops(False)
self.predictor = paddle.inference.create_predictor(config)

Expand Down
7 changes: 5 additions & 2 deletions tests/test_tipc/ernie_information_extraction/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def __init__(self,
config.set_cpu_math_library_num_threads(cpu_threads)
elif device == "xpu":
# set XPU configs accordingly
config.enable_xpu(100)
config.enable_xpu()
elif device == "npu":
# set NPU configs accordingly
config.enable_npu()

config.switch_use_feed_fetch_ops(False)
self.predictor = paddle.inference.create_predictor(config)
Expand Down Expand Up @@ -250,7 +253,7 @@ def predict(self,
parser.add_argument("--model_dir", type=str, default='./output', help="The path to parameters in static graph.")
parser.add_argument("--data_dir", type=str, default="./waybill_ie/data", help="The folder where the dataset is located.")
parser.add_argument("--batch_size", type=int, default=32, help="The number of sequences contained in a mini-batch.")
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu"] ,help="The device to select to train the model, is must be cpu/gpu.")
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "npu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu.")
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"], help='The tensorrt precision.')
parser.add_argument('--cpu_threads', default=10, type=int, help='Number of threads to predict when using cpu.')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tipc/ernie_information_extraction/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def do_train(args):
parser.add_argument("--save_dir", default='./checkpoint', type=str, help="The output directory where the model checkpoints will be written.")
parser.add_argument("--epochs", default=10, type=int, help="Total number of training epochs to perform.")
parser.add_argument("--batch_size", default=200, type=int, help="Batch size per GPU/CPU for training.")
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu"] ,help="The device to select to train the model, is must be cpu/gpu.")
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "npu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu.")
parser.add_argument("--seed", type=int, default=1000, help="Random seed for initialization.")
parser.add_argument("--max_steps", default=-1, type=int, help="If > 0: set total number of training steps to perform.")
parser.add_argument("--data_dir", default='./waybill_ie/data', type=str, help="The folder where the dataset is located.")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tipc/ernie_text_cls/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def predict(self, data, tokenizer, label_map):
parser.add_argument("--model_dir", type=str, required=True, help="The directory to static model.")
parser.add_argument("--max_seq_length", default=128, type=int, help="The maximum total input sequence length after tokenization. Sequences longer than this will be truncated, sequences shorter will be padded.")
parser.add_argument("--batch_size", default=2, type=int, help="Batch size per GPU/CPU for training.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu', 'npu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"], help='The tensorrt precision.')
parser.add_argument('--cpu_threads', default=10, type=int, help='Number of threads to predict when using cpu.')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tipc/ernie_text_cls/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def do_train(args):
parser.add_argument("--init_from_ckpt", type=str, default=None, help="The path of checkpoint to be loaded.")
parser.add_argument("--seed", type=int, default=1000, help="random seed for initialization")
parser.add_argument("--max_steps", default=-1, type=int, help="If > 0: set total number of training steps to perform.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu', 'npu'], default="gpu", help="Select which device to train model, defaults to gpu.")
args = parser.parse_args()
# yapf: enable

Expand Down
7 changes: 5 additions & 2 deletions tests/test_tipc/ernie_text_matching/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ def __init__(self,
config.set_cpu_math_library_num_threads(cpu_threads)
elif device == "xpu":
# set XPU configs accordingly
config.enable_xpu(100)
config.enable_xpu()
elif device == "npu":
# set NPU configs accordingly
config.enable_npu()

config.switch_use_feed_fetch_ops(False)
self.predictor = paddle.inference.create_predictor(config)
Expand Down Expand Up @@ -183,7 +186,7 @@ def predict(self, data, tokenizer, label_map):
parser.add_argument("--model_dir", type=str, required=True, help="The directory to static model.")
parser.add_argument("--max_seq_length", default=128, type=int, help="The maximum total input sequence length after tokenization. Sequences longer than this will be truncated, sequences shorter will be padded.")
parser.add_argument("--batch_size", default=32, type=int, help="Batch size per GPU/CPU for training.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu', 'npu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"], help='The tensorrt precision.')
parser.add_argument('--cpu_threads', default=10, type=int, help='Number of threads to predict when using cpu.')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tipc/ernie_text_matching/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def do_train(args):
parser.add_argument("--init_from_ckpt", type=str, default=None, help="The path of checkpoint to be loaded.")
parser.add_argument("--seed", type=int, default=1000, help="Random seed for initialization.")
parser.add_argument("--max_steps", default=-1, type=int, help="If > 0: set total number of training steps to perform.")
parser.add_argument('--device', choices=['cpu', 'gpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'npu', 'xpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
args = parser.parse_args()
# yapf: enable
do_train(args)
58 changes: 58 additions & 0 deletions tests/test_tipc/test_train_inference_python_npu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


source test_tipc/common_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}
}

BASEDIR=$(dirname "$0")
REPO_ROOT_PATH=$(readlinkf ${BASEDIR}/../)

FILENAME=$1

# change gpu to npu in tipc txt configs
sed -i "s/--device:gpu/--device:npu/g" $FILENAME
sed -i "s/state=GPU/state=NPU/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/--device:cpu|gpu/--device:cpu|npu/g" $FILENAME
sed -i "s/--device:gpu|cpu/--device:cpu|npu/g" $FILENAME
sed -i "s/--benchmark:True/--benchmark:False/g" $FILENAME
sed -i "s/--use_tensorrt:False|True/--use_tensorrt:False/g" $FILENAME
sed -i 's/\"gpu\"/\"npu\"/g' test_tipc/test_train_inference_python.sh

# parser params
dataline=`cat $FILENAME`
IFS=$'\n'
lines=(${dataline})

# pass parameters to test_train_inference_python.sh
cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} $2"
echo $cmd
eval $cmd


58 changes: 58 additions & 0 deletions tests/test_tipc/test_train_inference_python_xpu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


source test_tipc/common_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}
}

BASEDIR=$(dirname "$0")
REPO_ROOT_PATH=$(readlinkf ${BASEDIR}/../)

FILENAME=$1

# change gpu to npu in tipc txt configs
sed -i "s/--device:gpu/--device:xpu/g" $FILENAME
sed -i "s/state=GPU/state=XPU/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/--device:cpu|gpu/--device:cpu|xpu/g" $FILENAME
sed -i "s/--device:gpu|cpu/--device:cpu|xpu/g" $FILENAME
sed -i "s/--benchmark:True/--benchmark:False/g" $FILENAME
sed -i "s/--use_tensorrt:False|True/--use_tensorrt:False/g" $FILENAME
sed -i 's/\"gpu\"/\"npu\"/g' test_tipc/test_train_inference_python.sh

# parser params
dataline=`cat $FILENAME`
IFS=$'\n'
lines=(${dataline})

# pass parameters to test_train_inference_python.sh
cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} $2"
echo $cmd
eval $cmd


Loading

0 comments on commit 3abb358

Please sign in to comment.