storage.py 20.5 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import warnings
rusty1s's avatar
rusty1s committed
2
import os.path as osp
rusty1s's avatar
rusty1s committed
3
from typing import Optional, List
rusty1s's avatar
rusty1s committed
4

rusty1s's avatar
rusty1s committed
5
import torch
rusty1s's avatar
rusty1s committed
6
from torch_scatter import segment_csr, scatter_add
rusty1s's avatar
rusty1s committed
7
from torch_sparse.utils import Final
rusty1s's avatar
rusty1s committed
8

rusty1s's avatar
rusty1s committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
try:
    torch.ops.load_library(
        osp.join(osp.dirname(osp.abspath(__file__)), '_convert.so'))
except OSError:
    warnings.warn('Failed to load `convert` binaries.')

    def ind2ptr_placeholder(ind: torch.Tensor, M: int) -> torch.Tensor:
        raise ImportError
        return ind

    def ptr2ind_placeholder(ptr: torch.Tensor, E: int) -> torch.Tensor:
        raise ImportError
        return ptr

    torch.ops.torch_sparse.ind2ptr = ind2ptr_placeholder
    torch.ops.torch_sparse.ptr2ind = ptr2ind_placeholder

rusty1s's avatar
rusty1s committed
26
layouts: Final[List[str]] = ['coo', 'csr', 'csc']
rusty1s's avatar
rusty1s committed
27
28


rusty1s's avatar
rusty1s committed
29
def get_layout(layout: Optional[str] = None) -> str:
rusty1s's avatar
rusty1s committed
30
31
32
33
    if layout is None:
        layout = 'coo'
        warnings.warn('`layout` argument unset, using default layout '
                      '"coo". This may lead to unexpected behaviour.')
rusty1s's avatar
rusty1s committed
34
    assert layout == 'coo' or layout == 'csr' or layout == 'csc'
rusty1s's avatar
rusty1s committed
35
36
37
    return layout


rusty1s's avatar
rusty1s committed
38
@torch.jit.script
rusty1s's avatar
rusty1s committed
39
class SparseStorage(object):
rusty1s's avatar
rusty1s committed
40
41
42
43
    _row: Optional[torch.Tensor]
    _rowptr: Optional[torch.Tensor]
    _col: torch.Tensor
    _value: Optional[torch.Tensor]
rusty1s's avatar
rusty1s committed
44
    _sparse_sizes: List[int]
rusty1s's avatar
rusty1s committed
45
46
47
48
49
50
51
52
53
54
    _rowcount: Optional[torch.Tensor]
    _colptr: Optional[torch.Tensor]
    _colcount: Optional[torch.Tensor]
    _csr2csc: Optional[torch.Tensor]
    _csc2csr: Optional[torch.Tensor]

    def __init__(self, row: Optional[torch.Tensor] = None,
                 rowptr: Optional[torch.Tensor] = None,
                 col: Optional[torch.Tensor] = None,
                 value: Optional[torch.Tensor] = None,
rusty1s's avatar
rusty1s committed
55
                 sparse_sizes: Optional[List[int]] = None,
rusty1s's avatar
rusty1s committed
56
57
58
59
60
61
                 rowcount: Optional[torch.Tensor] = None,
                 colptr: Optional[torch.Tensor] = None,
                 colcount: Optional[torch.Tensor] = None,
                 csr2csc: Optional[torch.Tensor] = None,
                 csc2csr: Optional[torch.Tensor] = None,
                 is_sorted: bool = False):
rusty1s's avatar
rusty1s committed
62

rusty1s's avatar
rusty1s committed
63
64
65
66
        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
67
        col = col.contiguous()
rusty1s's avatar
rusty1s committed
68

rusty1s's avatar
rusty1s committed
69
        if sparse_sizes is None:
rusty1s's avatar
rusty1s committed
70
71
72
73
74
75
            if rowptr is not None:
                M = rowptr.numel() - 1
            elif row is not None:
                M = row.max().item() + 1
            else:
                raise ValueError
rusty1s's avatar
rusty1s committed
76
            N = col.max().item() + 1
rusty1s's avatar
rusty1s committed
77
            sparse_sizes = torch.Size([int(M), int(N)])
rusty1s's avatar
rusty1s committed
78
        else:
rusty1s's avatar
rusty1s committed
79
            assert len(sparse_sizes) == 2
rusty1s's avatar
rusty1s committed
80

rusty1s's avatar
rusty1s committed
81
82
83
84
85
        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
86
            row = row.contiguous()
rusty1s's avatar
rusty1s committed
87

rusty1s's avatar
rusty1s committed
88
        if rowptr is not None:
rusty1s's avatar
rusty1s committed
89
            assert rowptr.dtype == torch.long
rusty1s's avatar
rusty1s committed
90
91
            assert rowptr.device == col.device
            assert rowptr.dim() == 1
rusty1s's avatar
rusty1s committed
92
            assert rowptr.numel() - 1 == sparse_sizes[0]
rusty1s's avatar
rusty1s committed
93
            rowptr = rowptr.contiguous()
rusty1s's avatar
rusty1s committed
94

rusty1s's avatar
rusty1s committed
95
96
97
        if value is not None:
            assert value.device == col.device
            assert value.size(0) == col.size(0)
rusty1s's avatar
rusty1s committed
98
            value = value.contiguous()
rusty1s's avatar
rusty1s committed
99
100
101
102
103

        if rowcount is not None:
            assert rowcount.dtype == torch.long
            assert rowcount.device == col.device
            assert rowcount.dim() == 1
rusty1s's avatar
rusty1s committed
104
            assert rowcount.numel() == sparse_sizes[0]
rusty1s's avatar
rusty1s committed
105
            rowcount = rowcount.contiguous()
rusty1s's avatar
rusty1s committed
106

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

        if colcount is not None:
            assert colcount.dtype == torch.long
            assert colcount.device == col.device
            assert colcount.dim() == 1
rusty1s's avatar
rusty1s committed
118
            assert colcount.numel() == sparse_sizes[1]
rusty1s's avatar
rusty1s committed
119
            colcount = colcount.contiguous()
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
            csr2csc = csr2csc.contiguous()
rusty1s's avatar
rusty1s committed
127

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

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

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

rusty1s's avatar
rusty1s committed
158
    def has_row(self) -> bool:
rusty1s's avatar
rusty1s committed
159
        return self._row is not None
rusty1s's avatar
rusty1s committed
160

rusty1s's avatar
rusty1s committed
161
    def row(self):
rusty1s's avatar
rusty1s committed
162
163
164
        row = self._row
        if row is not None:
            return row
rusty1s's avatar
rusty1s committed
165

rusty1s's avatar
rusty1s committed
166
167
        rowptr = self._rowptr
        if rowptr is not None:
rusty1s's avatar
rusty1s committed
168
            row = torch.ops.torch_sparse.ptr2ind(rowptr, self._col.numel())
rusty1s's avatar
rusty1s committed
169
170
171
172
173
174
            self._row = row
            return row

        raise ValueError

    def has_rowptr(self) -> bool:
rusty1s's avatar
rusty1s committed
175
176
        return self._rowptr is not None

rusty1s's avatar
rusty1s committed
177
178
179
180
    def rowptr(self) -> torch.Tensor:
        rowptr = self._rowptr
        if rowptr is not None:
            return rowptr
rusty1s's avatar
rusty1s committed
181

rusty1s's avatar
rusty1s committed
182
183
        row = self._row
        if row is not None:
rusty1s's avatar
rusty1s committed
184
            rowptr = torch.ops.torch_sparse.ind2ptr(row, self._sparse_sizes[0])
rusty1s's avatar
rusty1s committed
185
186
187
188
189
190
            self._rowptr = rowptr
            return rowptr

        raise ValueError

    def col(self) -> torch.Tensor:
rusty1s's avatar
rusty1s committed
191
        return self._col
rusty1s's avatar
rusty1s committed
192

rusty1s's avatar
rusty1s committed
193
    def has_value(self) -> bool:
rusty1s's avatar
rusty1s committed
194
        return self._value is not None
rusty1s's avatar
rusty1s committed
195

rusty1s's avatar
rusty1s committed
196
    def value(self) -> Optional[torch.Tensor]:
rusty1s's avatar
rusty1s committed
197
198
        return self._value

rusty1s's avatar
rusty1s committed
199
200
201
    def set_value_(self, value: Optional[torch.Tensor],
                   layout: Optional[str] = None):
        if value is not None:
rusty1s's avatar
rusty1s committed
202
            if get_layout(layout) == 'csc':
rusty1s's avatar
rusty1s committed
203
204
205
206
                value = value[self.csc2csr()]
            value = value.contiguous()
            assert value.device == self._col.device
            assert value.size(0) == self._col.numel()
rusty1s's avatar
rusty1s committed
207

rusty1s's avatar
rusty1s committed
208
209
        self._value = value
        return self
rusty1s's avatar
rusty1s committed
210

rusty1s's avatar
rusty1s committed
211
212
213
    def set_value(self, value: Optional[torch.Tensor],
                  layout: Optional[str] = None):
        if value is not None:
rusty1s's avatar
rusty1s committed
214
            if get_layout(layout) == 'csc':
rusty1s's avatar
rusty1s committed
215
216
217
218
                value = value[self.csc2csr()]
            value = value.contiguous()
            assert value.device == self._col.device
            assert value.size(0) == self._col.numel()
rusty1s's avatar
rusty1s committed
219

rusty1s's avatar
rusty1s committed
220
        return SparseStorage(row=self._row, rowptr=self._rowptr, col=self._col,
rusty1s's avatar
rusty1s committed
221
                             value=value, sparse_sizes=self._sparse_sizes,
rusty1s's avatar
rusty1s committed
222
223
224
                             rowcount=self._rowcount, colptr=self._colptr,
                             colcount=self._colcount, csr2csc=self._csr2csc,
                             csc2csr=self._csc2csr, is_sorted=True)
rusty1s's avatar
rusty1s committed
225

rusty1s's avatar
rusty1s committed
226
227
    def sparse_sizes(self) -> List[int]:
        return self._sparse_sizes
rusty1s's avatar
rusty1s committed
228

rusty1s's avatar
rusty1s committed
229
230
    def sparse_size(self, dim: int) -> int:
        return self._sparse_sizes[dim]
rusty1s's avatar
rusty1s committed
231

rusty1s's avatar
rusty1s committed
232
233
234
    def sparse_resize(self, sparse_sizes: List[int]):
        assert len(sparse_sizes) == 2
        old_sparse_sizes, nnz = self._sparse_sizes, self._col.numel()
rusty1s's avatar
rusty1s committed
235

rusty1s's avatar
rusty1s committed
236
        diff_0 = sparse_sizes[0] - old_sparse_sizes[0]
rusty1s's avatar
rusty1s committed
237
238
239
240
241
242
243
244
245
246
247
248
        rowcount, rowptr = self._rowcount, self._rowptr
        if diff_0 > 0:
            if rowptr is not None:
                rowptr = torch.cat([rowptr, rowptr.new_full((diff_0, ), nnz)])
            if rowcount is not None:
                rowcount = torch.cat([rowcount, rowcount.new_zeros(diff_0)])
        else:
            if rowptr is not None:
                rowptr = rowptr[:-diff_0]
            if rowcount is not None:
                rowcount = rowcount[:-diff_0]

rusty1s's avatar
rusty1s committed
249
        diff_1 = sparse_sizes[1] - old_sparse_sizes[1]
rusty1s's avatar
rusty1s committed
250
251
252
253
254
255
256
257
258
259
260
261
262
        colcount, colptr = self._colcount, self._colptr
        if diff_1 > 0:
            if colptr is not None:
                colptr = torch.cat([colptr, colptr.new_full((diff_1, ), nnz)])
            if colcount is not None:
                colcount = torch.cat([colcount, colcount.new_zeros(diff_1)])
        else:
            if colptr is not None:
                colptr = colptr[:-diff_1]
            if colcount is not None:
                colcount = colcount[:-diff_1]

        return SparseStorage(row=self._row, rowptr=rowptr, col=self._col,
rusty1s's avatar
rusty1s committed
263
                             value=self._value, sparse_sizes=sparse_sizes,
rusty1s's avatar
rusty1s committed
264
265
266
                             rowcount=rowcount, colptr=colptr,
                             colcount=colcount, csr2csc=self._csr2csc,
                             csc2csr=self._csc2csr, is_sorted=True)
rusty1s's avatar
rusty1s committed
267
268

    def has_rowcount(self) -> bool:
rusty1s's avatar
rusty1s committed
269
270
        return self._rowcount is not None

rusty1s's avatar
rusty1s committed
271
272
273
274
275
276
    def rowcount(self) -> torch.Tensor:
        rowcount = self._rowcount
        if rowcount is not None:
            return rowcount

        rowptr = self.rowptr()
277
        rowcount = rowptr[1:] - rowptr[:-1]
rusty1s's avatar
rusty1s committed
278
279
        self._rowcount = rowcount
        return rowcount
rusty1s's avatar
rusty1s committed
280

rusty1s's avatar
rusty1s committed
281
    def has_colptr(self) -> bool:
rusty1s's avatar
rusty1s committed
282
        return self._colptr is not None
rusty1s's avatar
rusty1s committed
283

rusty1s's avatar
rusty1s committed
284
285
286
    def colptr(self) -> torch.Tensor:
        colptr = self._colptr
        if colptr is not None:
rusty1s's avatar
rusty1s committed
287
            return colptr
rusty1s's avatar
rusty1s committed
288

rusty1s's avatar
rusty1s committed
289
290
        csr2csc = self._csr2csc
        if csr2csc is not None:
rusty1s's avatar
rusty1s committed
291
292
            colptr = torch.ops.torch_sparse.ind2ptr(self._col[csr2csc],
                                                    self._sparse_sizes[1])
rusty1s's avatar
rusty1s committed
293
        else:
rusty1s's avatar
rusty1s committed
294
            colptr = self._col.new_zeros(self._sparse_sizes[1] + 1)
rusty1s's avatar
rusty1s committed
295
296
297
298
299
            torch.cumsum(self.colcount(), dim=0, out=colptr[1:])
        self._colptr = colptr
        return colptr

    def has_colcount(self) -> bool:
rusty1s's avatar
rusty1s committed
300
301
        return self._colcount is not None

rusty1s's avatar
rusty1s committed
302
303
304
305
306
307
308
    def colcount(self) -> torch.Tensor:
        colcount = self._colcount
        if colcount is not None:
            return colcount

        colptr = self._colptr
        if colptr is not None:
309
            colcount = colptr[1:] - colptr[:-1]
rusty1s's avatar
rusty1s committed
310
        else:
rusty1s's avatar
rusty1s committed
311
312
            colcount = scatter_add(torch.ones_like(self._col), self._col,
                                   dim_size=self._sparse_sizes[1])
rusty1s's avatar
rusty1s committed
313
314
        self._colcount = colcount
        return colcount
rusty1s's avatar
rusty1s committed
315

rusty1s's avatar
rusty1s committed
316
    def has_csr2csc(self) -> bool:
rusty1s's avatar
rusty1s committed
317
        return self._csr2csc is not None
rusty1s's avatar
rusty1s committed
318

rusty1s's avatar
rusty1s committed
319
320
321
322
    def csr2csc(self) -> torch.Tensor:
        csr2csc = self._csr2csc
        if csr2csc is not None:
            return csr2csc
rusty1s's avatar
rusty1s committed
323

rusty1s's avatar
rusty1s committed
324
        idx = self._sparse_sizes[0] * self._col + self.row()
rusty1s's avatar
rusty1s committed
325
326
327
328
329
        csr2csc = idx.argsort()
        self._csr2csc = csr2csc
        return csr2csc

    def has_csc2csr(self) -> bool:
rusty1s's avatar
rusty1s committed
330
331
        return self._csc2csr is not None

rusty1s's avatar
rusty1s committed
332
333
334
335
    def csc2csr(self) -> torch.Tensor:
        csc2csr = self._csc2csr
        if csc2csr is not None:
            return csc2csr
rusty1s's avatar
rusty1s committed
336

rusty1s's avatar
rusty1s committed
337
338
339
        csc2csr = self.csr2csc().argsort()
        self._csc2csr = csc2csr
        return csc2csr
rusty1s's avatar
rusty1s committed
340

rusty1s's avatar
rusty1s committed
341
342
    def is_coalesced(self) -> bool:
        idx = self._col.new_full((self._col.numel() + 1, ), -1)
rusty1s's avatar
rusty1s committed
343
        idx[1:] = self._sparse_sizes[1] * self.row() + self._col
rusty1s's avatar
rusty1s committed
344
345
346
347
        return bool((idx[1:] > idx[:-1]).all())

    def coalesce(self, reduce: str = "add"):
        idx = self._col.new_full((self._col.numel() + 1, ), -1)
rusty1s's avatar
rusty1s committed
348
        idx[1:] = self._sparse_sizes[1] * self.row() + self._col
rusty1s's avatar
rusty1s committed
349
        mask = idx[1:] > idx[:-1]
rusty1s's avatar
rusty1s committed
350

rusty1s's avatar
rusty1s committed
351
        if mask.all():  # Skip if indices are already coalesced.
rusty1s's avatar
rusty1s committed
352
353
            return self

rusty1s's avatar
rusty1s committed
354
355
        row = self.row()[mask]
        col = self._col[mask]
rusty1s's avatar
rusty1s committed
356

rusty1s's avatar
rusty1s committed
357
358
        value = self._value
        if value is not None:
rusty1s's avatar
rusty1s committed
359
360
            ptr = mask.nonzero().flatten()
            ptr = torch.cat([ptr, ptr.new_full((1, ), value.size(0))])
rusty1s's avatar
rusty1s committed
361
            value = segment_csr(value, ptr, reduce=reduce)
rusty1s's avatar
rusty1s committed
362
363
            value = value[0] if isinstance(value, tuple) else value

rusty1s's avatar
rusty1s committed
364
        return SparseStorage(row=row, rowptr=None, col=col, value=value,
rusty1s's avatar
rusty1s committed
365
                             sparse_sizes=self._sparse_sizes, rowcount=None,
rusty1s's avatar
rusty1s committed
366
367
368
369
370
371
372
373
374
375
376
                             colptr=None, colcount=None, csr2csc=None,
                             csc2csr=None, is_sorted=True)

    def fill_cache_(self):
        self.row()
        self.rowptr()
        self.rowcount()
        self.colptr()
        self.colcount()
        self.csr2csc()
        self.csc2csr()
rusty1s's avatar
rusty1s committed
377
        return self
rusty1s's avatar
rusty1s committed
378

rusty1s's avatar
rusty1s committed
379
380
381
382
383
384
    def clear_cache_(self):
        self._rowcount = None
        self._colptr = None
        self._colcount = None
        self._csr2csc = None
        self._csc2csr = None
rusty1s's avatar
rusty1s committed
385
        return self
rusty1s's avatar
rusty1s committed
386

rusty1s's avatar
rusty1s committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
    def num_cached_keys(self) -> int:
        count = 0
        if self.has_rowcount():
            count += 1
        if self.has_colptr():
            count += 1
        if self.has_colcount():
            count += 1
        if self.has_csr2csc():
            count += 1
        if self.has_csc2csr():
            count += 1
        return count

rusty1s's avatar
rusty1s committed
401
    def copy(self):
rusty1s's avatar
rusty1s committed
402
        return SparseStorage(row=self._row, rowptr=self._rowptr, col=self._col,
rusty1s's avatar
rusty1s committed
403
404
                             value=self._value,
                             sparse_sizes=self._sparse_sizes,
rusty1s's avatar
rusty1s committed
405
406
407
                             rowcount=self._rowcount, colptr=self._colptr,
                             colcount=self._colcount, csr2csc=self._csr2csc,
                             csc2csr=self._csc2csr, is_sorted=True)
rusty1s's avatar
rusty1s committed
408

rusty1s's avatar
test  
rusty1s committed
409
    def clone(self):
rusty1s's avatar
rusty1s committed
410
411
412
413
414
415
        row = self._row
        if row is not None:
            row = row.clone()
        rowptr = self._rowptr
        if rowptr is not None:
            rowptr = rowptr.clone()
rusty1s's avatar
rusty1s committed
416
        col = self._col.clone()
rusty1s's avatar
rusty1s committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
        value = self._value
        if value is not None:
            value = value.clone()
        rowcount = self._rowcount
        if rowcount is not None:
            rowcount = rowcount.clone()
        colptr = self._colptr
        if colptr is not None:
            colptr = colptr.clone()
        colcount = self._colcount
        if colcount is not None:
            colcount = colcount.clone()
        csr2csc = self._csr2csc
        if csr2csc is not None:
            csr2csc = csr2csc.clone()
        csc2csr = self._csc2csr
        if csc2csr is not None:
            csc2csr = csc2csr.clone()
rusty1s's avatar
rusty1s committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
        return SparseStorage(row=row, rowptr=rowptr, col=col, value=value,
                             sparse_sizes=self._sparse_sizes,
                             rowcount=rowcount, colptr=colptr,
                             colcount=colcount, csr2csc=csr2csc,
                             csc2csr=csc2csr, is_sorted=True)

    def type_as(self, tensor=torch.Tensor):
        value = self._value
        if value is not None:
            if tensor.dtype == value.dtype:
                return self
            else:
                return self.set_value(value.type_as(tensor), layout='coo')
        else:
            return self

    def device_as(self, tensor: torch.Tensor, non_blocking: bool = False):
        if tensor.device == self._col.device:
            return self

        row = self._row
        if row is not None:
            row = row.to(tensor.device, non_blocking=non_blocking)
        rowptr = self._rowptr
        if rowptr is not None:
            rowptr = rowptr.to(tensor.device, non_blocking=non_blocking)
        col = self._col.to(tensor.device, non_blocking=non_blocking)
        value = self._value
        if value is not None:
            value = value.to(tensor.device, non_blocking=non_blocking)
        rowcount = self._rowcount
        if rowcount is not None:
            rowcount = rowcount.to(tensor.device, non_blocking=non_blocking)
        colptr = self._colptr
        if colptr is not None:
            colptr = colptr.to(tensor.device, non_blocking=non_blocking)
        colcount = self._colcount
        if colcount is not None:
            colcount = colcount.to(tensor.device, non_blocking=non_blocking)
        csr2csc = self._csr2csc
        if csr2csc is not None:
            csr2csc = csr2csc.to(tensor.device, non_blocking=non_blocking)
        csc2csr = self._csc2csr
        if csc2csr is not None:
            csc2csr = csc2csr.to(tensor.device, non_blocking=non_blocking)
        return SparseStorage(row=row, rowptr=rowptr, col=col, value=value,
                             sparse_sizes=self._sparse_sizes,
                             rowcount=rowcount, colptr=colptr,
                             colcount=colcount, csr2csc=csr2csc,
                             csc2csr=csc2csr, is_sorted=True)

    def pin_memory(self):
        row = self._row
        if row is not None:
            row = row.pin_memory()
        rowptr = self._rowptr
        if rowptr is not None:
            rowptr = rowptr.pin_memory()
        col = self._col.pin_memory()
        value = self._value
        if value is not None:
            value = value.pin_memory()
        rowcount = self._rowcount
        if rowcount is not None:
            rowcount = rowcount.pin_memory()
        colptr = self._colptr
        if colptr is not None:
            colptr = colptr.pin_memory()
        colcount = self._colcount
        if colcount is not None:
            colcount = colcount.pin_memory()
        csr2csc = self._csr2csc
        if csr2csc is not None:
            csr2csc = csr2csc.pin_memory()
        csc2csr = self._csc2csr
        if csc2csr is not None:
            csc2csr = csc2csr.pin_memory()
        return SparseStorage(row=row, rowptr=rowptr, col=col, value=value,
                             sparse_sizes=self._sparse_sizes,
rusty1s's avatar
rusty1s committed
514
515
516
                             rowcount=rowcount, colptr=colptr,
                             colcount=colcount, csr2csc=csr2csc,
                             csc2csr=csc2csr, is_sorted=True)
rusty1s's avatar
rusty1s committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607

    def is_pinned(self) -> bool:
        is_pinned = True
        row = self._row
        if row is not None:
            is_pinned = is_pinned and row.is_pinned()
        rowptr = self._rowptr
        if rowptr is not None:
            is_pinned = is_pinned and rowptr.is_pinned()
        is_pinned = self._col.is_pinned()
        value = self._value
        if value is not None:
            is_pinned = is_pinned and value.is_pinned()
        rowcount = self._rowcount
        if rowcount is not None:
            is_pinned = is_pinned and rowcount.is_pinned()
        colptr = self._colptr
        if colptr is not None:
            is_pinned = is_pinned and colptr.is_pinned()
        colcount = self._colcount
        if colcount is not None:
            is_pinned = is_pinned and colcount.is_pinned()
        csr2csc = self._csr2csc
        if csr2csc is not None:
            is_pinned = is_pinned and csr2csc.is_pinned()
        csc2csr = self._csc2csr
        if csc2csr is not None:
            is_pinned = is_pinned and csc2csr.is_pinned()
        return is_pinned


def share_memory_(self) -> SparseStorage:
    row = self._row
    if row is not None:
        row.share_memory_()
    rowptr = self._rowptr
    if rowptr is not None:
        rowptr.share_memory_()
    self._col.share_memory_()
    value = self._value
    if value is not None:
        value.share_memory_()
    rowcount = self._rowcount
    if rowcount is not None:
        rowcount.share_memory_()
    colptr = self._colptr
    if colptr is not None:
        colptr.share_memory_()
    colcount = self._colcount
    if colcount is not None:
        colcount.share_memory_()
    csr2csc = self._csr2csc
    if csr2csc is not None:
        csr2csc.share_memory_()
    csc2csr = self._csc2csr
    if csc2csr is not None:
        csc2csr.share_memory_()


def is_shared(self) -> bool:
    is_shared = True
    row = self._row
    if row is not None:
        is_shared = is_shared and row.is_shared()
    rowptr = self._rowptr
    if rowptr is not None:
        is_shared = is_shared and rowptr.is_shared()
    is_shared = is_shared and self._col.is_shared()
    value = self._value
    if value is not None:
        is_shared = is_shared and value.is_shared()
    rowcount = self._rowcount
    if rowcount is not None:
        is_shared = is_shared and rowcount.is_shared()
    colptr = self._colptr
    if colptr is not None:
        is_shared = is_shared and colptr.is_shared()
    colcount = self._colcount
    if colcount is not None:
        is_shared = is_shared and colcount.is_shared()
    csr2csc = self._csr2csc
    if csr2csc is not None:
        is_shared = is_shared and csr2csc.is_shared()
    csc2csr = self._csc2csr
    if csc2csr is not None:
        is_shared = is_shared and csc2csr.is_shared()
    return is_shared


SparseStorage.share_memory_ = share_memory_
SparseStorage.is_shared = is_shared