Skip to content

Commit 6042fcd

Browse files
authored
[npu-tipc] fix npu tipc (PaddlePaddle#8196)
* add npu inference support * change aligned=false for npu * fix typo
1 parent d0076cb commit 6042fcd

16 files changed

+63
-57
lines changed

deploy/pipeline/pipeline.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ def main():
13151315
parser = argsparser()
13161316
FLAGS = parser.parse_args()
13171317
FLAGS.device = FLAGS.device.upper()
1318-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
1319-
], "device should be CPU, GPU or XPU"
1318+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
1319+
], "device should be CPU, GPU, XPU or NPU"
13201320

13211321
main()

deploy/pipeline/pphuman/action_infer.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SkeletonActionRecognizer(Detector):
4141
"""
4242
Args:
4343
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
44-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
44+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
4545
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
4646
batch_size (int): size of pre batch in inference
4747
trt_min_shape (int): min shape for dynamic shape in trt
@@ -285,7 +285,7 @@ class DetActionRecognizer(object):
285285
"""
286286
Args:
287287
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
288-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
288+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
289289
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
290290
batch_size (int): size of pre batch in inference
291291
trt_min_shape (int): min shape for dynamic shape in trt
@@ -454,7 +454,7 @@ class ClsActionRecognizer(AttrDetector):
454454
"""
455455
Args:
456456
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
457-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
457+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
458458
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
459459
batch_size (int): size of pre batch in inference
460460
trt_min_shape (int): min shape for dynamic shape in trt
@@ -684,8 +684,8 @@ def main():
684684
FLAGS = parser.parse_args()
685685
print_arguments(FLAGS)
686686
FLAGS.device = FLAGS.device.upper()
687-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
688-
], "device should be CPU, GPU or XPU"
687+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
688+
], "device should be CPU, GPU, NPU or XPU"
689689
assert not FLAGS.use_gpu, "use_gpu has been deprecated, please use --device"
690690

691691
main()

deploy/pipeline/pphuman/attr_infer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AttrDetector(Detector):
4242
"""
4343
Args:
4444
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
45-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
45+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
4646
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
4747
batch_size (int): size of pre batch in inference
4848
trt_min_shape (int): min shape for dynamic shape in trt
@@ -341,8 +341,8 @@ def main():
341341
FLAGS = parser.parse_args()
342342
print_arguments(FLAGS)
343343
FLAGS.device = FLAGS.device.upper()
344-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
345-
], "device should be CPU, GPU or XPU"
344+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
345+
], "device should be CPU, GPU, XPU or NPU"
346346
assert not FLAGS.use_gpu, "use_gpu has been deprecated, please use --device"
347347

348348
main()

deploy/pipeline/pphuman/reid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ReID(object):
3232
Args:
3333
pred_config (object): config of model, defined by `Config(model_dir)`
3434
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
35-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
35+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
3636
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
3737
batch_size (int): size of per batch in inference, default 50 means at most
3838
50 sub images can be made a batch and send into ReID model

deploy/pipeline/pphuman/video_action_infer.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class VideoActionRecognizer(object):
4747
"""
4848
Args:
4949
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
50-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
50+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
5151
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
5252
batch_size (int): size of pre batch in inference
5353
trt_min_shape (int): min shape for dynamic shape in trt
@@ -105,6 +105,10 @@ def __init__(self,
105105

106106
if device == "GPU" or device == "gpu":
107107
self.config.enable_use_gpu(8000, 0)
108+
elif device == "XPU" or device == "xpu":
109+
self.config.enable_xpu(10 * 1024 * 1024)
110+
elif device == "NPU" or device == "npu":
111+
self.config.enable_custom_device('npu')
108112
else:
109113
self.config.disable_gpu()
110114
if self.enable_mkldnn:
@@ -308,7 +312,7 @@ def main():
308312
FLAGS = parser.parse_args()
309313
print_arguments(FLAGS)
310314
FLAGS.device = FLAGS.device.upper()
311-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
312-
], "device should be CPU, GPU or XPU"
315+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
316+
], "device should be CPU, GPU, XPU or NPU"
313317

314318
main()

deploy/pipeline/ppvehicle/vehicle_attr.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class VehicleAttr(AttrDetector):
4141
"""
4242
Args:
4343
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
44-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
44+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
4545
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
4646
batch_size (int): size of pre batch in inference
4747
trt_min_shape (int): min shape for dynamic shape in trt
@@ -143,8 +143,8 @@ def postprocess(self, inputs, result):
143143
FLAGS = parser.parse_args()
144144
print_arguments(FLAGS)
145145
FLAGS.device = FLAGS.device.upper()
146-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
147-
], "device should be CPU, GPU or XPU"
146+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
147+
], "device should be CPU, GPU, NPU or XPU"
148148
assert not FLAGS.use_gpu, "use_gpu has been deprecated, please use --device"
149149

150150
main()

deploy/pipeline/ppvehicle/vehicle_plate.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def main():
325325
parser = argsparser()
326326
FLAGS = parser.parse_args()
327327
FLAGS.device = FLAGS.device.upper()
328-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
329-
], "device should be CPU, GPU or XPU"
328+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
329+
], "device should be CPU, GPU, NPU or XPU"
330330

331331
main()

deploy/pptracking/python/det_infer.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Detector(object):
7070
Args:
7171
pred_config (object): config of model, defined by `Config(model_dir)`
7272
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
73-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
73+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
7474
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
7575
batch_size (int): size of pre batch in inference
7676
trt_min_shape (int): min shape for dynamic shape in trt
@@ -400,7 +400,7 @@ def load_predictor(model_dir,
400400
"""set AnalysisConfig, generate AnalysisPredictor
401401
Args:
402402
model_dir (str): root path of __model__ and __params__
403-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
403+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
404404
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16/trt_int8)
405405
use_dynamic_shape (bool): use dynamic shape or not
406406
trt_min_shape (int): min shape for dynamic shape in trt
@@ -432,8 +432,13 @@ def load_predictor(model_dir,
432432
# optimize graph and fuse op
433433
config.switch_ir_optim(True)
434434
elif device == 'XPU':
435-
config.enable_lite_engine()
435+
if config.lite_engine_enabled():
436+
config.enable_lite_engine()
436437
config.enable_xpu(10 * 1024 * 1024)
438+
elif device == 'NPU':
439+
if config.lite_engine_enabled():
440+
config.enable_lite_engine()
441+
config.enable_custom_device('npu')
437442
else:
438443
config.disable_gpu()
439444
config.set_cpu_math_library_num_threads(cpu_threads)

deploy/python/keypoint_infer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class KeyPointDetector(Detector):
5050
"""
5151
Args:
5252
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
53-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
53+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
5454
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
5555
batch_size (int): size of pre batch in inference
5656
trt_min_shape (int): min shape for dynamic shape in trt
@@ -408,8 +408,8 @@ def main():
408408
FLAGS = parser.parse_args()
409409
print_arguments(FLAGS)
410410
FLAGS.device = FLAGS.device.upper()
411-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
412-
], "device should be CPU, GPU or XPU"
411+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
412+
], "device should be CPU, GPU, XPU or NPU"
413413
assert not FLAGS.use_gpu, "use_gpu has been deprecated, please use --device"
414414

415415
main()

deploy/python/mot_centertrack_infer.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CenterTrack(Detector):
6565
"""
6666
Args:
6767
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
68-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
68+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
6969
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
7070
batch_size (int): size of pre batch in inference
7171
trt_min_shape (int): min shape for dynamic shape in trt
@@ -130,7 +130,7 @@ def __init__(
130130
vertical_ratio=vertical_ratio,
131131
track_thresh=track_thresh,
132132
pre_thresh=pre_thresh)
133-
133+
134134
self.pre_image = None
135135

136136
def get_additional_inputs(self, dets, meta, with_hm=True):
@@ -173,19 +173,18 @@ def preprocess(self, image_list):
173173
#inputs = create_inputs(im, im_info)
174174
inputs = {}
175175
inputs['image'] = np.array((im, )).astype('float32')
176-
inputs['im_shape'] = np.array(
177-
(im_info['im_shape'], )).astype('float32')
176+
inputs['im_shape'] = np.array((im_info['im_shape'], )).astype('float32')
178177
inputs['scale_factor'] = np.array(
179178
(im_info['scale_factor'], )).astype('float32')
180-
179+
181180
inputs['trans_input'] = im_info['trans_input']
182181
inputs['inp_width'] = im_info['inp_width']
183182
inputs['inp_height'] = im_info['inp_height']
184183
inputs['center'] = im_info['center']
185184
inputs['scale'] = im_info['scale']
186185
inputs['out_height'] = im_info['out_height']
187186
inputs['out_width'] = im_info['out_width']
188-
187+
189188
if self.pre_image is None:
190189
self.pre_image = inputs['image']
191190
# initializing tracker for the first frame
@@ -196,7 +195,7 @@ def preprocess(self, image_list):
196195
# render input heatmap from tracker status
197196
pre_hm = self.get_additional_inputs(
198197
self.tracker.tracks, inputs, with_hm=True)
199-
inputs['pre_hm'] = pre_hm #.to_tensor(pre_hm)
198+
inputs['pre_hm'] = pre_hm #.to_tensor(pre_hm)
200199

201200
input_names = self.predictor.get_input_names()
202201
for i in range(len(input_names)):
@@ -256,8 +255,8 @@ def centertrack_post_process(self, dets, meta, out_thresh):
256255
return preds
257256

258257
def tracking(self, inputs, det_results):
259-
result = self.centertrack_post_process(
260-
det_results, inputs, self.tracker.out_thresh)
258+
result = self.centertrack_post_process(det_results, inputs,
259+
self.tracker.out_thresh)
261260
online_targets = self.tracker.update(result)
262261

263262
online_tlwhs, online_scores, online_ids = [], [], []
@@ -292,10 +291,7 @@ def predict(self, repeats=1):
292291
tracking_tensor = self.predictor.get_output_handle(output_names[2])
293292
np_tracking = tracking_tensor.copy_to_cpu()
294293

295-
result = dict(
296-
bboxes=np_bboxes,
297-
cts=np_cts,
298-
tracking=np_tracking)
294+
result = dict(bboxes=np_bboxes, cts=np_cts, tracking=np_tracking)
299295
return result
300296

301297
def predict_image(self,
@@ -333,8 +329,8 @@ def predict_image(self,
333329
# tracking
334330
result_warmup = self.tracking(inputs, det_result)
335331
self.det_times.tracking_time_s.start()
336-
online_tlwhs, online_scores, online_ids = self.tracking(inputs,
337-
det_result)
332+
online_tlwhs, online_scores, online_ids = self.tracking(
333+
inputs, det_result)
338334
self.det_times.tracking_time_s.end()
339335
self.det_times.img_num += 1
340336

@@ -358,8 +354,8 @@ def predict_image(self,
358354

359355
# tracking process
360356
self.det_times.tracking_time_s.start()
361-
online_tlwhs, online_scores, online_ids = self.tracking(inputs,
362-
det_result)
357+
online_tlwhs, online_scores, online_ids = self.tracking(
358+
inputs, det_result)
363359
self.det_times.tracking_time_s.end()
364360
self.det_times.img_num += 1
365361

@@ -499,7 +495,7 @@ def main():
499495
FLAGS = parser.parse_args()
500496
print_arguments(FLAGS)
501497
FLAGS.device = FLAGS.device.upper()
502-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
503-
], "device should be CPU, GPU or XPU"
498+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
499+
], "device should be CPU, GPU, NPU or XPU"
504500

505501
main()

deploy/python/mot_jde_infer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class JDE_Detector(Detector):
4545
"""
4646
Args:
4747
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
48-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
48+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
4949
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
5050
batch_size (int): size of pre batch in inference
5151
trt_min_shape (int): min shape for dynamic shape in trt
@@ -375,7 +375,7 @@ def main():
375375
FLAGS = parser.parse_args()
376376
print_arguments(FLAGS)
377377
FLAGS.device = FLAGS.device.upper()
378-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
379-
], "device should be CPU, GPU or XPU"
378+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
379+
], "device should be CPU, GPU, NPU or XPU"
380380

381381
main()

deploy/python/mot_keypoint_unite_infer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def main():
295295
FLAGS = parser.parse_args()
296296
print_arguments(FLAGS)
297297
FLAGS.device = FLAGS.device.upper()
298-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
299-
], "device should be CPU, GPU or XPU"
298+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
299+
], "device should be CPU, GPU, NPU or XPU"
300300

301301
main()

deploy/python/mot_keypoint_unite_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def argsparser():
7878
"--device",
7979
type=str,
8080
default='cpu',
81-
help="Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU."
81+
help="Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU."
8282
)
8383
parser.add_argument(
8484
"--run_benchmark",

deploy/python/mot_sde_infer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SDE_Detector(Detector):
4040
Args:
4141
model_dir (str): root path of model.pdiparams, model.pdmodel and infer_cfg.yml
4242
tracker_config (str): tracker config path
43-
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU
43+
device (str): Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU
4444
run_mode (str): mode of running(paddle/trt_fp32/trt_fp16)
4545
batch_size (int): size of pre batch in inference
4646
trt_min_shape (int): min shape for dynamic shape in trt
@@ -516,7 +516,7 @@ def main():
516516
FLAGS = parser.parse_args()
517517
print_arguments(FLAGS)
518518
FLAGS.device = FLAGS.device.upper()
519-
assert FLAGS.device in ['CPU', 'GPU', 'XPU'
520-
], "device should be CPU, GPU or XPU"
519+
assert FLAGS.device in ['CPU', 'GPU', 'XPU', 'NPU'
520+
], "device should be CPU, GPU, NPU or XPU"
521521

522522
main()

deploy/python/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def argsparser():
6464
"--device",
6565
type=str,
6666
default='cpu',
67-
help="Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU."
67+
help="Choose the device you want to run, it can be: CPU/GPU/XPU/NPU, default is CPU."
6868
)
6969
parser.add_argument(
7070
"--use_gpu",

test_tipc/test_train_inference_python_npu.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ grep -n '.yml' $FILENAME | cut -d ":" -f 1 \
4949
| while read line_num ; do
5050
train_cmd=$(func_parser_value "${lines[line_num-1]}")
5151
trainer_config=$(func_parser_config ${train_cmd})
52-
echo ${trainer_config}
5352
sed -i 's/use_gpu/use_npu/g' "$REPO_ROOT_PATH/$trainer_config"
53+
sed -i 's/aligned: True/aligned: False/g' "$REPO_ROOT_PATH/$trainer_config"
5454
# fine use_gpu in those included yaml
5555
sub_datalinee=`cat $REPO_ROOT_PATH/$trainer_config`
5656
IFS=$'\n'
@@ -60,9 +60,10 @@ grep -n '.yml' $FILENAME | cut -d ":" -f 1 \
6060
sub_config=${sub_lines[sub_line_num-1]}
6161
dst=${#sub_config}-5
6262
sub_path=$(func_parser_dir "${trainer_config}")
63-
sub_config_path="${REPO_ROOT_PATH}${sub_path}/${sub_config:3:${dst}}"
64-
echo ${sub_config_path}
63+
sub_config_name=$(echo "$sub_config" | awk -F"'" '{ print $2 }')
64+
sub_config_path="${REPO_ROOT_PATH}${sub_path}/${sub_config_name}"
6565
sed -i 's/use_gpu/use_npu/g' "$sub_config_path"
66+
sed -i 's/aligned: True/aligned: False/g' "$sub_config_path"
6667
done
6768
done
6869
# pass parameters to test_train_inference_python.sh

0 commit comments

Comments
 (0)