Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
12c15beb
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看板
未验证
提交
12c15beb
编写于
2月 08, 2021
作者:
L
liym27
提交者:
GitHub
2月 08, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Static setitem] Support index is ellipsis for setitem in static mode (#30836)
上级
97f7a70c
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
68 addition
and
2 deletion
+68
-2
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+29
-0
python/paddle/fluid/tests/unittests/test_set_value_op.py
python/paddle/fluid/tests/unittests/test_set_value_op.py
+39
-2
未找到文件。
python/paddle/fluid/framework.py
浏览文件 @
12c15beb
...
...
@@ -1866,6 +1866,35 @@ class Variable(object):
starts
=
[]
ends
=
[]
max_integer
=
sys
.
maxsize
def
replace_ellipsis
(
item
):
# Use slice(None) to replace Ellipsis.
# For var, var.shape = [3,4,5,6]
#
# var[..., 1:2] -> var[:, :, :, 1:2]
# var[0, ...] -> var[0]
# var[0, ..., 1:2] -> var[0, :, :, 1:2]
item
=
list
(
item
)
ell_count
=
item
.
count
(
Ellipsis
)
if
ell_count
==
0
:
return
item
elif
ell_count
>
1
:
raise
IndexError
(
"An index can only have a single ellipsis ('...')"
)
ell_idx
=
item
.
index
(
Ellipsis
)
if
ell_idx
==
len
(
item
)
-
1
:
return
item
[:
-
1
]
else
:
item
[
ell_idx
:
ell_idx
+
1
]
=
[
slice
(
None
)]
*
(
len
(
self
.
shape
)
-
len
(
item
)
+
1
)
return
item
item
=
replace_ellipsis
(
item
)
for
dim
,
slice_item
in
enumerate
(
item
):
if
isinstance
(
slice_item
,
slice
):
start
=
slice_item
.
start
...
...
python/paddle/fluid/tests/unittests/test_set_value_op.py
浏览文件 @
12c15beb
...
...
@@ -52,7 +52,6 @@ class TestSetValueApi(TestSetValueBase):
exe
=
paddle
.
static
.
Executor
(
paddle
.
CPUPlace
())
out
=
exe
.
run
(
self
.
program
,
fetch_list
=
[
x
])
self
.
_get_answer
()
self
.
assertTrue
(
(
self
.
data
==
out
).
all
(),
...
...
@@ -60,7 +59,7 @@ class TestSetValueApi(TestSetValueBase):
self
.
data
,
out
))
# 1. Test different type of item: int, python slice
# 1. Test different type of item: int, python slice
, Ellipsis
class
TestSetValueItemInt
(
TestSetValueApi
):
def
_call_setitem
(
self
,
x
):
x
[
0
]
=
self
.
value
...
...
@@ -101,6 +100,38 @@ class TestSetValueItemSlice4(TestSetValueApi):
self
.
data
[
0
:,
1
:
2
,
:]
=
self
.
value
class
TestSetValueItemEllipsis1
(
TestSetValueApi
):
def
_call_setitem
(
self
,
x
):
x
[
0
:,
...,
1
:]
=
self
.
value
def
_get_answer
(
self
):
self
.
data
[
0
:,
...,
1
:]
=
self
.
value
class
TestSetValueItemEllipsis2
(
TestSetValueApi
):
def
_call_setitem
(
self
,
x
):
x
[
0
:,
...]
=
self
.
value
def
_get_answer
(
self
):
self
.
data
[
0
:,
...]
=
self
.
value
class
TestSetValueItemEllipsis3
(
TestSetValueApi
):
def
_call_setitem
(
self
,
x
):
x
[...,
1
:]
=
self
.
value
def
_get_answer
(
self
):
self
.
data
[...,
1
:]
=
self
.
value
class
TestSetValueItemEllipsis4
(
TestSetValueApi
):
def
_call_setitem
(
self
,
x
):
x
[...]
=
self
.
value
def
_get_answer
(
self
):
self
.
data
[...]
=
self
.
value
# 2. Test different type of value: int, float, numpy.ndarray, Tensor
# 2.1 value is int32, int64, float32, float64, bool
...
...
@@ -499,6 +530,12 @@ class TestError(TestSetValueBase):
x
=
paddle
.
ones
(
shape
=
self
.
shape
,
dtype
=
self
.
dtype
)
x
[
0
:
1
:
2
]
=
self
.
value
def
_ellipsis_error
(
self
):
with
self
.
assertRaisesRegexp
(
IndexError
,
"An index can only have a single ellipsis"
):
x
=
paddle
.
ones
(
shape
=
self
.
shape
,
dtype
=
self
.
dtype
)
x
[...,
...]
=
self
.
value
def
_broadcast_mismatch
(
self
):
program
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
program
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录