reduce.py 3.87 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
import torch
import torch_scatter
from torch_scatter import segment_csr
4
5


rusty1s's avatar
rusty1s committed
6
def reduction(src, dim=None, reduce='add', deterministic=False):
rusty1s's avatar
typos  
rusty1s committed
7
8
    assert reduce in ['add', 'mean', 'min', 'max']

9
10
11
    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
12
13
14
15
16

    if dim is None and not src.has_value():
        value = src.nnz() if reduce == 'add' else 1
        return torch.tensor(value, device=src.device)

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

    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():
27
28
        func = getattr(torch, 'sum' if reduce == 'add' else reduce)
        return func(value, dim=(0, ) + dense_dims)
rusty1s's avatar
rusty1s committed
29
30
31
32
33

    if len(sparse_dims) == 2 and not src.has_value():
        value = src.nnz() if reduce == 'add' else 1
        return torch.tensor(value, device=src.device)

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

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

rusty1s's avatar
rusty1s committed
48
    if sparse_dims[0] == 1 and src.has_value():
rusty1s's avatar
rusty1s committed
49
50
51
52
        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
53
54
55
56
    if sparse_dims[0] == 1 and not src.has_value():
        if reduce == 'add':
            return src.storage.rowcount.to(torch.get_default_dtype())
        elif reduce == 'min' or 'max':
rusty1s's avatar
typos  
rusty1s committed
57
            # Return an additional `None` arg(min|max) tensor for consistency.
rusty1s's avatar
rusty1s committed
58
59
60
61
            return torch.ones(src.size(0), device=src.device), None
        else:
            return torch.ones(src.size(0), device=src.device)

rusty1s's avatar
typos  
rusty1s committed
62
    deterministic = src.storage.has_csr2csc() or deterministic
rusty1s's avatar
rusty1s committed
63
64

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

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

rusty1s's avatar
rusty1s committed
76
77
78
79
    if sparse_dims[0] == 0 and not src.has_value():
        if reduce == 'add':
            return src.storage.colcount.to(torch.get_default_dtype())
        elif reduce == 'min' or 'max':
rusty1s's avatar
typos  
rusty1s committed
80
            # Return an additional `None` arg(min|max) tensor for consistency.
rusty1s's avatar
rusty1s committed
81
82
83
84
            return torch.ones(src.size(1), device=src.device), None
        else:
            return torch.ones(src.size(1), device=src.device)

85
86

def sum(src, dim=None, deterministic=False):
rusty1s's avatar
rusty1s committed
87
    return reduction(src, dim, reduce='add', deterministic=deterministic)
88
89
90


def mean(src, dim=None, deterministic=False):
rusty1s's avatar
rusty1s committed
91
    return reduction(src, dim, reduce='mean', deterministic=deterministic)
92
93
94


def min(src, dim=None, deterministic=False):
rusty1s's avatar
rusty1s committed
95
    return reduction(src, dim, reduce='min', deterministic=deterministic)
96
97
98


def max(src, dim=None, deterministic=False):
rusty1s's avatar
rusty1s committed
99
    return reduction(src, dim, reduce='max', deterministic=deterministic)