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

【Sparse】add new API/OP(csr->csr) of SparseTensor softmax #43475

Merged
merged 2 commits into from Jun 20, 2022

Conversation

zhwesky2010
Copy link
Contributor

@zhwesky2010 zhwesky2010 commented Jun 13, 2022

PR types

New features

PR changes

OPs

Describe

新增稀疏Tensor激活函数paddle.incubate.sparse.nn.Softmaxpaddle.incubate.sparse.nn.functional.softmax

paddle.incubate.sparse.nn.Softmax

import paddle
import numpy as np
from paddle.fluid.framework import _test_eager_guard

paddle.seed(100)

with _test_eager_guard():
    mask = np.random.rand(3, 4) < 0.5
    np_x = np.random.rand(3, 4) * mask
    # [[0.         0.         0.96823406 0.19722934]
    #  [0.94373937 0.         0.02060066 0.71456372]
    #  [0.         0.         0.         0.98275049]]

    csr = paddle.to_tensor(np_x).to_sparse_csr()
    # Tensor(shape=[3, 4], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, 
    #        crows=[0, 2, 5, 6], 
    #        cols=[2, 3, 0, 2, 3, 3], 
    #        values=[0.96823406, 0.19722934, 0.94373937, 0.02060066, 0.71456372,
    #                0.98275049])

    m = paddle.incubate.sparse.nn.Softmax()
    out = m(csr)
    # Tensor(shape=[3, 4], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, 
    #        crows=[0, 2, 5, 6], 
    #        cols=[2, 3, 0, 2, 3, 3], 
    #        values=[0.68373820, 0.31626180, 0.45610887, 0.18119845, 0.36269269,
    #                1.        ])

paddle.incubate.sparse.nn.functional.softmax

import paddle
import numpy as np
from paddle.fluid.framework import _test_eager_guard

paddle.seed(100)

with _test_eager_guard():
    mask = np.random.rand(3, 4) < 0.5
    np_x = np.random.rand(3, 4) * mask
    # [[0.         0.         0.96823406 0.19722934]
    #  [0.94373937 0.         0.02060066 0.71456372]
    #  [0.         0.         0.         0.98275049]]

    csr = paddle.to_tensor(np_x).to_sparse_csr()
    # Tensor(shape=[3, 4], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, 
    #        crows=[0, 2, 5, 6], 
    #        cols=[2, 3, 0, 2, 3, 3], 
    #        values=[0.96823406, 0.19722934, 0.94373937, 0.02060066, 0.71456372,
    #                0.98275049])

    out = paddle.incubate.sparse.nn.functional.softmax(csr)
    # Tensor(shape=[3, 4], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, 
    #        crows=[0, 2, 5, 6], 
    #        cols=[2, 3, 0, 2, 3, 3], 
    #        values=[0.68373820, 0.31626180, 0.45610887, 0.18119845, 0.36269269,
    #                1.        ])

@paddle-bot-old
Copy link

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

chenwhql
chenwhql previously approved these changes Jun 17, 2022
lanxianghit
lanxianghit previously approved these changes Jun 17, 2022
auto functor = phi::funcs::CudaExpFunctor<T>();
out_values[row_first + idx] =
functor(x_values[row_first + idx] - row_max_val);
exp_sum += functor(x_values[row_first + idx] - row_max_val);
Copy link
Contributor

Choose a reason for hiding this comment

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

T exp = functor(x_values[row_first + idx] - row_max_val);
out_values[row_first + idx] = exp;
exp_sum += exp;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thx~

sparse softmax activation, x must be SparseCsrTensor or SparseCooTensor.

Note:
Only supported axis=-1 for SparseCsrTensor, which is faster when read data
Copy link
Contributor

Choose a reason for hiding this comment

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

only support axis=-1 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

supported写错了,应该是support,下个PR改下哈

@zhwesky2010 zhwesky2010 merged commit 2ddbc64 into PaddlePaddle:develop Jun 20, 2022
sneaxiy pushed a commit to sneaxiy/Paddle that referenced this pull request Jun 27, 2022
…e#43475)

* add new API/OP(csr->csr) of SparseTensor softmax

* fix comment
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.

None yet

5 participants