matmul

paddle.sparse. matmul ( x, y, name=None ) [source]

Note

This API is only supported from CUDA 11.0 .

Applies matrix multiplication of two Tensors.

The supported input/output Tensor type are as follows:

Note

x[SparseCsrTensor] @ y[SparseCsrTensor] -> out[SparseCsrTensor] x[SparseCsrTensor] @ y[DenseTensor] -> out[DenseTensor] x[SparseCooTensor] @ y[SparseCooTensor] -> out[SparseCooTensor] x[SparseCooTensor] @ y[DenseTensor] -> out[DenseTensor]

It supports backward propagation.

Dimensions x and y must be >= 2D. Automatic broadcasting of Tensor is not supported. the shape of x should be [*, M, K] , and the shape of y should be [*, K, N] , where * is zero or more batch dimensions.

Parameters
  • x (SparseTensor) – The input tensor. It can be SparseCooTensor/SparseCsrTensor. The data type can be float32 or float64.

  • y (SparseTensor|DenseTensor) – The input tensor. It can be SparseCooTensor/SparseCsrTensor/DenseTensor. The data type can be float32 or float64.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Determined by x and y .

Return type

SparseTensor|DenseTensor

Examples

>>> 
>>> import paddle
>>> paddle.device.set_device('gpu')

>>> # csr @ dense -> dense
>>> crows = [0, 1, 2, 3]
>>> cols = [1, 2, 0]
>>> values = [1., 2., 3.]
>>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, [3, 3])
>>> print(csr)
Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
       crows=[0, 1, 2, 3],
       cols=[1, 2, 0],
       values=[1., 2., 3.])

>>> dense = paddle.ones([3, 2])
>>> out = paddle.sparse.matmul(csr, dense)
>>> print(out)
Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[1., 1.],
        [2., 2.],
        [3., 3.]])

>>> # coo @ dense -> dense
>>> indices = [[0, 1, 2], [1, 2, 0]]
>>> values = [1., 2., 3.]
>>> coo = paddle.sparse.sparse_coo_tensor(indices, values, [3, 3])
>>> print(coo)
Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
       indices=[[0, 1, 2],
                [1, 2, 0]],
       values=[1., 2., 3.])

>>> dense = paddle.ones([3, 2])
>>> out = paddle.sparse.matmul(coo, dense)
>>> print(out)
Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[1., 1.],
        [2., 2.],
        [3., 3.]])