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

【Paddle Tensor 第二期】使paddle.all支持0-size输入 #70228

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

aquagull
Copy link
Contributor

PR Category

User Experience

PR Types

Improvements

Description

初始paddle.all输入0-size的情况:
image

修复完成后,再次运行同一个代码:
2E9C0A7B756227CD3234C05B81500E78

复现代码

import paddle

zero_size_tensor_with_dim = paddle.empty(shape=[3, 0, 2], dtype=bool)
print("Input:", zero_size_tensor_with_dim)
print("0-size tensor with dim=0", paddle.all(zero_size_tensor_with_dim, axis=0))
print("0-size tensor with dim=1", paddle.all(zero_size_tensor_with_dim, axis=1))
print("0-size tensor with dim=2", paddle.all(zero_size_tensor_with_dim, axis=2))

print("0-size tensor with dim=0, keepdim=True", paddle.all(zero_size_tensor_with_dim, axis=0, keepdim=True))
print("0-size tensor with dim=1, keepdim=True", paddle.all(zero_size_tensor_with_dim, axis=1, keepdim=True))
print("0-size tensor with dim=2, keepdim=True", paddle.all(zero_size_tensor_with_dim, axis=2, keepdim=True))

print("0-size tensor with keepdim=True", paddle.all(zero_size_tensor_with_dim, keepdim=True))
print("0-size tensor with keepdim=False", paddle.all(zero_size_tensor_with_dim))

Copy link

paddle-bot bot commented Dec 14, 2024

你的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.

@paddle-bot paddle-bot bot added the contributor External developers label Dec 14, 2024
Comment on lines 47 to 49
const int64_t* dims_data = out->dims().Get();
int num_dims = out->dims().size();
std::vector<int64_t> vec_dims(dims_data, dims_data + num_dims);
Copy link
Contributor

Choose a reason for hiding this comment

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

out_dims_vec = common::vectorize(out->dims());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已更改

}

Copy link
Contributor

Choose a reason for hiding this comment

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

删除

Comment on lines 2259 to 2284
def calculate_expected_result(self, axis, keepdim):
if axis is None:
expected_result = np.array(True)
elif isinstance(axis, int):
if keepdim:
expected_shape = list(self.shape)
expected_shape[axis] = 1
expected_result = np.ones(expected_shape, dtype=bool)
else:
expected_shape = list(self.shape)
del expected_shape[axis]
expected_result = np.ones(expected_shape, dtype=bool)
# axis is tuple
else:
if keepdim:
expected_shape = list(self.shape)
for i in axis:
expected_shape[i] = 1
expected_result = np.ones(expected_shape, dtype=bool)
else:
expected_shape = list(self.shape)
for i in sorted(axis, reverse=True):
del expected_shape[i]
expected_result = np.ones(expected_shape, dtype=bool)

return expected_result
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是不是能直接用np.all代替?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

确实,可以直接用np.all


x = paddle.to_tensor(x_np)
dygraph_result = paddle.all(x, axis=axis, keepdim=keepdim).numpy()
expected_result = self.calculate_expected_result(axis, keepdim)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里直接用np.all就行了吧,不用自己手写一个

@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants