Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8225a6a1
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8225a6a1
编写于
6月 30, 2021
作者:
H
houj04
提交者:
GitHub
6月 30, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] support set_device (#33815)
* support set_device for NPU. * minor update doc and add more unit test.
上级
1835b72c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
56 addition
and
6 deletion
+56
-6
paddle/fluid/imperative/tracer.cc
paddle/fluid/imperative/tracer.cc
+8
-0
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+2
-0
python/paddle/device.py
python/paddle/device.py
+26
-6
python/paddle/fluid/tests/unittests/test_device.py
python/paddle/fluid/tests/unittests/test_device.py
+20
-0
未找到文件。
paddle/fluid/imperative/tracer.cc
浏览文件 @
8225a6a1
...
...
@@ -194,6 +194,14 @@ void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
#else
PADDLE_THROW
(
platform
::
errors
::
PreconditionNotMet
(
"PaddlePaddle should compile with XPU if use XPUPlace."
));
#endif
}
else
if
(
platform
::
is_npu_place
(
place
))
{
#ifdef PADDLE_WITH_ASCEND_CL
platform
::
SetNPUDeviceId
(
BOOST_GET_CONST
(
platform
::
NPUPlace
,
place
).
device
);
#else
PADDLE_THROW
(
platform
::
errors
::
PreconditionNotMet
(
"PaddlePaddle should compile with NPU if use NPUPlace."
));
#endif
}
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
8225a6a1
...
...
@@ -1718,6 +1718,8 @@ All parameter, weight, gradient are variables in Paddle.
.
def
(
"_equals"
,
&
IsSamePlace
<
platform
::
NPUPlace
,
platform
::
NPUPlace
>
)
.
def
(
"_equals"
,
&
IsSamePlace
<
platform
::
NPUPlace
,
platform
::
CUDAPinnedPlace
>
)
.
def
(
"get_device_id"
,
[](
const
platform
::
NPUPlace
&
self
)
{
return
self
.
GetDeviceId
();
})
.
def
(
"__str__"
,
string
::
to_string
<
const
platform
::
NPUPlace
&>
);
py
::
class_
<
platform
::
Place
>
(
m
,
"Place"
)
...
...
python/paddle/device.py
浏览文件 @
8225a6a1
...
...
@@ -133,12 +133,20 @@ def _convert_to_place(device):
selected_xpus
=
os
.
getenv
(
"FLAGS_selected_xpus"
,
"0"
).
split
(
","
)
device_id
=
int
(
selected_xpus
[
0
])
place
=
core
.
XPUPlace
(
device_id
)
elif
lower_device
==
'npu'
:
if
not
core
.
is_compiled_with_npu
():
raise
ValueError
(
"The device should not be 'npu', "
"since PaddlePaddle is not compiled with NPU"
)
selected_npus
=
os
.
getenv
(
"FLAGS_selected_npus"
,
"0"
).
split
(
","
)
device_id
=
int
(
selected_npus
[
0
])
place
=
core
.
NPUPlace
(
device_id
)
else
:
avaliable_gpu_device
=
re
.
match
(
r
'gpu:\d+'
,
lower_device
)
avaliable_xpu_device
=
re
.
match
(
r
'xpu:\d+'
,
lower_device
)
if
not
avaliable_gpu_device
and
not
avaliable_xpu_device
:
avaliable_npu_device
=
re
.
match
(
r
'npu:\d+'
,
lower_device
)
if
not
avaliable_gpu_device
and
not
avaliable_xpu_device
and
not
avaliable_npu_device
:
raise
ValueError
(
"The device must be a string which is like 'cpu', 'gpu', 'gpu:x', 'xpu'
or 'x
pu:x'"
"The device must be a string which is like 'cpu', 'gpu', 'gpu:x', 'xpu'
, 'xpu:x', 'npu' or 'n
pu:x'"
)
if
avaliable_gpu_device
:
if
not
core
.
is_compiled_with_cuda
():
...
...
@@ -158,19 +166,28 @@ def _convert_to_place(device):
device_id
=
device_info_list
[
1
]
device_id
=
int
(
device_id
)
place
=
core
.
XPUPlace
(
device_id
)
if
avaliable_npu_device
:
if
not
core
.
is_compiled_with_npu
():
raise
ValueError
(
"The device should not be {}, since PaddlePaddle is "
"not compiled with NPU"
.
format
(
avaliable_npu_device
))
device_info_list
=
device
.
split
(
':'
,
1
)
device_id
=
device_info_list
[
1
]
device_id
=
int
(
device_id
)
place
=
core
.
NPUPlace
(
device_id
)
return
place
def
set_device
(
device
):
"""
Paddle supports running calculations on various types of devices, including CPU, GPU
and X
PU.
Paddle supports running calculations on various types of devices, including CPU, GPU
, XPU and N
PU.
They are represented by string identifiers. This function can specify the global device
which the OP will run.
Parameters:
device(str): This parameter determines the specific running device.
It can be ``cpu``, ``gpu
:x`` and ``xpu:x``, where ``x`` is the
index of the GPUs or XPUs.
It can be ``cpu``, ``gpu
``, ``xpu``, ``npu``, ``gpu:x``, ``xpu:x`` and ``npu:x``,
where ``x`` is the index of the GPUs, XPUs or NPUs.
Examples:
...
...
@@ -191,7 +208,7 @@ def set_device(device):
def
get_device
():
"""
This funciton can get the current global device of the program is running.
It's a string which is like 'cpu', 'gpu:x'
and 'x
pu:x'. if the global device is not
It's a string which is like 'cpu', 'gpu:x'
, 'xpu:x' and 'n
pu:x'. if the global device is not
set, it will return a string which is 'gpu:x' when cuda is avaliable or it
will return a string which is 'cpu' when cuda is not avaliable.
...
...
@@ -213,5 +230,8 @@ def get_device():
elif
isinstance
(
place
,
core
.
XPUPlace
):
device_id
=
place
.
get_device_id
()
device
=
'xpu:'
+
str
(
device_id
)
elif
isinstance
(
place
,
core
.
NPUPlace
):
device_id
=
place
.
get_device_id
()
device
=
'npu:'
+
str
(
device_id
)
return
device
python/paddle/fluid/tests/unittests/test_device.py
浏览文件 @
8225a6a1
...
...
@@ -49,6 +49,10 @@ class TestStaticDeviceManage(unittest.TestCase):
if
core
.
is_compiled_with_xpu
():
self
.
_test_device
(
"xpu:0"
,
core
.
XPUPlace
)
def
test_npu_device
(
self
):
if
core
.
is_compiled_with_npu
():
self
.
_test_device
(
"npu:0"
,
core
.
NPUPlace
)
class
TestImperativeDeviceManage
(
unittest
.
TestCase
):
def
test_cpu
(
self
):
...
...
@@ -87,6 +91,22 @@ class TestImperativeDeviceManage(unittest.TestCase):
self
.
assertTrue
(
out
.
place
.
is_xpu_place
())
self
.
assertEqual
(
device
,
"xpu:0"
)
def
test_npu
(
self
):
if
core
.
is_compiled_with_npu
():
with
fluid
.
dygraph
.
guard
():
paddle
.
set_device
(
'npu:0'
)
out1
=
paddle
.
zeros
(
shape
=
[
1
,
3
],
dtype
=
'float32'
)
out2
=
paddle
.
ones
(
shape
=
[
1
,
3
],
dtype
=
'float32'
)
out3
=
paddle
.
concat
(
x
=
[
out1
,
out2
],
axis
=
0
)
device
=
paddle
.
get_device
()
self
.
assertEqual
(
isinstance
(
framework
.
_current_expected_place
(),
core
.
NPUPlace
),
True
)
self
.
assertTrue
(
out1
.
place
.
is_npu_place
())
self
.
assertTrue
(
out2
.
place
.
is_npu_place
())
self
.
assertTrue
(
out3
.
place
.
is_npu_place
())
self
.
assertEqual
(
device
,
"npu:0"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录