|
1 | 1 | from typing import Optional |
2 | 2 |
|
3 | 3 | import torch |
| 4 | +from torch import Tensor |
4 | 5 | from torch_scatter import gather_csr |
5 | 6 | from torch_sparse.tensor import SparseTensor |
6 | 7 |
|
7 | 8 |
|
8 | | -def add(src: SparseTensor, other: torch.Tensor) -> SparseTensor: |
9 | | - rowptr, col, value = src.csr() |
10 | | - if other.size(0) == src.size(0) and other.size(1) == 1: # Row-wise... |
11 | | - other = gather_csr(other.squeeze(1), rowptr) |
12 | | - pass |
13 | | - elif other.size(0) == 1 and other.size(1) == src.size(1): # Col-wise... |
14 | | - other = other.squeeze(0)[col] |
15 | | - else: |
16 | | - raise ValueError( |
17 | | - f'Size mismatch: Expected size ({src.size(0)}, 1, ...) or ' |
18 | | - f'(1, {src.size(1)}, ...), but got size {other.size()}.') |
19 | | - if value is not None: |
20 | | - value = other.to(value.dtype).add_(value) |
| 9 | +@torch.jit._overload # noqa: F811 |
| 10 | +def add(src, other): # noqa: F811 |
| 11 | + # type: (SparseTensor, Tensor) -> SparseTensor |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +@torch.jit._overload # noqa: F811 |
| 16 | +def add(src, other): # noqa: F811 |
| 17 | + # type: (SparseTensor, SparseTensor) -> SparseTensor |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +def add(src, other): # noqa: F811 |
| 22 | + if isinstance(other, Tensor): |
| 23 | + rowptr, col, value = src.csr() |
| 24 | + if other.size(0) == src.size(0) and other.size(1) == 1: # Row-wise. |
| 25 | + other = gather_csr(other.squeeze(1), rowptr) |
| 26 | + elif other.size(0) == 1 and other.size(1) == src.size(1): # Col-wise. |
| 27 | + other = other.squeeze(0)[col] |
| 28 | + else: |
| 29 | + raise ValueError( |
| 30 | + f'Size mismatch: Expected size ({src.size(0)}, 1, ...) or ' |
| 31 | + f'(1, {src.size(1)}, ...), but got size {other.size()}.') |
| 32 | + if value is not None: |
| 33 | + value = other.to(value.dtype).add_(value) |
| 34 | + else: |
| 35 | + value = other.add_(1) |
| 36 | + return src.set_value(value, layout='coo') |
| 37 | + |
| 38 | + elif isinstance(other, SparseTensor): |
| 39 | + rowA, colA, valueA = src.coo() |
| 40 | + rowB, colB, valueB = other.coo() |
| 41 | + |
| 42 | + row = torch.cat([rowA, rowB], dim=0) |
| 43 | + col = torch.cat([colA, colB], dim=0) |
| 44 | + |
| 45 | + value: Optional[Tensor] = None |
| 46 | + if valueA is not None and valueB is not None: |
| 47 | + value = torch.cat([valueA, valueB], dim=0) |
| 48 | + |
| 49 | + M = max(src.size(0), other.size(0)) |
| 50 | + N = max(src.size(1), other.size(1)) |
| 51 | + sparse_sizes = (M, N) |
| 52 | + |
| 53 | + out = SparseTensor(row=row, col=col, value=value, |
| 54 | + sparse_sizes=sparse_sizes) |
| 55 | + out = out.coalesce(reduce='sum') |
| 56 | + return out |
| 57 | + |
21 | 58 | else: |
22 | | - value = other.add_(1) |
23 | | - return src.set_value(value, layout='coo') |
| 59 | + raise NotImplementedError |
24 | 60 |
|
25 | 61 |
|
26 | 62 | def add_(src: SparseTensor, other: torch.Tensor) -> SparseTensor: |
27 | 63 | rowptr, col, value = src.csr() |
28 | | - if other.size(0) == src.size(0) and other.size(1) == 1: # Row-wise... |
| 64 | + if other.size(0) == src.size(0) and other.size(1) == 1: # Row-wise. |
29 | 65 | other = gather_csr(other.squeeze(1), rowptr) |
30 | | - pass |
31 | | - elif other.size(0) == 1 and other.size(1) == src.size(1): # Col-wise... |
| 66 | + elif other.size(0) == 1 and other.size(1) == src.size(1): # Col-wise. |
32 | 67 | other = other.squeeze(0)[col] |
33 | 68 | else: |
34 | 69 | raise ValueError( |
|
0 commit comments