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

add xpu and npu support for ernie series. (#1639)

上级 08c56a7d
...@@ -60,8 +60,36 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -60,8 +60,36 @@ class ErnieGen(hub.NLPPredictionModule):
self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD] self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD]
self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i]) self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i])
# detect npu
npu_id = self._get_device_id("FLAGS_selected_npus")
if npu_id != -1:
# use npu
self.use_device = "npu"
else:
# detect gpu
gpu_id = self._get_device_id("CUDA_VISIBLE_DEVICES")
if gpu_id != -1:
# use gpu
self.use_device = "gpu"
else:
# detect xpu
xpu_id = self._get_device_id("XPU_VISIBLE_DEVICES")
if xpu_id != -1:
# use xpu
self.use_device = "xpu"
else:
self.use_device = "cpu"
def _get_device_id(self, places):
try:
places = os.environ[places]
id = int(places)
except:
id = -1
return id
@serving @serving
def generate(self, texts, use_gpu=False, beam_width=5): def generate(self, texts, use_gpu=False, beam_width=5, use_device=None):
""" """
Get the continuation of the input poetry. Get the continuation of the input poetry.
...@@ -69,6 +97,7 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -69,6 +97,7 @@ class ErnieGen(hub.NLPPredictionModule):
texts(list): the front part of a poetry. texts(list): the front part of a poetry.
use_gpu(bool): whether use gpu to predict or not use_gpu(bool): whether use gpu to predict or not
beam_width(int): the beam search width. beam_width(int): the beam search width.
use_device (str): use cpu, gpu, xpu or npu, overwrites use_gpu flag.
Returns: Returns:
results(list): the poetry continuations. results(list): the poetry continuations.
...@@ -91,13 +120,25 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -91,13 +120,25 @@ class ErnieGen(hub.NLPPredictionModule):
'The input text: %s, contains non-Chinese characters, which may result in magic output' % text) 'The input text: %s, contains non-Chinese characters, which may result in magic output' % text)
break break
if use_gpu and "CUDA_VISIBLE_DEVICES" not in os.environ: if use_device is not None:
use_gpu = False # check 'use_device' match 'device on init'
logger.warning( if use_device != self.use_device:
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True" raise RuntimeError(
) "the 'use_device' parameter when calling generate, does not match internal device found on init.")
else:
# use_device is None, follow use_gpu flag
if use_gpu == False:
use_device = "cpu"
elif use_gpu == True and self.use_device != 'gpu':
use_device = "cpu"
logger.warning(
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True"
)
else:
# use_gpu and self.use_device are both true
use_device = "gpu"
paddle.set_device('gpu') if use_gpu else paddle.set_device('cpu') paddle.set_device(use_device)
self.model.eval() self.model.eval()
results = [] results = []
...@@ -135,8 +176,11 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -135,8 +176,11 @@ class ErnieGen(hub.NLPPredictionModule):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction") '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width") self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width")
self.arg_config_group.add_argument(
'--use_device',
choices=["cpu", "gpu", "xpu", "npu"],
help="use cpu, gpu, xpu or npu. overwrites use_gpu flag.")
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
...@@ -164,7 +208,8 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -164,7 +208,8 @@ class ErnieGen(hub.NLPPredictionModule):
self.parser.print_help() self.parser.print_help()
return None return None
results = self.generate(texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width) results = self.generate(
texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width, use_device=args.use_device)
return results return results
......
...@@ -54,8 +54,36 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -54,8 +54,36 @@ class ErnieGen(hub.NLPPredictionModule):
self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD] self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD]
self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i]) self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i])
# detect npu
npu_id = self._get_device_id("FLAGS_selected_npus")
if npu_id != -1:
# use npu
self.use_device = "npu"
else:
# detect gpu
gpu_id = self._get_device_id("CUDA_VISIBLE_DEVICES")
if gpu_id != -1:
# use gpu
self.use_device = "gpu"
else:
# detect xpu
xpu_id = self._get_device_id("XPU_VISIBLE_DEVICES")
if xpu_id != -1:
# use xpu
self.use_device = "xpu"
else:
self.use_device = "cpu"
def _get_device_id(self, places):
try:
places = os.environ[places]
id = int(places)
except:
id = -1
return id
@serving @serving
def generate(self, texts, use_gpu=False, beam_width=5): def generate(self, texts, use_gpu=False, beam_width=5, use_device=None):
""" """
Get the right rolls from the left rolls. Get the right rolls from the left rolls.
...@@ -63,6 +91,7 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -63,6 +91,7 @@ class ErnieGen(hub.NLPPredictionModule):
texts(list): the left rolls. texts(list): the left rolls.
use_gpu(bool): whether use gpu to predict or not use_gpu(bool): whether use gpu to predict or not
beam_width(int): the beam search width. beam_width(int): the beam search width.
use_device (str): use cpu, gpu, xpu or npu, overwrites use_gpu flag.
Returns: Returns:
results(list): the right rolls. results(list): the right rolls.
...@@ -80,13 +109,25 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -80,13 +109,25 @@ class ErnieGen(hub.NLPPredictionModule):
'The input text: %s, contains non-Chinese characters, which may result in magic output' % text) 'The input text: %s, contains non-Chinese characters, which may result in magic output' % text)
break break
if use_gpu and "CUDA_VISIBLE_DEVICES" not in os.environ: if use_device is not None:
use_gpu = False # check 'use_device' match 'device on init'
logger.warning( if use_device != self.use_device:
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True" raise RuntimeError(
) "the 'use_device' parameter when calling generate, does not match internal device found on init.")
else:
paddle.set_device('gpu') if use_gpu else paddle.set_device('cpu') # use_device is None, follow use_gpu flag
if use_gpu == False:
use_device = "cpu"
elif use_gpu == True and self.use_device != 'gpu':
use_device = "cpu"
logger.warning(
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True"
)
else:
# use_gpu and self.use_device are both true
use_device = "gpu"
paddle.set_device(use_device)
self.model.eval() self.model.eval()
results = [] results = []
...@@ -124,8 +165,11 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -124,8 +165,11 @@ class ErnieGen(hub.NLPPredictionModule):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction") '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width") self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width")
self.arg_config_group.add_argument(
'--use_device',
choices=["cpu", "gpu", "xpu", "npu"],
help="use cpu, gpu, xpu or npu. overwrites use_gpu flag.")
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
...@@ -153,7 +197,8 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -153,7 +197,8 @@ class ErnieGen(hub.NLPPredictionModule):
self.parser.print_help() self.parser.print_help()
return None return None
results = self.generate(texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width) results = self.generate(
texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width, use_device=args.use_device)
return results return results
......
...@@ -54,8 +54,36 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -54,8 +54,36 @@ class ErnieGen(hub.NLPPredictionModule):
self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD] self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD]
self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i]) self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i])
# detect npu
npu_id = self._get_device_id("FLAGS_selected_npus")
if npu_id != -1:
# use npu
self.use_device = "npu"
else:
# detect gpu
gpu_id = self._get_device_id("CUDA_VISIBLE_DEVICES")
if gpu_id != -1:
# use gpu
self.use_device = "gpu"
else:
# detect xpu
xpu_id = self._get_device_id("XPU_VISIBLE_DEVICES")
if xpu_id != -1:
# use xpu
self.use_device = "xpu"
else:
self.use_device = "cpu"
def _get_device_id(self, places):
try:
places = os.environ[places]
id = int(places)
except:
id = -1
return id
@serving @serving
def generate(self, texts, use_gpu=False, beam_width=5): def generate(self, texts, use_gpu=False, beam_width=5, use_device=None):
""" """
Get the continuation of the input poetry. Get the continuation of the input poetry.
...@@ -63,6 +91,7 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -63,6 +91,7 @@ class ErnieGen(hub.NLPPredictionModule):
texts(list): the front part of a poetry. texts(list): the front part of a poetry.
use_gpu(bool): whether use gpu to predict or not use_gpu(bool): whether use gpu to predict or not
beam_width(int): the beam search width. beam_width(int): the beam search width.
use_device (str): use cpu, gpu, xpu or npu, overwrites use_gpu flag.
Returns: Returns:
results(list): the poetry continuations. results(list): the poetry continuations.
...@@ -74,12 +103,25 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -74,12 +103,25 @@ class ErnieGen(hub.NLPPredictionModule):
else: else:
raise ValueError("The input texts should be a list with nonempty string elements.") raise ValueError("The input texts should be a list with nonempty string elements.")
if use_gpu and "CUDA_VISIBLE_DEVICES" not in os.environ: if use_device is not None:
use_gpu = False # check 'use_device' match 'device on init'
logger.warning( if use_device != self.use_device:
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True" raise RuntimeError(
) "the 'use_device' parameter when calling generate, does not match internal device found on init.")
paddle.set_device('gpu') if use_gpu else paddle.set_device('cpu') else:
# use_device is None, follow use_gpu flag
if use_gpu == False:
use_device = "cpu"
elif use_gpu == True and self.use_device != 'gpu':
use_device = "cpu"
logger.warning(
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True"
)
else:
# use_gpu and self.use_device are both true
use_device = "gpu"
paddle.set_device(use_device)
self.model.eval() self.model.eval()
results = [] results = []
for text in predicted_data: for text in predicted_data:
...@@ -116,8 +158,11 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -116,8 +158,11 @@ class ErnieGen(hub.NLPPredictionModule):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction") '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width") self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width")
self.arg_config_group.add_argument(
'--use_device',
choices=["cpu", "gpu", "xpu", "npu"],
help="use cpu, gpu, xpu or npu. overwrites use_gpu flag.")
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
...@@ -145,7 +190,8 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -145,7 +190,8 @@ class ErnieGen(hub.NLPPredictionModule):
self.parser.print_help() self.parser.print_help()
return None return None
results = self.generate(texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width) results = self.generate(
texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width, use_device=args.use_device)
return results return results
......
...@@ -54,8 +54,36 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -54,8 +54,36 @@ class ErnieGen(hub.NLPPredictionModule):
self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD] self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD]
self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i]) self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i])
# detect npu
npu_id = self._get_device_id("FLAGS_selected_npus")
if npu_id != -1:
# use npu
self.use_device = "npu"
else:
# detect gpu
gpu_id = self._get_device_id("CUDA_VISIBLE_DEVICES")
if gpu_id != -1:
# use gpu
self.use_device = "gpu"
else:
# detect xpu
xpu_id = self._get_device_id("XPU_VISIBLE_DEVICES")
if xpu_id != -1:
# use xpu
self.use_device = "xpu"
else:
self.use_device = "cpu"
def _get_device_id(self, places):
try:
places = os.environ[places]
id = int(places)
except:
id = -1
return id
@serving @serving
def generate(self, texts, use_gpu=False, beam_width=5): def generate(self, texts, use_gpu=False, beam_width=5, use_device=None):
""" """
Get the continuation of the input poetry. Get the continuation of the input poetry.
...@@ -63,6 +91,7 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -63,6 +91,7 @@ class ErnieGen(hub.NLPPredictionModule):
texts(list): the front part of a poetry. texts(list): the front part of a poetry.
use_gpu(bool): whether use gpu to predict or not use_gpu(bool): whether use gpu to predict or not
beam_width(int): the beam search width. beam_width(int): the beam search width.
use_device (str): use cpu, gpu, xpu or npu, overwrites use_gpu flag.
Returns: Returns:
results(list): the poetry continuations. results(list): the poetry continuations.
...@@ -91,12 +120,26 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -91,12 +120,26 @@ class ErnieGen(hub.NLPPredictionModule):
% text) % text)
break break
if use_gpu and "CUDA_VISIBLE_DEVICES" not in os.environ: if use_device is not None:
use_gpu = False # check 'use_device' match 'device on init'
logger.warning( if use_device != self.use_device:
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True" raise RuntimeError(
) "the 'use_device' parameter when calling generate, does not match internal device found on init.")
paddle.set_device('gpu') if use_gpu else paddle.set_device('cpu') else:
# use_device is None, follow use_gpu flag
if use_gpu == False:
use_device = "cpu"
elif use_gpu == True and self.use_device != 'gpu':
use_device = "cpu"
logger.warning(
"use_gpu has been set False as you didn't set the environment variable CUDA_VISIBLE_DEVICES while using use_gpu=True"
)
else:
# use_gpu and self.use_device are both true
use_device = "gpu"
paddle.set_device(use_device)
self.model.eval() self.model.eval()
results = [] results = []
for text in predicted_data: for text in predicted_data:
...@@ -133,8 +176,11 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -133,8 +176,11 @@ class ErnieGen(hub.NLPPredictionModule):
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument(
'--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction") '--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU for prediction")
self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width") self.arg_config_group.add_argument('--beam_width', type=int, default=5, help="the beam search width")
self.arg_config_group.add_argument(
'--use_device',
choices=["cpu", "gpu", "xpu", "npu"],
help="use cpu, gpu, xpu or npu. overwrites use_gpu flag.")
@runnable @runnable
def run_cmd(self, argvs): def run_cmd(self, argvs):
...@@ -162,7 +208,8 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -162,7 +208,8 @@ class ErnieGen(hub.NLPPredictionModule):
self.parser.print_help() self.parser.print_help()
return None return None
results = self.generate(texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width) results = self.generate(
texts=input_data, use_gpu=args.use_gpu, beam_width=args.beam_width, use_device=args.use_device)
return results return results
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册