Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
3d4e8268
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看板
提交
3d4e8268
编写于
5月 19, 2019
作者:
Z
Zeng Jinle
提交者:
Tao Luo
5月 19, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix recurrent fwd bug when no backward and scope clear (#17460)
上级
14f22362
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
20 deletion
+36
-20
paddle/fluid/operators/recurrent_op.cc
paddle/fluid/operators/recurrent_op.cc
+36
-20
未找到文件。
paddle/fluid/operators/recurrent_op.cc
浏览文件 @
3d4e8268
...
@@ -37,6 +37,20 @@ constexpr char kInitStateGrads[] = "initial_states" GRAD_SUFFIX;
...
@@ -37,6 +37,20 @@ constexpr char kInitStateGrads[] = "initial_states" GRAD_SUFFIX;
using
StepScopeVar
=
std
::
vector
<
framework
::
Scope
*>
;
using
StepScopeVar
=
std
::
vector
<
framework
::
Scope
*>
;
static
void
ClearStepScopes
(
const
platform
::
DeviceContext
&
dev_ctx
,
framework
::
Scope
*
parent_scope
,
StepScopeVar
*
step_scopes
)
{
if
(
step_scopes
->
empty
())
return
;
dev_ctx
.
Wait
();
for
(
auto
*
sub_scope
:
*
step_scopes
)
{
parent_scope
->
DeleteScope
(
sub_scope
);
}
step_scopes
->
clear
();
}
// StepScopes manages scopes inside RNN.
// StepScopes manages scopes inside RNN.
// StepScopes::CurScope() get the current scope
// StepScopes::CurScope() get the current scope
// StepScopes::ExScope() get the ex-scope, or scope in previous time step.
// StepScopes::ExScope() get the ex-scope, or scope in previous time step.
...
@@ -53,7 +67,8 @@ using StepScopeVar = std::vector<framework::Scope *>;
...
@@ -53,7 +67,8 @@ using StepScopeVar = std::vector<framework::Scope *>;
// access scopes from begin to end.
// access scopes from begin to end.
class
StepScopes
{
class
StepScopes
{
public:
public:
StepScopes
(
const
framework
::
Scope
&
parent
,
StepScopeVar
*
scopes
,
StepScopes
(
const
platform
::
DeviceContext
&
dev_ctx
,
const
framework
::
Scope
&
parent
,
StepScopeVar
*
scopes
,
bool
is_train
,
size_t
seq_len
,
bool
is_backward
=
false
)
bool
is_train
,
size_t
seq_len
,
bool
is_backward
=
false
)
:
counter_
(
is_backward
?
seq_len
-
1
:
0UL
),
:
counter_
(
is_backward
?
seq_len
-
1
:
0UL
),
scopes_
(
scopes
),
scopes_
(
scopes
),
...
@@ -63,7 +78,7 @@ class StepScopes {
...
@@ -63,7 +78,7 @@ class StepScopes {
PADDLE_ENFORCE
(
is_train
||
!
is_backward
,
PADDLE_ENFORCE
(
is_train
||
!
is_backward
,
"Cannot backward when is not training"
);
"Cannot backward when is not training"
);
if
(
!
is_backward_
)
{
if
(
!
is_backward_
)
{
PADDLE_ENFORCE
(
scopes
->
empty
()
);
ClearStepScopes
(
dev_ctx
,
const_cast
<
framework
::
Scope
*>
(
&
parent
),
scopes
);
scopes
->
reserve
(
static_cast
<
size_t
>
(
num_step_scopes
));
scopes
->
reserve
(
static_cast
<
size_t
>
(
num_step_scopes
));
for
(
size_t
i
=
0
;
i
<
num_step_scopes
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
num_step_scopes
;
++
i
)
{
scopes
->
emplace_back
(
&
parent
.
NewScope
());
scopes
->
emplace_back
(
&
parent
.
NewScope
());
...
@@ -244,14 +259,15 @@ class RecurrentOp : public RecurrentBase {
...
@@ -244,14 +259,15 @@ class RecurrentOp : public RecurrentBase {
const
platform
::
Place
&
place
)
const
override
{
const
platform
::
Place
&
place
)
const
override
{
bool
has_state
=
Attr
<
bool
>
(
kHasStates
);
bool
has_state
=
Attr
<
bool
>
(
kHasStates
);
auto
seq_len
=
static_cast
<
size_t
>
(
this
->
GetSequenceLength
(
scope
));
auto
seq_len
=
static_cast
<
size_t
>
(
this
->
GetSequenceLength
(
scope
));
VLOG
(
3
)
<<
"Static RNN input sequence length = "
<<
seq_len
;
StepScopes
scopes
=
CreateStepScopes
(
scope
,
seq_len
);
auto
reverse
=
Attr
<
bool
>
(
kReverse
);
// get device context from pool
// get device context from pool
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
&
dev_ctx
=
*
pool
.
Get
(
place
);
auto
&
dev_ctx
=
*
pool
.
Get
(
place
);
VLOG
(
3
)
<<
"Static RNN input sequence length = "
<<
seq_len
;
StepScopes
scopes
=
CreateStepScopes
(
dev_ctx
,
scope
,
seq_len
);
auto
reverse
=
Attr
<
bool
>
(
kReverse
);
framework
::
Executor
executor
(
place
);
framework
::
Executor
executor
(
place
);
auto
*
block
=
Attr
<
framework
::
BlockDesc
*>
(
kStepBlock
);
auto
*
block
=
Attr
<
framework
::
BlockDesc
*>
(
kStepBlock
);
...
@@ -316,11 +332,12 @@ class RecurrentOp : public RecurrentBase {
...
@@ -316,11 +332,12 @@ class RecurrentOp : public RecurrentBase {
}
}
private:
private:
StepScopes
CreateStepScopes
(
const
framework
::
Scope
&
scope
,
StepScopes
CreateStepScopes
(
const
platform
::
DeviceContext
&
dev_ctx
,
const
framework
::
Scope
&
scope
,
size_t
seq_len
)
const
{
size_t
seq_len
)
const
{
auto
*
var
=
scope
.
FindVar
(
Output
(
kStepScopes
));
auto
*
var
=
scope
.
FindVar
(
Output
(
kStepScopes
));
PADDLE_ENFORCE
(
var
!=
nullptr
);
PADDLE_ENFORCE
(
var
!=
nullptr
);
return
StepScopes
(
scope
,
var
->
GetMutable
<
StepScopeVar
>
(),
return
StepScopes
(
dev_ctx
,
scope
,
var
->
GetMutable
<
StepScopeVar
>
(),
Attr
<
bool
>
(
kIsTrain
),
seq_len
);
Attr
<
bool
>
(
kIsTrain
),
seq_len
);
}
}
};
};
...
@@ -338,17 +355,18 @@ class RecurrentGradOp : public RecurrentBase {
...
@@ -338,17 +355,18 @@ class RecurrentGradOp : public RecurrentBase {
const
platform
::
Place
&
place
)
const
override
{
const
platform
::
Place
&
place
)
const
override
{
bool
has_state
=
Attr
<
bool
>
(
kHasStates
);
bool
has_state
=
Attr
<
bool
>
(
kHasStates
);
const
size_t
seq_len
=
static_cast
<
size_t
>
(
GetSequenceLength
(
scope
));
const
size_t
seq_len
=
static_cast
<
size_t
>
(
GetSequenceLength
(
scope
));
StepScopes
scopes
=
CreateStepScopes
(
scope
,
seq_len
);
// get device context from pool
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
&
dev_ctx
=
*
pool
.
Get
(
place
);
StepScopes
scopes
=
CreateStepScopes
(
dev_ctx
,
scope
,
seq_len
);
auto
reverse
=
Attr
<
bool
>
(
kReverse
);
auto
reverse
=
Attr
<
bool
>
(
kReverse
);
framework
::
Executor
executor
(
place
);
framework
::
Executor
executor
(
place
);
auto
*
block
=
Attr
<
framework
::
BlockDesc
*>
(
kStepBlock
);
auto
*
block
=
Attr
<
framework
::
BlockDesc
*>
(
kStepBlock
);
auto
*
program
=
block
->
Program
();
auto
*
program
=
block
->
Program
();
// get device context from pool
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
&
dev_ctx
=
*
pool
.
Get
(
place
);
for
(
size_t
step_id
=
0
;
step_id
<
seq_len
;
++
step_id
)
{
for
(
size_t
step_id
=
0
;
step_id
<
seq_len
;
++
step_id
)
{
size_t
seq_offset
=
reverse
?
step_id
:
seq_len
-
step_id
-
1
;
size_t
seq_offset
=
reverse
?
step_id
:
seq_len
-
step_id
-
1
;
VLOG
(
3
)
<<
"Recurrent backward operate at the time step "
<<
seq_offset
;
VLOG
(
3
)
<<
"Recurrent backward operate at the time step "
<<
seq_offset
;
...
@@ -501,22 +519,20 @@ class RecurrentGradOp : public RecurrentBase {
...
@@ -501,22 +519,20 @@ class RecurrentGradOp : public RecurrentBase {
scopes
.
Next
();
scopes
.
Next
();
}
}
// Delete the scope of StepScopes
// Delete the scope of StepScopes
dev_ctx
.
Wait
();
auto
*
var
=
scope
.
FindVar
(
Input
(
kStepScopes
));
auto
*
var
=
scope
.
FindVar
(
Input
(
kStepScopes
));
PADDLE_ENFORCE
(
var
!=
nullptr
);
PADDLE_ENFORCE
(
var
!=
nullptr
);
auto
step_scopes
=
var
->
GetMutable
<
StepScopeVar
>
();
auto
*
step_scopes
=
var
->
GetMutable
<
StepScopeVar
>
();
for
(
auto
*
sub_scope
:
*
step_scopes
)
{
ClearStepScopes
(
dev_ctx
,
const_cast
<
framework
::
Scope
*>
(
&
scope
),
const_cast
<
framework
::
Scope
&>
(
scope
).
DeleteScope
(
sub_scope
);
step_scopes
);
}
step_scopes
->
clear
();
}
}
private:
private:
StepScopes
CreateStepScopes
(
const
framework
::
Scope
&
scope
,
StepScopes
CreateStepScopes
(
const
platform
::
DeviceContext
&
dev_ctx
,
const
framework
::
Scope
&
scope
,
size_t
seq_len
)
const
{
size_t
seq_len
)
const
{
auto
*
var
=
scope
.
FindVar
(
Input
(
kStepScopes
));
auto
*
var
=
scope
.
FindVar
(
Input
(
kStepScopes
));
PADDLE_ENFORCE
(
var
!=
nullptr
);
PADDLE_ENFORCE
(
var
!=
nullptr
);
return
StepScopes
(
scope
,
var
->
GetMutable
<
StepScopeVar
>
(),
return
StepScopes
(
dev_ctx
,
scope
,
var
->
GetMutable
<
StepScopeVar
>
(),
Attr
<
bool
>
(
kIsTrain
),
seq_len
,
true
/*is_backward*/
);
Attr
<
bool
>
(
kIsTrain
),
seq_len
,
true
/*is_backward*/
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录