"vscode:/vscode.git/clone" did not exist on "2f0d3864962343ce894b7abc705b876028f2c668"
reduce.py 3.85 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
import torch
import torch_scatter
from torch_scatter import segment_csr
4
5
6
7
8
9


def __reduce__(src, dim=None, reduce='add', deterministic=False):
    if dim is None and src.has_value():
        func = getattr(torch, 'sum' if reduce == 'add' else reduce)
        return func(src.storage.value)
rusty1s's avatar
rusty1s committed
10
11
12
13
14
15
16

    if dim is None and not src.has_value():
        assert reduce in ['add', 'mean', 'min', 'max']
        value = src.nnz() if reduce == 'add' else 1
        return torch.tensor(value, device=src.device)

    dims = [dim] if isinstance(dim, int) else sorted(list(dim))
rusty1s's avatar
rusty1s committed
17
    assert dims[-1] < src.dim()
rusty1s's avatar
rusty1s committed
18
19
20
21
22
23
24

    rowptr, col, value = src.csr()

    sparse_dims = tuple(set([d for d in dims if d < 2]))
    dense_dims = tuple(set([d - 1 for d in dims if d > 1]))

    if len(sparse_dims) == 2 and src.has_value():
25
26
        func = getattr(torch, 'sum' if reduce == 'add' else reduce)
        return func(value, dim=(0, ) + dense_dims)
rusty1s's avatar
rusty1s committed
27
28
29
30
31
32

    if len(sparse_dims) == 2 and not src.has_value():
        assert reduce in ['add', 'mean', 'min', 'max']
        value = src.nnz() if reduce == 'add' else 1
        return torch.tensor(value, device=src.device)

rusty1s's avatar
rusty1s committed
33
    if len(dense_dims) > 0 and len(sparse_dims) == 0:  # src.has_value()
34
        func = getattr(torch, 'sum' if reduce == 'add' else reduce)
rusty1s's avatar
rusty1s committed
35
        dense_dims = dense_dims[0] if len(dense_dims) == 1 else dense_dims
36
        value = func(value, dim=dense_dims)
rusty1s's avatar
bugfix  
rusty1s committed
37
        if isinstance(value, tuple):
38
            return (src.set_value(value[0], layout='csr'), ) + value[1:]
rusty1s's avatar
rusty1s committed
39
40
41
        return src.set_value(value, layout='csr')

    if len(dense_dims) > 0 and len(sparse_dims) > 0:
42
        func = getattr(torch, 'sum' if reduce == 'add' else reduce)
rusty1s's avatar
rusty1s committed
43
        dense_dims = dense_dims[0] if len(dense_dims) == 1 else dense_dims
44
        value = func(value, dim=dense_dims)
rusty1s's avatar
rusty1s committed
45
46
        value = value[0] if isinstance(value, tuple) else value

rusty1s's avatar
rusty1s committed
47
    if sparse_dims[0] == 1 and src.has_value():
rusty1s's avatar
rusty1s committed
48
49
50
51
        out = segment_csr(value, rowptr)
        out = out[0] if len(dense_dims) > 0 and isinstance(out, tuple) else out
        return out

rusty1s's avatar
rusty1s committed
52
53
54
55
56
57
58
59
60
61
62
63
    if sparse_dims[0] == 1 and not src.has_value():
        assert reduce in ['add', 'mean', 'min', 'max']
        if reduce == 'add':
            return src.storage.rowcount.to(torch.get_default_dtype())
        elif reduce == 'min' or 'max':
            return torch.ones(src.size(0), device=src.device), None
        else:
            return torch.ones(src.size(0), device=src.device)

    deterministic = src.storage._csr2csc is not None or deterministic

    if sparse_dims[0] == 0 and deterministic and src.has_value():
rusty1s's avatar
rusty1s committed
64
65
66
67
68
        csr2csc, colptr = src.storage.csr2csc, src.storage.colptr
        out = segment_csr(value[csr2csc], colptr)
        out = out[0] if len(dense_dims) > 0 and isinstance(out, tuple) else out
        return out

rusty1s's avatar
rusty1s committed
69
    if sparse_dims[0] == 0 and src.has_value():
70
        func = getattr(torch_scatter, f'scatter_{reduce}')
rusty1s's avatar
rusty1s committed
71
        out = func(value, col, dim=0, dim_size=src.sparse_size(1))
rusty1s's avatar
rusty1s committed
72
73
        out = out[0] if len(dense_dims) > 0 and isinstance(out, tuple) else out
        return out
74

rusty1s's avatar
rusty1s committed
75
76
77
78
79
80
81
82
83
    if sparse_dims[0] == 0 and not src.has_value():
        assert reduce in ['add', 'mean', 'min', 'max']
        if reduce == 'add':
            return src.storage.colcount.to(torch.get_default_dtype())
        elif reduce == 'min' or 'max':
            return torch.ones(src.size(1), device=src.device), None
        else:
            return torch.ones(src.size(1), device=src.device)

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

def sum(src, dim=None, deterministic=False):
    return __reduce__(src, dim, reduce='add', deterministic=deterministic)


def mean(src, dim=None, deterministic=False):
    return __reduce__(src, dim, reduce='mean', deterministic=deterministic)


def min(src, dim=None, deterministic=False):
    return __reduce__(src, dim, reduce='min', deterministic=deterministic)


def max(src, dim=None, deterministic=False):
    return __reduce__(src, dim, reduce='max', deterministic=deterministic)