-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpass.py
972 lines (840 loc) · 41.7 KB
/
msgpass.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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
514
515
516
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
import inspect
import os
import os.path as osp
import random
import re
from collections import OrderedDict
from inspect import Parameter
from itertools import chain
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Set,
Union,
get_type_hints,
)
import torch
from torch import Tensor
from torch.utils.hooks import RemovableHandle
from torch_geometric.nn.aggr import Aggregation
from torch_geometric.nn.conv.utils.inspector import (
Inspector,
func_body_repr,
func_header_repr,
)
from torch_geometric.nn.conv.utils.jit import class_from_module_repr
from torch_geometric.nn.conv.utils.typing import (
parse_types,
resolve_types,
sanitize,
split_types_repr,
)
from torch_geometric.nn.resolver import aggregation_resolver as aggr_resolver
from torch_geometric.typing import Adj, Size, SparseTensor
from torch_geometric.utils import (
is_sparse,
is_torch_sparse_tensor,
#to_edge_index,
)
#from torch_geometric.utils.sparse import ptr2index
FUSE_AGGRS = {'add', 'sum', 'mean', 'min', 'max'}
def ptr2ind(ptr: Tensor) -> Tensor:
ind = torch.arange(ptr.numel() - 1, device=ptr.device)
return ind.repeat_interleave(ptr[1:] - ptr[:-1])
class MessagePassing(torch.nn.Module):
r"""Base class for creating message passing layers of the form
.. math::
\mathbf{x}_i^{\prime} = \gamma_{\mathbf{\Theta}} \left( \mathbf{x}_i,
\bigoplus_{j \in \mathcal{N}(i)} \, \phi_{\mathbf{\Theta}}
\left(\mathbf{x}_i, \mathbf{x}_j,\mathbf{e}_{j,i}\right) \right),
where :math:`\bigoplus` denotes a differentiable, permutation invariant
function, *e.g.*, sum, mean, min, max or mul, and
:math:`\gamma_{\mathbf{\Theta}}` and :math:`\phi_{\mathbf{\Theta}}` denote
differentiable functions such as MLPs.
See `here <https://pytorch-geometric.readthedocs.io/en/latest/tutorial/
create_gnn.html>`__ for the accompanying tutorial.
Args:
aggr (str or [str] or Aggregation, optional): The aggregation scheme
to use, *e.g.*, :obj:`"add"`, :obj:`"sum"` :obj:`"mean"`,
:obj:`"min"`, :obj:`"max"` or :obj:`"mul"`.
In addition, can be any
:class:`~torch_geometric.nn.aggr.Aggregation` module (or any string
that automatically resolves to it).
If given as a list, will make use of multiple aggregations in which
different outputs will get concatenated in the last dimension.
If set to :obj:`None`, the :class:`MessagePassing` instantiation is
expected to implement its own aggregation logic via
:meth:`aggregate`. (default: :obj:`"add"`)
aggr_kwargs (Dict[str, Any], optional): Arguments passed to the
respective aggregation function in case it gets automatically
resolved. (default: :obj:`None`)
flow (str, optional): The flow direction of message passing
(:obj:`"source_to_target"` or :obj:`"target_to_source"`).
(default: :obj:`"source_to_target"`)
node_dim (int, optional): The axis along which to propagate.
(default: :obj:`-2`)
decomposed_layers (int, optional): The number of feature decomposition
layers, as introduced in the `"Optimizing Memory Efficiency of
Graph Neural Networks on Edge Computing Platforms"
<https://arxiv.org/abs/2104.03058>`_ paper.
Feature decomposition reduces the peak memory usage by slicing
the feature dimensions into separated feature decomposition layers
during GNN aggregation.
This method can accelerate GNN execution on CPU-based platforms
(*e.g.*, 2-3x speedup on the
:class:`~torch_geometric.datasets.Reddit` dataset) for common GNN
models such as :class:`~torch_geometric.nn.models.GCN`,
:class:`~torch_geometric.nn.models.GraphSAGE`,
:class:`~torch_geometric.nn.models.GIN`, etc.
However, this method is not applicable to all GNN operators
available, in particular for operators in which message computation
can not easily be decomposed, *e.g.* in attention-based GNNs.
The selection of the optimal value of :obj:`decomposed_layers`
depends both on the specific graph dataset and available hardware
resources.
A value of :obj:`2` is suitable in most cases.
Although the peak memory usage is directly associated with the
granularity of feature decomposition, the same is not necessarily
true for execution speedups. (default: :obj:`1`)
"""
special_args: Set[str] = {
'edge_index', 'adj_t', 'edge_index_i', 'edge_index_j', 'size',
'size_i', 'size_j', 'ptr', 'index', 'dim_size'
}
def __init__(
self,
aggr: Optional[Union[str, List[str], Aggregation]] = "add",
*,
aggr_kwargs: Optional[Dict[str, Any]] = None,
flow: str = "source_to_target",
node_dim: int = -2,
decomposed_layers: int = 1,
**kwargs,
):
super().__init__()
if aggr is None:
self.aggr = None
elif isinstance(aggr, (str, Aggregation)):
self.aggr = str(aggr)
elif isinstance(aggr, (tuple, list)):
self.aggr = [str(x) for x in aggr]
self.aggr_module = aggr_resolver(aggr, **(aggr_kwargs or {}))
self.flow = flow
if flow not in ['source_to_target', 'target_to_source']:
raise ValueError(f"Expected 'flow' to be either 'source_to_target'"
f" or 'target_to_source' (got '{flow}')")
self.node_dim = node_dim
self.decomposed_layers = decomposed_layers
self.inspector = Inspector(self)
self.inspector.inspect(self.message)
self.inspector.inspect(self.aggregate, pop_first=True)
self.inspector.params['aggregate'].pop('aggr', None)
self.inspector.inspect(self.message_and_aggregate, pop_first=True)
self.inspector.inspect(self.update, pop_first=True)
self.inspector.inspect(self.edge_update)
self._user_args = self.inspector.keys(
['message', 'aggregate', 'update']).difference(self.special_args)
self._fused_user_args = self.inspector.keys(
['message_and_aggregate', 'update']).difference(self.special_args)
self._edge_user_args = self.inspector.keys(['edge_update']).difference(
self.special_args)
# Support for "fused" message passing.
self.fuse = self.inspector.implements('message_and_aggregate')
if self.aggr is not None:
self.fuse &= isinstance(self.aggr, str) and self.aggr in FUSE_AGGRS
# Support for explainability.
self._explain = False
self._edge_mask = None
self._loop_mask = None
self._apply_sigmoid = True
# Hooks:
self._propagate_forward_pre_hooks = OrderedDict()
self._propagate_forward_hooks = OrderedDict()
self._message_forward_pre_hooks = OrderedDict()
self._message_forward_hooks = OrderedDict()
self._aggregate_forward_pre_hooks = OrderedDict()
self._aggregate_forward_hooks = OrderedDict()
self._message_and_aggregate_forward_pre_hooks = OrderedDict()
self._message_and_aggregate_forward_hooks = OrderedDict()
self._edge_update_forward_pre_hooks = OrderedDict()
self._edge_update_forward_hooks = OrderedDict()
def reset_parameters(self):
r"""Resets all learnable parameters of the module."""
if self.aggr_module is not None:
self.aggr_module.reset_parameters()
def forward(self, *args, **kwargs) -> Any:
r"""Runs the forward pass of the module."""
pass
def _check_input(self, edge_index, size):
the_size: List[Optional[int]] = [None, None]
if is_sparse(edge_index):
if self.flow == 'target_to_source':
raise ValueError(
('Flow direction "target_to_source" is invalid for '
'message propagation via `torch_sparse.SparseTensor` '
'or `torch.sparse.Tensor`. If you really want to make '
'use of a reverse message passing flow, pass in the '
'transposed sparse tensor to the message passing module, '
'e.g., `adj_t.t()`.'))
the_size[0] = edge_index.size(1)
the_size[1] = edge_index.size(0)
return the_size
elif isinstance(edge_index, Tensor):
int_dtypes = (torch.uint8, torch.int8, torch.int32, torch.int64)
if edge_index.dtype not in int_dtypes:
raise ValueError(f"Expected 'edge_index' to be of integer "
f"type (got '{edge_index.dtype}')")
if edge_index.dim() != 2:
raise ValueError(f"Expected 'edge_index' to be two-dimensional"
f" (got {edge_index.dim()} dimensions)")
if not torch.jit.is_tracing() and edge_index.size(0) != 2:
raise ValueError(f"Expected 'edge_index' to have size '2' in "
f"the first dimension (got "
f"'{edge_index.size(0)}')")
if size is not None:
the_size[0] = size[0]
the_size[1] = size[1]
return the_size
raise ValueError(
('`MessagePassing.propagate` only supports integer tensors of '
'shape `[2, num_messages]`, `torch_sparse.SparseTensor` or '
'`torch.sparse.Tensor` for argument `edge_index`.'))
def _set_size(self, size: List[Optional[int]], dim: int, src: Tensor):
the_size = size[dim]
if the_size is None:
size[dim] = src.size(self.node_dim)
elif the_size != src.size(self.node_dim):
raise ValueError(
(f'Encountered tensor with size {src.size(self.node_dim)} in '
f'dimension {self.node_dim}, but expected size {the_size}.'))
def _lift(self, src, edge_index, dim):
if is_torch_sparse_tensor(edge_index):
print('SPARSE Not Implemented')
# assert dim == 0 or dim == 1
# if edge_index.layout == torch.sparse_coo:
# index = edge_index._indices()[1 - dim]
# elif edge_index.layout == torch.sparse_csr:
# if dim == 0:
# index = edge_index.col_indices()
# else:
# index = ptr2index(edge_index.crow_indices())
# elif edge_index.layout == torch.sparse_csc:
# if dim == 0:
# index = ptr2index(edge_index.ccol_indices())
# else:
# index = edge_index.row_indices()
# else:
# raise ValueError(f"Unsupported sparse tensor layout "
# f"(got '{edge_index.layout}')")
# return src.index_select(self.node_dim, index)
elif isinstance(edge_index, Tensor):
try:
index = edge_index[dim]
return src.index_select(self.node_dim, index)
except (IndexError, RuntimeError) as e:
if index.min() < 0 or index.max() >= src.size(self.node_dim):
raise IndexError(
f"Encountered an index error. Please ensure that all "
f"indices in 'edge_index' point to valid indices in "
f"the interval [0, {src.size(self.node_dim) - 1}] "
f"(got interval "
f"[{int(index.min())}, {int(index.max())}])")
else:
raise e
if index.numel() > 0 and index.min() < 0:
raise ValueError(
f"Found negative indices in 'edge_index' (got "
f"{index.min().item()}). Please ensure that all "
f"indices in 'edge_index' point to valid indices "
f"in the interval [0, {src.size(self.node_dim)}) in "
f"your node feature matrix and try again.")
if (index.numel() > 0
and index.max() >= src.size(self.node_dim)):
raise ValueError(
f"Found indices in 'edge_index' that are larger "
f"than {src.size(self.node_dim) - 1} (got "
f"{index.max().item()}). Please ensure that all "
f"indices in 'edge_index' point to valid indices "
f"in the interval [0, {src.size(self.node_dim)}) in "
f"your node feature matrix and try again.")
raise e
elif isinstance(edge_index, SparseTensor):
row, col, _ = edge_index.coo()
if dim == 0:
return src.index_select(self.node_dim, col)
elif dim == 1:
return src.index_select(self.node_dim, row)
raise ValueError(
('`MessagePassing.propagate` only supports integer tensors of '
'shape `[2, num_messages]`, `torch_sparse.SparseTensor` '
'or `torch.sparse.Tensor` for argument `edge_index`.'))
def _collect(self, args, edge_index, size, kwargs):
i, j = (1, 0) if self.flow == 'source_to_target' else (0, 1)
lAP = j
rAP = i
# for x in range(20):
# print('edge (j,i): ',edge_index[0][x],edge_index[1][x])
out = {}
for arg in args:
if arg[-2:] not in ['_i', '_j','_s'] and arg[-4:] not in ['_rAP', '_lAP']:
out[arg] = kwargs.get(arg, Parameter.empty)
else:
if arg[-2:] == '_s':
data = kwargs.get(arg, Parameter.empty)
dim = j
# print(data.shape)
elif arg[-2:] in ['_i', '_j']:
dim = j if arg[-2:] == '_j' else i
data = kwargs.get(arg[:-2], Parameter.empty)
# print('ARGGG',arg,arg[-2:])
# print(data[0].shape,data[1].shape)
# print(data)
elif arg[-4:] in ['_rAP', '_lAP']:
dim = lAP if arg[-4:] == '_lAP' else rAP
data = kwargs.get(arg[:-4], Parameter.empty)
# print('ARGGG',arg,arg[-4:])
# print(data[0].shape,data[1].shape)
# print(data)
if isinstance(data, (tuple, list)):
#print('tuple listtt')
# print('tpleee',arg)
# if arg[-2:]=='_j':
# print('THIS IS _j, dim: ',dim)
# print(data)
# if arg[-2:]=='_i':
# print('THIS IS _i, dim: ',dim)
# print(data)
assert len(data) == 2
if isinstance(data[1 - dim], Tensor):
self._set_size(size, 1 - dim, data[1 - dim])
data = data[dim]
# print(data.shape)
# print(data)
# for x in range(20):
# print(data[x])
# if arg[-2:]=='_j':
# print('THIS IS _j also , dim: ',dim)
# print(data)
# if arg[-2:]=='_i':
# print('THIS IS _i also, dim: ',dim)
# print(data)
#print(data.shape)
if isinstance(data, Tensor):
#print('tensorrrr')
self._set_size(size, dim, data)
data = self._lift(data, edge_index, dim)
#print('tensorr',arg)
# print(data.shape)
# print(data)
# for x in range(20):
# print(data[x])
out[arg] = data
if is_torch_sparse_tensor(edge_index):
print('SPARSE not implemented')
# indices, values = to_edge_index(edge_index)
# out['adj_t'] = edge_index
# out['edge_index'] = None
# out['edge_index_i'] = indices[0]
# out['edge_index_j'] = indices[1]
# out['ptr'] = None # TODO Get `rowptr` from CSR representation.
# if out.get('edge_weight', None) is None:
# out['edge_weight'] = values
# if out.get('edge_attr', None) is None:
# out['edge_attr'] = None if values.dim() == 1 else values
# if out.get('edge_type', None) is None:
# out['edge_type'] = values
elif isinstance(edge_index, Tensor):
out['adj_t'] = None
out['edge_index'] = edge_index
out['edge_index_i'] = edge_index[i]
out['edge_index_j'] = edge_index[j]
out['ptr'] = None
elif isinstance(edge_index, SparseTensor):
row, col, value = edge_index.coo()
rowptr, _, _ = edge_index.csr()
out['adj_t'] = edge_index
out['edge_index'] = None
out['edge_index_i'] = row
out['edge_index_j'] = col
out['ptr'] = rowptr
if out.get('edge_weight', None) is None:
out['edge_weight'] = value
if out.get('edge_attr', None) is None:
out['edge_attr'] = value
if out.get('edge_type', None) is None:
out['edge_type'] = value
out['index'] = out['edge_index_i']
out['size'] = size
out['size_i'] = size[i] if size[i] is not None else size[j]
out['size_j'] = size[j] if size[j] is not None else size[i]
out['dim_size'] = out['size_i']
return out
def propagate(self, edge_index: Adj, size: Size = None, **kwargs):
r"""The initial call to start propagating messages.
Args:
edge_index (torch.Tensor or SparseTensor): A :class:`torch.Tensor`,
a :class:`torch_sparse.SparseTensor` or a
:class:`torch.sparse.Tensor` that defines the underlying
graph connectivity/message passing flow.
:obj:`edge_index` holds the indices of a general (sparse)
assignment matrix of shape :obj:`[N, M]`.
If :obj:`edge_index` is a :obj:`torch.Tensor`, its :obj:`dtype`
should be :obj:`torch.long` and its shape needs to be defined
as :obj:`[2, num_messages]` where messages from nodes in
:obj:`edge_index[0]` are sent to nodes in :obj:`edge_index[1]`
(in case :obj:`flow="source_to_target"`).
If :obj:`edge_index` is a :class:`torch_sparse.SparseTensor` or
a :class:`torch.sparse.Tensor`, its sparse indices
:obj:`(row, col)` should relate to :obj:`row = edge_index[1]`
and :obj:`col = edge_index[0]`.
The major difference between both formats is that we need to
input the *transposed* sparse adjacency matrix into
:meth:`propagate`.
size ((int, int), optional): The size :obj:`(N, M)` of the
assignment matrix in case :obj:`edge_index` is a
:class:`torch.Tensor`.
If set to :obj:`None`, the size will be automatically inferred
and assumed to be quadratic.
This argument is ignored in case :obj:`edge_index` is a
:class:`torch_sparse.SparseTensor` or
a :class:`torch.sparse.Tensor`. (default: :obj:`None`)
**kwargs: Any additional data which is needed to construct and
aggregate messages, and to update node embeddings.
"""
decomposed_layers = 1 if self.explain else self.decomposed_layers
for hook in self._propagate_forward_pre_hooks.values():
res = hook(self, (edge_index, size, kwargs))
if res is not None:
edge_index, size, kwargs = res
size = self._check_input(edge_index, size)
# Run "fused" message and aggregation (if applicable).
if is_sparse(edge_index) and self.fuse and not self.explain:
coll_dict = self._collect(self._fused_user_args, edge_index, size,
kwargs)
msg_aggr_kwargs = self.inspector.distribute(
'message_and_aggregate', coll_dict)
for hook in self._message_and_aggregate_forward_pre_hooks.values():
res = hook(self, (edge_index, msg_aggr_kwargs))
if res is not None:
edge_index, msg_aggr_kwargs = res
out = self.message_and_aggregate(edge_index, **msg_aggr_kwargs)
for hook in self._message_and_aggregate_forward_hooks.values():
res = hook(self, (edge_index, msg_aggr_kwargs), out)
if res is not None:
out = res
update_kwargs = self.inspector.distribute('update', coll_dict)
out = self.update(out, **update_kwargs)
else: # Otherwise, run both functions in separation.
if decomposed_layers > 1:
user_args = self._user_args
decomp_args = {a[:-2] for a in user_args if a[-2:] == '_j'}
decomp_kwargs = {
a: kwargs[a].chunk(decomposed_layers, -1)
for a in decomp_args
}
decomp_out = []
for i in range(decomposed_layers):
if decomposed_layers > 1:
for arg in decomp_args:
kwargs[arg] = decomp_kwargs[arg][i]
coll_dict = self._collect(self._user_args, edge_index, size,
kwargs)
#print('coll_dict')
# for k,v in coll_dict.items():
# print('---',k,'---')
# if isinstance(v,Tensor):
# print(v.shape)
# else:
# print(v)
msg_kwargs = self.inspector.distribute('message', coll_dict)
#print('msg_kwargs')
# for k,v in msg_kwargs.items():
# print('---',k,'---')
# if isinstance(v,Tensor):
# print(v.shape)
# else:
# print(v)
for hook in self._message_forward_pre_hooks.values():
res = hook(self, (msg_kwargs, ))
if res is not None:
msg_kwargs = res[0] if isinstance(res, tuple) else res
out = self.message(**msg_kwargs)
for hook in self._message_forward_hooks.values():
res = hook(self, (msg_kwargs, ), out)
if res is not None:
out = res
if self.explain:
explain_msg_kwargs = self.inspector.distribute(
'explain_message', coll_dict)
out = self.explain_message(out, **explain_msg_kwargs)
aggr_kwargs = self.inspector.distribute('aggregate', coll_dict)
for hook in self._aggregate_forward_pre_hooks.values():
res = hook(self, (aggr_kwargs, ))
if res is not None:
aggr_kwargs = res[0] if isinstance(res, tuple) else res
out = self.aggregate(out, **aggr_kwargs)
for hook in self._aggregate_forward_hooks.values():
res = hook(self, (aggr_kwargs, ), out)
if res is not None:
out = res
update_kwargs = self.inspector.distribute('update', coll_dict)
out = self.update(out, **update_kwargs)
if decomposed_layers > 1:
decomp_out.append(out)
if decomposed_layers > 1:
out = torch.cat(decomp_out, dim=-1)
for hook in self._propagate_forward_hooks.values():
res = hook(self, (edge_index, size, kwargs), out)
if res is not None:
out = res
return out
def edge_updater(self, edge_index: Adj, **kwargs):
r"""The initial call to compute or update features for each edge in the
graph.
Args:
edge_index (torch.Tensor or SparseTensor): A :obj:`torch.Tensor`, a
:class:`torch_sparse.SparseTensor` or a
:class:`torch.sparse.Tensor` that defines the underlying graph
connectivity/message passing flow.
See :meth:`propagate` for more information.
**kwargs: Any additional data which is needed to compute or update
features for each edge in the graph.
"""
for hook in self._edge_update_forward_pre_hooks.values():
res = hook(self, (edge_index, kwargs))
if res is not None:
edge_index, kwargs = res
size = self._check_input(edge_index, size=None)
coll_dict = self._collect(self._edge_user_args, edge_index, size,
kwargs)
edge_kwargs = self.inspector.distribute('edge_update', coll_dict)
out = self.edge_update(**edge_kwargs)
for hook in self._edge_update_forward_hooks.values():
res = hook(self, (edge_index, kwargs), out)
if res is not None:
out = res
return out
def message(self, x_j: Tensor) -> Tensor:
r"""Constructs messages from node :math:`j` to node :math:`i`
in analogy to :math:`\phi_{\mathbf{\Theta}}` for each edge in
:obj:`edge_index`.
This function can take any argument as input which was initially
passed to :meth:`propagate`.
Furthermore, tensors passed to :meth:`propagate` can be mapped to the
respective nodes :math:`i` and :math:`j` by appending :obj:`_i` or
:obj:`_j` to the variable name, *.e.g.* :obj:`x_i` and :obj:`x_j`.
"""
return x_j
@property
def explain(self) -> bool:
return self._explain
@explain.setter
def explain(self, explain: bool):
if explain:
methods = ['message', 'explain_message', 'aggregate', 'update']
else:
methods = ['message', 'aggregate', 'update']
self._explain = explain
self.inspector.inspect(self.explain_message, pop_first=True)
self._user_args = self.inspector.keys(methods).difference(
self.special_args)
def explain_message(self, inputs: Tensor, size_i: int) -> Tensor:
# NOTE Replace this method in custom explainers per message-passing
# layer to customize how messages shall be explained, e.g., via:
# conv.explain_message = explain_message.__get__(conv, MessagePassing)
# see stackoverflow.com: 394770/override-a-method-at-instance-level
edge_mask = self._edge_mask
if edge_mask is None:
raise ValueError(f"Could not find a pre-defined 'edge_mask' as "
f"part of {self.__class__.__name__}.")
if self._apply_sigmoid:
edge_mask = edge_mask.sigmoid()
# Some ops add self-loops to `edge_index`. We need to do the same for
# `edge_mask` (but do not train these entries).
if inputs.size(self.node_dim) != edge_mask.size(0):
edge_mask = edge_mask[self._loop_mask]
loop = edge_mask.new_ones(size_i)
edge_mask = torch.cat([edge_mask, loop], dim=0)
assert inputs.size(self.node_dim) == edge_mask.size(0)
size = [1] * inputs.dim()
size[self.node_dim] = -1
return inputs * edge_mask.view(size)
def aggregate(self, inputs: Tensor, index: Tensor,
ptr: Optional[Tensor] = None,
dim_size: Optional[int] = None) -> Tensor:
r"""Aggregates messages from neighbors as
:math:`\bigoplus_{j \in \mathcal{N}(i)}`.
Takes in the output of message computation as first argument and any
argument which was initially passed to :meth:`propagate`.
By default, this function will delegate its call to the underlying
:class:`~torch_geometric.nn.aggr.Aggregation` module to reduce messages
as specified in :meth:`__init__` by the :obj:`aggr` argument.
"""
return self.aggr_module(inputs, index, ptr=ptr, dim_size=dim_size,
dim=self.node_dim)
def message_and_aggregate(
self,
adj_t: Union[SparseTensor, Tensor],
) -> Tensor:
r"""Fuses computations of :func:`message` and :func:`aggregate` into a
single function.
If applicable, this saves both time and memory since messages do not
explicitly need to be materialized.
This function will only gets called in case it is implemented and
propagation takes place based on a :obj:`torch_sparse.SparseTensor`
or a :obj:`torch.sparse.Tensor`.
"""
raise NotImplementedError
def update(self, inputs: Tensor) -> Tensor:
r"""Updates node embeddings in analogy to
:math:`\gamma_{\mathbf{\Theta}}` for each node
:math:`i \in \mathcal{V}`.
Takes in the output of aggregation as first argument and any argument
which was initially passed to :meth:`propagate`.
"""
return inputs
def edge_update(self) -> Tensor:
r"""Computes or updates features for each edge in the graph.
This function can take any argument as input which was initially passed
to :meth:`edge_updater`.
Furthermore, tensors passed to :meth:`edge_updater` can be mapped to
the respective nodes :math:`i` and :math:`j` by appending :obj:`_i` or
:obj:`_j` to the variable name, *.e.g.* :obj:`x_i` and :obj:`x_j`.
"""
raise NotImplementedError
def register_propagate_forward_pre_hook(self,
hook: Callable) -> RemovableHandle:
r"""Registers a forward pre-hook on the module.
The hook will be called every time before :meth:`propagate` is invoked.
It should have the following signature:
.. code-block:: python
hook(module, inputs) -> None or modified input
The hook can modify the input.
Input keyword arguments are passed to the hook as a dictionary in
:obj:`inputs[-1]`.
Returns a :class:`torch.utils.hooks.RemovableHandle` that can be used
to remove the added hook by calling :obj:`handle.remove()`.
"""
handle = RemovableHandle(self._propagate_forward_pre_hooks)
self._propagate_forward_pre_hooks[handle.id] = hook
return handle
def register_propagate_forward_hook(self,
hook: Callable) -> RemovableHandle:
r"""Registers a forward hook on the module.
The hook will be called every time after :meth:`propagate` has computed
an output.
It should have the following signature:
.. code-block:: python
hook(module, inputs, output) -> None or modified output
The hook can modify the output.
Input keyword arguments are passed to the hook as a dictionary in
:obj:`inputs[-1]`.
Returns a :class:`torch.utils.hooks.RemovableHandle` that can be used
to remove the added hook by calling :obj:`handle.remove()`.
"""
handle = RemovableHandle(self._propagate_forward_hooks)
self._propagate_forward_hooks[handle.id] = hook
return handle
def register_message_forward_pre_hook(self,
hook: Callable) -> RemovableHandle:
r"""Registers a forward pre-hook on the module.
The hook will be called every time before :meth:`message` is invoked.
See :meth:`register_propagate_forward_pre_hook` for more information.
"""
handle = RemovableHandle(self._message_forward_pre_hooks)
self._message_forward_pre_hooks[handle.id] = hook
return handle
def register_message_forward_hook(self, hook: Callable) -> RemovableHandle:
r"""Registers a forward hook on the module.
The hook will be called every time after :meth:`message` has computed
an output.
See :meth:`register_propagate_forward_hook` for more information.
"""
handle = RemovableHandle(self._message_forward_hooks)
self._message_forward_hooks[handle.id] = hook
return handle
def register_aggregate_forward_pre_hook(self,
hook: Callable) -> RemovableHandle:
r"""Registers a forward pre-hook on the module.
The hook will be called every time before :meth:`aggregate` is invoked.
See :meth:`register_propagate_forward_pre_hook` for more information.
"""
handle = RemovableHandle(self._aggregate_forward_pre_hooks)
self._aggregate_forward_pre_hooks[handle.id] = hook
return handle
def register_aggregate_forward_hook(self,
hook: Callable) -> RemovableHandle:
r"""Registers a forward hook on the module.
The hook will be called every time after :meth:`aggregate` has computed
an output.
See :meth:`register_propagate_forward_hook` for more information.
"""
handle = RemovableHandle(self._aggregate_forward_hooks)
self._aggregate_forward_hooks[handle.id] = hook
return handle
def register_message_and_aggregate_forward_pre_hook(
self, hook: Callable) -> RemovableHandle:
r"""Registers a forward pre-hook on the module.
The hook will be called every time before :meth:`message_and_aggregate`
is invoked.
See :meth:`register_propagate_forward_pre_hook` for more information.
"""
handle = RemovableHandle(self._message_and_aggregate_forward_pre_hooks)
self._message_and_aggregate_forward_pre_hooks[handle.id] = hook
return handle
def register_message_and_aggregate_forward_hook(
self, hook: Callable) -> RemovableHandle:
r"""Registers a forward hook on the module.
The hook will be called every time after :meth:`message_and_aggregate`
has computed an output.
See :meth:`register_propagate_forward_hook` for more information.
"""
handle = RemovableHandle(self._message_and_aggregate_forward_hooks)
self._message_and_aggregate_forward_hooks[handle.id] = hook
return handle
def register_edge_update_forward_pre_hook(
self, hook: Callable) -> RemovableHandle:
r"""Registers a forward pre-hook on the module.
The hook will be called every time before :meth:`edge_update` is
invoked. See :meth:`register_propagate_forward_pre_hook` for more
information.
"""
handle = RemovableHandle(self._edge_update_forward_pre_hooks)
self._edge_update_forward_pre_hooks[handle.id] = hook
return handle
def register_edge_update_forward_hook(self,
hook: Callable) -> RemovableHandle:
r"""Registers a forward hook on the module.
The hook will be called every time after :meth:`edge_update` has
computed an output.
See :meth:`register_propagate_forward_hook` for more information.
"""
handle = RemovableHandle(self._edge_update_forward_hooks)
self._edge_update_forward_hooks[handle.id] = hook
return handle
@torch.jit.unused
def jittable(self, typing: Optional[str] = None) -> 'MessagePassing':
r"""Analyzes the :class:`MessagePassing` instance and produces a new
jittable module that can be used in combination with
:meth:`torch.jit.script`.
Args:
typing (str, optional): If given, will generate a concrete instance
with :meth:`forward` types based on :obj:`typing`, *e.g.*,
:obj:`"(Tensor, Optional[Tensor]) -> Tensor"`.
"""
try:
from jinja2 import Template
except ImportError:
raise ModuleNotFoundError(
"No module named 'jinja2' found on this machine. "
"Run 'pip install jinja2' to install the library.")
source = inspect.getsource(self.__class__)
# Find and parse `propagate()` types to format `{arg1: type1, ...}`.
if hasattr(self, 'propagate_type'):
prop_types = {
k: sanitize(str(v))
for k, v in self.propagate_type.items()
}
else:
match = re.search(r'#\s*propagate_type:\s*\((.*)\)', source)
if match is None:
raise TypeError(
'TorchScript support requires the definition of the types '
'passed to `propagate()`. Please specify them via\n\n'
'propagate_type = {"arg1": type1, "arg2": type2, ... }\n\n'
'or via\n\n'
'# propagate_type: (arg1: type1, arg2: type2, ...)\n\n'
'inside the `MessagePassing` module.')
prop_types = split_types_repr(match.group(1))
prop_types = dict([re.split(r'\s*:\s*', t) for t in prop_types])
# Find and parse `edge_updater` types to format `{arg1: type1, ...}`.
if 'edge_update' in self.__class__.__dict__.keys():
if hasattr(self, 'edge_updater_type'):
edge_updater_types = {
k: sanitize(str(v))
for k, v in self.edge_updater.items()
}
else:
match = re.search(r'#\s*edge_updater_type:\s*\((.*)\)', source)
if match is None:
raise TypeError(
'TorchScript support requires the definition of the '
'types passed to `edge_updater()`. Please specify '
'them via\n\n edge_updater_type = {"arg1": type1, '
'"arg2": type2, ... }\n\n or via\n\n'
'# edge_updater_type: (arg1: type1, arg2: type2, ...)'
'\n\ninside the `MessagePassing` module.')
edge_updater_types = split_types_repr(match.group(1))
edge_updater_types = dict(
[re.split(r'\s*:\s*', t) for t in edge_updater_types])
else:
edge_updater_types = {}
type_hints = get_type_hints(self.__class__.update)
prop_return_type = type_hints.get('return', 'Tensor')
if str(prop_return_type)[:6] == '<class':
prop_return_type = prop_return_type.__name__
type_hints = get_type_hints(self.__class__.edge_update)
edge_updater_return_type = type_hints.get('return', 'Tensor')
if str(edge_updater_return_type)[:6] == '<class':
edge_updater_return_type = edge_updater_return_type.__name__
# Parse `_collect()` types to format `{arg:1, type1, ...}`.
collect_types = self.inspector.types(
['message', 'aggregate', 'update'])
# Parse `_collect()` types to format `{arg:1, type1, ...}`,
# specific to the argument used for edge updates.
edge_collect_types = self.inspector.types(['edge_update'])
# Collect `forward()` header, body and @overload types.
forward_types = parse_types(self.forward)
forward_types = [resolve_types(*types) for types in forward_types]
forward_types = list(chain.from_iterable(forward_types))
keep_annotation = len(forward_types) < 2
forward_header = func_header_repr(self.forward, keep_annotation)
forward_body = func_body_repr(self.forward, keep_annotation)
if keep_annotation:
forward_types = []
elif typing is not None:
forward_types = []
forward_body = 8 * ' ' + f'# type: {typing}\n{forward_body}'
root = os.path.dirname(osp.realpath(__file__))
with open(osp.join(root, 'message_passing.jinja'), 'r') as f:
template = Template(f.read())
uid = '%06x' % random.randrange(16**6)
cls_name = f'{self.__class__.__name__}Jittable_{uid}'
jit_module_repr = template.render(
uid=uid,
module=str(self.__class__.__module__),
cls_name=cls_name,
parent_cls_name=self.__class__.__name__,
prop_types=prop_types,
prop_return_type=prop_return_type,
fuse=self.fuse,
collect_types=collect_types,
user_args=self._user_args,
edge_user_args=self._edge_user_args,
forward_header=forward_header,
forward_types=forward_types,
forward_body=forward_body,
msg_args=self.inspector.keys(['message']),
aggr_args=self.inspector.keys(['aggregate']),
msg_and_aggr_args=self.inspector.keys(['message_and_aggregate']),
update_args=self.inspector.keys(['update']),
edge_collect_types=edge_collect_types,
edge_update_args=self.inspector.keys(['edge_update']),
edge_updater_types=edge_updater_types,
edge_updater_return_type=edge_updater_return_type,
check_input=inspect.getsource(self._check_input)[:-1],
)
# Instantiate a class from the rendered JIT module representation.
cls = class_from_module_repr(cls_name, jit_module_repr)
module = cls.__new__(cls)
module.__dict__ = self.__dict__.copy()
module.jittable = None
return module
def __repr__(self) -> str:
if hasattr(self, 'in_channels') and hasattr(self, 'out_channels'):
return (f'{self.__class__.__name__}({self.in_channels}, '
f'{self.out_channels})')
return f'{self.__class__.__name__}()'