Skip to content

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 committed Jun 14, 2022
1 parent 60857b0 commit bcf3bfb
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 50 deletions.
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/cpu/matmul_grad_kernel.cc
Expand Up @@ -56,7 +56,7 @@ PD_REGISTER_KERNEL(csr_dense_matmul_grad,
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

PD_REGISTER_KERNEL(csr_masked_mm_grad,
PD_REGISTER_KERNEL(csr_masked_matmul_grad,
CPU,
ALL_LAYOUT,
phi::sparse::CsrMaskedMatmulGradKernel,
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/cpu/matmul_kernel.cc
Expand Up @@ -53,7 +53,7 @@ PD_REGISTER_KERNEL(csr_dense_matmul,
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

PD_REGISTER_KERNEL(csr_masked_mm,
PD_REGISTER_KERNEL(csr_masked_matmul,
CPU,
ALL_LAYOUT,
phi::sparse::CsrMaskedMatmulKernel,
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/gpu/matmul_grad_kernel.cu
Expand Up @@ -141,7 +141,7 @@ PD_REGISTER_KERNEL(csr_dense_matmul_grad,
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

PD_REGISTER_KERNEL(csr_masked_mm_grad,
PD_REGISTER_KERNEL(csr_masked_matmul_grad,
GPU,
ALL_LAYOUT,
phi::sparse::CsrMaskedMatmulGradKernel,
Expand Down
10 changes: 5 additions & 5 deletions paddle/phi/kernels/sparse/gpu/matmul_kernel.cu
Expand Up @@ -144,19 +144,19 @@ void CsrMaskedMatmulKernel(const Context& dev_ctx,
"The shape of Input(x) and Input(y) is not suitable for matmul "
"opetation, x_dim[-1] must be eaqual to y_dim[-2]."));

PADDLE_ENFORCE_GE(
PADDLE_ENFORCE_EQ(
maskdim_vec[mask_ndims - 2],
xdim_vec[x_ndims - 2],
phi::errors::PreconditionNotMet(
"The shape of Input(x) and Input(y) is not suitable for matmul "
"opetation, x_dim[-1] must be eaqual to y_dim[-2]."));
"opetation, mask_dim[-2] must be eaqual to x_dim[-2]."));

PADDLE_ENFORCE_GE(
PADDLE_ENFORCE_EQ(
maskdim_vec[mask_ndims - 1],
ydim_vec[y_ndims - 1],
phi::errors::PreconditionNotMet(
"The shape of Input(x) and Input(y) is not suitable for matmul "
"opetation, x_dim[-1] must be eaqual to y_dim[-2]."));
"opetation, mask_dim[-1] must be eaqual to y_dim[-1]."));

// InferMeta of SparseCsrTensor 'out'
out->set_dims(mask.dims());
Expand Down Expand Up @@ -199,7 +199,7 @@ PD_REGISTER_KERNEL(csr_dense_matmul,
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

PD_REGISTER_KERNEL(csr_masked_mm,
PD_REGISTER_KERNEL(csr_masked_matmul,
GPU,
ALL_LAYOUT,
phi::sparse::CsrMaskedMatmulKernel,
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_sparse_matmul_op.py
Expand Up @@ -61,7 +61,7 @@ def test_matmul(self):

csr = paddle.to_tensor(np_x, stop_gradient=False).to_sparse_csr()
dense = paddle.to_tensor(np_dense, stop_gradient=False)
out = paddle.incubate.sparse.mm(csr, dense)
out = paddle.incubate.sparse.matmul(csr, dense)

self.assertTrue(np.allclose(np_out, out.numpy()))

Expand Down Expand Up @@ -102,7 +102,7 @@ def test_matmul(self):
x = paddle.to_tensor(np_x, stop_gradient=False)
y = paddle.to_tensor(np_y, stop_gradient=False)
mask = paddle.to_tensor(np.ones([10, 6]) * np_mask).to_sparse_csr()
out = paddle.incubate.sparse.masked_mm(x, y, mask)
out = paddle.incubate.sparse.masked_matmul(x, y, mask)

self.assertTrue(np.allclose(np_out.indptr, out.crows().numpy()))
self.assertTrue(np.allclose(np_out.indices, out.cols().numpy()))
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/incubate/sparse/__init__.py
Expand Up @@ -19,8 +19,8 @@
from .unary import sin
from .unary import tanh

from .binary import mm
from .binary import masked_mm
from .binary import matmul
from .binary import masked_matmul

from . import nn

Expand All @@ -30,6 +30,6 @@
'sqrt',
'sin',
'tanh',
'mm',
'masked_mm',
'matmul',
'masked_matmul',
]
38 changes: 21 additions & 17 deletions python/paddle/incubate/sparse/binary.py
Expand Up @@ -19,7 +19,7 @@


@dygraph_only
def mm(x, y, name=None):
def matmul(x, y, name=None):
"""
Warning:
This API is only used from ``CUDA 11.0`` .
Expand Down Expand Up @@ -52,34 +52,36 @@ def mm(x, y, name=None):
.. code-block:: python
import numpy as np
import paddle
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.framework import _test_eager_guard
paddle.seed(100)
paddle.set_device('gpu')
with _test_eager_guard():
# csr @ dense -> dense
# 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.mm(csr, dense)
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 ]])
"""
return _C_ops.final_state_sparse_mm(x, y)
return _C_ops.final_state_sparse_matmul(x, y)


@dygraph_only
def masked_mm(x, y, mask, name=None):
def masked_matmul(x, y, mask, name=None):
"""
Warning:
This API is only used from ``CUDA 11.3`` .
Expand Down Expand Up @@ -111,29 +113,31 @@ def masked_mm(x, y, mask, name=None):
.. code-block:: python
import numpy as np
import paddle
from paddle.fluid.framework import _test_eager_guard
paddle.seed(100)
paddle.set_device('gpu')
# dense @ dense * csr_mask -> csr
with _test_eager_guard():
# dense @ dense * csr_mask -> csr
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, 4])
out = paddle.incubate.sparse.masked_mm(x, y, mask)
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])
"""
return _C_ops.final_state_sparse_masked_mm(x, y, mask)
return _C_ops.final_state_sparse_masked_matmul(x, y, mask)
28 changes: 14 additions & 14 deletions python/paddle/utils/code_gen/sparse_api.yaml
Expand Up @@ -97,24 +97,15 @@
layout : x
data_type : dtype

- api: masked_mm
- api: masked_matmul
args : (Tensor x, Tensor y, Tensor mask)
output : Tensor(out)
kernel :
func : csr_masked_mm{dense, dense, sparse_csr -> sparse_csr}
func : csr_masked_matmul{dense, dense, sparse_csr -> sparse_csr}
layout : x
backward: masked_mm_grad
backward: masked_matmul_grad

- api: maxpool
args : (Tensor x, int[] kernel_sizes, int[] paddings, int[] dilations, int[] strides)
output : Tensor(out), Tensor(rulebook)
kernel :
func : sparse_maxpool{sparse_coo -> sparse_coo, dense}
layout : x
intermediate : rulebook
backward : sparse_maxpool_grad

- api: mm
- api: matmul
args : (Tensor x, Tensor y)
output : Tensor(out)
kernel :
Expand All @@ -123,4 +114,13 @@
coo_dense_matmul{sparse_coo, dense -> dense},
coo_coo_matmul{sparse_coo, sparse_coo -> sparse_coo}
layout : x
backward: mm_grad
backward: matmul_grad

- api: maxpool
args : (Tensor x, int[] kernel_sizes, int[] paddings, int[] dilations, int[] strides)
output : Tensor(out), Tensor(rulebook)
kernel :
func : sparse_maxpool{sparse_coo -> sparse_coo, dense}
layout : x
intermediate : rulebook
backward : sparse_maxpool_grad
10 changes: 5 additions & 5 deletions python/paddle/utils/code_gen/sparse_bw_api.yaml
Expand Up @@ -25,15 +25,15 @@
output : Tensor(x_grad)
invoke : to_dense_impl(out_grad)

- backward_api : masked_mm_grad
forward : masked_mm(Tensor x, Tensor y, Tensor mask) -> Tensor(out)
- backward_api : masked_matmul_grad
forward : masked_matmul(Tensor x, Tensor y, Tensor mask) -> Tensor(out)
args : (Tensor x, Tensor y, Tensor out_grad)
output : Tensor(x_grad), Tensor(y_grad)
kernel :
func : csr_masked_mm_grad{dense, dense, sparse_csr -> dense, dense}
func : csr_masked_matmul_grad{dense, dense, sparse_csr -> dense, dense}

- backward_api : mm_grad
forward : mm(Tensor x, Tensor y) -> Tensor(out)
- backward_api : matmul_grad
forward : matmul(Tensor x, Tensor y) -> Tensor(out)
args : (Tensor x, Tensor y, Tensor out_grad)
output : Tensor(x_grad), Tensor(y_grad)
kernel :
Expand Down

0 comments on commit bcf3bfb

Please sign in to comment.