Skip to content

Conversation

@zyfncg
Copy link
Contributor

@zyfncg zyfncg commented Aug 19, 2021

PR types

New features

PR changes

APIs

Describe

Support gettiem by Bool index

Example:

x = paddle.to_tensor([[1, 2], [3, 4], [5, 6]])

# 1. 支持bool类型list索引(一维list且长度与x的第0维相同)
y = x[[True, True, False]]

# 2. 支持bool类型Tensor索引(维度不超过x的维度,且从前到后每一维的长度需和x对应维度相同)
idx = paddle.to_tensor([[True, False], [False, False], [True, False]])
z = x[idx]


PADDLE_ENFORCE_EQ(index_dim[0] != 0, true,
platform::errors::InvalidArgument(
"The length of Input(Index) can't be 0."));
Copy link
Contributor

Choose a reason for hiding this comment

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

length -> dimension ? 其他维度可以是0吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

目前IndexSelectOp只支持一维的index索引,所以这里只需判断第0维的长度

}
}
if (all_bool) {
if (list_size != shape[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use PADDLE_ENFORCE_EQ directly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里会在setitem支持Bool类型索引的PR中改为使用PADDLE_ENFORCE_EQ

}
}
} else {
for (int j = 0; j < list_size; ++j) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why this branch no need check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

当索引中存在整数时,整个index索引list将会以整数类型进行处理,整数list类型的索引没有长度限制,所以没有进行check

PyObject *list_item = PyList_GetItem(slice_item, j);
if (PyCheckInteger(list_item)) {
list_select_idxs->push_back(
static_cast<int>(PyLong_AsLong(list_item)));
Copy link
Contributor

Choose a reason for hiding this comment

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

int only can be 0 or 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.

当索引中存在整数时,bool类型的True和False分别会被替换为整数类型的1和0进行处理

dim += rank - specified_dims;
} else if (slice_item == Py_None) {
none_axes->push_back(dim);
} else if (PyList_Check(slice_item)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

是否需要检测tuple的情况,例如:index=(False,True),tensor[index]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ParseIndexingSlice在开始处理参数前有针对tuple类型的预处理,这里可以不用再做检测
image

Copy link
Contributor

@chenwhql chenwhql left a comment

Choose a reason for hiding this comment

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

LGTM

@hbwx24 hbwx24 merged commit b6dc16c into PaddlePaddle:develop Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants