-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
268 lines (216 loc) · 8.99 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import torch
from torch_sparse import SparseTensor
from torch import Tensor
import torch_sparse
from typing import List, Tuple
class PermIterator:
'''
Iterator of a permutation
'''
def __init__(self, device, size, bs, training=True) -> None:
self.bs = bs
self.training = training
self.idx = torch.randperm(
size, device=device) if training else torch.arange(size,
device=device)
def __len__(self):
return (self.idx.shape[0] + (self.bs - 1) *
(not self.training)) // self.bs
def __iter__(self):
self.ptr = 0
return self
def __next__(self):
if self.ptr + self.bs * self.training > self.idx.shape[0]:
raise StopIteration
ret = self.idx[self.ptr:self.ptr + self.bs]
self.ptr += self.bs
return ret
def sparsesample(adj: SparseTensor, deg: int) -> SparseTensor:
'''
sampling elements from a adjacency matrix
'''
rowptr, col, _ = adj.csr()
rowcount = adj.storage.rowcount()
mask = rowcount > 0
rowcount = rowcount[mask]
rowptr = rowptr[:-1][mask]
rand = torch.rand((rowcount.size(0), deg), device=col.device)
rand.mul_(rowcount.to(rand.dtype).reshape(-1, 1))
rand = rand.to(torch.long)
rand.add_(rowptr.reshape(-1, 1))
samplecol = col[rand]
samplerow = torch.arange(adj.size(0), device=adj.device())[mask]
ret = SparseTensor(row=samplerow.reshape(-1, 1).expand(-1, deg).flatten(),
col=samplecol.flatten(),
sparse_sizes=adj.sparse_sizes()).to_device(
adj.device()).coalesce().fill_value_(1.0)
#print(ret.storage.value())
return ret
def sparsesample2(adj: SparseTensor, deg: int) -> SparseTensor:
'''
another implementation for sampling elements from a adjacency matrix
'''
rowptr, col, _ = adj.csr()
rowcount = adj.storage.rowcount()
mask = rowcount > deg
rowcount = rowcount[mask]
rowptr = rowptr[:-1][mask]
rand = torch.rand((rowcount.size(0), deg), device=col.device)
rand.mul_(rowcount.to(rand.dtype).reshape(-1, 1))
rand = rand.to(torch.long)
rand.add_(rowptr.reshape(-1, 1))
samplecol = col[rand].flatten()
samplerow = torch.arange(adj.size(0), device=adj.device())[mask].reshape(
-1, 1).expand(-1, deg).flatten()
mask = torch.logical_not(mask)
nosamplerow, nosamplecol = adj[mask].coo()[:2]
nosamplerow = torch.arange(adj.size(0),
device=adj.device())[mask][nosamplerow]
ret = SparseTensor(
row=torch.cat((samplerow, nosamplerow)),
col=torch.cat((samplecol, nosamplecol)),
sparse_sizes=adj.sparse_sizes()).to_device(
adj.device()).fill_value_(1.0).coalesce() #.fill_value_(1)
#assert (ret.sum(dim=-1) == torch.clip(adj.sum(dim=-1), 0, deg)).all()
return ret
def sparsesample_reweight(adj: SparseTensor, deg: int) -> SparseTensor:
'''
another implementation for sampling elements from a adjacency matrix. It will also scale the sampled elements.
'''
rowptr, col, _ = adj.csr()
rowcount = adj.storage.rowcount()
mask = rowcount > deg
rowcount = rowcount[mask]
rowptr = rowptr[:-1][mask]
rand = torch.rand((rowcount.size(0), deg), device=col.device)
rand.mul_(rowcount.to(rand.dtype).reshape(-1, 1))
rand = rand.to(torch.long)
rand.add_(rowptr.reshape(-1, 1))
samplecol = col[rand].flatten()
samplerow = torch.arange(adj.size(0), device=adj.device())[mask].reshape(
-1, 1).expand(-1, deg).flatten()
samplevalue = (rowcount * (1/deg)).reshape(-1, 1).expand(-1, deg).flatten()
mask = torch.logical_not(mask)
nosamplerow, nosamplecol = adj[mask].coo()[:2]
nosamplerow = torch.arange(adj.size(0),
device=adj.device())[mask][nosamplerow]
ret = SparseTensor(row=torch.cat((samplerow, nosamplerow)),
col=torch.cat((samplecol, nosamplecol)),
value=torch.cat((samplevalue,
torch.ones_like(nosamplerow))),
sparse_sizes=adj.sparse_sizes()).to_device(
adj.device()).coalesce() #.fill_value_(1)
#assert (ret.sum(dim=-1) == torch.clip(adj.sum(dim=-1), 0, deg)).all()
return ret
def elem2spm(element: Tensor, sizes: List[int]) -> SparseTensor:
# Convert adjacency matrix to a 1-d vector
col = torch.bitwise_and(element, 0xffffffff)
row = torch.bitwise_right_shift(element, 32)
return SparseTensor(row=row, col=col, sparse_sizes=sizes).to_device(
element.device).fill_value_(1.0)
def spm2elem(spm: SparseTensor) -> Tensor:
# Convert 1-d vector to an adjacency matrix
sizes = spm.sizes()
elem = torch.bitwise_left_shift(spm.storage.row(),
32).add_(spm.storage.col())
#elem = spm.storage.row()*sizes[-1] + spm.storage.col()
#assert torch.all(torch.diff(elem) > 0)
return elem
def spmoverlap_(adj1: SparseTensor, adj2: SparseTensor) -> SparseTensor:
'''
Compute the overlap of neighbors (rows in adj). The returned matrix is similar to the hadamard product of adj1 and adj2
'''
assert adj1.sizes() == adj2.sizes()
element1 = spm2elem(adj1)
element2 = spm2elem(adj2)
if element2.shape[0] > element1.shape[0]:
element1, element2 = element2, element1
idx = torch.searchsorted(element1[:-1], element2)
mask = (element1[idx] == element2)
retelem = element2[mask]
'''
nnz1 = adj1.nnz()
element = torch.cat((adj1.storage.row(), adj2.storage.row()), dim=-1)
element.bitwise_left_shift_(32)
element[:nnz1] += adj1.storage.col()
element[nnz1:] += adj2.storage.col()
element = torch.sort(element, dim=-1)[0]
mask = (element[1:] == element[:-1])
retelem = element[:-1][mask]
'''
return elem2spm(retelem, adj1.sizes())
def spmnotoverlap_(adj1: SparseTensor,
adj2: SparseTensor) -> Tuple[SparseTensor, SparseTensor]:
'''
return elements in adj1 but not in adj2 and in adj2 but not adj1
'''
# assert adj1.sizes() == adj2.sizes()
element1 = spm2elem(adj1)
element2 = spm2elem(adj2)
idx = torch.searchsorted(element1[:-1], element2)
matchedmask = (element1[idx] == element2)
maskelem1 = torch.ones_like(element1, dtype=torch.bool)
maskelem1[idx[matchedmask]] = 0
retelem1 = element1[maskelem1]
retelem2 = element2[torch.logical_not(matchedmask)]
return elem2spm(retelem1, adj1.sizes()), elem2spm(retelem2, adj2.sizes())
def spmoverlap_notoverlap_(
adj1: SparseTensor,
adj2: SparseTensor) -> Tuple[SparseTensor, SparseTensor, SparseTensor]:
'''
return elements in adj1 but not in adj2 and in adj2 but not adj1
'''
# assert adj1.sizes() == adj2.sizes()
element1 = spm2elem(adj1)
element2 = spm2elem(adj2)
if element1.shape[0] == 0:
retoverlap = element1
retelem1 = element1
retelem2 = element2
else:
idx = torch.searchsorted(element1[:-1], element2)
matchedmask = (element1[idx] == element2)
maskelem1 = torch.ones_like(element1, dtype=torch.bool)
maskelem1[idx[matchedmask]] = 0
retelem1 = element1[maskelem1]
retoverlap = element2[matchedmask]
retelem2 = element2[torch.logical_not(matchedmask)]
sizes = adj1.sizes()
return elem2spm(retoverlap,
sizes), elem2spm(retelem1,
sizes), elem2spm(retelem2, sizes)
def adjoverlap(adj1: SparseTensor,
adj2: SparseTensor,
tarei: Tensor,
filled1: bool = False,
calresadj: bool = False,
cnsampledeg: int = -1,
ressampledeg: int = -1):
# a wrapper for functions above.
adj1 = adj1[tarei[0]]
adj2 = adj2[tarei[1]]
if calresadj:
adjoverlap, adjres1, adjres2 = spmoverlap_notoverlap_(adj1, adj2)
if cnsampledeg > 0:
adjoverlap = sparsesample_reweight(adjoverlap, cnsampledeg)
if ressampledeg > 0:
adjres1 = sparsesample_reweight(adjres1, ressampledeg)
adjres2 = sparsesample_reweight(adjres2, ressampledeg)
return adjoverlap, adjres1, adjres2
else:
adjoverlap = spmoverlap_(adj1, adj2)
if cnsampledeg > 0:
adjoverlap = sparsesample_reweight(adjoverlap, cnsampledeg)
return adjoverlap
if __name__ == "__main__":
adj1 = SparseTensor.from_edge_index(
torch.LongTensor([[0, 0, 1, 2, 3], [0, 1, 1, 2, 3]]))
adj2 = SparseTensor.from_edge_index(
torch.LongTensor([[0, 3, 1, 2, 3], [0, 1, 1, 2, 3]]))
adj3 = SparseTensor.from_edge_index(
torch.LongTensor([[0, 1, 2, 2, 2,2, 3, 3, 3], [1, 0, 2,3,4, 5, 4, 5, 6]]))
print(spmnotoverlap_(adj1, adj2))
print(spmoverlap_(adj1, adj2))
print(spmoverlap_notoverlap_(adj1, adj2))
print(sparsesample2(adj3, 2))
print(sparsesample_reweight(adj3, 2))