Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
775ddb5a
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看板
未验证
提交
775ddb5a
编写于
3月 30, 2022
作者:
C
Chen Weihang
提交者:
GitHub
3月 30, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix double grad var judging (#41072)
上级
83efeeae
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
28 addition
and
15 deletion
+28
-15
paddle/fluid/framework/custom_operator.cc
paddle/fluid/framework/custom_operator.cc
+28
-15
未找到文件。
paddle/fluid/framework/custom_operator.cc
浏览文件 @
775ddb5a
...
@@ -62,11 +62,6 @@ static T* DynLoad(void* handle, std::string name) {
...
@@ -62,11 +62,6 @@ static T* DynLoad(void* handle, std::string name) {
return
func
;
return
func
;
}
}
inline
static
bool
IsGradVar
(
const
std
::
string
&
var_name
)
{
std
::
string
suffix
=
kGradVarSuffix
;
return
var_name
.
rfind
(
suffix
)
!=
std
::
string
::
npos
;
}
inline
static
bool
IsDuplicableVar
(
const
std
::
string
&
var_name
)
{
inline
static
bool
IsDuplicableVar
(
const
std
::
string
&
var_name
)
{
std
::
string
suffix
=
kTensorVectorSuffix
;
std
::
string
suffix
=
kTensorVectorSuffix
;
return
var_name
.
rfind
(
suffix
)
!=
std
::
string
::
npos
;
return
var_name
.
rfind
(
suffix
)
!=
std
::
string
::
npos
;
...
@@ -77,6 +72,17 @@ inline static std::string NoGrad(const std::string& var_name) {
...
@@ -77,6 +72,17 @@ inline static std::string NoGrad(const std::string& var_name) {
return
var_name
.
substr
(
0
,
var_name
.
size
()
-
kGradVarSuffixSize
);
return
var_name
.
substr
(
0
,
var_name
.
size
()
-
kGradVarSuffixSize
);
}
}
inline
static
bool
IsGradVar
(
const
std
::
string
&
var_name
,
bool
is_double_grad
)
{
std
::
string
suffix
=
kGradVarSuffix
;
if
(
!
is_double_grad
)
{
return
var_name
.
rfind
(
suffix
)
!=
std
::
string
::
npos
;
}
else
{
// for double grad cases, the X@GRAD is not a grad var, X@GRAD@GRAD is a
// grad var, here we remove a @GRAD suffix
return
NoGrad
(
var_name
).
rfind
(
suffix
)
!=
std
::
string
::
npos
;
}
}
inline
static
bool
IsMemberOf
(
const
std
::
vector
<
std
::
string
>&
vec
,
inline
static
bool
IsMemberOf
(
const
std
::
vector
<
std
::
string
>&
vec
,
const
std
::
string
&
name
)
{
const
std
::
string
&
name
)
{
return
std
::
find
(
vec
.
cbegin
(),
vec
.
cend
(),
name
)
!=
vec
.
cend
();
return
std
::
find
(
vec
.
cbegin
(),
vec
.
cend
(),
name
)
!=
vec
.
cend
();
...
@@ -493,11 +499,12 @@ class CustomGradOpMaker<OpDesc> : public SingleGradOpMaker<OpDesc> {
...
@@ -493,11 +499,12 @@ class CustomGradOpMaker<OpDesc> : public SingleGradOpMaker<OpDesc> {
std
::
unordered_map
<
std
::
string
,
std
::
string
>*
grad_to_var
,
std
::
unordered_map
<
std
::
string
,
std
::
string
>*
grad_to_var
,
const
std
::
vector
<
BlockDesc
*>&
grad_block
,
const
std
::
string
&
name
,
const
std
::
vector
<
BlockDesc
*>&
grad_block
,
const
std
::
string
&
name
,
const
std
::
vector
<
std
::
string
>&
inputs
,
const
std
::
vector
<
std
::
string
>&
inputs
,
const
std
::
vector
<
std
::
string
>&
outputs
)
const
std
::
vector
<
std
::
string
>&
outputs
,
bool
is_double_grad
)
:
SingleGradOpMaker
<
OpDesc
>
(
fwd_op
,
no_grad_set
,
grad_to_var
,
grad_block
),
:
SingleGradOpMaker
<
OpDesc
>
(
fwd_op
,
no_grad_set
,
grad_to_var
,
grad_block
),
name_
(
name
),
name_
(
name
),
inputs_
(
inputs
),
inputs_
(
inputs
),
outputs_
(
outputs
)
{}
outputs_
(
outputs
),
is_double_grad_
(
is_double_grad
)
{}
protected:
protected:
void
Apply
(
GradOpPtr
<
OpDesc
>
grad_op
)
const
override
{
void
Apply
(
GradOpPtr
<
OpDesc
>
grad_op
)
const
override
{
...
@@ -508,7 +515,7 @@ class CustomGradOpMaker<OpDesc> : public SingleGradOpMaker<OpDesc> {
...
@@ -508,7 +515,7 @@ class CustomGradOpMaker<OpDesc> : public SingleGradOpMaker<OpDesc> {
for
(
auto
&
in_name
:
inputs_
)
{
for
(
auto
&
in_name
:
inputs_
)
{
VLOG
(
3
)
<<
"Custom Operator: GradOpDescMaker - input: "
<<
in_name
;
VLOG
(
3
)
<<
"Custom Operator: GradOpDescMaker - input: "
<<
in_name
;
if
(
!
detail
::
IsGradVar
(
in_name
))
{
if
(
!
detail
::
IsGradVar
(
in_name
,
is_double_grad_
))
{
if
(
detail
::
IsMemberOf
(
fwd_op_inputs
,
in_name
))
{
if
(
detail
::
IsMemberOf
(
fwd_op_inputs
,
in_name
))
{
grad_op
->
SetInput
(
in_name
,
this
->
Input
(
in_name
));
grad_op
->
SetInput
(
in_name
,
this
->
Input
(
in_name
));
}
else
if
(
detail
::
IsMemberOf
(
fwd_op_outputs
,
in_name
))
{
}
else
if
(
detail
::
IsMemberOf
(
fwd_op_outputs
,
in_name
))
{
...
@@ -540,6 +547,7 @@ class CustomGradOpMaker<OpDesc> : public SingleGradOpMaker<OpDesc> {
...
@@ -540,6 +547,7 @@ class CustomGradOpMaker<OpDesc> : public SingleGradOpMaker<OpDesc> {
std
::
string
name_
;
std
::
string
name_
;
std
::
vector
<
std
::
string
>
inputs_
;
std
::
vector
<
std
::
string
>
inputs_
;
std
::
vector
<
std
::
string
>
outputs_
;
std
::
vector
<
std
::
string
>
outputs_
;
bool
is_double_grad_
{
false
};
};
};
template
<
>
template
<
>
...
@@ -553,12 +561,13 @@ class CustomGradOpMaker<imperative::OpBase>
...
@@ -553,12 +561,13 @@ class CustomGradOpMaker<imperative::OpBase>
const
AttributeMap
&
attrs
,
const
AttributeMap
&
attrs
,
const
std
::
map
<
std
::
string
,
std
::
string
>&
inplace_map
,
const
std
::
map
<
std
::
string
,
std
::
string
>&
inplace_map
,
const
std
::
string
&
name
,
const
std
::
vector
<
std
::
string
>&
inputs
,
const
std
::
string
&
name
,
const
std
::
vector
<
std
::
string
>&
inputs
,
const
std
::
vector
<
std
::
string
>&
outputs
)
const
std
::
vector
<
std
::
string
>&
outputs
,
bool
is_double_grad
)
:
SingleGradOpMaker
<
imperative
::
OpBase
>
(
:
SingleGradOpMaker
<
imperative
::
OpBase
>
(
type
,
var_base_map_in
,
var_base_map_out
,
attrs
,
inplace_map
),
type
,
var_base_map_in
,
var_base_map_out
,
attrs
,
inplace_map
),
name_
(
name
),
name_
(
name
),
inputs_
(
inputs
),
inputs_
(
inputs
),
outputs_
(
outputs
)
{}
outputs_
(
outputs
),
is_double_grad_
(
is_double_grad
)
{}
protected:
protected:
// TODO(chenweihang): The code is duplicated with the previous one, because
// TODO(chenweihang): The code is duplicated with the previous one, because
...
@@ -574,7 +583,7 @@ class CustomGradOpMaker<imperative::OpBase>
...
@@ -574,7 +583,7 @@ class CustomGradOpMaker<imperative::OpBase>
for
(
auto
&
in_name
:
inputs_
)
{
for
(
auto
&
in_name
:
inputs_
)
{
VLOG
(
3
)
<<
"Custom Operator: GradOpBaseMaker - input: "
<<
in_name
;
VLOG
(
3
)
<<
"Custom Operator: GradOpBaseMaker - input: "
<<
in_name
;
if
(
!
detail
::
IsGradVar
(
in_name
))
{
if
(
!
detail
::
IsGradVar
(
in_name
,
is_double_grad_
))
{
if
(
detail
::
IsMemberOf
(
fwd_op_inputs
,
in_name
))
{
if
(
detail
::
IsMemberOf
(
fwd_op_inputs
,
in_name
))
{
grad_op
->
SetInput
(
in_name
,
this
->
Input
(
in_name
));
grad_op
->
SetInput
(
in_name
,
this
->
Input
(
in_name
));
}
else
if
(
detail
::
IsMemberOf
(
fwd_op_outputs
,
in_name
))
{
}
else
if
(
detail
::
IsMemberOf
(
fwd_op_outputs
,
in_name
))
{
...
@@ -600,6 +609,7 @@ class CustomGradOpMaker<imperative::OpBase>
...
@@ -600,6 +609,7 @@ class CustomGradOpMaker<imperative::OpBase>
std
::
string
name_
;
std
::
string
name_
;
std
::
vector
<
std
::
string
>
inputs_
;
std
::
vector
<
std
::
string
>
inputs_
;
std
::
vector
<
std
::
string
>
outputs_
;
std
::
vector
<
std
::
string
>
outputs_
;
bool
is_double_grad_
{
false
};
};
};
//////////// Operator and Kernel Register //////////////
//////////// Operator and Kernel Register //////////////
...
@@ -832,21 +842,24 @@ void RegisterOperatorWithMetaInfo(const std::vector<OpMetaInfo>& op_meta_infos,
...
@@ -832,21 +842,24 @@ void RegisterOperatorWithMetaInfo(const std::vector<OpMetaInfo>& op_meta_infos,
VLOG
(
3
)
<<
"Custom Operator: backward, op outputs: "
VLOG
(
3
)
<<
"Custom Operator: backward, op outputs: "
<<
string
::
join_strings
(
grad_op_outputs
,
','
);
<<
string
::
join_strings
(
grad_op_outputs
,
','
);
bool
is_double_grad
=
(
i
==
2
);
// GradOpDescMaker
// GradOpDescMaker
info
.
grad_op_maker_
=
[
grad_op_name
,
grad_op_inputs
,
grad_op_outputs
](
info
.
grad_op_maker_
=
[
grad_op_name
,
grad_op_inputs
,
grad_op_outputs
,
is_double_grad
](
const
OpDesc
&
fwd_op
,
const
OpDesc
&
fwd_op
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_set
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_set
,
std
::
unordered_map
<
std
::
string
,
std
::
string
>*
grad_to_var
,
std
::
unordered_map
<
std
::
string
,
std
::
string
>*
grad_to_var
,
const
std
::
vector
<
BlockDesc
*>&
grad_block
)
{
const
std
::
vector
<
BlockDesc
*>&
grad_block
)
{
CustomGradOpMaker
<
paddle
::
framework
::
OpDesc
>
maker
(
CustomGradOpMaker
<
paddle
::
framework
::
OpDesc
>
maker
(
fwd_op
,
no_grad_set
,
grad_to_var
,
grad_block
,
grad_op_name
,
fwd_op
,
no_grad_set
,
grad_to_var
,
grad_block
,
grad_op_name
,
grad_op_inputs
,
grad_op_outputs
);
grad_op_inputs
,
grad_op_outputs
,
is_double_grad
);
return
maker
();
return
maker
();
};
};
// GradOpBaseMaker
// GradOpBaseMaker
info
.
dygraph_grad_op_maker_
=
[
grad_op_name
,
grad_op_inputs
,
info
.
dygraph_grad_op_maker_
=
[
grad_op_name
,
grad_op_inputs
,
grad_op_outputs
](
grad_op_outputs
,
is_double_grad
](
const
std
::
string
&
type
,
const
std
::
string
&
type
,
const
imperative
::
NameVarBaseMap
&
var_base_map_in
,
const
imperative
::
NameVarBaseMap
&
var_base_map_in
,
const
imperative
::
NameVarBaseMap
&
var_base_map_out
,
const
imperative
::
NameVarBaseMap
&
var_base_map_out
,
...
@@ -855,7 +868,7 @@ void RegisterOperatorWithMetaInfo(const std::vector<OpMetaInfo>& op_meta_infos,
...
@@ -855,7 +868,7 @@ void RegisterOperatorWithMetaInfo(const std::vector<OpMetaInfo>& op_meta_infos,
const
std
::
map
<
std
::
string
,
std
::
string
>&
inplace_map
)
{
const
std
::
map
<
std
::
string
,
std
::
string
>&
inplace_map
)
{
CustomGradOpMaker
<
paddle
::
imperative
::
OpBase
>
maker
(
CustomGradOpMaker
<
paddle
::
imperative
::
OpBase
>
maker
(
type
,
var_base_map_in
,
var_base_map_out
,
attrs
,
inplace_map
,
type
,
var_base_map_in
,
var_base_map_out
,
attrs
,
inplace_map
,
grad_op_name
,
grad_op_inputs
,
grad_op_outputs
);
grad_op_name
,
grad_op_inputs
,
grad_op_outputs
,
is_double_grad
);
maker
.
SetDygraphDefaultAttrsMap
(
default_attrs
);
maker
.
SetDygraphDefaultAttrsMap
(
default_attrs
);
return
maker
();
return
maker
();
};
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录