未验证 提交 35e710c5 编写于 作者: H houj04 提交者: GitHub

add support for xpu. (#5176)

上级 0a3d768c
...@@ -25,7 +25,9 @@ import paddle.version as fluid_version ...@@ -25,7 +25,9 @@ import paddle.version as fluid_version
from .logger import setup_logger from .logger import setup_logger
logger = setup_logger(__name__) 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): def check_npu(use_npu):
...@@ -47,6 +49,25 @@ def check_npu(use_npu): ...@@ -47,6 +49,25 @@ def check_npu(use_npu):
pass 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): def check_gpu(use_gpu):
""" """
Log error and exit when set use_gpu=true in paddlepaddle Log error and exit when set use_gpu=true in paddlepaddle
......
...@@ -41,7 +41,7 @@ try: ...@@ -41,7 +41,7 @@ try:
from ppdet.utils.eval_utils import parse_fetches from ppdet.utils.eval_utils import parse_fetches
from ppdet.utils.cli import ArgsParser 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 from ppdet.utils.visualizer import visualize_results
import ppdet.utils.checkpoint as checkpoint import ppdet.utils.checkpoint as checkpoint
...@@ -113,6 +113,10 @@ def main(): ...@@ -113,6 +113,10 @@ def main():
if 'use_npu' not in cfg: if 'use_npu' not in cfg:
cfg.use_npu = False cfg.use_npu = False
check_npu(cfg.use_npu) 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 if paddlepaddle version is satisfied
check_version() check_version()
...@@ -127,6 +131,8 @@ def main(): ...@@ -127,6 +131,8 @@ def main():
place = fluid.CUDAPlace(0) place = fluid.CUDAPlace(0)
elif cfg.use_npu: elif cfg.use_npu:
place = fluid.NPUPlace(0) place = fluid.NPUPlace(0)
elif cfg.use_xpu:
place = fluid.XPUPlace(0)
else: else:
place = fluid.CPUPlace() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
......
...@@ -30,7 +30,7 @@ warnings.filterwarnings('ignore') ...@@ -30,7 +30,7 @@ warnings.filterwarnings('ignore')
import paddle import paddle
from ppdet.core.workspace import load_config, merge_config 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.utils.cli import ArgsParser
from ppdet.engine import Trainer, init_parallel_env from ppdet.engine import Trainer, init_parallel_env
from ppdet.metrics.coco_utils import json_eval_results from ppdet.metrics.coco_utils import json_eval_results
...@@ -120,10 +120,16 @@ def main(): ...@@ -120,10 +120,16 @@ def main():
if 'use_npu' not in cfg: if 'use_npu' not in cfg:
cfg.use_npu = False cfg.use_npu = False
# disable xpu in config by default
if 'use_xpu' not in cfg:
cfg.use_xpu = False
if cfg.use_gpu: if cfg.use_gpu:
place = paddle.set_device('gpu') place = paddle.set_device('gpu')
elif cfg.use_npu: elif cfg.use_npu:
place = paddle.set_device('npu') place = paddle.set_device('npu')
elif cfg.use_xpu:
place = paddle.set_device('xpu')
else: else:
place = paddle.set_device('cpu') place = paddle.set_device('cpu')
...@@ -136,6 +142,7 @@ def main(): ...@@ -136,6 +142,7 @@ def main():
check_config(cfg) check_config(cfg)
check_gpu(cfg.use_gpu) check_gpu(cfg.use_gpu)
check_npu(cfg.use_npu) check_npu(cfg.use_npu)
check_xpu(cfg.use_xpu)
check_version() check_version()
run(FLAGS, cfg) run(FLAGS, cfg)
......
...@@ -31,7 +31,7 @@ import glob ...@@ -31,7 +31,7 @@ import glob
import paddle import paddle
from ppdet.core.workspace import load_config, merge_config from ppdet.core.workspace import load_config, merge_config
from ppdet.engine import Trainer 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.utils.cli import ArgsParser
from ppdet.slim import build_slim_model from ppdet.slim import build_slim_model
...@@ -145,10 +145,16 @@ def main(): ...@@ -145,10 +145,16 @@ def main():
if 'use_npu' not in cfg: if 'use_npu' not in cfg:
cfg.use_npu = False cfg.use_npu = False
# disable xpu in config by default
if 'use_xpu' not in cfg:
cfg.use_xpu = False
if cfg.use_gpu: if cfg.use_gpu:
place = paddle.set_device('gpu') place = paddle.set_device('gpu')
elif cfg.use_npu: elif cfg.use_npu:
place = paddle.set_device('npu') place = paddle.set_device('npu')
elif cfg.use_xpu:
place = paddle.set_device('xpu')
else: else:
place = paddle.set_device('cpu') place = paddle.set_device('cpu')
...@@ -161,6 +167,7 @@ def main(): ...@@ -161,6 +167,7 @@ def main():
check_config(cfg) check_config(cfg)
check_gpu(cfg.use_gpu) check_gpu(cfg.use_gpu)
check_npu(cfg.use_npu) check_npu(cfg.use_npu)
check_xpu(cfg.use_xpu)
check_version() check_version()
run(FLAGS, cfg) run(FLAGS, cfg)
......
...@@ -144,10 +144,16 @@ def main(): ...@@ -144,10 +144,16 @@ def main():
if 'use_npu' not in cfg: if 'use_npu' not in cfg:
cfg.use_npu = False cfg.use_npu = False
# disable xpu in config by default
if 'use_xpu' not in cfg:
cfg.use_xpu = False
if cfg.use_gpu: if cfg.use_gpu:
place = paddle.set_device('gpu') place = paddle.set_device('gpu')
elif cfg.use_npu: elif cfg.use_npu:
place = paddle.set_device('npu') place = paddle.set_device('npu')
elif cfg.use_xpu:
place = paddle.set_device('xpu')
else: else:
place = paddle.set_device('cpu') place = paddle.set_device('cpu')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册