storage.py 12.9 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import warnings
rusty1s's avatar
rusty1s committed
2

rusty1s's avatar
rusty1s committed
3
import torch
rusty1s's avatar
rusty1s committed
4
from torch_scatter import segment_csr, scatter_add
rusty1s's avatar
rusty1s committed
5

rusty1s's avatar
rusty1s committed
6
from torch_sparse import convert_cpu
7

rusty1s's avatar
rusty1s committed
8
try:
rusty1s's avatar
rusty1s committed
9
    from torch_sparse import convert_cuda
rusty1s's avatar
rusty1s committed
10
except ImportError:
rusty1s's avatar
rusty1s committed
11
    convert_cuda = None
12

rusty1s's avatar
typo  
rusty1s committed
13
__cache__ = {'enabled': True}
rusty1s's avatar
rusty1s committed
14

rusty1s's avatar
rusty1s committed
15
16

def is_cache_enabled():
rusty1s's avatar
typo  
rusty1s committed
17
    return __cache__['enabled']
rusty1s's avatar
rusty1s committed
18
19
20


def set_cache_enabled(mode):
rusty1s's avatar
typo  
rusty1s committed
21
    __cache__['enabled'] = mode
rusty1s's avatar
rusty1s committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


class no_cache(object):
    def __enter__(self):
        self.prev = is_cache_enabled()
        set_cache_enabled(False)

    def __exit__(self, *args):
        set_cache_enabled(self.prev)
        return False

    def __call__(self, func):
        def decorate_no_cache(*args, **kwargs):
            with self:
                return func(*args, **kwargs)

        return decorate_no_cache
rusty1s's avatar
rusty1s committed
39
40


rusty1s's avatar
rusty1s committed
41
42
43
class cached_property(object):
    def __init__(self, func):
        self.func = func
rusty1s's avatar
sorting  
rusty1s committed
44

rusty1s's avatar
rusty1s committed
45
46
47
48
    def __get__(self, obj, cls):
        value = getattr(obj, f'_{self.func.__name__}', None)
        if value is None:
            value = self.func(obj)
rusty1s's avatar
typo  
rusty1s committed
49
            if is_cache_enabled():
rusty1s's avatar
rusty1s committed
50
                setattr(obj, f'_{self.func.__name__}', value)
rusty1s's avatar
rusty1s committed
51
52
53
        return value


rusty1s's avatar
rusty1s committed
54
55
56
57
def optional(func, src):
    return func(src) if src is not None else src


rusty1s's avatar
rusty1s committed
58
59
60
61
62
63
64
65
66
67
68
69
layouts = ['coo', 'csr', 'csc']


def get_layout(layout=None):
    if layout is None:
        layout = 'coo'
        warnings.warn('`layout` argument unset, using default layout '
                      '"coo". This may lead to unexpected behaviour.')
    assert layout in layouts
    return layout


rusty1s's avatar
rusty1s committed
70
class SparseStorage(object):
rusty1s's avatar
rusty1s committed
71
    cache_keys = ['rowcount', 'colptr', 'colcount', 'csr2csc', 'csc2csr']
rusty1s's avatar
rusty1s committed
72

rusty1s's avatar
rusty1s committed
73
74
75
    def __init__(self, row=None, rowptr=None, col=None, value=None,
                 sparse_size=None, rowcount=None, colptr=None, colcount=None,
                 csr2csc=None, csc2csr=None, is_sorted=False):
rusty1s's avatar
rusty1s committed
76

rusty1s's avatar
rusty1s committed
77
78
79
80
        assert row is not None or rowptr is not None
        assert col is not None
        assert col.dtype == torch.long
        assert col.dim() == 1
rusty1s's avatar
rusty1s committed
81

rusty1s's avatar
rusty1s committed
82
        if sparse_size is None:
rusty1s's avatar
rusty1s committed
83
84
85
            M = rowptr.numel() - 1 if rowptr is None else row.max().item() + 1
            N = col.max().item() + 1
            sparse_size = torch.Size([M, N])
rusty1s's avatar
rusty1s committed
86

rusty1s's avatar
rusty1s committed
87
88
89
90
91
        if row is not None:
            assert row.dtype == torch.long
            assert row.device == col.device
            assert row.dim() == 1
            assert row.numel() == col.numel()
rusty1s's avatar
rusty1s committed
92

rusty1s's avatar
rusty1s committed
93
        if rowptr is not None:
rusty1s's avatar
rusty1s committed
94
            assert rowptr.dtype == torch.long
rusty1s's avatar
rusty1s committed
95
96
97
            assert rowptr.device == col.device
            assert rowptr.dim() == 1
            assert rowptr.numel() - 1 == sparse_size[0]
rusty1s's avatar
rusty1s committed
98

rusty1s's avatar
rusty1s committed
99
100
101
102
103
104
105
106
107
        if value is not None:
            assert value.device == col.device
            assert value.size(0) == col.size(0)

        if rowcount is not None:
            assert rowcount.dtype == torch.long
            assert rowcount.device == col.device
            assert rowcount.dim() == 1
            assert rowcount.numel() == sparse_size[0]
rusty1s's avatar
rusty1s committed
108

rusty1s's avatar
rusty1s committed
109
        if colptr is not None:
rusty1s's avatar
rusty1s committed
110
            assert colptr.dtype == torch.long
rusty1s's avatar
rusty1s committed
111
112
113
114
115
116
117
118
119
            assert colptr.device == col.device
            assert colptr.dim() == 1
            assert colptr.numel() - 1 == sparse_size[1]

        if colcount is not None:
            assert colcount.dtype == torch.long
            assert colcount.device == col.device
            assert colcount.dim() == 1
            assert colcount.numel() == sparse_size[1]
rusty1s's avatar
rusty1s committed
120

rusty1s's avatar
rusty1s committed
121
122
        if csr2csc is not None:
            assert csr2csc.dtype == torch.long
rusty1s's avatar
rusty1s committed
123
            assert csr2csc.device == col.device
rusty1s's avatar
rusty1s committed
124
            assert csr2csc.dim() == 1
rusty1s's avatar
rusty1s committed
125
            assert csr2csc.numel() == col.size(0)
rusty1s's avatar
rusty1s committed
126

rusty1s's avatar
rusty1s committed
127
128
        if csc2csr is not None:
            assert csc2csr.dtype == torch.long
rusty1s's avatar
rusty1s committed
129
            assert csc2csr.device == col.device
rusty1s's avatar
rusty1s committed
130
            assert csc2csr.dim() == 1
rusty1s's avatar
rusty1s committed
131
            assert csc2csr.numel() == col.size(0)
rusty1s's avatar
rusty1s committed
132

rusty1s's avatar
rusty1s committed
133
134
135
        self._row = row
        self._rowptr = rowptr
        self._col = col
rusty1s's avatar
rusty1s committed
136
137
        self._value = value
        self._sparse_size = sparse_size
rusty1s's avatar
rusty1s committed
138
        self._rowcount = rowcount
rusty1s's avatar
rusty1s committed
139
        self._colptr = colptr
rusty1s's avatar
rusty1s committed
140
        self._colcount = colcount
rusty1s's avatar
rusty1s committed
141
142
        self._csr2csc = csr2csc
        self._csc2csr = csc2csr
rusty1s's avatar
rusty1s committed
143

rusty1s's avatar
rusty1s committed
144
145
146
147
148
149
150
151
152
153
154
155
156
        if not is_sorted:
            idx = self.col.new_zeros(col.numel() + 1)
            idx[1:] = sparse_size[1] * self.row + self.col
            if (idx[1:] < idx[:-1]).any():
                perm = idx.argsort()
                self._row = self.row[perm]
                self._col = self.col[perm]
                self._value = self.value[perm] if self.has_value() else None
                self._csr2csc = None
                self._csc2csr = None

    def has_row(self):
        return self._row is not None
rusty1s's avatar
rusty1s committed
157
158

    @property
rusty1s's avatar
rusty1s committed
159
    def row(self):
rusty1s's avatar
rusty1s committed
160
        if self._row is None:
rusty1s's avatar
rusty1s committed
161
            func = convert_cuda if self.rowptr.is_cuda else convert_cpu
rusty1s's avatar
rusty1s committed
162
            self._row = func.ptr2ind(self.rowptr, self.col.numel())
rusty1s's avatar
rusty1s committed
163
164
165
166
167
168
169
170
        return self._row

    def has_rowptr(self):
        return self._rowptr is not None

    @property
    def rowptr(self):
        if self._rowptr is None:
rusty1s's avatar
rusty1s committed
171
172
            func = convert_cuda if self.row.is_cuda else convert_cpu
            self._rowptr = func.ind2ptr(self.row, self.sparse_size[0])
rusty1s's avatar
rusty1s committed
173
        return self._rowptr
rusty1s's avatar
rusty1s committed
174
175

    @property
rusty1s's avatar
rusty1s committed
176
    def col(self):
rusty1s's avatar
rusty1s committed
177
        return self._col
rusty1s's avatar
rusty1s committed
178

rusty1s's avatar
rusty1s committed
179
180
    def has_value(self):
        return self._value is not None
rusty1s's avatar
rusty1s committed
181
182

    @property
rusty1s's avatar
rusty1s committed
183
184
185
    def value(self):
        return self._value

rusty1s's avatar
rusty1s committed
186
    def set_value_(self, value, layout=None, dtype=None):
rusty1s's avatar
rusty1s committed
187
        if isinstance(value, int) or isinstance(value, float):
rusty1s's avatar
rusty1s committed
188
            value = torch.full((self.col.numel(), ), dtype=dtype,
rusty1s's avatar
rusty1s committed
189
190
                               device=self.col.device)

rusty1s's avatar
rusty1s committed
191
        elif torch.is_tensor(value) and get_layout(layout) == 'csc':
rusty1s's avatar
rusty1s committed
192
            value = value[self.csc2csr]
rusty1s's avatar
rusty1s committed
193

rusty1s's avatar
rusty1s committed
194
        if torch.is_tensor(value):
rusty1s's avatar
rusty1s committed
195
196
197
198
            value = value if dtype is None else value.to(dtype)
            assert value.device == self.col.device
            assert value.size(0) == self.col.numel()

rusty1s's avatar
rusty1s committed
199
200
        self._value = value
        return self
rusty1s's avatar
rusty1s committed
201

rusty1s's avatar
rusty1s committed
202
    def set_value(self, value, layout=None, dtype=None):
rusty1s's avatar
rusty1s committed
203
        if isinstance(value, int) or isinstance(value, float):
rusty1s's avatar
rusty1s committed
204
            value = torch.full((self.col.numel(), ), dtype=dtype,
rusty1s's avatar
rusty1s committed
205
206
                               device=self.col.device)

rusty1s's avatar
rusty1s committed
207
208
        elif torch.is_tensor(value) and get_layout(layout) == 'csc':
            value = value[self.csc2csr]
rusty1s's avatar
rusty1s committed
209

rusty1s's avatar
rusty1s committed
210
        if torch.is_tensor(value):
rusty1s's avatar
rusty1s committed
211
212
213
            value = value if dtype is None else value.to(dtype)
            assert value.device == self.col.device
            assert value.size(0) == self.col.numel()
rusty1s's avatar
rusty1s committed
214

rusty1s's avatar
rusty1s committed
215
216
217
218
219
220
221
222
223
        return self.__class__(row=self._row, rowptr=self._rowptr, col=self.col,
                              value=value, sparse_size=self._sparse_size,
                              rowcount=self._rowcount, colptr=self._colptr,
                              colcount=self._colcount, csr2csc=self._csr2csc,
                              csc2csr=self._csc2csr, is_sorted=True)

    @property
    def sparse_size(self):
        return self._sparse_size
rusty1s's avatar
rusty1s committed
224

rusty1s's avatar
rusty1s committed
225
    def sparse_resize(self, *sizes):
rusty1s's avatar
rusty1s committed
226
        old_sparse_size, nnz = self.sparse_size, self.col.numel()
rusty1s's avatar
rusty1s committed
227

rusty1s's avatar
rusty1s committed
228
        diff_0 = sizes[0] - old_sparse_size[0]
rusty1s's avatar
rusty1s committed
229
230
        rowcount, rowptr = self._rowcount, self._rowptr
        if diff_0 > 0:
rusty1s's avatar
rusty1s committed
231
            if rowptr is not None:
rusty1s's avatar
rusty1s committed
232
                rowptr = torch.cat([rowptr, rowptr.new_full((diff_0, ), nnz)])
rusty1s's avatar
rusty1s committed
233
234
            if rowcount is not None:
                rowcount = torch.cat([rowcount, rowcount.new_zeros(diff_0)])
rusty1s's avatar
rusty1s committed
235
        else:
rusty1s's avatar
rusty1s committed
236
            if rowptr is not None:
rusty1s's avatar
rusty1s committed
237
                rowptr = rowptr[:-diff_0]
rusty1s's avatar
rusty1s committed
238
239
            if rowcount is not None:
                rowcount = rowcount[:-diff_0]
rusty1s's avatar
rusty1s committed
240

rusty1s's avatar
rusty1s committed
241
        diff_1 = sizes[1] - old_sparse_size[1]
rusty1s's avatar
rusty1s committed
242
243
        colcount, colptr = self._colcount, self._colptr
        if diff_1 > 0:
rusty1s's avatar
rusty1s committed
244
            if colptr is not None:
rusty1s's avatar
rusty1s committed
245
                colptr = torch.cat([colptr, colptr.new_full((diff_1, ), nnz)])
rusty1s's avatar
rusty1s committed
246
247
            if colcount is not None:
                colcount = torch.cat([colcount, colcount.new_zeros(diff_1)])
rusty1s's avatar
rusty1s committed
248
        else:
rusty1s's avatar
rusty1s committed
249
            if colptr is not None:
rusty1s's avatar
rusty1s committed
250
                colptr = colptr[:-diff_1]
rusty1s's avatar
rusty1s committed
251
252
            if colcount is not None:
                colcount = colcount[:-diff_1]
rusty1s's avatar
rusty1s committed
253

rusty1s's avatar
rusty1s committed
254
255
256
257
258
        return self.__class__(row=self._row, rowptr=rowptr, col=self.col,
                              value=self.value, sparse_size=sizes,
                              rowcount=rowcount, colptr=colptr,
                              colcount=colcount, csr2csc=self._csr2csc,
                              csc2csr=self._csc2csr, is_sorted=True)
rusty1s's avatar
rusty1s committed
259

rusty1s's avatar
rusty1s committed
260
261
262
    def has_rowcount(self):
        return self._rowcount is not None

rusty1s's avatar
rusty1s committed
263
264
    @cached_property
    def rowcount(self):
rusty1s's avatar
rusty1s committed
265
        return self.rowptr[1:] - self.rowptr[:-1]
rusty1s's avatar
rusty1s committed
266

rusty1s's avatar
rusty1s committed
267
268
    def has_colptr(self):
        return self._colptr is not None
rusty1s's avatar
rusty1s committed
269

rusty1s's avatar
rusty1s committed
270
    @cached_property
rusty1s's avatar
rusty1s committed
271
272
    def colptr(self):
        if self.has_csr2csc():
rusty1s's avatar
rusty1s committed
273
274
            func = convert_cuda if self.col.is_cuda else convert_cpu
            return func.ind2ptr(self.col[self.csr2csc], self.sparse_size[1])
rusty1s's avatar
rusty1s committed
275
276
277
278
        else:
            colptr = self.col.new_zeros(self.sparse_size[1] + 1)
            torch.cumsum(self.colcount, dim=0, out=colptr[1:])
            return colptr
rusty1s's avatar
rusty1s committed
279

rusty1s's avatar
rusty1s committed
280
281
282
    def has_colcount(self):
        return self._colcount is not None

rusty1s's avatar
rusty1s committed
283
284
    @cached_property
    def colcount(self):
rusty1s's avatar
typos  
rusty1s committed
285
        if self.has_colptr():
rusty1s's avatar
rusty1s committed
286
            return self.colptr[1:] - self.colptr[:-1]
rusty1s's avatar
rusty1s committed
287
        else:
rusty1s's avatar
rusty1s committed
288
289
            return scatter_add(torch.ones_like(self.col), self.col,
                               dim_size=self.sparse_size[1])
rusty1s's avatar
rusty1s committed
290
291
292

    def has_csr2csc(self):
        return self._csr2csc is not None
rusty1s's avatar
rusty1s committed
293
294

    @cached_property
rusty1s's avatar
rusty1s committed
295
    def csr2csc(self):
rusty1s's avatar
rusty1s committed
296
        idx = self.sparse_size[0] * self.col + self.row
rusty1s's avatar
rusty1s committed
297
298
        return idx.argsort()

rusty1s's avatar
rusty1s committed
299
300
301
    def has_csc2csr(self):
        return self._csc2csr is not None

rusty1s's avatar
rusty1s committed
302
    @cached_property
rusty1s's avatar
rusty1s committed
303
304
    def csc2csr(self):
        return self.csr2csc.argsort()
rusty1s's avatar
rusty1s committed
305

rusty1s's avatar
rusty1s committed
306
    def is_coalesced(self):
rusty1s's avatar
rusty1s committed
307
308
309
        idx = self.col.new_zeros(self.col.numel() + 1)
        idx[1:] = self.sparse_size[1] * self.row + self.col
        return (idx[1:] > idx[:-1]).all().item()
rusty1s's avatar
rusty1s committed
310

rusty1s's avatar
rusty1s committed
311
    def coalesce(self, reduce='add'):
rusty1s's avatar
rusty1s committed
312
313
314
        idx = self.col.new_zeros(self.col.numel() + 1)
        idx[1:] = self.sparse_size[1] * self.row + self.col
        mask = idx[1:] > idx[:-1]
rusty1s's avatar
rusty1s committed
315

rusty1s's avatar
rusty1s committed
316
        if mask.all():  # Skip if indices are already coalesced.
rusty1s's avatar
rusty1s committed
317
318
            return self

rusty1s's avatar
rusty1s committed
319
320
        row = self.row[mask]
        col = self.col[mask]
rusty1s's avatar
rusty1s committed
321
322
323

        value = self.value
        if self.has_value():
rusty1s's avatar
rusty1s committed
324
            idx = mask.cumsum(0).sub_(1)
rusty1s's avatar
rusty1s committed
325
            value = segment_csr(idx, value, reduce=reduce)
rusty1s's avatar
rusty1s committed
326
327
            value = value[0] if isinstance(value, tuple) else value

rusty1s's avatar
rusty1s committed
328
329
        return self.__class__(row=row, col=col, value=value,
                              sparse_size=self.sparse_size, is_sorted=True)
rusty1s's avatar
rusty1s committed
330

rusty1s's avatar
rusty1s committed
331
332
333
334
335
336
    def cached_keys(self):
        return [
            key for key in self.cache_keys
            if getattr(self, f'_{key}', None) is not None
        ]

rusty1s's avatar
rusty1s committed
337
    def fill_cache_(self, *args):
rusty1s's avatar
rusty1s committed
338
        for arg in args or self.cache_keys + ['row', 'rowptr']:
rusty1s's avatar
rusty1s committed
339
            getattr(self, arg)
rusty1s's avatar
rusty1s committed
340
        return self
rusty1s's avatar
rusty1s committed
341

rusty1s's avatar
rusty1s committed
342
343
344
345
    def clear_cache_(self, *args):
        for arg in args or self.cache_keys:
            setattr(self, f'_{arg}', None)
        return self
rusty1s's avatar
rusty1s committed
346

rusty1s's avatar
rusty1s committed
347
348
349
    def __copy__(self):
        return self.apply(lambda x: x)

rusty1s's avatar
test  
rusty1s committed
350
351
352
353
354
    def clone(self):
        return self.apply(lambda x: x.clone())

    def __deepcopy__(self, memo):
        new_storage = self.clone()
rusty1s's avatar
rusty1s committed
355
        memo[id(self)] = new_storage
rusty1s's avatar
test  
rusty1s committed
356
357
        return new_storage

rusty1s's avatar
rusty1s committed
358
    def apply_value_(self, func):
rusty1s's avatar
rusty1s committed
359
        self._value = optional(func, self.value)
rusty1s's avatar
rusty1s committed
360
        return self
rusty1s's avatar
rusty1s committed
361

rusty1s's avatar
rusty1s committed
362
    def apply_value(self, func):
rusty1s's avatar
rusty1s committed
363
364
365
366
367
368
        return self.__class__(row=self._row, rowptr=self._rowptr, col=self.col,
                              value=optional(func, self.value),
                              sparse_size=self.sparse_size,
                              rowcount=self._rowcount, colptr=self._colptr,
                              colcount=self._colcount, csr2csc=self._csr2csc,
                              csc2csr=self._csc2csr, is_sorted=True)
rusty1s's avatar
rusty1s committed
369
370

    def apply_(self, func):
rusty1s's avatar
rusty1s committed
371
372
373
374
        self._row = optional(func, self._row)
        self._rowptr = optional(func, self._rowptr)
        self._col = func(self.col)
        self._value = optional(func, self.value)
rusty1s's avatar
rusty1s committed
375
        for key in self.cached_keys():
rusty1s's avatar
rusty1s committed
376
            setattr(self, f'_{key}', func(getattr(self, f'_{key}')))
rusty1s's avatar
rusty1s committed
377
        return self
rusty1s's avatar
rusty1s committed
378
379
380

    def apply(self, func):
        return self.__class__(
rusty1s's avatar
rusty1s committed
381
382
383
384
385
386
387
388
389
390
            row=optional(func, self._row),
            rowptr=optional(func, self._rowptr),
            col=func(self.col),
            value=optional(func, self.value),
            sparse_size=self.sparse_size,
            rowcount=optional(func, self._rowcount),
            colptr=optional(func, self._colptr),
            colcount=optional(func, self._colcount),
            csr2csc=optional(func, self._csr2csc),
            csc2csr=optional(func, self._csc2csr),
rusty1s's avatar
rusty1s committed
391
392
393
            is_sorted=True,
        )

rusty1s's avatar
rusty1s committed
394
    def map(self, func):
rusty1s's avatar
rusty1s committed
395
396
397
398
399
400
        data = []
        if self.has_row():
            data += [func(self.row)]
        if self.has_rowptr():
            data += [func(self.rowptr)]
        data += [func(self.col)]
rusty1s's avatar
rusty1s committed
401
402
        if self.has_value():
            data += [func(self.value)]
rusty1s's avatar
rusty1s committed
403
        data += [func(getattr(self, f'_{key}')) for key in self.cached_keys()]
rusty1s's avatar
rusty1s committed
404
        return data