Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e5fef8f3
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看板
未验证
提交
e5fef8f3
编写于
3月 21, 2020
作者:
Z
Zeng Jinle
提交者:
GitHub
3月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dygraph double grad]Code polish (#23121)
* fix dygraph double grad, test=develop * fix unpack constructor, test=develop
上级
9258e960
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
48 deletion
+43
-48
paddle/fluid/imperative/layer.cc
paddle/fluid/imperative/layer.cc
+7
-12
paddle/fluid/imperative/layer.h
paddle/fluid/imperative/layer.h
+3
-2
paddle/fluid/imperative/partial_grad_engine.cc
paddle/fluid/imperative/partial_grad_engine.cc
+33
-34
未找到文件。
paddle/fluid/imperative/layer.cc
浏览文件 @
e5fef8f3
...
@@ -210,15 +210,10 @@ std::string LayerDebugString(const std::string& op_type,
...
@@ -210,15 +210,10 @@ std::string LayerDebugString(const std::string& op_type,
return
LayerDebugStringImpl
<
VariableWrapper
>
(
op_type
,
ins
,
outs
);
return
LayerDebugStringImpl
<
VariableWrapper
>
(
op_type
,
ins
,
outs
);
}
}
VarBase
::
VarBase
(
bool
has_grad
,
const
std
::
shared_ptr
<
VariableWrapper
>&
var
)
VarBase
::
VarBase
(
const
std
::
shared_ptr
<
VariableWrapper
>&
var
)
:
var_
(
var
),
grad_node_
(
var
->
GetGradNode
())
{
:
var_
(
var
),
grad_node_
(
var
->
GetGradNode
())
{
if
(
has_grad
)
{
if
(
auto
grad_var
=
var_
->
GetGradVar
())
{
if
(
auto
grad_var
=
var_
->
GetGradVar
())
{
grad_var_
=
std
::
make_shared
<
VarBase
>
(
grad_var
);
grad_var_
=
std
::
make_shared
<
VarBase
>
(
false
,
grad_var
);
}
else
{
grad_var_
=
std
::
make_shared
<
VarBase
>
(
false
,
GradVarName
());
var_
->
SetGradVar
(
grad_var_
->
var_
);
}
}
}
if
(
IsDebugEnabled
())
{
if
(
IsDebugEnabled
())
{
...
@@ -417,10 +412,10 @@ std::shared_ptr<GradOpNode> CreateGradOpNode(
...
@@ -417,10 +412,10 @@ std::shared_ptr<GradOpNode> CreateGradOpNode(
auto
grad_node
=
info
.
dygraph_grad_op_maker_
(
op
.
Type
(),
ins
,
outs
,
attrs
);
auto
grad_node
=
info
.
dygraph_grad_op_maker_
(
op
.
Type
(),
ins
,
outs
,
attrs
);
if
(
grad_node
&&
!
grad_node
->
empty
())
{
if
(
grad_node
&&
!
grad_node
->
empty
())
{
for
(
auto
&
op
:
*
grad_node
)
{
for
(
auto
&
grad_
op
:
*
grad_node
)
{
op
.
SetId
(
OpBase
::
GenerateUniqueId
());
grad_
op
.
SetId
(
OpBase
::
GenerateUniqueId
());
op
.
SetPlace
(
place
);
grad_
op
.
SetPlace
(
place
);
ClearNoNeedBufferInputs
(
&
op
);
ClearNoNeedBufferInputs
(
&
grad_
op
);
}
}
return
grad_node
;
return
grad_node
;
}
else
{
}
else
{
...
...
paddle/fluid/imperative/layer.h
浏览文件 @
e5fef8f3
...
@@ -76,7 +76,8 @@ class VarBase {
...
@@ -76,7 +76,8 @@ class VarBase {
explicit
VarBase
(
const
std
::
string
&
name
)
:
VarBase
(
true
,
name
)
{}
explicit
VarBase
(
const
std
::
string
&
name
)
:
VarBase
(
true
,
name
)
{}
// NOTE(zengjinle): be careful when you use this constructor!!!
// NOTE(zengjinle): be careful when you use this constructor!!!
explicit
VarBase
(
bool
has_grad
,
const
std
::
shared_ptr
<
VariableWrapper
>&
var
);
// Unpack VarBase from VariableWrapper.
explicit
VarBase
(
const
std
::
shared_ptr
<
VariableWrapper
>&
var
);
~
VarBase
()
{
~
VarBase
()
{
VLOG
(
10
)
<<
"Destruct VarBase: "
<<
Name
();
VLOG
(
10
)
<<
"Destruct VarBase: "
<<
Name
();
...
@@ -100,7 +101,7 @@ class VarBase {
...
@@ -100,7 +101,7 @@ class VarBase {
const
std
::
shared_ptr
<
VarBase
>&
MutableGradVarBase
()
{
const
std
::
shared_ptr
<
VarBase
>&
MutableGradVarBase
()
{
if
(
grad_var_
==
nullptr
)
{
if
(
grad_var_
==
nullptr
)
{
if
(
auto
grad_var_wrapper
=
var_
->
GetGradVar
())
{
if
(
auto
grad_var_wrapper
=
var_
->
GetGradVar
())
{
grad_var_
=
std
::
make_shared
<
VarBase
>
(
false
,
grad_var_wrapper
);
grad_var_
=
std
::
make_shared
<
VarBase
>
(
grad_var_wrapper
);
}
else
{
}
else
{
grad_var_
=
std
::
make_shared
<
VarBase
>
(
false
,
GradVarName
());
grad_var_
=
std
::
make_shared
<
VarBase
>
(
false
,
GradVarName
());
var_
->
SetGradVar
(
grad_var_
->
var_
);
var_
->
SetGradVar
(
grad_var_
->
var_
);
...
...
paddle/fluid/imperative/partial_grad_engine.cc
浏览文件 @
e5fef8f3
...
@@ -719,7 +719,7 @@ PartialGradTask::PartialGradTask(
...
@@ -719,7 +719,7 @@ PartialGradTask::PartialGradTask(
auto
grad_accumulator_iter
=
grad_accumulators_
.
find
(
mapped_out_grad_var
);
auto
grad_accumulator_iter
=
grad_accumulators_
.
find
(
mapped_out_grad_var
);
if
(
grad_accumulator_iter
==
grad_accumulators_
.
end
())
{
if
(
grad_accumulator_iter
==
grad_accumulators_
.
end
())
{
ready_grad_vars_
.
Set
(
mapped_out_grad_var
,
ready_grad_vars_
.
Set
(
mapped_out_grad_var
,
std
::
make_shared
<
VarBase
>
(
false
,
out_grad_var
));
std
::
make_shared
<
VarBase
>
(
out_grad_var
));
VLOG
(
10
)
<<
"Fill 1.0f or user-provided gradient as ready var "
VLOG
(
10
)
<<
"Fill 1.0f or user-provided gradient as ready var "
<<
out_grad_var
->
Name
();
<<
out_grad_var
->
Name
();
}
else
{
}
else
{
...
@@ -783,7 +783,7 @@ void PartialGradTask::RunEachOp(const OpBase *op) {
...
@@ -783,7 +783,7 @@ void PartialGradTask::RunEachOp(const OpBase *op) {
if
(
!
input_pair
.
second
.
IsGrad
())
{
if
(
!
input_pair
.
second
.
IsGrad
())
{
for
(
auto
&
fwd_var
:
input_pair
.
second
)
{
for
(
auto
&
fwd_var
:
input_pair
.
second
)
{
if
(
fwd_var
)
{
if
(
fwd_var
)
{
new_inputs
.
emplace_back
(
new
VarBase
(
true
,
fwd_var
));
new_inputs
.
emplace_back
(
new
VarBase
(
fwd_var
));
VLOG
(
10
)
<<
"Unpacked forward var "
<<
fwd_var
->
Name
()
VLOG
(
10
)
<<
"Unpacked forward var "
<<
fwd_var
->
Name
()
<<
", grad ops: "
<<
GradOpTypes
(
*
new_inputs
.
back
());
<<
", grad ops: "
<<
GradOpTypes
(
*
new_inputs
.
back
());
}
else
{
}
else
{
...
@@ -813,7 +813,7 @@ void PartialGradTask::RunEachOp(const OpBase *op) {
...
@@ -813,7 +813,7 @@ void PartialGradTask::RunEachOp(const OpBase *op) {
for
(
auto
&
fwd_var
:
output_pair
.
second
)
{
for
(
auto
&
fwd_var
:
output_pair
.
second
)
{
// unpack forward var
// unpack forward var
if
(
fwd_var
)
{
if
(
fwd_var
)
{
new_outputs
.
emplace_back
(
new
VarBase
(
true
,
fwd_var
));
new_outputs
.
emplace_back
(
new
VarBase
(
fwd_var
));
VLOG
(
10
)
<<
"Unpacked forward var "
<<
fwd_var
->
Name
();
VLOG
(
10
)
<<
"Unpacked forward var "
<<
fwd_var
->
Name
();
}
else
{
}
else
{
new_outputs
.
emplace_back
();
new_outputs
.
emplace_back
();
...
@@ -878,44 +878,43 @@ void PartialGradTask::RunEachOp(const OpBase *op) {
...
@@ -878,44 +878,43 @@ void PartialGradTask::RunEachOp(const OpBase *op) {
auto
partial_grad_grads
=
accumulator_info
->
SumGradient
(
auto
partial_grad_grads
=
accumulator_info
->
SumGradient
(
std
::
move
(
grad_var
),
op
->
id
(),
&
is_finished
);
std
::
move
(
grad_var
),
op
->
id
(),
&
is_finished
);
if
(
!
partial_grad_grads
.
empty
())
{
auto
sum_grad_var_grad
=
accumulator_info
->
GradVarBase
()
->
MutableGradVarBase
();
sum_grad_var_grad
->
SetOverridedStopGradient
(
false
);
auto
assign_node
=
std
::
make_shared
<
GradOpNode
>
();
sum_grad_var_grad
->
SetGradNode
(
assign_node
);
VLOG
(
10
)
<<
"Add "
<<
partial_grad_grads
.
size
()
<<
" assign op for "
<<
sum_grad_var_grad
->
Name
();
for
(
auto
&
grad_grad
:
partial_grad_grads
)
{
auto
*
assign_op
=
&
(
assign_node
->
emplace_back
());
assign_op
->
SetType
(
"assign"
);
// Can use "scale" as static graph mode
assign_op
->
SetInput
(
"X"
,
{
sum_grad_var_grad
->
SharedVar
()},
true
);
assign_op
->
SetOutput
(
"Out"
,
{
grad_grad
},
true
);
assign_op
->
CheckAttrs
();
assign_op
->
SetId
(
OpBase
::
GenerateUniqueId
());
assign_op
->
SetPlace
(
op
->
place
());
if
(
auto
grad_pending_node
=
grad_grad
->
GetGradNode
())
{
assign_node
->
InsertGradPendingNode
(
std
::
move
(
grad_pending_node
));
}
}
VLOG
(
10
)
<<
"Pending ops of assign is "
<<
GradPendingOpTypes
(
*
assign_node
);
double_grad_nodes_
.
emplace_back
(
assign_node
);
}
if
(
is_finished
)
{
if
(
is_finished
)
{
VLOG
(
10
)
<<
"Sum has finished for "
VLOG
(
10
)
<<
"Sum has finished for "
<<
accumulator_info
->
MappedGradVar
()
->
Name
()
<<
" "
<<
accumulator_info
->
MappedGradVar
()
->
Name
()
<<
" "
<<
accumulator_info
->
GradVarBase
();
<<
accumulator_info
->
GradVarBase
();
ready_grad_vars_
.
Set
(
accumulator_info
->
MappedGradVar
(),
ready_grad_vars_
.
Set
(
accumulator_info
->
MappedGradVar
(),
accumulator_info
->
GradVarBase
());
accumulator_info
->
GradVarBase
());
grad_accumulators_
.
erase
(
accumulator_info
->
MappedGradVar
());
}
}
if
(
partial_grad_grads
.
empty
())
{
continue
;
}
auto
sum_grad_var_grad
=
accumulator_info
->
GradVarBase
()
->
MutableGradVarBase
();
sum_grad_var_grad
->
SetOverridedStopGradient
(
false
);
auto
assign_node
=
std
::
make_shared
<
GradOpNode
>
();
sum_grad_var_grad
->
SetGradNode
(
assign_node
);
VLOG
(
10
)
<<
"Add "
<<
partial_grad_grads
.
size
()
<<
" assign op for "
<<
sum_grad_var_grad
->
Name
();
for
(
auto
&
grad_grad
:
partial_grad_grads
)
{
auto
*
assign_op
=
&
(
assign_node
->
emplace_back
());
assign_op
->
SetType
(
"assign"
);
// Can use "scale" as static graph mode
assign_op
->
SetInput
(
"X"
,
{
sum_grad_var_grad
->
SharedVar
()},
true
);
assign_op
->
SetOutput
(
"Out"
,
{
grad_grad
},
true
);
assign_op
->
CheckAttrs
();
assign_op
->
SetId
(
OpBase
::
GenerateUniqueId
());
assign_op
->
SetPlace
(
op
->
place
());
if
(
auto
grad_pending_node
=
grad_grad
->
GetGradNode
())
{
assign_node
->
InsertGradPendingNode
(
std
::
move
(
grad_pending_node
));
}
}
VLOG
(
10
)
<<
"Pending ops of assign is "
<<
GradPendingOpTypes
(
*
assign_node
);
grad_accumulators_
.
erase
(
accumulator_info
->
MappedGradVar
());
double_grad_nodes_
.
emplace_back
(
assign_node
);
}
}
grads_to_accumulate_
.
clear
();
grads_to_accumulate_
.
clear
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录