Skip to content

Commit fe1b451

Browse files
authored
Support mlu in infer.py
1 parent 7701372 commit fe1b451

File tree

1 file changed

+74
-79
lines changed

1 file changed

+74
-79
lines changed

deploy/python/infer.py

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def auto_tune(args, imgs, img_nums):
8686

8787

8888
class Predictor:
89+
8990
def __init__(self, args):
9091
"""
9192
Prepare for prediction.
@@ -101,6 +102,8 @@ def __init__(self, args):
101102
self._init_cpu_config()
102103
elif args.device == 'npu':
103104
self.pred_cfg.enable_custom_device('npu')
105+
elif args.device == 'mlu':
106+
self.pred_cfg.enable_custom_device('mlu')
104107
elif args.device == 'xpu':
105108
self.pred_cfg.enable_xpu()
106109
else:
@@ -119,21 +122,22 @@ def __init__(self, args):
119122
if hasattr(args, 'benchmark') and args.benchmark:
120123
import auto_log
121124
pid = os.getpid()
122-
self.autolog = auto_log.AutoLogger(
123-
model_name=args.model_name,
124-
model_precision=args.precision,
125-
batch_size=args.batch_size,
126-
data_shape="dynamic",
127-
save_path=None,
128-
inference_config=self.pred_cfg,
129-
pids=pid,
130-
process_name=None,
131-
gpu_ids=0,
132-
time_keys=[
133-
'preprocess_time', 'inference_time', 'postprocess_time'
134-
],
135-
warmup=0,
136-
logger=logger)
125+
self.autolog = auto_log.AutoLogger(model_name=args.model_name,
126+
model_precision=args.precision,
127+
batch_size=args.batch_size,
128+
data_shape="dynamic",
129+
save_path=None,
130+
inference_config=self.pred_cfg,
131+
pids=pid,
132+
process_name=None,
133+
gpu_ids=0,
134+
time_keys=[
135+
'preprocess_time',
136+
'inference_time',
137+
'postprocess_time'
138+
],
139+
warmup=0,
140+
logger=logger)
137141

138142
def _init_base_config(self):
139143
self.pred_cfg = PredictConfig(self.cfg.model, self.cfg.params)
@@ -224,9 +228,8 @@ def run(self, imgs_path):
224228
if args.benchmark:
225229
self.autolog.times.start()
226230

227-
data = np.array([
228-
self._preprocess(p) for p in imgs_path[i:i + args.batch_size]
229-
])
231+
data = np.array(
232+
[self._preprocess(p) for p in imgs_path[i:i + args.batch_size]])
230233
input_handle.reshape(data.shape)
231234
input_handle.copy_from_cpu(data)
232235

@@ -268,35 +271,32 @@ def _save_imgs(self, results, imgs_path):
268271

269272
def parse_args():
270273
parser = argparse.ArgumentParser(description='Test')
271-
parser.add_argument(
272-
"--config",
273-
dest="cfg",
274-
help="The config file.",
275-
default=None,
276-
type=str,
277-
required=True)
274+
parser.add_argument("--config",
275+
dest="cfg",
276+
help="The config file.",
277+
default=None,
278+
type=str,
279+
required=True)
278280
parser.add_argument(
279281
'--image_path',
280282
dest='image_path',
281283
help='The directory or path or file list of the images to be predicted.',
282284
type=str,
283285
default=None,
284286
required=True)
285-
parser.add_argument(
286-
'--batch_size',
287-
dest='batch_size',
288-
help='Mini batch size of one gpu or cpu.',
289-
type=int,
290-
default=1)
291-
parser.add_argument(
292-
'--save_dir',
293-
dest='save_dir',
294-
help='The directory for saving the predict result.',
295-
type=str,
296-
default='./output')
287+
parser.add_argument('--batch_size',
288+
dest='batch_size',
289+
help='Mini batch size of one gpu or cpu.',
290+
type=int,
291+
default=1)
292+
parser.add_argument('--save_dir',
293+
dest='save_dir',
294+
help='The directory for saving the predict result.',
295+
type=str,
296+
default='./output')
297297
parser.add_argument(
298298
'--device',
299-
choices=['cpu', 'gpu', 'xpu', 'npu'],
299+
choices=['cpu', 'gpu', 'xpu', 'npu', 'mlu'],
300300
default="gpu",
301301
help="Select which device to inference, defaults to gpu.")
302302

@@ -306,48 +306,45 @@ def parse_args():
306306
type=eval,
307307
choices=[True, False],
308308
help='Whether to use Nvidia TensorRT to accelerate prediction.')
309-
parser.add_argument(
310-
"--precision",
311-
default="fp32",
312-
type=str,
313-
choices=["fp32", "fp16", "int8"],
314-
help='The tensorrt precision.')
315-
parser.add_argument(
316-
'--min_subgraph_size',
317-
default=3,
318-
type=int,
319-
help='The min subgraph size in tensorrt prediction.')
309+
parser.add_argument("--precision",
310+
default="fp32",
311+
type=str,
312+
choices=["fp32", "fp16", "int8"],
313+
help='The tensorrt precision.')
314+
parser.add_argument('--min_subgraph_size',
315+
default=3,
316+
type=int,
317+
help='The min subgraph size in tensorrt prediction.')
320318
parser.add_argument(
321319
'--enable_auto_tune',
322320
default=False,
323321
type=eval,
324322
choices=[True, False],
325-
help='Whether to enable tuned dynamic shape. We uses some images to collect '
323+
help=
324+
'Whether to enable tuned dynamic shape. We uses some images to collect '
326325
'the dynamic shape for trt sub graph, which avoids setting dynamic shape manually.'
327326
)
328-
parser.add_argument(
329-
'--auto_tuned_shape_file',
330-
type=str,
331-
default="auto_tune_tmp.pbtxt",
332-
help='The temp file to save tuned dynamic shape.')
333-
334-
parser.add_argument(
335-
'--cpu_threads',
336-
default=10,
337-
type=int,
338-
help='Number of threads to predict when using cpu.')
339-
parser.add_argument(
340-
'--enable_mkldnn',
341-
default=False,
342-
type=eval,
343-
choices=[True, False],
344-
help='Enable to use mkldnn to speed up when using cpu.')
327+
parser.add_argument('--auto_tuned_shape_file',
328+
type=str,
329+
default="auto_tune_tmp.pbtxt",
330+
help='The temp file to save tuned dynamic shape.')
331+
332+
parser.add_argument('--cpu_threads',
333+
default=10,
334+
type=int,
335+
help='Number of threads to predict when using cpu.')
336+
parser.add_argument('--enable_mkldnn',
337+
default=False,
338+
type=eval,
339+
choices=[True, False],
340+
help='Enable to use mkldnn to speed up when using cpu.')
345341

346342
parser.add_argument(
347343
"--benchmark",
348344
type=eval,
349345
default=False,
350-
help="Whether to log some information about environment, model, configuration and performance."
346+
help=
347+
"Whether to log some information about environment, model, configuration and performance."
351348
)
352349
parser.add_argument(
353350
"--model_name",
@@ -356,17 +353,15 @@ def parse_args():
356353
help='When `--benchmark` is True, the specified model name is displayed.'
357354
)
358355

359-
parser.add_argument(
360-
'--with_argmax',
361-
dest='with_argmax',
362-
help='Perform argmax operation on the predict result.',
363-
action='store_true')
364-
parser.add_argument(
365-
'--print_detail',
366-
default=True,
367-
type=eval,
368-
choices=[True, False],
369-
help='Print GLOG information of Paddle Inference.')
356+
parser.add_argument('--with_argmax',
357+
dest='with_argmax',
358+
help='Perform argmax operation on the predict result.',
359+
action='store_true')
360+
parser.add_argument('--print_detail',
361+
default=True,
362+
type=eval,
363+
choices=[True, False],
364+
help='Print GLOG information of Paddle Inference.')
370365

371366
return parser.parse_args()
372367

0 commit comments

Comments
 (0)