Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
9e045eeb
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
9e045eeb
编写于
5月 17, 2023
作者:
D
duanyanhui
提交者:
GitHub
5月 17, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[CustomDevice] suport device_guard for custom device (#53808)
* suport device_guard for npu * fix comment * fix typo
上级
734dc448
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
87 addition
and
3 deletion
+87
-3
paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc
...id/framework/new_executor/interpreter/interpreter_util.cc
+37
-0
paddle/fluid/framework/operator.cc
paddle/fluid/framework/operator.cc
+42
-1
paddle/fluid/framework/operator.h
paddle/fluid/framework/operator.h
+3
-0
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+5
-2
未找到文件。
paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc
浏览文件 @
9e045eeb
...
...
@@ -36,6 +36,9 @@
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
#include "paddle/phi/backends/device_manager.h"
#endif
PADDLE_DEFINE_EXPORTED_bool
(
new_executor_log_memory_stats
,
false
,
...
...
@@ -400,6 +403,40 @@ void ApplyDeviceGuard(const OperatorBase* op_base,
}
VLOG
(
3
)
<<
"Switch into "
<<
expected_kernel_key
->
place_
<<
" by device_guard."
;
}
else
if
(
platform
::
is_custom_place
(
place
))
{
#ifdef PADDLE_WITH_CUSTOM_DEVICE
auto
device_types
=
phi
::
DeviceManager
::
GetAllCustomDeviceTypes
();
bool
is_custom_device_op
=
false
;
for
(
auto
dev_type
:
device_types
)
{
if
(
op_device
.
find
(
dev_type
)
!=
std
::
string
::
npos
)
{
is_custom_device_op
=
true
;
break
;
}
}
PADDLE_ENFORCE_EQ
(
is_custom_device_op
,
true
,
phi
::
errors
::
Unimplemented
(
"Unsupported current device %s with Paddle CustomDevice "
,
op_device
));
#else
VLOG
(
1
)
<<
string
::
Sprintf
(
"Cannot use get_all_custom_device_type because you have installed"
"CPU/GPU version PaddlePaddle.
\n
"
"If you want to use get_all_custom_device_type, please try to "
"install CustomDevice version "
"PaddlePaddle by: pip install paddlepaddle
\n
"
);
#endif
if
(
op_base
->
SupportCustomDevice
())
{
expected_kernel_key
->
place_
=
place
;
}
else
{
expected_kernel_key
->
place_
=
platform
::
CPUPlace
();
LOG_FIRST_N
(
WARNING
,
1
)
<<
"Op("
<<
op_base
->
Type
()
<<
") has no Custom Place implementation. It "
"will be assigned to CPUPlace."
;
}
VLOG
(
3
)
<<
"Switch into "
<<
expected_kernel_key
->
place_
<<
" by device_guard."
;
}
else
{
PADDLE_THROW
(
platform
::
errors
::
Fatal
(
"Unsupported current place %s"
,
op_device
));
...
...
paddle/fluid/framework/operator.cc
浏览文件 @
9e045eeb
...
...
@@ -1365,6 +1365,42 @@ bool OperatorWithKernel::SupportXPU() const {
#endif
}
bool
OperatorWithKernel
::
SupportCustomDevice
()
const
{
#ifdef PADDLE_WITH_CUSTOM_DEVICE
auto
phi_kernels
=
phi
::
KernelFactory
::
Instance
().
SelectKernelMap
(
phi
::
TransToPhiKernelName
(
type_
));
auto
has_phi_kernel
=
std
::
any_of
(
phi_kernels
.
begin
(),
phi_kernels
.
end
(),
[](
phi
::
KernelKeyMap
::
const_reference
kern_pair
)
{
return
platform
::
is_custom_place
(
phi
::
TransToPhiPlace
(
kern_pair
.
first
.
backend
()));
});
if
(
has_phi_kernel
)
{
return
true
;
}
else
{
auto
kernel_iter
=
OperatorWithKernel
::
AllOpKernels
().
find
(
type_
);
if
(
kernel_iter
==
OperatorWithKernel
::
AllOpKernels
().
end
())
{
return
false
;
}
else
{
auto
&
op_kernels
=
kernel_iter
->
second
;
return
std
::
any_of
(
op_kernels
.
begin
(),
op_kernels
.
end
(),
[
this
](
OpKernelMap
::
const_reference
kern_pair
)
{
return
platform
::
is_custom_place
(
kern_pair
.
first
.
place_
);
});
}
}
#else
PADDLE_THROW
(
platform
::
errors
::
PreconditionNotMet
(
"should not call OperatorWithKernel::SupportCustomDevice() when not "
"compiled with "
"CustomDevice support."
));
return
false
;
#endif
}
bool
OperatorWithKernel
::
SupportsMKLDNN
(
const
phi
::
DataType
data_type
)
const
{
auto
phi_kernels
=
phi
::
KernelFactory
::
Instance
().
SelectKernelMap
(
phi
::
TransToPhiKernelName
(
type_
));
...
...
@@ -2088,7 +2124,12 @@ OpKernelType OperatorWithKernel::InnerGetExpectedKernelType(
// CPUKernel will be executed and a warning will be given at the same
// time.
expected_kernel_key
.
place_
=
platform
::
CPUPlace
();
#ifdef PADDLE_WITH_CUSTOM_DEVICE
if
(
SupportCustomDevice
())
{
auto
&
dev_ctx
=
ctx
.
device_context
();
expected_kernel_key
.
place_
=
dev_ctx
.
GetPlace
();
}
#endif
if
(
platform
::
is_cpu_place
(
expected_kernel_key
.
place_
))
{
LOG_FIRST_N
(
WARNING
,
1
)
<<
"Op("
<<
type_
...
...
paddle/fluid/framework/operator.h
浏览文件 @
9e045eeb
...
...
@@ -288,6 +288,7 @@ class OperatorBase {
virtual
bool
SupportGPU
()
const
{
return
false
;
}
virtual
bool
SupportXPU
()
const
{
return
false
;
}
virtual
bool
SupportCustomDevice
()
const
{
return
false
;
}
const
std
::
string
&
Type
()
const
{
return
type_
;
}
...
...
@@ -748,6 +749,8 @@ class OperatorWithKernel : public OperatorBase {
bool
SupportXPU
()
const
override
;
bool
SupportCustomDevice
()
const
override
;
bool
SupportsMKLDNN
(
phi
::
DataType
data_type
)
const
;
bool
SupportsCUDNN
(
phi
::
DataType
data_type
)
const
;
...
...
python/paddle/fluid/framework.py
浏览文件 @
9e045eeb
...
...
@@ -7431,9 +7431,12 @@ def device_guard(device=None):
device
,
index
=
device
.
split
(
':'
)
if
device
==
'cpu'
:
raise
ValueError
(
"Should not set device id for cpu."
)
if
device
not
in
[
'cpu'
,
'gpu'
,
'xpu'
,
''
,
None
]:
if
(
device
not
in
[
'cpu'
,
'gpu'
,
'xpu'
,
''
,
None
]
and
device
not
in
core
.
get_all_custom_device_type
()
):
raise
ValueError
(
"The Attr(device) should be 'cpu'
or 'gpu'
, and it can also be empty string or None "
"The Attr(device) should be 'cpu'
, 'xpu', 'gpu' or custom device
, and it can also be empty string or None "
"when there is no need to specify device. But received %s"
%
device
)
if
index
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录