Skip to content

[Zero-Dim] support 0-D output and 0-D as indice in __getitem__/__setitem__ #52814

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

Merged

Conversation

zoooo0820
Copy link
Contributor

@zoooo0820 zoooo0820 commented Apr 12, 2023

PR types

Others

PR changes

APIs

Describe

Pcard-66985

This PR is to support 0-D Tensor in Tensor __getitem__. From now, 0-D Tensor is semantically equivalent to scalar while 1-D Tensor is vector.

There will be two incompatible behavior changes in __getitem__:

  • all axis has a scalar indice, the output will be a 0-D tensor.
>>> x = paddle.ones((2,3,4))
>>> y = x[1,1,1]

# before this PR:
>>> y
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       1.)

# after this PR:
>>> y
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       1.)
  • In index element, 0-D Tensor will be treated as scalar, and 1-D Tensor will be treat as vector
>>> x = paddle.ones((2,3,4))
>>> y = x[1,1,1]
>>> y
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       1.)

# is same as y = x[1,1,1]
>>> indice = paddle.ones((), dtype='int32')
>>> x[indice, indice, indice]
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       1.)

# behavior changes for 1-D Tensor
>>> x = paddle.ones((2,3,4))
>>> indice = paddle.ones([1], dtype='int32')

# before this PR, 1-D Tensor is treated as scalar
>>> x[indice].shape
[3, 4]

# after this PR, 1-D Tensor is treated as vector
>>> x[indice].shape
[1, 3, 4]

NOTE : Combine indexing is not supported now, and 1-d Tensor is treated as vector, so following usages are not supported, we will try to support these usages as soon as possible.
x[1, paddle.ones([1], dtype='int32')] / x[:, paddle.ones([1], dtype='int32')]

For __setitem__ , now we can also use 0-D Tensor as indice

>>> x = paddle.ones((2,3,2))
>>> indice = paddle.ones([], dtype='int32')
>>> x[indice, indice] = 10
>>> x
Tensor(shape=[2, 3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[[1. , 1. ],
         [1. , 1. ],
         [1. , 1. ]],

        [[1. , 1. ],
         [10., 10.],
         [1. , 1. ]]])

0-D Tensor can also be used as a value to fill.

>>> x = paddle.ones((2,3,2))
>>> indice = paddle.ones([], dtype='int32')
>>> value = paddle.ones([]) * 10
>>> x[indice, indice] = value
>>> x
Tensor(shape=[2, 3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[[1. , 1. ],
         [1. , 1. ],
         [1. , 1. ]],

        [[1. , 1. ],
         [10., 10.],
         [1. , 1. ]]])

@paddle-bot
Copy link

paddle-bot bot commented Apr 12, 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.

@zoooo0820 zoooo0820 changed the title support 0-D output and 0-D as indice in __getitem__ support 0-D output and 0-D as indice in __getitem__/__setitem__ Apr 14, 2023
@zoooo0820 zoooo0820 force-pushed the support_0d_for_getitem_output branch from ea71121 to f2559ac Compare April 14, 2023 10:58
zhwesky2010
zhwesky2010 previously approved these changes Apr 19, 2023
assert len(ref_dims_mapping) == len(out_dims_mapping)
assert ref_dims_mapping[0] == out_dims_mapping[0]
Copy link
Contributor

@zhwesky2010 zhwesky2010 Apr 19, 2023

Choose a reason for hiding this comment

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

这个感觉可以判断out_dims_mapping如果维度>0,就判断 assert ref_dims_mapping[0] == out_dims_mapping[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.

这里特化了ref_dims_mapping==[]的场景,在下面else分支中会逐轴比对ref_dims_mapping[i] / out_dims_mapping[i]

@zhwesky2010
Copy link
Contributor

test_set_value_op_xpu、test_slice_op_xpu挂了

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

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

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

LGTM for paddle_enforce

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 merged commit 4e939c8 into PaddlePaddle:develop Apr 21, 2023
@zoooo0820 zoooo0820 deleted the support_0d_for_getitem_output branch April 21, 2023 13:47
@zhwesky2010 zhwesky2010 changed the title support 0-D output and 0-D as indice in __getitem__/__setitem__ [Zero-Dim] support 0-D output and 0-D as indice in __getitem__/__setitem__ May 8, 2023
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.

5 participants