Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
14f83707
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看板
提交
14f83707
编写于
2月 22, 2018
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add block.fwd_block_id
上级
78cc64a5
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
78 addition
and
17 deletion
+78
-17
paddle/fluid/framework/block_desc.cc
paddle/fluid/framework/block_desc.cc
+30
-8
paddle/fluid/framework/block_desc.h
paddle/fluid/framework/block_desc.h
+7
-1
paddle/fluid/framework/framework.proto
paddle/fluid/framework/framework.proto
+1
-0
paddle/fluid/framework/program_desc.h
paddle/fluid/framework/program_desc.h
+7
-1
paddle/fluid/operators/while_op.cc
paddle/fluid/operators/while_op.cc
+5
-2
paddle/fluid/pybind/protobuf.cc
paddle/fluid/pybind/protobuf.cc
+2
-0
python/paddle/v2/fluid/backward.py
python/paddle/v2/fluid/backward.py
+4
-1
python/paddle/v2/fluid/framework.py
python/paddle/v2/fluid/framework.py
+22
-4
未找到文件。
paddle/fluid/framework/block_desc.cc
浏览文件 @
14f83707
...
...
@@ -46,11 +46,25 @@ VarDesc *BlockDesc::FindVarRecursive(const std::string &name) const {
if
(
name
==
kEmptyVarName
)
return
nullptr
;
auto
it
=
vars_
.
find
(
name
);
if
(
it
==
vars_
.
end
())
{
return
Parent
()
==
kNoneBlockIndex
?
nullptr
:
ParentBlock
()
->
FindVarRecursive
(
name
);
if
(
it
!=
vars_
.
end
())
{
return
it
->
second
.
get
();
}
return
it
->
second
.
get
();
BlockDesc
*
tmp
=
ParentBlock
();
if
(
tmp
!=
nullptr
)
{
auto
ptr
=
tmp
->
FindVarRecursive
(
name
);
if
(
ptr
!=
nullptr
)
{
return
ptr
;
}
}
tmp
=
ForwardBlock
();
if
(
tmp
!=
nullptr
)
{
return
tmp
->
FindVarRecursive
(
name
);
}
return
nullptr
;
}
VarDesc
&
BlockDesc
::
FindRecursiveOrCreateVar
(
const
std
::
string
&
name_bytes
)
{
...
...
@@ -136,10 +150,7 @@ void BlockDesc::Flush() {
}
BlockDesc
*
BlockDesc
::
ParentBlock
()
const
{
if
(
this
->
desc_
->
parent_idx
()
==
kNoneBlockIndex
)
{
return
nullptr
;
}
return
prog_
->
MutableBlock
(
static_cast
<
size_t
>
(
this
->
desc_
->
parent_idx
()));
return
prog_
->
MutableBlock
(
static_cast
<
size_t
>
(
desc_
->
parent_idx
()));
}
proto
::
BlockDesc
*
BlockDesc
::
Proto
()
{
...
...
@@ -186,5 +197,16 @@ void BlockDesc::ClearPBVars() {
}
}
void
BlockDesc
::
SetForwardBlockID
(
int32_t
forward_block_id
)
{
PADDLE_ENFORCE
(
!
desc_
->
has_forward_block_idx
(),
"Parent block ID has been set to %d. Cannot set to %d"
,
desc_
->
forward_block_idx
(),
forward_block_id
);
desc_
->
set_forward_block_idx
(
forward_block_id
);
}
BlockDesc
*
BlockDesc
::
ForwardBlock
()
const
{
return
prog_
->
MutableBlock
(
static_cast
<
size_t
>
(
desc_
->
forward_block_idx
()));
}
}
// namespace framework
}
// namespace paddle
paddle/fluid/framework/block_desc.h
浏览文件 @
14f83707
...
...
@@ -49,6 +49,8 @@ class BlockDesc {
int32_t
Parent
()
const
{
return
desc_
->
parent_idx
();
}
int32_t
ForwardBlockID
()
const
{
return
desc_
->
forward_block_idx
();
}
VarDesc
*
Var
(
const
std
::
string
&
name_bytes
);
VarDesc
*
FindVar
(
const
std
::
string
&
name_bytes
)
const
;
...
...
@@ -73,6 +75,10 @@ class BlockDesc {
BlockDesc
*
ParentBlock
()
const
;
BlockDesc
*
ForwardBlock
()
const
;
void
SetForwardBlockID
(
int32_t
forward_block_id
);
OpDesc
*
AppendOp
();
void
AppendAllocatedOp
(
std
::
unique_ptr
<
OpDesc
>
&&
op_desc
);
...
...
@@ -91,7 +97,7 @@ class BlockDesc {
proto
::
BlockDesc
*
Proto
();
ProgramDesc
*
Program
()
{
return
this
->
prog_
;
}
ProgramDesc
*
Program
()
const
{
return
this
->
prog_
;
}
private:
void
ClearPBOps
();
...
...
paddle/fluid/framework/framework.proto
浏览文件 @
14f83707
...
...
@@ -158,6 +158,7 @@ message BlockDesc {
required
int32
parent_idx
=
2
;
repeated
VarDesc
vars
=
3
;
repeated
OpDesc
ops
=
4
;
optional
int32
forward_block_idx
=
5
[
default
=
-
1
];
}
// Please refer to
...
...
paddle/fluid/framework/program_desc.h
浏览文件 @
14f83707
...
...
@@ -38,7 +38,13 @@ class ProgramDesc {
BlockDesc
*
AppendBlock
(
const
BlockDesc
&
parent
);
BlockDesc
*
MutableBlock
(
size_t
idx
)
{
return
blocks_
[
idx
].
get
();
}
BlockDesc
*
MutableBlock
(
size_t
idx
)
{
if
(
idx
==
static_cast
<
size_t
>
(
kNoneBlockIndex
))
{
return
nullptr
;
}
else
{
return
blocks_
[
idx
].
get
();
}
}
const
BlockDesc
&
Block
(
size_t
idx
)
const
{
return
*
blocks_
[
idx
];
}
...
...
paddle/fluid/operators/while_op.cc
浏览文件 @
14f83707
...
...
@@ -231,7 +231,8 @@ class WhileGradOpDescMaker : public framework::SingleGradOpDescMaker {
while_grad
->
SetInput
(
kStepScopes
,
Output
(
kStepScopes
));
auto
*
grad_block
=
this
->
grad_block_
[
0
];
auto
*
fwd_block
=
grad_block
->
ParentBlock
();
auto
*
fwd_block
=
grad_block
->
ForwardBlock
();
auto
*
parent_block
=
grad_block
->
ParentBlock
();
// Not all of IGs will be generated by inner gradient operators of while op.
// Ignore IGs that is not generated by the inside block.
...
...
@@ -265,8 +266,10 @@ class WhileGradOpDescMaker : public framework::SingleGradOpDescMaker {
for
(
auto
&
input_name
:
op
->
InputArgumentNames
())
{
// If the input of Op has been recorded or is generated by the forward
// block, do not make it as input again.
if
(
block_ins
.
find
(
input_name
)
!=
block_ins
.
end
()
||
fwd_block
->
FindVar
(
input_name
)
!=
nullptr
)
{
fwd_block
->
FindVar
(
input_name
)
!=
nullptr
||
parent_block
->
FindVar
(
input_name
)
!=
nullptr
)
{
continue
;
}
extra_inputs
.
insert
(
input_name
);
...
...
paddle/fluid/pybind/protobuf.cc
浏览文件 @
14f83707
...
...
@@ -155,6 +155,8 @@ void BindBlockDesc(py::module &m) {
py
::
class_
<
BlockDesc
>
(
m
,
"BlockDesc"
,
""
)
.
def_property_readonly
(
"id"
,
&
BlockDesc
::
ID
)
.
def_property_readonly
(
"parent"
,
&
BlockDesc
::
Parent
)
.
def
(
"get_forward_block_idx"
,
&
BlockDesc
::
ForwardBlockID
)
.
def
(
"set_forward_block_idx"
,
&
BlockDesc
::
SetForwardBlockID
)
.
def
(
"append_op"
,
&
BlockDesc
::
AppendOp
,
py
::
return_value_policy
::
reference
)
.
def
(
"prepend_op"
,
&
BlockDesc
::
PrependOp
,
...
...
python/paddle/v2/fluid/backward.py
浏览文件 @
14f83707
...
...
@@ -298,7 +298,8 @@ def _append_backward_ops_(block,
# If the op has its own sub-block, deal with the sub-block first
if
op
.
has_attr
(
"sub_block"
):
sub_block
=
program
.
block
(
op
.
block_attr
(
"sub_block"
))
grad_sub_block
=
program
.
create_block
(
parent_idx
=
sub_block
.
idx
)
grad_sub_block
=
program
.
create_block
()
grad_sub_block
.
set_forward_block_idx
(
sub_block
.
idx
)
cb
=
_callback_lookup_
(
op
)
if
cb
is
not
None
:
if
callbacks
is
None
:
...
...
@@ -310,6 +311,8 @@ def _append_backward_ops_(block,
else
:
_append_backward_ops_
(
sub_block
,
sub_block
.
ops
,
grad_sub_block
,
no_grad_dict
,
grad_to_var
,
callbacks
)
program
.
rollback
()
grad_sub_block_list
.
append
(
grad_sub_block
.
desc
)
# Getting op's corresponding grad_op
...
...
python/paddle/v2/fluid/framework.py
浏览文件 @
14f83707
...
...
@@ -678,6 +678,13 @@ class Block(object):
def
parent_idx
(
self
):
return
self
.
desc
.
parent
@
property
def
forward_block_idx
(
self
):
return
self
.
desc
.
get_forward_block_idx
()
def
set_forward_block_idx
(
self
,
idx
):
self
.
desc
.
set_forward_block_idx
(
idx
)
@
property
def
idx
(
self
):
return
self
.
desc
.
id
...
...
@@ -695,11 +702,22 @@ class Block(object):
return
self
.
var
(
name
)
else
:
if
self
.
idx
==
0
:
raise
ValueError
(
"var %s is not in block(%d) nor its parents."
%
name
,
self
.
idx
)
raise
ValueError
(
"var {0} is not in block({1}) nor its parents."
.
format
(
name
,
self
.
idx
))
else
:
parent_block
=
self
.
program
.
block
(
self
.
parent_idx
)
return
parent_block
.
var_recursive
(
name
)
# DFS
try
:
parent_block
=
self
.
program
.
block
(
self
.
parent_idx
)
return
parent_block
.
var_recursive
(
name
)
except
ValueError
:
fwd_block
=
self
.
program
.
block
(
self
.
forward_block_idx
)
if
self
.
forward_block_idx
!=
-
1
else
None
if
fwd_block
is
not
None
:
return
fwd_block
.
var_recursive
(
name
)
else
:
raise
def
all_parameters
(
self
):
return
list
(
self
.
iter_parameters
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录