Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
a270dbb7
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a270dbb7
编写于
10月 04, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for rnn_op
上级
c61e82bc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
14 deletion
+48
-14
paddle/framework/backward.cc
paddle/framework/backward.cc
+41
-12
paddle/framework/block_desc.h
paddle/framework/block_desc.h
+7
-2
未找到文件。
paddle/framework/backward.cc
浏览文件 @
a270dbb7
...
...
@@ -20,6 +20,7 @@
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/program_desc.h"
#include "paddle/operators/net_op.h"
#include "paddle/operators/recurrent_op.h"
...
...
@@ -254,7 +255,7 @@ static bool AllGradInSet(const std::vector<std::string>& names,
return
true
;
}
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
Make
GradOpDescs
(
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
Make
OpGrad
(
const
std
::
unique_ptr
<
OpDescBind
>&
op_desc
,
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
)
{
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
grad_op_descs
;
...
...
@@ -295,20 +296,35 @@ std::vector<std::unique_ptr<OpDescBind>> MakeGradOpDescs(
for
(
auto
&
p
:
pending_fill_zeros_ops
)
{
grad_op_descs
.
push_back
(
std
::
move
(
p
));
}
// TODO(fengjiayi): RNN op
return
grad_op_descs
;
}
void
AppendBackwardOpDescs
(
BlockDescBind
&
block_desc
,
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
)
{
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
MakeBlockBackward
(
ProgramDescBind
&
program_desc
,
int
block_idx
,
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
)
{
BlockDescBind
*
cur_block
=
program_desc
.
Block
(
block_idx
);
std
::
deque
<
std
::
unique_ptr
<
OpDescBind
>>&
op_descs
=
cur_block
->
ops_
;
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
size_t
>>
dup_out_ops
;
size_t
grad_desc_idx
=
0
;
std
::
deque
<
std
::
unique_ptr
<
OpDescBind
>>&
block_op_descs
=
block_desc
.
ops_
;
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
backward_descs
;
for
(
auto
it
=
block_op_descs
.
rbegin
();
it
!=
block_
op_descs
.
rend
();
++
it
)
{
for
(
auto
it
=
op_descs
.
rbegin
();
it
!=
op_descs
.
rend
();
++
it
)
{
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
op_grads
=
MakeGradOpDescs
(
*
it
,
no_grad_vars
);
MakeOpGrad
(
*
it
,
no_grad_vars
);
if
((
*
it
)
->
Type
()
==
"recurrent"
)
{
PADDLE_ENFORCE_EQ
(
op_grads
.
size
(),
size_t
(
1
),
"rnn_op's gradient process should contain only one op."
);
int
step_block_idx
=
(
*
it
)
->
GetBlockAttr
(
"stop_block"
);
auto
backward_block_op_descs
=
MakeBlockBackward
(
program_desc
,
step_block_idx
,
no_grad_vars
);
BlockDescBind
*
backward_block
=
program_desc
.
AppendBlock
(
*
cur_block
);
for
(
auto
&
ptr
:
backward_block_op_descs
)
{
backward_block
->
ops_
.
push_back
(
std
::
move
(
ptr
));
}
op_grads
[
0
]
->
SetBlockAttr
(
"step_block"
,
*
backward_block
);
}
for
(
const
auto
&
desc
:
op_grads
)
{
for
(
const
std
::
string
&
out_name
:
desc
->
OutputArgumentNames
())
{
dup_out_ops
[
out_name
].
emplace_back
(
grad_desc_idx
);
...
...
@@ -345,11 +361,24 @@ void AppendBackwardOpDescs(BlockDescBind& block_desc,
backward_descs
.
insert
(
backward_descs
.
begin
()
+
p
.
first
+
1
,
std
::
move
(
p
.
second
));
}
// Append backward_descs to BlockDescBind::ops_
for
(
std
::
unique_ptr
<
OpDescBind
>&
ptr
:
backward_descs
)
{
block_op_descs
.
push_back
(
std
::
move
(
ptr
));
return
backward_descs
;
}
void
AppendBackward
(
ProgramDescBind
&
program_desc
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
)
{
std
::
unordered_set
<
std
::
string
>
no_grad_var_names
;
no_grad_var_names
.
reserve
(
no_grad_vars
.
size
()
+
1
);
no_grad_var_names
.
insert
(
std
::
string
(
kEmptyVarName
)
+
kGradVarSuffix
);
for
(
auto
&
name
:
no_grad_vars
)
{
no_grad_var_names
.
insert
(
GradVarName
(
name
));
}
const
int
root_block_idx
=
0
;
auto
backward_op_descs
=
MakeBlockBackward
(
program_desc
,
root_block_idx
,
no_grad_var_names
);
auto
&
forw_op_descs
=
program_desc
.
Block
(
root_block_idx
)
->
ops_
;
for
(
auto
&
ptr
:
backward_op_descs
)
{
forw_op_descs
.
push_back
(
std
::
move
(
ptr
));
}
return
;
}
}
// namespace framework
...
...
paddle/framework/block_desc.h
浏览文件 @
a270dbb7
...
...
@@ -32,8 +32,13 @@ class ProgramDescBind;
class
BlockDescBind
{
public:
friend
void
AppendBackwardOpDescs
(
BlockDescBind
&
block_desc
,
std
::
unordered_set
<
std
::
string
>
&
no_grad_vars
);
friend
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
MakeBlockBackward
(
ProgramDescBind
&
program_desc
,
int
block_idx
,
std
::
unordered_set
<
std
::
string
>
&
no_grad_vars
);
friend
void
AppendBackward
(
ProgramDescBind
&
program_desc
,
const
std
::
unordered_set
<
std
::
string
>
&
no_grad_vars
);
BlockDescBind
(
ProgramDescBind
*
prog
,
BlockDesc
*
desc
)
:
prog_
(
prog
),
desc_
(
desc
),
need_update_
(
false
)
{}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录