Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
29de0d97
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
29de0d97
编写于
6月 04, 2020
作者:
L
lilong12
提交者:
GitHub
6月 04, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add the support to specify device index for device_guard (#24555)
* add the support of device index for device_guard.
上级
3016a4ac
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
1 deletion
+48
-1
paddle/fluid/framework/operator.cc
paddle/fluid/framework/operator.cc
+10
-1
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+7
-0
python/paddle/fluid/tests/unittests/test_device_guard.py
python/paddle/fluid/tests/unittests/test_device_guard.py
+31
-0
未找到文件。
paddle/fluid/framework/operator.cc
浏览文件 @
29de0d97
...
...
@@ -1050,7 +1050,16 @@ void OperatorWithKernel::ChooseKernel(const RuntimeContext& ctx,
if
(
HasAttr
(
"op_device"
))
{
if
(
Attr
<
std
::
string
>
(
"op_device"
)
==
"cpu"
)
{
expected_kernel_key
.
place_
=
platform
::
CPUPlace
();
}
else
if
(
Attr
<
std
::
string
>
(
"op_device"
)
==
"gpu"
)
{
}
else
if
(
Attr
<
std
::
string
>
(
"op_device"
).
find
(
"gpu"
)
!=
std
::
string
::
npos
)
{
auto
device
=
Attr
<
std
::
string
>
(
"op_device"
);
size_t
pos
=
device
.
find
(
':'
);
if
(
pos
!=
std
::
string
::
npos
)
{
device
=
device
.
substr
(
0
,
pos
);
LOG_FIRST_N
(
WARNING
,
1
)
<<
"Device index is only supported under pipeline parallelism, "
<<
"so it will be ignored."
;
}
// when the Op that only has CPUKernel is assigned to GPU, the CPUKernel
// will be executed and a warning will be given at the same time.
if
(
SupportGPU
())
{
...
...
python/paddle/fluid/framework.py
浏览文件 @
29de0d97
...
...
@@ -5455,10 +5455,17 @@ def device_guard(device=None):
result = exe.run(fetch_list=[out])
"""
index
=
None
if
device
and
':'
in
device
:
device
,
index
=
device
.
split
(
':'
)
if
device
==
'cpu'
:
raise
ValueError
(
"Should not set device id for cpu."
)
if
device
not
in
[
'cpu'
,
'gpu'
,
''
,
None
]:
raise
ValueError
(
"The Attr(device) should be 'cpu' or 'gpu', and it can also be empty string or None "
"when there is no need to specify device. But received %s"
%
device
)
if
index
:
device
=
":"
.
join
([
device
,
index
])
pre_device
=
switch_device
(
device
)
try
:
yield
...
...
python/paddle/fluid/tests/unittests/test_device_guard.py
浏览文件 @
29de0d97
...
...
@@ -59,6 +59,31 @@ class TestDeviceGuard(unittest.TestCase):
execute
(
main_program
,
startup_program
)
def
test_device_guard_with_id
(
self
):
main_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main_program
,
startup_program
):
data1
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
,
3
,
8
,
8
],
value
=
0.5
,
dtype
=
'float32'
)
data2
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
,
3
,
5
,
5
],
value
=
0.5
,
dtype
=
'float32'
)
shape
=
fluid
.
layers
.
shape
(
data2
)
with
fluid
.
device_guard
(
"cpu"
):
shape
=
fluid
.
layers
.
slice
(
shape
,
axes
=
[
0
],
starts
=
[
0
],
ends
=
[
4
])
with
fluid
.
device_guard
(
"gpu:1"
):
out
=
fluid
.
layers
.
crop_tensor
(
data1
,
shape
=
shape
)
# check if the device attr is set correctly
all_ops
=
main_program
.
global_block
().
ops
device_attr_name
=
core
.
op_proto_and_checker_maker
.
kOpDeviceAttrName
()
for
op
in
all_ops
:
if
op
.
type
==
'slice'
:
self
.
assertEqual
(
op
.
desc
.
attr
(
device_attr_name
),
"cpu"
)
if
op
.
type
==
'crop_tensor'
:
self
.
assertEqual
(
op
.
desc
.
attr
(
device_attr_name
),
"gpu:1"
)
execute
(
main_program
,
startup_program
)
def
test_cpu_only_op
(
self
):
main_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
...
...
@@ -123,7 +148,13 @@ class TestDeviceGuard(unittest.TestCase):
out
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
value
=
0.2
,
dtype
=
'float32'
)
def
device_attr2
():
with
fluid
.
device_guard
(
"cpu:1"
):
out
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
value
=
0.2
,
dtype
=
'float32'
)
self
.
assertRaises
(
ValueError
,
device_attr
)
self
.
assertRaises
(
ValueError
,
device_attr2
)
def
test_warning
(
self
):
main_program
=
fluid
.
Program
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录