Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support index is ellipsis for getitem in static mode #32876

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions python/paddle/fluid/tests/unittests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import unittest
import paddle
from paddle.fluid.framework import default_main_program, Program, convert_np_dtype_to_dtype_, in_dygraph_mode
import paddle
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.core as core
Expand Down Expand Up @@ -218,6 +219,26 @@ def _test_slice_index_list(self, place):
self.assertTrue((result[2] == expected[2]).all())
self.assertTrue((result[3] == expected[3]).all())

def _test_slice_index_ellipsis(self, place):
data = np.random.rand(2, 3, 4).astype("float32")
prog = paddle.static.Program()
with paddle.static.program_guard(prog):
x = paddle.assign(data)
out1 = x[0:, ..., 1:]
out2 = x[0:, ...]
out3 = x[..., 1:]
out4 = x[...]

exe = paddle.static.Executor(place)
result = exe.run(prog, fetch_list=[out1, out2, out3, out4])

expected = [data[0:, ..., 1:], data[0:, ...], data[..., 1:], data[...]]

self.assertTrue((result[0] == expected[0]).all())
self.assertTrue((result[1] == expected[1]).all())
self.assertTrue((result[2] == expected[2]).all())
self.assertTrue((result[3] == expected[3]).all())

with self.assertRaises(IndexError):
res = x[[1, 0], [0, 0]]

Expand All @@ -233,6 +254,7 @@ def test_slice(self):
self._test_slice(place)
self._test_slice_index_tensor(place)
self._test_slice_index_list(place)
self._test_slice_index_ellipsis(place)

def _tostring(self):
b = default_main_program().current_block()
Expand Down
1 change: 1 addition & 0 deletions python/paddle/fluid/variable_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def _getitem_impl_(var, item):

use_strided_slice = False
item, none_axes = replace_none(item)
item = replace_ellipsis(var, item)

for dim, slice_item in enumerate(item):
if is_integer_or_scalar_tensor(slice_item):
Expand Down