Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
5ea3818a
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
5ea3818a
编写于
5月 07, 2020
作者:
C
Chen Weihang
提交者:
GitHub
5月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix slice bug in while loop, test=develop (#24326)
上级
420707c2
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
8 deletion
+37
-8
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+9
-8
python/paddle/fluid/tests/unittests/test_while_loop_op.py
python/paddle/fluid/tests/unittests/test_while_loop_op.py
+28
-0
未找到文件。
python/paddle/fluid/framework.py
浏览文件 @
5ea3818a
...
...
@@ -647,6 +647,7 @@ def _getitem_impl_(var, item):
slice_step
=
[]
use_strided_slice
=
False
reverse_axis
=
[]
target_block
=
default_main_program
().
current_block
()
def
fill_constant
(
shape
,
value
,
force_cpu
=
False
,
out
=
None
):
var
.
block
.
append_op
(
...
...
@@ -701,8 +702,8 @@ def _getitem_impl_(var, item):
if
isinstance
(
slice_item
,
Variable
):
temp_1
=
var
.
block
.
create_var
(
dtype
=
'int32'
)
fill_constant
([
1
],
1
,
force_cpu
=
True
,
out
=
temp_1
)
temp_end
=
var
.
block
.
create_var
(
dtype
=
'int32'
)
var
.
block
.
append_op
(
temp_end
=
target_
block
.
create_var
(
dtype
=
'int32'
)
target_
block
.
append_op
(
type
=
'elementwise_add'
,
inputs
=
{
'X'
:
slice_item
,
'Y'
:
temp_1
},
...
...
@@ -785,11 +786,11 @@ def _getitem_impl_(var, item):
out
=
var
if
use_strided_slice
==
False
and
len
(
slice_axis
)
>
0
:
# append slice_op here
slice_out_var
=
var
.
block
.
create_var
(
slice_out_var
=
target_
block
.
create_var
(
name
=
unique_name
.
generate_with_ignorable_key
(
var
.
name
+
"_slice"
),
dtype
=
var
.
dtype
)
var
.
block
.
append_op
(
target_
block
.
append_op
(
type
=
"slice"
,
inputs
=
inputs
,
outputs
=
{
'Out'
:
[
slice_out_var
]},
...
...
@@ -797,11 +798,11 @@ def _getitem_impl_(var, item):
out
=
slice_out_var
elif
use_strided_slice
==
True
and
len
(
slice_axis
)
>
0
:
strided_slice_out_var
=
var
.
block
.
create_var
(
strided_slice_out_var
=
target_
block
.
create_var
(
name
=
unique_name
.
generate_with_ignorable_key
(
var
.
name
+
"_strided_slice"
),
dtype
=
var
.
dtype
)
var
.
block
.
append_op
(
target_
block
.
append_op
(
type
=
"strided_slice"
,
inputs
=
inputs
,
outputs
=
{
'Out'
:
[
strided_slice_out_var
]},
...
...
@@ -810,11 +811,11 @@ def _getitem_impl_(var, item):
out
=
strided_slice_out_var
if
len
(
reverse_axis
)
>
0
:
reverse_out_var
=
var
.
block
.
create_var
(
reverse_out_var
=
target_
block
.
create_var
(
name
=
unique_name
.
generate_with_ignorable_key
(
var
.
name
+
"_slice_reverse"
),
dtype
=
var
.
dtype
)
var
.
block
.
append_op
(
target_
block
.
append_op
(
type
=
"reverse"
,
inputs
=
{
'X'
:
out
},
outputs
=
{
'Out'
:
[
reverse_out_var
]},
...
...
python/paddle/fluid/tests/unittests/test_while_loop_op.py
浏览文件 @
5ea3818a
...
...
@@ -494,5 +494,33 @@ class TestApiWhileLoop_Error(unittest.TestCase):
value_error_body_returns_with_mutable_list
)
class
TestApiWhileLoopSliceInBody
(
unittest
.
TestCase
):
def
test_var_slice
(
self
):
def
cond
(
z
,
i
):
return
i
+
1
<=
x_shape
[
0
]
def
body
(
z
,
i
):
z
=
z
+
x
[
i
]
i
+=
1
return
z
,
i
main_program
=
Program
()
startup_program
=
Program
()
with
program_guard
(
main_program
,
startup_program
):
x
=
fluid
.
layers
.
data
(
name
=
'x'
,
shape
=
[
5
],
dtype
=
'int32'
)
z
=
fluid
.
layers
.
fill_constant
([
1
],
'int32'
,
0
)
x_shape
=
fluid
.
layers
.
shape
(
x
)
i
=
fluid
.
layers
.
fill_constant
([
1
],
'int32'
,
0
)
z
,
_
=
fluid
.
layers
.
while_loop
(
cond
,
body
,
[
z
,
i
])
place
=
fluid
.
CUDAPlace
(
0
)
if
core
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
np_x
=
np
.
array
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'int32'
)
res
=
exe
.
run
(
main_program
,
feed
=
{
'x'
:
np_x
},
fetch_list
=
[
z
])
self
.
assertTrue
(
np
.
array_equal
(
res
[
0
],
[
np
.
sum
(
np_x
)]))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录