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

Normalize multi dim list in indexing #56893

Merged

Conversation

zoooo0820
Copy link
Contributor

@zoooo0820 zoooo0820 commented Sep 1, 2023

PR types

Breaking changes

PR changes

APIs

Description

Pcard-66985

Background Information:

1. Semantic of List/Tensor and Tuple: In indexing, the semantics of List type is same with Tensor/Array type (corresponding to one axis). As a comparison,Tuple type is different (corresponding to multiple axes, each element is an index on the corresponding axis).

2. The current semantic of List are contradictory:Currently in Paddle, List type have two semantics.

  • If rank of List is one, it corresponds to one axis (same with Tensor/Array, which is naturally).
>>> a = paddle.randn((3,2))
>>> a
Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[ 0.07773950,  0.19835377],
        [-0.81423134,  0.60788709],
        [ 1.38384938,  0.00627374]])

# one-dim List is same as Tensor
>>> a[[0,1]]
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[ 0.07773950,  0.19835377],
        [-0.81423134,  0.60788709]])
>>> a[paddle.to_tensor([0,1])]
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[ 0.07773950,  0.19835377],
        [-0.81423134,  0.60788709]])
  • if its rank is greater than 1, it means that it may correspond to multiple axes (The outermost [] is equivalent to tuple,which is to wrap indexes on different axes together).
# In this case, List is same with Tuple, and different with Tensor/Array
>>> a[[[0],[1]]]
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [0.19835377])
>>> a[([0],[1])]
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [0.19835377])
>>> a[paddle.to_tensor([[0],[1]])]
Tensor(shape=[2, 1, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[[ 0.07773950,  0.19835377]],

        [[-0.81423134,  0.60788709]]])

What this PR did

This PR unified the semantics of List in indexing, like Numpy(as described in #51466 ). From this PR, List is same with Tensor/Array in any case.

@paddle-bot
Copy link

paddle-bot bot commented Sep 1, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@jeff41404
Copy link
Contributor

The fundamental modification of thist PR is to unify the tensor indexing semantics of one-dimensional list and multidimensional list. So, it is recommended to use tensor indexing of one-dimensional list and tensor indexing of multidimensional list for comparison in the examples of description.

Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

LGTM

@jeff41404 jeff41404 merged commit 6e17e66 into PaddlePaddle:develop Sep 8, 2023
@zoooo0820 zoooo0820 deleted the update_multi_dim_list_in_indexing branch September 8, 2023 10:44
BeingGod pushed a commit to BeingGod/Paddle that referenced this pull request Sep 9, 2023
* add unit test for bool-list index

* normative semantics of multi-dim List

* Adapt to different CI environment Numpy versions
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.

2 participants