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 some kernels(csr*dense->csr, dense*dense->csr) of SparseTensor matmul #42935

Merged
merged 8 commits into from
Jun 15, 2022

Conversation

zhwesky2010
Copy link
Contributor

@zhwesky2010 zhwesky2010 commented May 23, 2022

PR types

New features

PR changes

APIs

Describe

稀疏矩阵乘系列一共7个API,14个计算Kernel,本PR增加其中2个:

序号 API 输入输出类型1 输入输出类型2 输入输出类型3(csr) 输入输出类型4(csr)
1 paddle.incubate.sparse.mv
coo dense -> dense csr dense -> dense
2 paddle.incubate.sparse.matmul coo coo -> coo coo dense -> dense csr csr -> csr ✅ csr dense -> dense
3 paddle.incubate.sparse.matmul_sp coo dense -> coo
4 paddle.incubate.sparse.matmul_hsp
coo dense -> hybrid coo
5 paddle.incubate.sparse.addmm coo + coo * coo -> coo
dense + coo * dense -> dense
csr + csr * csr -> csr
dense + csr * dense -> dense
6 paddle.incubate.sparse.addmm_sp coo + coo * dense -> coo
7 paddle.incubate.sparse.masked_matmul
✅ dense dense csr_mask-> csr

paddle.incubate.sparse.matmul

通用稀疏矩阵乘,支持 sparse与sparse运算、sparse与dense交互运算 等常规运算

import paddle
from paddle.fluid.framework import _test_eager_guard 
paddle.seed(100)

# csr @ dense -> dense

with _test_eager_guard():         
    crows = [0, 2, 3, 5]
    cols = [1, 3, 2, 0, 1]
    values = [1., 2., 3., 4., 5.]
    dense_shape = [3, 4]
    csr = paddle.incubate.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
    # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
    #       crows=[0, 2, 3, 5],
    #       cols=[1, 3, 2, 0, 1],
    #       values=[1., 2., 3., 4., 5.])
    dense = paddle.randn([4, 3])
    
    out = paddle.incubate.sparse.matmul(csr, dense)
    # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
    #        [[-1.94294846 , -3.33990622 ,  0.62359387 ],
    #         [-4.12815523 ,  3.46535444 , -3.27413893 ],
    #         [-0.15209436 , -19.23207283, -3.35593438 ]])

paddle.incubate.sparse.masked_matmul

特殊矩阵乘,相当于 dense矩阵乘、mask稀疏采样 两个操作的叠加,在某些模型使用

import paddle
from paddle.fluid.framework import _test_eager_guard
paddle.seed(100)

# dense @ dense * csr_mask -> csr

with _test_eager_guard():
    crows = [0, 2, 3, 5]
    cols = [1, 3, 2, 0, 1]
    values = [1., 2., 3., 4., 5.]
    dense_shape = [3, 4]
    mask = paddle.incubate.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
    # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
    #       crows=[0, 2, 3, 5],
    #       cols=[1, 3, 2, 0, 1],
    #       values=[1., 2., 3., 4., 5.])

    x = paddle.rand([3, 5])
    y = paddle.rand([5, 3])

    out = paddle.incubate.sparse.masked_matmul(x, y, mask)
    # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True, 
    #        crows=[0, 2, 3, 5], 
    #        cols=[1, 3, 2, 0, 1], 
    #        values=[0.98986477, 0.97800624, 1.14591956, 0.68561077, 0.94714981])

@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.

@zhwesky2010 zhwesky2010 force-pushed the sparse_mm branch 3 times, most recently from b9ca201 to 1c86c1c Compare May 23, 2022 08:48
@zhwesky2010 zhwesky2010 force-pushed the sparse_mm branch 2 times, most recently from e73c7c6 to f2aa2f0 Compare June 6, 2022 11:49
@zhwesky2010 zhwesky2010 changed the title add csr dense matmul of API:paddle.sparse.mm add some kernel(csr*dense->csr, dense*dense->csr) of SparseTensor matmul Jun 6, 2022
@zhwesky2010 zhwesky2010 force-pushed the sparse_mm branch 4 times, most recently from cf56517 to dc128d8 Compare June 7, 2022 02:58
@zhwesky2010 zhwesky2010 force-pushed the sparse_mm branch 7 times, most recently from 100c8fa to eb2d869 Compare June 7, 2022 14:39
zkh2016
zkh2016 previously approved these changes Jun 13, 2022
kolinwei
kolinwei previously approved these changes Jun 13, 2022
@@ -1777,6 +1777,8 @@ PyMethodDef variable_methods[] = {
METH_VARARGS | METH_KEYWORDS, NULL},
{"cols", (PyCFunction)(void (*)(void))tensor_method_get_non_zero_cols,
METH_VARARGS | METH_KEYWORDS, NULL},
{"is_dense", (PyCFunction)(void (*)(void))tensor_method_is_dense,
Copy link
Contributor

Choose a reason for hiding this comment

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

这个已经有了,在前面,可以看下,这里重复了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已删除

@@ -0,0 +1,113 @@
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

这些文件名,前缀能去掉吗?sparse/cpu/sparse...,目录已经有sparse信息了,文件名不需要再重复一遍,略显冗余

Copy link
Contributor Author

@zhwesky2010 zhwesky2010 Jun 13, 2022

Choose a reason for hiding this comment

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

去掉了,后续统一与dense kernel保持一致文件命名风格,不加前缀

#include "paddle/phi/kernels/copy_kernel.h"
#include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/funcs/sparse/sparse_blas.h"
#include "paddle/phi/kernels/sparse/sparse_mm_kernel.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

头文件要在最上面,这里头文件顺序不太对

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 Author

@zhwesky2010 zhwesky2010 Jun 13, 2022

Choose a reason for hiding this comment

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

cu文件会被pre-commit自动按字母序修改,所以cu没办法保持full_kernel.h在首行了

Copy link
Contributor

Choose a reason for hiding this comment

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

加空行隔开也不行吗,之前是可以的

Copy link
Contributor Author

@zhwesky2010 zhwesky2010 Jun 14, 2022

Choose a reason for hiding this comment

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

试了下不行,看了下其他cu,应该都没成功

#include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/funcs/sparse/sparse_blas.h"
#include "paddle/phi/kernels/sparse/sparse_mm_grad_kernel.h"
#include "paddle/phi/kernels/transpose_kernel.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改头文件规范,thx~

@zhwesky2010 zhwesky2010 dismissed stale reviews from kolinwei and zkh2016 via 60857b0 June 13, 2022 15:06
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.

API 名称建议修改一下mm -> matmul

@zhwesky2010
Copy link
Contributor Author

API 名称建议修改一下mm -> matmul

已修改,thx~

XiaoguangHu01
XiaoguangHu01 previously approved these changes Jun 14, 2022
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

chenwhql
chenwhql previously approved these changes Jun 14, 2022
zkh2016
zkh2016 previously approved these changes Jun 14, 2022
kolinwei
kolinwei previously approved these changes Jun 14, 2022
wanghuancoder
wanghuancoder previously approved these changes Jun 14, 2022
Copy link
Contributor

@wanghuancoder wanghuancoder 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

@wanghuancoder wanghuancoder 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

@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 346efe9 into PaddlePaddle:develop Jun 15, 2022
@zhwesky2010 zhwesky2010 changed the title add some kernels(csr*dense->csr, dense*dense->csr) of SparseTensor matmul 【Sparse】add some kernels(csr*dense->csr, dense*dense->csr) of SparseTensor matmul Jun 15, 2022
sneaxiy pushed a commit to sneaxiy/Paddle that referenced this pull request Jun 27, 2022
…tmul (PaddlePaddle#42935)

* add some kernel(csr*dense->csr, dense*dense->csr) of SparseTensor matmul

* fix CI

* fix CI

* fix comment

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

6 participants