mul.py 2.47 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
from typing import Optional

rusty1s's avatar
rusty1s committed
3
4
import torch
from torch_scatter import gather_csr
rusty1s's avatar
rusty1s committed
5
6
7
8
9
10
from torch_sparse.tensor import SparseTensor


def mul(src: SparseTensor, other: torch.Tensor) -> SparseTensor:
    rowptr, col, value = src.csr()
    if other.size(0) == src.size(0) and other.size(1) == 1:  # Row-wise...
rusty1s's avatar
rusty1s committed
11
        other = gather_csr(other.squeeze(1), rowptr)
rusty1s's avatar
rusty1s committed
12
13
14
15
        pass
    elif other.size(0) == 1 and other.size(1) == src.size(1):  # Col-wise...
        other = other.squeeze(0)[col]
    else:
rusty1s's avatar
rusty1s committed
16
        raise ValueError(
rusty1s's avatar
rusty1s committed
17
18
            f'Size mismatch: Expected size ({src.size(0)}, 1, ...) or '
            f'(1, {src.size(1)}, ...), but got size {other.size()}.')
rusty1s's avatar
rusty1s committed
19

rusty1s's avatar
rusty1s committed
20
    if value is not None:
rusty1s's avatar
fixes  
rusty1s committed
21
        value = other.to(value.dtype).mul_(value)
rusty1s's avatar
rusty1s committed
22
23
24
25
26
27
28
29
    else:
        value = other
    return src.set_value(value, layout='coo')


def mul_(src: SparseTensor, other: torch.Tensor) -> SparseTensor:
    rowptr, col, value = src.csr()
    if other.size(0) == src.size(0) and other.size(1) == 1:  # Row-wise...
rusty1s's avatar
rusty1s committed
30
        other = gather_csr(other.squeeze(1), rowptr)
rusty1s's avatar
rusty1s committed
31
32
33
34
        pass
    elif other.size(0) == 1 and other.size(1) == src.size(1):  # Col-wise...
        other = other.squeeze(0)[col]
    else:
rusty1s's avatar
rusty1s committed
35
        raise ValueError(
rusty1s's avatar
rusty1s committed
36
37
            f'Size mismatch: Expected size ({src.size(0)}, 1, ...) or '
            f'(1, {src.size(1)}, ...), but got size {other.size()}.')
rusty1s's avatar
rusty1s committed
38

rusty1s's avatar
rusty1s committed
39
    if value is not None:
rusty1s's avatar
fixes  
rusty1s committed
40
        value = value.mul_(other.to(value.dtype))
rusty1s's avatar
rusty1s committed
41
42
43
44
45
46
47
48
49
    else:
        value = other
    return src.set_value_(value, layout='coo')


def mul_nnz(src: SparseTensor, other: torch.Tensor,
            layout: Optional[str] = None) -> SparseTensor:
    value = src.storage.value()
    if value is not None:
rusty1s's avatar
fixes  
rusty1s committed
50
        value = value.mul(other.to(value.dtype))
rusty1s's avatar
rusty1s committed
51
52
53
54
55
56
57
58
59
    else:
        value = other
    return src.set_value(value, layout=layout)


def mul_nnz_(src: SparseTensor, other: torch.Tensor,
             layout: Optional[str] = None) -> SparseTensor:
    value = src.storage.value()
    if value is not None:
rusty1s's avatar
fixes  
rusty1s committed
60
        value = value.mul_(other.to(value.dtype))
rusty1s's avatar
rusty1s committed
61
62
63
64
65
66
67
68
69
70
71
    else:
        value = other
    return src.set_value_(value, layout=layout)


SparseTensor.mul = lambda self, other: mul(self, other)
SparseTensor.mul_ = lambda self, other: mul_(self, other)
SparseTensor.mul_nnz = lambda self, other, layout=None: mul_nnz(
    self, other, layout)
SparseTensor.mul_nnz_ = lambda self, other, layout=None: mul_nnz_(
    self, other, layout)
rusty1s's avatar
fixes  
rusty1s committed
72
73
74
SparseTensor.__mul__ = SparseTensor.mul
SparseTensor.__rmul__ = SparseTensor.mul
SparseTensor.__imul__ = SparseTensor.mul_