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 getitem by ellipsis index in dynamic mode #34267

Merged
merged 4 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 27 additions & 10 deletions paddle/fluid/pybind/imperative.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,31 @@ static void ParseIndexingSlice(framework::LoDTensor *tensor, PyObject *_index,
const auto &shape = tensor->dims();
const int rank = shape.size();
const int size = PyTuple_GET_SIZE(index);

// specified_dims is the number of dimensions which indexed by Interger,
// Slices.
int specified_dims = 0;
for (int dim = 0; dim < size; ++dim) {
PyObject *slice_item = PyTuple_GetItem(index, dim);
if (PyCheckInteger(slice_item) || PySlice_Check(slice_item)) {
specified_dims++;
}
}

PADDLE_ENFORCE_EQ(
size <= rank, true,
platform::errors::InvalidArgument(
"too many indices (%d) for tensor of dimension %d", size, rank));
for (int dim = 0; dim < size; ++dim) {
PyObject *slice_item = PyTuple_GetItem(index, dim);
PADDLE_ENFORCE_EQ(PyCheckInteger(slice_item) || PySlice_Check(slice_item),
true,
platform::errors::InvalidArgument(
"Currently, VarBase.__getitem__() only allows "
"indexing by Integers, Slices, and tuples of "
"these types, but received %s in %dth slice item",
std::string(Py_TYPE(slice_item)->tp_name), dim + 1));
for (int i = 0, dim = 0; i < size; ++i) {
PyObject *slice_item = PyTuple_GetItem(index, i);
PADDLE_ENFORCE_EQ(
PyCheckInteger(slice_item) || PySlice_Check(slice_item) ||
slice_item == Py_Ellipsis,
true, platform::errors::InvalidArgument(
"Currently, VarBase.__getitem__() only allows "
"indexing by Integers, Slices, Ellipsis, and tuples of "
"these types, but received %s in %dth slice item",
std::string(Py_TYPE(slice_item)->tp_name), i + 1));
infer_flags->push_back(1);
int dim_len = shape[dim];
if (PyCheckInteger(slice_item)) {
Expand All @@ -467,20 +479,25 @@ static void ParseIndexingSlice(framework::LoDTensor *tensor, PyObject *_index,
slice_ends->push_back(start + 1);
slice_strides->push_back(1);
decrease_axis->push_back(dim);
} else {
dim++;
} else if (PySlice_Check(slice_item)) {
// slice item
Py_ssize_t start, end, step;
PySliceObject *p = reinterpret_cast<PySliceObject *>(slice_item);
_PySlice_GetIndices(p, dim_len, &start, &end, &step);

// :: or : or 0:dim_len:1
if (start == 0 && end == dim_len && step == 1) {
dim++;
continue;
}
slice_axes->push_back(dim);
slice_starts->push_back(start);
slice_ends->push_back(end);
slice_strides->push_back(step);
dim++;
} else if (slice_item == Py_Ellipsis) {
dim += rank - specified_dims;
Copy link
Contributor

@chenwhql chenwhql Jul 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be adding else branch is better, we can move the error hint into else branch, such as

} else {
    PADDLE_THROW(
        platform::errors::InvalidArgument(
                  "Currently, VarBase.__getitem__() only allows "
                  "indexing by Integers, Slices, Ellipsis, and tuples of "
                  "these types, but received %s in %dth slice item",
                  std::string(Py_TYPE(slice_item)->tp_name), i + 1));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}
}
if (!PyTuple_Check(_index)) Py_DecRef(index);
Expand Down
54 changes: 54 additions & 0 deletions python/paddle/fluid/tests/unittests/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,59 @@ def _test_slice_for_tensor_attr(self):
np.array_equal(local_out[15], tensor_array[::-1, ::-1, ::-1]))
self.assertTrue(np.array_equal(local_out[16], tensor_array[-4:4]))

def _test_for_getitem_ellipsis_index(self):
shape = (64, 3, 5, 256)
np_fp32_value = np.random.random(shape).astype('float32')
np_int_value = np.random.randint(1, 100, shape)

var_fp32 = paddle.to_tensor(np_fp32_value)
var_int = paddle.to_tensor(np_int_value)

# test for float32
var = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate code? put the duplicate code in another func?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

var_fp32[..., 0].numpy(),
var_fp32[..., 1, 0].numpy(),
var_fp32[0, ..., 1, 0].numpy(),
var_fp32[1, ..., 1].numpy(),
var_fp32[2, ...].numpy(),
var_fp32[2, 0, ...].numpy(),
var_fp32[2, 0, 1, ...].numpy(),
var_fp32[...].numpy(),
var_fp32[:, ..., 100].numpy(),
]

self.assertTrue(np.array_equal(var[0], np_fp32_value[..., 0]))
self.assertTrue(np.array_equal(var[1], np_fp32_value[..., 1, 0]))
self.assertTrue(np.array_equal(var[2], np_fp32_value[0, ..., 1, 0]))
self.assertTrue(np.array_equal(var[3], np_fp32_value[1, ..., 1]))
self.assertTrue(np.array_equal(var[4], np_fp32_value[2, ...]))
self.assertTrue(np.array_equal(var[5], np_fp32_value[2, 0, ...]))
self.assertTrue(np.array_equal(var[6], np_fp32_value[2, 0, 1, ...]))
self.assertTrue(np.array_equal(var[7], np_fp32_value[...]))
self.assertTrue(np.array_equal(var[8], np_fp32_value[:, ..., 100]))

# test for int type
var = [
var_int[..., 0].numpy(),
var_int[..., 1, 0].numpy(),
var_int[0, ..., 1, 0].numpy(),
var_int[1, ..., 1].numpy(),
var_int[2, ...].numpy(),
var_int[2, 0, ...].numpy(),
var_int[2, 0, 1, ...].numpy(),
var_int[...].numpy(),
var_int[:, ..., 100].numpy(),
]
self.assertTrue(np.array_equal(var[0], np_int_value[..., 0]))
self.assertTrue(np.array_equal(var[1], np_int_value[..., 1, 0]))
self.assertTrue(np.array_equal(var[2], np_int_value[0, ..., 1, 0]))
self.assertTrue(np.array_equal(var[3], np_int_value[1, ..., 1]))
self.assertTrue(np.array_equal(var[4], np_int_value[2, ...]))
self.assertTrue(np.array_equal(var[5], np_int_value[2, 0, ...]))
self.assertTrue(np.array_equal(var[6], np_int_value[2, 0, 1, ...]))
self.assertTrue(np.array_equal(var[7], np_int_value[...]))
self.assertTrue(np.array_equal(var[8], np_int_value[:, ..., 100]))

def _test_for_var(self):
np_value = np.random.random((30, 100, 100)).astype('float32')
w = fluid.dygraph.to_variable(np_value)
Expand All @@ -664,6 +717,7 @@ def test_slice(self):
self._test_slice()
self._test_slice_for_tensor_attr()
self._test_for_var()
self._test_for_getitem_ellipsis_index()

var = fluid.dygraph.to_variable(self.array)
self.assertTrue(np.array_equal(var[1, :].numpy(), self.array[1, :]))
Expand Down