Skip to content

Conversation

zoooo0820
Copy link
Contributor

@zoooo0820 zoooo0820 commented Mar 1, 2023

PR types

Bug fixes

PR changes

APIs

Describe

This PR is a patch, which is to support bool tensor indexing with shape -1.

import paddle

paddle.enable_static()
x = paddle.static.data(name='x', shape=[-1, 1, -1], dtype='float32')
index = paddle.static.data(name='index', shape=[-1, 1, -1], dtype='bool')
a = paddle.static.data(name="a", shape=[-1, ])
# getitem for bool tensor
out = x[index]

# setitem for bool tensor
x[index] = a

Note1: A incompatible change will be introduced by this PR. However, it is consistent with Numpy.

x = paddle.rand((2,3,4))
index = x < 0   # all elements are False
x[index].shape 

# before this PR :  [0, 3, 4]
# After this PR: [0]

index = index[:, :, 0]  # shape of index is [2, 3]
x[index].shape 
# before this PR :  [0, 3, 4]
# After this PR: [0, 4]

Note2: This PR is just a patch, which cannot completely solve indexing problem. The indexing of Paddle still needs to be re-designed. The following case are still not be supported.

# when length of index is less than x, and the later part of x.shape contains -1
x = paddle.static.data(name='x', shape=[-1, 1, -1], dtype='float32')
index = paddle.static.data(name='index', shape=[-1, 1], dtype='bool')
x[index] = a   # will raise a error

@paddle-bot
Copy link

paddle-bot bot commented Mar 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.

@zoooo0820 zoooo0820 force-pushed the bool_tensor_indexing branch 2 times, most recently from 404b7fa to dd5ec74 Compare March 1, 2023 06:38
@jeff41404 jeff41404 merged commit 059699a into PaddlePaddle:develop Mar 13, 2023
@zoooo0820 zoooo0820 deleted the bool_tensor_indexing branch March 14, 2023 03:43
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