mul.py 3.2 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
import torch
from torch_scatter import gather_csr
rusty1s's avatar
rusty1s committed
3
from torch_sparse.utils import is_scalar
rusty1s's avatar
rusty1s committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93


def mul(src, other):
    if is_scalar(other):
        return mul_nnz(src, other)

    elif torch.is_tensor(other):
        rowptr, col, value = src.csr()
        if other.size(0) == src.size(0) and other.size(1) == 1:  # Row-wise...
            other = gather_csr(other.squeeze(1), rowptr)
            if src.has_value():
                value = other.mul_(src.storage.value)
            else:
                value = other
            return src.set_value(value, layout='csr')

        if other.size(0) == 1 and other.size(1) == src.size(1):  # Col-wise...
            other = other.squeeze(0)[col]
            if src.has_value():
                value = other.mul_(src.storage.value)
            else:
                value = other
            return src.set_value(value, layout='coo')

        raise ValueError(f'Size mismatch: Expected size ({src.size(0)}, 1,'
                         f' ...) or (1, {src.size(1)}, ...), but got size '
                         f'{other.size()}.')

    elif isinstance(other, src.__class__):
        raise NotImplementedError

    raise ValueError('Argument `other` needs to be of type `int`, `float`, '
                     '`torch.tensor` or `torch_sparse.SparseTensor`.')


def mul_(src, other):
    if is_scalar(other):
        return mul_nnz_(src, other)

    elif torch.is_tensor(other):
        rowptr, col, value = src.csr()
        if other.size(0) == src.size(0) and other.size(1) == 1:  # Row-wise...
            other = gather_csr(other.squeeze(1), rowptr)
            if src.has_value():
                value = src.storage.value.mul_(other)
            else:
                value = other
            return src.set_value_(value, layout='csr')

        if other.size(0) == 1 and other.size(1) == src.size(1):  # Col-wise...
            other = other.squeeze(0)[col]
            if src.has_value():
                value = src.storage.value.mul_(other)
            else:
                value = other
            return src.set_value_(value, layout='coo')

        raise ValueError(f'Size mismatch: Expected size ({src.size(0)}, 1,'
                         f' ...) or (1, {src.size(1)}, ...), but got size '
                         f'{other.size()}.')

    elif isinstance(other, src.__class__):
        raise NotImplementedError

    raise ValueError('Argument `other` needs to be of type `int`, `float`, '
                     '`torch.tensor` or `torch_sparse.SparseTensor`.')


def mul_nnz(src, other, layout=None):
    if torch.is_tensor(other) or is_scalar(other):
        if src.has_value():
            value = src.storage.value * other
        else:
            value = other
        return src.set_value(value, layout='coo')

    raise ValueError('Argument `other` needs to be of type `int`, `float` or '
                     '`torch.tensor`.')


def mul_nnz_(src, other, layout=None):
    if torch.is_tensor(other) or is_scalar(other):
        if src.has_value():
            value = src.storage.value.mul_(other)
        else:
            value = other
        return src.set_value_(value, layout='coo')

    raise ValueError('Argument `other` needs to be of type `int`, `float` or '
                     '`torch.tensor`.')