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

【Hackathon 4th No.25】为 Paddle 新增 paddle.sparse.any 稀疏 API #502

Merged
merged 3 commits into from
May 9, 2023

Conversation

@paddle-bot
Copy link

paddle-bot bot commented Apr 6, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请检查PR提交格式和内容是否完备,具体请参考示例模版
Your PR has been submitted. Thanks for your contribution!
Please check its format and content. For this, you can refer to Template and Demo.

@zrr1999
Copy link
Member Author

zrr1999 commented Apr 7, 2023

any 算子似乎并没有比较合理的梯度定义,在 Dense 张量上似乎也没有实现反向传播

目前paddle缺少相关功能实现。

# 三、业内方案调研
## Pytorch
Copy link

Choose a reason for hiding this comment

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

pytorch的any操作是否支持sparse tensor?

Copy link
Member Author

Choose a reason for hiding this comment

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

根据文档的描述,torch.any支持稀疏矩阵和稠密矩阵,两种实现是写在一起的
image
一些其他算子,例如sum是单独实现的。

Copy link
Member Author

@zrr1999 zrr1999 May 9, 2023

Choose a reason for hiding this comment

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

torch应该只支持coo格式,我测试了一下,csr确实是不支持的

import torch
import torch.nn as nn

x = torch.Tensor([[[1, 0], [1, 0]], [[1, 0], [1, 0]], [[1, 0], [1, 0]]])
coo_x = x.to_sparse_coo()
csr_x = x.to_sparse_csr()

print(torch.any(coo_x))
print(torch.any(csr_x)) # error

同时,any也是不支持求解梯度的

import torch
import torch.nn as nn

x = torch.Tensor([[[1, 0], [1, 0]], [[1, 0], [1, 0]], [[1, 0], [1, 0]]])
coo_x = x.to_sparse_coo()
coo_x.requires_grad = True

y = torch.any(coo_x)
z = torch.sum(coo_x)

z.backward()
y.backward() # error

scipy.sparse库中没有any()函数。但是,可以使用numpy库中的any()函数在稀疏矩阵中执行相同的操作。

## paddle DenseTensor
DenseTensor中的sum被定义为paddle.any(x, axis=None, dtype=None, keepdim=False, name=None),
Copy link

Choose a reason for hiding this comment

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

sum->any,包括后面的sum也应该相应修改下。

Copy link
Member Author

Choose a reason for hiding this comment

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

好的,已修改

@zrr1999 zrr1999 requested a review from zkh2016 May 9, 2023 07:00
@luotao1 luotao1 merged commit 24492e3 into PaddlePaddle:master May 9, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants