forked from TuGraph-family/TuGraph-AntGraphLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyg_inputs.py
More file actions
366 lines (322 loc) · 12.1 KB
/
pyg_inputs.py
File metadata and controls
366 lines (322 loc) · 12.1 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from typing import List, NamedTuple, Tuple, Dict, Optional, Any, Union
from abc import abstractmethod
import torch
import numpy as np
from torch import Tensor
def optional_func(x: Optional[np.ndarray], func):
return func(x) if x is not None else None
def optional_to(x: Optional, *args, **kwargs):
return x.to(*args, **kwargs) if x is not None else None
class TorchEdgeIndex(NamedTuple):
private_edge_index: Optional[Tensor]
size: Optional[Tuple[int, int]]
edge_indices: Optional[Tensor]
# if create from coo or csr,following property exists
row: Optional[Tensor]
col: Optional[Tensor]
row_ptr: Optional[Tensor]
@property
def edge_index(self):
if self.private_edge_index is None:
# todo property of NamedTuple readonly, update related property is not allowed
if self.row is not None:
assert self.col is not None
# coo format. direction has exchange before. refer collate::get_ego_edge_index
return torch.stack(
[torch.as_tensor(self.row), torch.as_tensor(self.col)], dim=0
)
elif self.row_ptr is not None:
assert self.col is not None
# CSR format
from torch_sparse import SparseTensor
# is sorted should be Ture, otherwise, edge_f_index [0~nnz-1] would be changed
sp = SparseTensor(
rowptr=self.row_ptr,
col=self.col,
value=self.edge_indices,
sparse_sizes=(self.size[0], self.size[1]),
is_sorted=True,
)
coo = sp.coo()
dst_indices, src_indices, edge_indices = coo[0], coo[1], coo[2]
# should adapt pyg for direction of edge index when format is coo
return torch.stack([src_indices, dst_indices], dim=0)
else:
raise NotImplementedError
else:
return self.private_edge_index
def to(self, *args, **kwargs):
if self.private_edge_index is None:
if self.row is not None:
assert self.col is not None
# coo format
row = optional_to(self.row, *args, **kwargs)
col = optional_to(self.col, *args, **kwargs)
edge_index = torch.stack(
[torch.as_tensor(row), torch.as_tensor(col)], dim=0
)
return TorchEdgeIndex(
private_edge_index=edge_index.to(*args, **kwargs),
size=self.size,
edge_indices=optional_to(self.edge_indices, *args, **kwargs),
row=row,
col=col,
row_ptr=optional_to(self.row_ptr, *args, **kwargs),
)
elif self.row_ptr is not None:
assert self.col is not None
# CSR format
p_row_ptr = optional_to(self.row_ptr, *args, **kwargs)
p_col = optional_to(self.col, *args, **kwargs)
p_edge_indices = optional_to(self.edge_indices, *args, **kwargs)
from torch_sparse import SparseTensor
# is sorted should be Ture, otherwise, edge_f_index [0~nnz-1] would be changed
sp = SparseTensor(
rowptr=p_row_ptr,
col=p_col,
value=p_edge_indices,
sparse_sizes=(self.size[0], self.size[1]),
is_sorted=True,
)
coo = sp.coo()
dst_indices, src_indices, edge_indices = coo[0], coo[1], coo[2]
# should adapt pyg for direction of edge index when format is coo
edge_index = torch.stack([src_indices, dst_indices], dim=0)
return TorchEdgeIndex(
private_edge_index=edge_index.to(*args, **kwargs),
size=self.size,
edge_indices=optional_to(edge_indices, *args, **kwargs),
row=optional_to(self.row, *args, **kwargs),
col=p_col,
row_ptr=p_row_ptr,
)
else:
raise NotImplementedError
else:
return TorchEdgeIndex(
private_edge_index=self.edge_index.to(*args, **kwargs),
size=self.size,
edge_indices=optional_to(self.edge_indices, *args, **kwargs),
row=optional_to(self.row, *args, **kwargs),
col=optional_to(self.col, *args, **kwargs),
row_ptr=optional_to(self.row_ptr, *args, **kwargs),
)
@staticmethod
def create_from_coo_tensor(
src: Tensor,
dst: Tensor,
size: Optional[Tuple[int, int]],
edge_indices: Optional[Tensor],
):
return TorchEdgeIndex(
private_edge_index=None,
size=size,
edge_indices=edge_indices,
row=src,
col=dst,
row_ptr=None,
)
@staticmethod
def create_from_csr_tensor(
row_ptr: Tensor,
col: Tensor,
size: Optional[Tuple[int, int]],
edge_indices: Optional[Tensor],
):
return TorchEdgeIndex(
private_edge_index=None,
size=size,
edge_indices=edge_indices,
row=None,
col=col,
row_ptr=row_ptr,
)
@staticmethod
def create_from_tensor(
adj: Tensor, size: Optional[Tuple[int, int]], edge_indices: Optional[Tensor]
):
return TorchEdgeIndex(
private_edge_index=adj,
size=size,
edge_indices=edge_indices,
row=None,
col=None,
row_ptr=None,
)
class TorchFeature(NamedTuple):
@abstractmethod
def to(self, *args, **kwargs):
pass
@abstractmethod
def to_dense(self):
raise NotImplementedError(f"{self.__class__}.TorchFeature")
@abstractmethod
def get(self):
pass
class TorchDenseFeature(TorchFeature, NamedTuple):
x: Tensor
def to(self, *args, **kwargs):
return TorchDenseFeature(x=self.x.to(*args, **kwargs))
def get(self):
return self.x
def to_dense(self):
return self.x
@staticmethod
def create(feat: Union[Tensor, np.ndarray]):
return TorchDenseFeature(x=torch.as_tensor(feat))
class TorchSparseFeature(TorchFeature, NamedTuple):
row: Optional[Tensor]
row_ptr: Optional[Tensor]
col: Optional[Tensor]
value: Optional[Tensor]
size: Tuple[int, int]
def to(self, *args, **kwargs):
return TorchSparseFeature(
row=optional_to(self.row, *args, **kwargs),
row_ptr=optional_to(self.row_ptr, *args, **kwargs),
col=optional_to(self.col, *args, **kwargs),
value=optional_to(self.value, *args, **kwargs),
size=self.size,
)
# todo should get sparse out of dataloader,
# torch sparse tensor now can not be pickled when using multi-process in dataloader
def get(self):
if self.row is not None:
assert self.col is not None
index = torch.stack([self.row, self.col], dim=0)
return torch.sparse_coo_tensor(index, self.value, self.size)
elif self.row_ptr is not None:
assert self.col is not None
return torch.sparse_csr_tensor(
crow_indices=self.row_ptr,
col_indices=self.col,
values=self.value,
size=self.size,
)
else:
raise NotImplementedError(
f"row = None? {self.row is None}, row_ptr = None ? {self.row_ptr is None}, not supported"
)
def to_dense(self):
return self.get().to_dense()
@staticmethod
def create_from_coo(
row: Union[Tensor, np.ndarray],
col: Union[Tensor, np.ndarray],
value: Union[Tensor, np.ndarray],
size: Tuple[int, int],
):
return TorchSparseFeature(
row=torch.as_tensor(row),
row_ptr=None,
col=torch.as_tensor(col),
value=torch.as_tensor(value),
size=size,
)
@staticmethod
def create_from_csr(
row_ptr: Union[Tensor, np.ndarray],
col: Union[Tensor, np.ndarray],
value: Union[Tensor, np.ndarray],
size: Tuple[int, int],
):
return TorchSparseFeature(
row=None,
row_ptr=torch.as_tensor(row_ptr),
col=torch.as_tensor(col),
value=torch.as_tensor(value),
size=size,
)
class TorchFeatures(NamedTuple):
features: Dict[str, TorchFeature]
def to(self, *args, **kwargs):
return TorchFeatures(
features={k: v.to(*args, **kwargs) for k, v in self.features.items()},
)
@staticmethod
def create_from_torch_feature(features: Dict[str, TorchFeature]):
return TorchFeatures(features=features)
class TorchEgoBatchData(NamedTuple):
n_feats: Optional[TorchFeatures]
e_feats: Optional[TorchFeatures]
y: Optional[Tensor]
other_feats: Optional[Dict[str, Tensor]]
other_raw: Optional[Any]
adjs_t: List[TorchEdgeIndex]
root_index: Optional[Tensor]
def to(self, *args, **kwargs):
assert self.adjs_t is not None
return TorchEgoBatchData(
n_feats=optional_to(self.n_feats, *args, **kwargs),
e_feats=optional_to(self.e_feats, *args, **kwargs),
y=optional_to(self.y, *args, **kwargs),
adjs_t=[adj_t.to(*args, **kwargs) for adj_t in self.adjs_t],
root_index=optional_to(self.root_index, *args, **kwargs),
other_feats={k: v.to(*args, **kwargs) for k, v in self.other_feats.items()},
other_raw=self.other_raw,
)
@staticmethod
def create_from_tensor(
n_feats: Optional[TorchFeatures],
e_feats: Optional[TorchFeatures],
y: Optional[Tensor],
adjs_t: List[TorchEdgeIndex],
root_index: Optional[Tensor],
other_feats: Optional[Dict[str, Tensor]],
other_raw: Optional[Any],
):
return TorchEgoBatchData(
n_feats=n_feats,
e_feats=e_feats,
y=y,
adjs_t=adjs_t,
root_index=root_index,
other_feats=other_feats,
other_raw=other_raw,
)
class TorchSubGraphBatchData(NamedTuple):
n_feats: Optional[TorchFeatures]
e_feats: Optional[TorchFeatures]
y: Optional[Tensor]
other_feats: Optional[Dict[str, Tensor]]
other_raw: Optional[Any]
root_index: Optional[Tensor]
adjs_t: TorchEdgeIndex
n_num_per_sample: Optional[Tensor]
e_num_per_sample: Optional[Tensor]
def to(self, *args, **kwargs):
assert self.adjs_t is not None
return TorchSubGraphBatchData(
n_feats=optional_to(self.n_feats, *args, **kwargs),
e_feats=optional_to(self.e_feats, *args, **kwargs),
y=optional_to(self.y, *args, **kwargs),
adjs_t=self.adjs_t.to(*args, **kwargs),
root_index=optional_to(self.root_index, *args, **kwargs),
n_num_per_sample=optional_to(self.n_num_per_sample, *args, **kwargs),
e_num_per_sample=optional_to(self.e_num_per_sample, *args, **kwargs),
other_feats={k: v.to(*args, **kwargs) for k, v in self.other_feats.items()},
other_raw=self.other_raw,
)
@staticmethod
def create_from_tensor(
n_feats: Optional[TorchFeatures],
e_feats: Optional[TorchFeatures],
y: Optional[Tensor],
adjs_t: TorchEdgeIndex,
root_index: Optional[Tensor],
n_num_per_sample: Optional[Tensor],
e_num_per_sample: Optional[Tensor],
other_feats: Optional[Dict[str, Tensor]],
other_raw: Optional[Any],
):
return TorchSubGraphBatchData(
n_feats=n_feats,
e_feats=e_feats,
y=y,
adjs_t=adjs_t,
root_index=root_index,
n_num_per_sample=n_num_per_sample,
e_num_per_sample=e_num_per_sample,
other_feats=other_feats,
other_raw=other_raw,
)