Skip to content

Commit 3abb358

Browse files
authored
[TIPC] Add scripts for npu and xpu, test=develop (#3377)
* 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
1 parent be4b6c2 commit 3abb358

File tree

16 files changed

+221
-15
lines changed

16 files changed

+221
-15
lines changed

examples/language_model/gpt-3/dygraph/args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def parse_args(MODEL_CLASSES):
286286
parser.add_argument("--device",
287287
type=str,
288288
default="gpu",
289-
choices=["cpu", "gpu", "xpu"],
289+
choices=["cpu", "gpu", "xpu", "npu"],
290290
help="select cpu, gpu, xpu devices.")
291291
parser.add_argument("--lr_decay_style",
292292
type=str,

examples/machine_translation/transformer/deploy/python/inference.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import os
216
import sys
317

@@ -28,7 +42,7 @@ def parse_args():
2842
parser.add_argument("--device",
2943
default="gpu",
3044
type=str,
31-
choices=["gpu", "xpu", "cpu"],
45+
choices=["gpu", "xpu", "cpu", "npu"],
3246
help="Device to use during inference. ")
3347
parser.add_argument("--use_mkl",
3448
default=False,
@@ -131,7 +145,9 @@ def create_predictor(cls,
131145
if args.device == "gpu":
132146
config.enable_use_gpu(100, 0)
133147
elif args.device == "xpu":
134-
config.enable_xpu(100)
148+
config.enable_xpu()
149+
elif args.device == "npu":
150+
config.enable_npu()
135151
else:
136152
# CPU
137153
config.disable_gpu()

examples/machine_translation/transformer/predict.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import os
216
import yaml
317
import logging
@@ -60,6 +74,11 @@ def parse_args():
6074
type=str,
6175
help="The eos token. It should be provided when use custom vocab_file. "
6276
)
77+
parser.add_argument("--device",
78+
default="gpu",
79+
choices=["gpu", "cpu", "xpu", "npu"],
80+
help="Device selected for inference.")
81+
6382
args = parser.parse_args()
6483
return args
6584

@@ -83,6 +102,10 @@ def post_process_seq(seq, bos_idx, eos_idx, output_bos=False, output_eos=False):
83102
def do_predict(args):
84103
if args.device == "gpu":
85104
place = "gpu"
105+
elif args.device == "xpu":
106+
place = "xpu"
107+
elif args.device == "npu":
108+
place = "npu"
86109
else:
87110
place = "cpu"
88111

@@ -157,6 +180,7 @@ def do_predict(args):
157180
args.unk_token = ARGS.unk_token
158181
args.bos_token = ARGS.bos_token
159182
args.eos_token = ARGS.eos_token
183+
args.device = ARGS.device
160184
pprint(args)
161185

162186
do_predict(args)

examples/machine_translation/transformer/train.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ def parse_args():
100100
type=str,
101101
choices=['true', 'false', 'True', 'False'],
102102
help="Whether to use amp to train Transformer. ")
103+
parser.add_argument("--device",
104+
default="gpu",
105+
choices=["gpu", "cpu", "xpu", "npu"],
106+
help="Device selected for inference.")
103107
parser.add_argument(
104108
"--amp_level",
105109
default=None,
@@ -126,6 +130,14 @@ def do_train(args):
126130
if args.device == "gpu":
127131
rank = dist.get_rank()
128132
trainer_count = dist.get_world_size()
133+
elif args.device == "npu":
134+
rank = dist.get_rank()
135+
trainer_count = dist.get_world_size()
136+
paddle.set_device("npu")
137+
elif args.device == "xpu":
138+
rank = dist.get_rank()
139+
trainer_count = dist.get_world_size()
140+
paddle.set_device("xpu")
129141
else:
130142
rank = 0
131143
trainer_count = 1
@@ -401,6 +413,7 @@ def do_train(args):
401413
args.bos_token = ARGS.bos_token
402414
args.eos_token = ARGS.eos_token
403415
args.to_static = ARGS.to_static
416+
args.device = ARGS.device
404417
pprint(args)
405418

406419
args.profiler_options = ARGS.profiler_options

model_zoo/bert/run_pretrain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def parse_args():
150150
parser.add_argument("--device",
151151
type=str,
152152
default="gpu",
153-
choices=["cpu", "gpu", "xpu"],
153+
choices=["cpu", "gpu", "xpu", "npu"],
154154
help="Device for selecting for the training.")
155155
parser.add_argument("--use_amp",
156156
type=distutils.util.strtobool,

model_zoo/gpt/args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def parse_args(MODEL_CLASSES):
241241
parser.add_argument("--device",
242242
type=str,
243243
default="gpu",
244-
choices=["cpu", "gpu", "xpu"],
244+
choices=["cpu", "gpu", "xpu", "npu"],
245245
help="select cpu, gpu, xpu devices.")
246246
parser.add_argument("--lr_decay_style",
247247
type=str,

tests/test_tipc/bigru_crf/deploy/predict.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
parser.add_argument("--data_dir", type=str, default=None, help="The folder where the dataset is located.")
2929
parser.add_argument("--batch_size", type=int, default=2, help="The number of sequences contained in a mini-batch.")
3030
parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.")
31-
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.")
31+
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.")
3232
parser.add_argument("--benchmark", type=eval, default=False, help="To log some information about environment and running.")
3333
parser.add_argument("--save_log_path", type=str, default="./log_output/", help="The file path to save log.")
3434
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
@@ -202,7 +202,10 @@ def __init__(self,
202202
config.set_cpu_math_library_num_threads(args.cpu_threads)
203203
elif device == "xpu":
204204
# set XPU configs accordingly
205-
config.enable_xpu(100)
205+
config.enable_xpu()
206+
elif device == "npu":
207+
# set NPU configs accordingly
208+
config.enable_npu()
206209
config.switch_use_feed_fetch_ops(False)
207210
self.predictor = paddle.inference.create_predictor(config)
208211

tests/test_tipc/ernie_information_extraction/predict.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ def __init__(self,
166166
config.set_cpu_math_library_num_threads(cpu_threads)
167167
elif device == "xpu":
168168
# set XPU configs accordingly
169-
config.enable_xpu(100)
169+
config.enable_xpu()
170+
elif device == "npu":
171+
# set NPU configs accordingly
172+
config.enable_npu()
170173

171174
config.switch_use_feed_fetch_ops(False)
172175
self.predictor = paddle.inference.create_predictor(config)
@@ -250,7 +253,7 @@ def predict(self,
250253
parser.add_argument("--model_dir", type=str, default='./output', help="The path to parameters in static graph.")
251254
parser.add_argument("--data_dir", type=str, default="./waybill_ie/data", help="The folder where the dataset is located.")
252255
parser.add_argument("--batch_size", type=int, default=32, help="The number of sequences contained in a mini-batch.")
253-
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.")
256+
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.")
254257
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
255258
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"], help='The tensorrt precision.')
256259
parser.add_argument('--cpu_threads', default=10, type=int, help='Number of threads to predict when using cpu.')

tests/test_tipc/ernie_information_extraction/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def do_train(args):
208208
parser.add_argument("--save_dir", default='./checkpoint', type=str, help="The output directory where the model checkpoints will be written.")
209209
parser.add_argument("--epochs", default=10, type=int, help="Total number of training epochs to perform.")
210210
parser.add_argument("--batch_size", default=200, type=int, help="Batch size per GPU/CPU for training.")
211-
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.")
211+
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.")
212212
parser.add_argument("--seed", type=int, default=1000, help="Random seed for initialization.")
213213
parser.add_argument("--max_steps", default=-1, type=int, help="If > 0: set total number of training steps to perform.")
214214
parser.add_argument("--data_dir", default='./waybill_ie/data', type=str, help="The folder where the dataset is located.")

tests/test_tipc/ernie_text_cls/predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def predict(self, data, tokenizer, label_map):
222222
parser.add_argument("--model_dir", type=str, required=True, help="The directory to static model.")
223223
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.")
224224
parser.add_argument("--batch_size", default=2, type=int, help="Batch size per GPU/CPU for training.")
225-
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
225+
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu', 'npu'], default="gpu", help="Select which device to train model, defaults to gpu.")
226226
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
227227
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"], help='The tensorrt precision.')
228228
parser.add_argument('--cpu_threads', default=10, type=int, help='Number of threads to predict when using cpu.')

0 commit comments

Comments
 (0)