From 35e710c5392cf04ce6b158b661b7f07e8bb049ef Mon Sep 17 00:00:00 2001 From: houj04 <35131887+houj04@users.noreply.github.com> Date: Wed, 9 Feb 2022 16:53:01 +0800 Subject: [PATCH] add support for xpu. (#5176) --- ppdet/utils/check.py | 23 ++++++++++++++++++++++- static/tools/infer.py | 8 +++++++- tools/eval.py | 9 ++++++++- tools/infer.py | 9 ++++++++- tools/train.py | 6 ++++++ 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/ppdet/utils/check.py b/ppdet/utils/check.py index 6c795b532..45da8857e 100644 --- a/ppdet/utils/check.py +++ b/ppdet/utils/check.py @@ -25,7 +25,9 @@ import paddle.version as fluid_version from .logger import setup_logger logger = setup_logger(__name__) -__all__ = ['check_gpu', 'check_npu', 'check_version', 'check_config'] +__all__ = [ + 'check_gpu', 'check_npu', 'check_xpu', 'check_version', 'check_config' +] def check_npu(use_npu): @@ -47,6 +49,25 @@ def check_npu(use_npu): pass +def check_xpu(use_xpu): + """ + Log error and exit when set use_xpu=true in paddlepaddle + cpu/gpu/npu version. + """ + err = "Config use_xpu cannot be set as true while you are " \ + "using paddlepaddle cpu/gpu/npu version ! \nPlease try: \n" \ + "\t1. Install paddlepaddle-xpu to run model on XPU \n" \ + "\t2. Set use_xpu as false in config file to run " \ + "model on CPU/GPU/NPU" + + try: + if use_xpu and not paddle.is_compiled_with_xpu(): + logger.error(err) + sys.exit(1) + except Exception as e: + pass + + def check_gpu(use_gpu): """ Log error and exit when set use_gpu=true in paddlepaddle diff --git a/static/tools/infer.py b/static/tools/infer.py index 3c09710ce..b1371c769 100644 --- a/static/tools/infer.py +++ b/static/tools/infer.py @@ -41,7 +41,7 @@ try: from ppdet.utils.eval_utils import parse_fetches from ppdet.utils.cli import ArgsParser - from ppdet.utils.check import check_gpu, check_npu, check_version, check_config, enable_static_mode + from ppdet.utils.check import check_gpu, check_npu, check_xpu, check_version, check_config, enable_static_mode from ppdet.utils.visualizer import visualize_results import ppdet.utils.checkpoint as checkpoint @@ -113,6 +113,10 @@ def main(): if 'use_npu' not in cfg: cfg.use_npu = False check_npu(cfg.use_npu) + # disable xpu in config by default and check use_xpu + if 'use_xpu' not in cfg: + cfg.use_xpu = False + check_xpu(cfg.use_xpu) # check if paddlepaddle version is satisfied check_version() @@ -127,6 +131,8 @@ def main(): place = fluid.CUDAPlace(0) elif cfg.use_npu: place = fluid.NPUPlace(0) + elif cfg.use_xpu: + place = fluid.XPUPlace(0) else: place = fluid.CPUPlace() exe = fluid.Executor(place) diff --git a/tools/eval.py b/tools/eval.py index f53949905..308dd8fbb 100755 --- a/tools/eval.py +++ b/tools/eval.py @@ -30,7 +30,7 @@ warnings.filterwarnings('ignore') import paddle from ppdet.core.workspace import load_config, merge_config -from ppdet.utils.check import check_gpu, check_npu, check_version, check_config +from ppdet.utils.check import check_gpu, check_npu, check_xpu, check_version, check_config from ppdet.utils.cli import ArgsParser from ppdet.engine import Trainer, init_parallel_env from ppdet.metrics.coco_utils import json_eval_results @@ -120,10 +120,16 @@ def main(): if 'use_npu' not in cfg: cfg.use_npu = False + # disable xpu in config by default + if 'use_xpu' not in cfg: + cfg.use_xpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: place = paddle.set_device('npu') + elif cfg.use_xpu: + place = paddle.set_device('xpu') else: place = paddle.set_device('cpu') @@ -136,6 +142,7 @@ def main(): check_config(cfg) check_gpu(cfg.use_gpu) check_npu(cfg.use_npu) + check_xpu(cfg.use_xpu) check_version() run(FLAGS, cfg) diff --git a/tools/infer.py b/tools/infer.py index ef0ec65af..406171f2b 100755 --- a/tools/infer.py +++ b/tools/infer.py @@ -31,7 +31,7 @@ import glob import paddle from ppdet.core.workspace import load_config, merge_config from ppdet.engine import Trainer -from ppdet.utils.check import check_gpu, check_npu, check_version, check_config +from ppdet.utils.check import check_gpu, check_npu, check_xpu, check_version, check_config from ppdet.utils.cli import ArgsParser from ppdet.slim import build_slim_model @@ -145,10 +145,16 @@ def main(): if 'use_npu' not in cfg: cfg.use_npu = False + # disable xpu in config by default + if 'use_xpu' not in cfg: + cfg.use_xpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: place = paddle.set_device('npu') + elif cfg.use_xpu: + place = paddle.set_device('xpu') else: place = paddle.set_device('cpu') @@ -161,6 +167,7 @@ def main(): check_config(cfg) check_gpu(cfg.use_gpu) check_npu(cfg.use_npu) + check_xpu(cfg.use_xpu) check_version() run(FLAGS, cfg) diff --git a/tools/train.py b/tools/train.py index 878aa60fa..088a41793 100755 --- a/tools/train.py +++ b/tools/train.py @@ -144,10 +144,16 @@ def main(): if 'use_npu' not in cfg: cfg.use_npu = False + # disable xpu in config by default + if 'use_xpu' not in cfg: + cfg.use_xpu = False + if cfg.use_gpu: place = paddle.set_device('gpu') elif cfg.use_npu: place = paddle.set_device('npu') + elif cfg.use_xpu: + place = paddle.set_device('xpu') else: place = paddle.set_device('cpu') -- GitLab