Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b2ee9190
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看板
提交
b2ee9190
编写于
12月 19, 2017
作者:
Y
Yang Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add parallel_do test
上级
aea5ccca
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
107 addition
and
19 deletion
+107
-19
paddle/operators/parallel_do_op.cc
paddle/operators/parallel_do_op.cc
+2
-2
python/paddle/v2/fluid/layers/control_flow.py
python/paddle/v2/fluid/layers/control_flow.py
+105
-17
未找到文件。
paddle/operators/parallel_do_op.cc
浏览文件 @
b2ee9190
...
...
@@ -23,9 +23,9 @@ namespace operators {
constexpr
char
kInputs
[]
=
"inputs"
;
constexpr
char
kParameters
[]
=
"parameters"
;
constexpr
char
kPlaces
[]
=
"places"
;
constexpr
char
kParallelBlock
[]
=
"
parallel
_block"
;
constexpr
char
kParallelBlock
[]
=
"
sub
_block"
;
constexpr
char
kOutputs
[]
=
"outputs"
;
constexpr
char
kParallelScopes
[]
=
"sub_
block
"
;
constexpr
char
kParallelScopes
[]
=
"sub_
scopes
"
;
// #define GRAD_SUFFIX "@GRAD"
// constexpr char kInputGrads[] = "inputs" GRAD_SUFFIX;
// constexpr char kOutputGrads[] = "outputs" GRAD_SUFFIX;
...
...
python/paddle/v2/fluid/layers/control_flow.py
浏览文件 @
b2ee9190
...
...
@@ -5,12 +5,12 @@ from tensor import assign, fill_constant
import
contextlib
__all__
=
[
'split_lod_tensor'
,
'merge_lod_tensor'
,
'BlockGuard'
,
'StaticRNNGuard'
,
'
StaticRNNMemoryLink'
,
'WhileGuard'
,
'While'
,
'lod_rank_tab
le'
,
'
max_sequence_len'
,
'topk'
,
'lod_tensor_to_array'
,
'array_to_lod_tensor
'
,
'
increment'
,
'array_write'
,
'create_array'
,
'less_than'
,
'array_read
'
,
'
shrink_memory'
,
'array_length'
,
'IfElse'
,
'DynamicRNN'
,
'ConditionalBlock
'
,
'
StaticRNN
'
'split_lod_tensor'
,
'merge_lod_tensor'
,
'BlockGuard'
,
'
BlockGuardWithCompletion'
,
'StaticRNNMemoryLink'
,
'WhileGuard'
,
'Whi
le'
,
'
lod_rank_table'
,
'max_sequence_len'
,
'topk'
,
'lod_tensor_to_array
'
,
'
array_to_lod_tensor'
,
'increment'
,
'array_write'
,
'create_array
'
,
'
less_than'
,
'array_read'
,
'shrink_memory'
,
'array_length'
,
'IfElse
'
,
'
DynamicRNN'
,
'ConditionalBlock'
,
'StaticRNN'
,
'ParallelDo
'
]
...
...
@@ -67,29 +67,117 @@ class BlockGuard(object):
return
True
class
StaticRNNGuard
(
BlockGuard
):
class
ParallelDo
(
object
):
"""
StaticRNNGuard
class.
ParallelDo
class.
StaticRNNGuard class is used to create a StaticRNN block in a program.
ParallelDo class is used to create a ParallelDo.
"""
def
__init__
(
self
,
places
,
name
=
None
):
self
.
helper
=
LayerHelper
(
"parallel_do"
,
name
=
name
)
self
.
inputs
=
[]
self
.
places
=
places
self
.
outputs
=
[]
self
.
status
=
StaticRNN
.
BEFORE_RNN_BLOCK
def
do
(
self
):
return
BlockGuardWithCompletion
(
self
)
def
parent_block
(
self
):
prog
=
self
.
helper
.
main_program
parent_idx
=
prog
.
current_block
().
parent_idx
assert
parent_idx
>=
0
parent_block
=
prog
.
block
(
parent_idx
)
return
parent_block
def
__call__
(
self
,
*
args
,
**
kwargs
):
if
self
.
status
!=
StaticRNN
.
AFTER_RNN_BLOCK
:
raise
ValueError
(
"RNN output can only be retrieved after rnn block"
)
if
len
(
self
.
outputs
)
==
0
:
raise
ValueError
(
"RNN has no output"
)
elif
len
(
self
.
outputs
)
==
1
:
return
self
.
outputs
[
0
]
else
:
return
self
.
outputs
def
read_input
(
self
,
var
):
self
.
inputs
.
append
(
var
)
def
write_output
(
self
,
var
):
self
.
outputs
.
append
(
var
)
def
get_parameters
(
self
):
main_program
=
self
.
helper
.
main_program
current_block
=
main_program
.
current_block
()
parent_block
=
self
.
parent_block
()
local_inputs
=
set
()
for
op
in
current_block
.
ops
:
for
oname
in
op
.
output_names
:
for
out_var_name
in
op
.
output
(
oname
):
local_inputs
.
add
(
out_var_name
)
for
var
in
self
.
inputs
:
local_inputs
.
add
(
var
.
name
)
params
=
list
()
for
op
in
current_block
.
ops
:
for
iname
in
op
.
input_names
:
for
in_var_name
in
op
.
input
(
iname
):
if
in_var_name
not
in
local_inputs
:
params
.
append
(
in_var_name
)
return
[
parent_block
.
var
(
name
)
for
name
in
params
]
def
complete_op
(
self
):
main_program
=
self
.
helper
.
main_program
current_block
=
main_program
.
current_block
()
parent_block
=
self
.
parent_block
()
step_scope
=
parent_block
.
create_var
(
type
=
core
.
VarDesc
.
VarType
.
STEP_SCOPES
)
inputs
=
[
parent_block
.
var
(
i
.
name
)
for
i
in
self
.
inputs
]
parent_block
.
append_op
(
type
=
'parallel_do'
,
inputs
=
{
'inputs'
:
inputs
,
'parameters'
:
self
.
get_parameters
(),
'places'
:
self
.
places
},
outputs
=
{
'outputs'
:
self
.
outputs
,
'step_scopes'
:
[
step_scope
]},
attrs
=
{
'sub_block'
:
current_block
})
class
BlockGuardWithCompletion
(
BlockGuard
):
"""
BlockGuardWithCompletion class.
BlockGuardWithCompletion class is used to create an op with a block in a program.
"""
def
__init__
(
self
,
rnn
):
if
not
isinstance
(
rnn
,
StaticRNN
):
raise
TypeError
(
"StaticRNNGuard takes a StaticRNN"
)
super
(
StaticRNNGuard
,
self
).
__init__
(
rnn
.
helper
.
main_program
)
if
not
(
isinstance
(
rnn
,
StaticRNN
)
or
isinstance
(
rnn
,
ParallelDo
)):
raise
TypeError
(
"BlockGuardWithCompletion takes a StaticRNN or ParallelDo"
)
super
(
BlockGuardWithCompletion
,
self
).
__init__
(
rnn
.
helper
.
main_program
)
self
.
rnn
=
rnn
def
__enter__
(
self
):
self
.
rnn
.
status
=
StaticRNN
.
IN_RNN_BLOCK
return
super
(
StaticRNNGuard
,
self
).
__enter__
()
return
super
(
BlockGuardWithCompletion
,
self
).
__enter__
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
if
exc_type
is
not
None
:
return
False
self
.
rnn
.
status
=
StaticRNN
.
AFTER_RNN_BLOCK
self
.
rnn
.
complete_rnn_op
()
return
super
(
StaticRNNGuard
,
self
).
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
self
.
rnn
.
complete_op
()
return
super
(
BlockGuardWithCompletion
,
self
).
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
class
StaticRNNMemoryLink
(
object
):
...
...
@@ -135,7 +223,7 @@ class StaticRNN(object):
self
.
seq_len
=
None
def
step
(
self
):
return
StaticRNNGuard
(
self
)
return
BlockGuardWithCompletion
(
self
)
def
_assert_in_rnn_block_
(
self
,
method
):
if
self
.
status
!=
StaticRNN
.
IN_RNN_BLOCK
:
...
...
@@ -251,7 +339,7 @@ class StaticRNN(object):
else
:
return
self
.
outputs
def
complete_
rnn_
op
(
self
):
def
complete_op
(
self
):
main_program
=
self
.
helper
.
main_program
rnn_block
=
main_program
.
current_block
()
parent_block
=
self
.
parent_block
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录