spmm.py 642 Bytes
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
from torch_scatter import scatter_add


def spmm(index, value, m, matrix):
rusty1s's avatar
docs  
rusty1s committed
5
6
7
8
9
10
11
12
13
14
    """Matrix product of sparse matrix with dense matrix.

    Args:
        index (:class:`LongTensor`): The index tensor of sparse matrix.
        value (:class:`Tensor`): The value tensor of sparse matrix.
        m (int): The first dimension of sparse matrix.
        matrix (:class:`Tensor`): The dense matrix.

    :rtype: :class:`Tensor`
    """
rusty1s's avatar
rusty1s committed
15
16
17
18
19
20
21
22
23

    row, col = index
    matrix = matrix if matrix.dim() > 1 else matrix.unsqueeze(-1)

    out = matrix[col]
    out = out * value.unsqueeze(-1)
    out = scatter_add(out, row, dim=0, dim_size=m)

    return out