Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
02a7b3cc
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看板
未验证
提交
02a7b3cc
编写于
8月 16, 2023
作者:
J
JYChen
提交者:
GitHub
8月 16, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add range support in indexing (#56272)
* add range support in indexing * add getitem ut case
上级
84482da8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
53 addition
and
4 deletion
+53
-4
python/paddle/fluid/dygraph/tensor_patch_methods.py
python/paddle/fluid/dygraph/tensor_patch_methods.py
+1
-1
python/paddle/fluid/variable_index.py
python/paddle/fluid/variable_index.py
+5
-3
test/indexing/test_getitem.py
test/indexing/test_getitem.py
+22
-0
test/indexing/test_setitem.py
test/indexing/test_setitem.py
+25
-0
未找到文件。
python/paddle/fluid/dygraph/tensor_patch_methods.py
浏览文件 @
02a7b3cc
...
...
@@ -732,7 +732,7 @@ def monkey_patch_tensor():
item
=
(
item
,)
for
slice_item
in
item
:
if
isinstance
(
slice_item
,
(
list
,
np
.
ndarray
,
Variable
)):
if
isinstance
(
slice_item
,
(
list
,
np
.
ndarray
,
Variable
,
range
)):
return
True
elif
isinstance
(
slice_item
,
slice
):
if
(
...
...
python/paddle/fluid/variable_index.py
浏览文件 @
02a7b3cc
...
...
@@ -259,11 +259,13 @@ def replace_ellipsis(var, item):
return
item
def
replace_ndarray
(
item
):
def
replace_ndarray
_and_range
(
item
):
new_item
=
[]
for
slice_item
in
item
:
if
isinstance
(
slice_item
,
np
.
ndarray
):
new_item
.
append
(
paddle
.
assign
(
slice_item
))
elif
isinstance
(
slice_item
,
range
):
new_item
.
append
(
list
(
slice_item
))
else
:
new_item
.
append
(
slice_item
)
return
new_item
...
...
@@ -416,7 +418,7 @@ def _setitem_impl_(var, item, value):
ends
=
[]
steps
=
[]
item
=
replace_ndarray
(
item
)
item
=
replace_ndarray
_and_range
(
item
)
item
=
replace_ellipsis
(
var
,
item
)
item
,
none_axes
=
replace_none
(
item
)
slice_info
=
SliceInfo
()
...
...
@@ -700,7 +702,7 @@ def parse_index(x, indices):
if
not
isinstance
(
indices
,
tuple
):
indices
=
(
indices
,)
indices
=
replace_ndarray
(
indices
)
indices
=
replace_ndarray
_and_range
(
indices
)
indices
=
replace_ellipsis
(
x
,
indices
)
indices
,
none_axes
=
replace_none
(
indices
)
...
...
test/indexing/test_getitem.py
浏览文件 @
02a7b3cc
...
...
@@ -129,6 +129,15 @@ class TestGetitemInDygraph(unittest.TestCase):
np
.
testing
.
assert_allclose
(
y
.
numpy
(),
np_res
)
def
test_index_has_range
(
self
):
np_data
=
np
.
arange
(
3
*
4
*
5
*
6
).
reshape
((
3
,
4
,
5
,
6
))
np_res
=
np_data
[:,
range
(
3
),
4
]
x
=
paddle
.
to_tensor
(
np_data
)
y
=
x
[:,
range
(
3
),
4
]
np
.
testing
.
assert_allclose
(
y
.
numpy
(),
np_res
)
class
TestGetitemInStatic
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
@@ -312,6 +321,19 @@ class TestGetitemInStatic(unittest.TestCase):
np
.
testing
.
assert_allclose
(
res
[
0
],
np_res
)
def
test_index_has_range
(
self
):
# only one bool tensor with all False
np_data
=
np
.
arange
(
3
*
4
*
5
*
6
).
reshape
((
3
,
4
,
5
,
6
))
np_res
=
np_data
[:,
range
(
3
),
4
]
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
(),
paddle
.
static
.
Program
()
):
x
=
paddle
.
to_tensor
(
np_data
)
y
=
_getitem_static
(
x
,
(
slice
(
None
,
None
,
None
),
range
(
3
),
4
))
res
=
self
.
exe
.
run
(
fetch_list
=
[
y
.
name
])
np
.
testing
.
assert_allclose
(
res
[
0
],
np_res
)
class
TestGetItemErrorCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
test/indexing/test_setitem.py
浏览文件 @
02a7b3cc
...
...
@@ -51,6 +51,15 @@ class TestSetitemInDygraph(unittest.TestCase):
np
.
testing
.
assert_allclose
(
x
.
numpy
(),
np_data
)
def
test_index_has_range
(
self
):
np_data
=
np
.
ones
((
3
,
4
,
5
,
6
),
dtype
=
'int32'
)
x
=
paddle
.
to_tensor
(
np_data
)
np_data
[:,
range
(
3
),
[
1
,
2
,
4
]]
=
10
x
[:,
range
(
3
),
[
1
,
2
,
4
]]
=
10
np
.
testing
.
assert_allclose
(
x
.
numpy
(),
np_data
)
class
TestSetitemInStatic
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
@@ -137,3 +146,19 @@ class TestSetitemInStatic(unittest.TestCase):
res
=
self
.
exe
.
run
(
fetch_list
=
[
y
.
name
])
np
.
testing
.
assert_allclose
(
res
[
0
],
np_data
)
def
test_index_has_range
(
self
):
np_data
=
np
.
ones
((
3
,
4
,
5
,
6
),
dtype
=
'int32'
)
np_data
[:,
range
(
3
),
[
1
,
2
,
4
]]
=
10
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
(),
paddle
.
static
.
Program
()
):
x
=
paddle
.
ones
((
3
,
4
,
5
,
6
),
dtype
=
'int32'
)
y
=
_setitem_static
(
x
,
(
slice
(
None
,
None
),
range
(
3
),
[
1
,
2
,
4
]),
10
,
)
res
=
self
.
exe
.
run
(
fetch_list
=
[
y
.
name
])
np
.
testing
.
assert_allclose
(
res
[
0
],
np_data
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录