pyg-nightly 2.6.0.dev20240511__py3-none-any.whl → 2.7.0.dev20250114__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. {pyg_nightly-2.6.0.dev20240511.dist-info → pyg_nightly-2.7.0.dev20250114.dist-info}/METADATA +30 -31
  2. {pyg_nightly-2.6.0.dev20240511.dist-info → pyg_nightly-2.7.0.dev20250114.dist-info}/RECORD +205 -181
  3. {pyg_nightly-2.6.0.dev20240511.dist-info → pyg_nightly-2.7.0.dev20250114.dist-info}/WHEEL +1 -1
  4. torch_geometric/__init__.py +26 -1
  5. torch_geometric/_compile.py +8 -1
  6. torch_geometric/_onnx.py +14 -0
  7. torch_geometric/config_mixin.py +113 -0
  8. torch_geometric/config_store.py +16 -14
  9. torch_geometric/data/__init__.py +24 -1
  10. torch_geometric/data/batch.py +2 -2
  11. torch_geometric/data/data.py +13 -8
  12. torch_geometric/data/database.py +15 -7
  13. torch_geometric/data/dataset.py +14 -6
  14. torch_geometric/data/feature_store.py +13 -22
  15. torch_geometric/data/graph_store.py +0 -4
  16. torch_geometric/data/hetero_data.py +4 -4
  17. torch_geometric/data/in_memory_dataset.py +2 -4
  18. torch_geometric/data/large_graph_indexer.py +677 -0
  19. torch_geometric/data/lightning/datamodule.py +4 -4
  20. torch_geometric/data/storage.py +15 -5
  21. torch_geometric/data/summary.py +14 -4
  22. torch_geometric/data/temporal.py +1 -2
  23. torch_geometric/datasets/__init__.py +11 -1
  24. torch_geometric/datasets/actor.py +9 -11
  25. torch_geometric/datasets/airfrans.py +15 -18
  26. torch_geometric/datasets/airports.py +10 -12
  27. torch_geometric/datasets/amazon.py +8 -11
  28. torch_geometric/datasets/amazon_book.py +9 -10
  29. torch_geometric/datasets/amazon_products.py +9 -10
  30. torch_geometric/datasets/aminer.py +8 -9
  31. torch_geometric/datasets/aqsol.py +10 -13
  32. torch_geometric/datasets/attributed_graph_dataset.py +10 -12
  33. torch_geometric/datasets/ba_multi_shapes.py +10 -12
  34. torch_geometric/datasets/ba_shapes.py +5 -6
  35. torch_geometric/datasets/bitcoin_otc.py +1 -1
  36. torch_geometric/datasets/brca_tgca.py +1 -1
  37. torch_geometric/datasets/dblp.py +2 -1
  38. torch_geometric/datasets/dbp15k.py +2 -2
  39. torch_geometric/datasets/fake.py +1 -3
  40. torch_geometric/datasets/flickr.py +2 -1
  41. torch_geometric/datasets/freebase.py +1 -1
  42. torch_geometric/datasets/gdelt_lite.py +3 -2
  43. torch_geometric/datasets/ged_dataset.py +3 -2
  44. torch_geometric/datasets/git_mol_dataset.py +263 -0
  45. torch_geometric/datasets/gnn_benchmark_dataset.py +6 -5
  46. torch_geometric/datasets/hgb_dataset.py +8 -8
  47. torch_geometric/datasets/imdb.py +2 -1
  48. torch_geometric/datasets/last_fm.py +2 -1
  49. torch_geometric/datasets/linkx_dataset.py +4 -3
  50. torch_geometric/datasets/lrgb.py +3 -5
  51. torch_geometric/datasets/malnet_tiny.py +4 -3
  52. torch_geometric/datasets/mnist_superpixels.py +2 -3
  53. torch_geometric/datasets/molecule_gpt_dataset.py +485 -0
  54. torch_geometric/datasets/molecule_net.py +7 -1
  55. torch_geometric/datasets/motif_generator/base.py +0 -1
  56. torch_geometric/datasets/neurograph.py +1 -3
  57. torch_geometric/datasets/ogb_mag.py +1 -1
  58. torch_geometric/datasets/opf.py +239 -0
  59. torch_geometric/datasets/ose_gvcs.py +1 -1
  60. torch_geometric/datasets/pascal_pf.py +1 -1
  61. torch_geometric/datasets/pcpnet_dataset.py +1 -1
  62. torch_geometric/datasets/pcqm4m.py +2 -1
  63. torch_geometric/datasets/ppi.py +1 -1
  64. torch_geometric/datasets/qm9.py +4 -3
  65. torch_geometric/datasets/reddit.py +2 -1
  66. torch_geometric/datasets/reddit2.py +2 -1
  67. torch_geometric/datasets/rel_link_pred_dataset.py +3 -3
  68. torch_geometric/datasets/s3dis.py +2 -2
  69. torch_geometric/datasets/shapenet.py +3 -3
  70. torch_geometric/datasets/shrec2016.py +2 -2
  71. torch_geometric/datasets/tag_dataset.py +350 -0
  72. torch_geometric/datasets/upfd.py +2 -1
  73. torch_geometric/datasets/web_qsp_dataset.py +246 -0
  74. torch_geometric/datasets/webkb.py +2 -2
  75. torch_geometric/datasets/wikics.py +1 -1
  76. torch_geometric/datasets/wikidata.py +3 -2
  77. torch_geometric/datasets/wikipedia_network.py +2 -2
  78. torch_geometric/datasets/word_net.py +2 -2
  79. torch_geometric/datasets/yelp.py +2 -1
  80. torch_geometric/datasets/zinc.py +1 -1
  81. torch_geometric/device.py +42 -0
  82. torch_geometric/distributed/local_feature_store.py +3 -2
  83. torch_geometric/distributed/local_graph_store.py +2 -1
  84. torch_geometric/distributed/partition.py +9 -8
  85. torch_geometric/edge_index.py +17 -8
  86. torch_geometric/explain/algorithm/base.py +0 -1
  87. torch_geometric/explain/algorithm/pg_explainer.py +1 -1
  88. torch_geometric/explain/explanation.py +2 -2
  89. torch_geometric/graphgym/checkpoint.py +2 -1
  90. torch_geometric/graphgym/logger.py +4 -4
  91. torch_geometric/graphgym/loss.py +1 -1
  92. torch_geometric/graphgym/utils/agg_runs.py +6 -6
  93. torch_geometric/index.py +20 -7
  94. torch_geometric/inspector.py +6 -2
  95. torch_geometric/io/fs.py +28 -2
  96. torch_geometric/io/npz.py +2 -1
  97. torch_geometric/io/off.py +2 -2
  98. torch_geometric/io/sdf.py +2 -2
  99. torch_geometric/io/tu.py +2 -3
  100. torch_geometric/loader/__init__.py +4 -0
  101. torch_geometric/loader/cluster.py +9 -3
  102. torch_geometric/loader/graph_saint.py +2 -1
  103. torch_geometric/loader/ibmb_loader.py +12 -4
  104. torch_geometric/loader/mixin.py +1 -1
  105. torch_geometric/loader/neighbor_loader.py +1 -1
  106. torch_geometric/loader/neighbor_sampler.py +2 -2
  107. torch_geometric/loader/prefetch.py +1 -1
  108. torch_geometric/loader/rag_loader.py +107 -0
  109. torch_geometric/loader/zip_loader.py +10 -0
  110. torch_geometric/metrics/__init__.py +11 -2
  111. torch_geometric/metrics/link_pred.py +159 -34
  112. torch_geometric/nn/aggr/__init__.py +2 -0
  113. torch_geometric/nn/aggr/attention.py +0 -2
  114. torch_geometric/nn/aggr/base.py +2 -4
  115. torch_geometric/nn/aggr/patch_transformer.py +143 -0
  116. torch_geometric/nn/aggr/set_transformer.py +1 -1
  117. torch_geometric/nn/attention/__init__.py +5 -1
  118. torch_geometric/nn/attention/qformer.py +71 -0
  119. torch_geometric/nn/conv/collect.jinja +6 -3
  120. torch_geometric/nn/conv/cugraph/base.py +0 -1
  121. torch_geometric/nn/conv/edge_conv.py +3 -2
  122. torch_geometric/nn/conv/gat_conv.py +35 -7
  123. torch_geometric/nn/conv/gatv2_conv.py +36 -6
  124. torch_geometric/nn/conv/general_conv.py +1 -1
  125. torch_geometric/nn/conv/gravnet_conv.py +3 -2
  126. torch_geometric/nn/conv/hetero_conv.py +3 -3
  127. torch_geometric/nn/conv/hgt_conv.py +1 -1
  128. torch_geometric/nn/conv/message_passing.py +100 -82
  129. torch_geometric/nn/conv/mixhop_conv.py +1 -1
  130. torch_geometric/nn/conv/rgcn_conv.py +2 -1
  131. torch_geometric/nn/conv/spline_conv.py +4 -4
  132. torch_geometric/nn/conv/x_conv.py +3 -2
  133. torch_geometric/nn/dense/linear.py +5 -4
  134. torch_geometric/nn/fx.py +3 -3
  135. torch_geometric/nn/model_hub.py +3 -1
  136. torch_geometric/nn/models/__init__.py +10 -2
  137. torch_geometric/nn/models/deep_graph_infomax.py +1 -2
  138. torch_geometric/nn/models/dimenet_utils.py +5 -7
  139. torch_geometric/nn/models/g_retriever.py +230 -0
  140. torch_geometric/nn/models/git_mol.py +336 -0
  141. torch_geometric/nn/models/glem.py +385 -0
  142. torch_geometric/nn/models/gnnff.py +0 -1
  143. torch_geometric/nn/models/graph_unet.py +12 -3
  144. torch_geometric/nn/models/jumping_knowledge.py +63 -4
  145. torch_geometric/nn/models/lightgcn.py +1 -1
  146. torch_geometric/nn/models/metapath2vec.py +3 -4
  147. torch_geometric/nn/models/molecule_gpt.py +222 -0
  148. torch_geometric/nn/models/node2vec.py +1 -2
  149. torch_geometric/nn/models/schnet.py +2 -1
  150. torch_geometric/nn/models/signed_gcn.py +3 -3
  151. torch_geometric/nn/module_dict.py +2 -2
  152. torch_geometric/nn/nlp/__init__.py +9 -0
  153. torch_geometric/nn/nlp/llm.py +322 -0
  154. torch_geometric/nn/nlp/sentence_transformer.py +134 -0
  155. torch_geometric/nn/nlp/vision_transformer.py +33 -0
  156. torch_geometric/nn/norm/batch_norm.py +1 -1
  157. torch_geometric/nn/parameter_dict.py +2 -2
  158. torch_geometric/nn/pool/__init__.py +7 -5
  159. torch_geometric/nn/pool/cluster_pool.py +145 -0
  160. torch_geometric/nn/pool/connect/base.py +0 -1
  161. torch_geometric/nn/pool/edge_pool.py +1 -1
  162. torch_geometric/nn/pool/graclus.py +4 -2
  163. torch_geometric/nn/pool/select/base.py +0 -1
  164. torch_geometric/nn/pool/voxel_grid.py +3 -2
  165. torch_geometric/nn/resolver.py +1 -1
  166. torch_geometric/nn/sequential.jinja +10 -23
  167. torch_geometric/nn/sequential.py +203 -77
  168. torch_geometric/nn/summary.py +1 -1
  169. torch_geometric/nn/to_hetero_with_bases_transformer.py +19 -19
  170. torch_geometric/profile/__init__.py +2 -0
  171. torch_geometric/profile/nvtx.py +66 -0
  172. torch_geometric/profile/profiler.py +24 -15
  173. torch_geometric/resolver.py +1 -1
  174. torch_geometric/sampler/base.py +34 -13
  175. torch_geometric/sampler/neighbor_sampler.py +11 -10
  176. torch_geometric/testing/decorators.py +17 -22
  177. torch_geometric/transforms/__init__.py +2 -0
  178. torch_geometric/transforms/add_metapaths.py +4 -4
  179. torch_geometric/transforms/add_positional_encoding.py +1 -1
  180. torch_geometric/transforms/delaunay.py +65 -14
  181. torch_geometric/transforms/face_to_edge.py +32 -3
  182. torch_geometric/transforms/gdc.py +7 -6
  183. torch_geometric/transforms/laplacian_lambda_max.py +2 -2
  184. torch_geometric/transforms/mask.py +5 -1
  185. torch_geometric/transforms/node_property_split.py +1 -2
  186. torch_geometric/transforms/pad.py +7 -6
  187. torch_geometric/transforms/random_link_split.py +1 -1
  188. torch_geometric/transforms/remove_self_loops.py +36 -0
  189. torch_geometric/transforms/svd_feature_reduction.py +1 -1
  190. torch_geometric/transforms/virtual_node.py +2 -1
  191. torch_geometric/typing.py +31 -5
  192. torch_geometric/utils/__init__.py +5 -1
  193. torch_geometric/utils/_negative_sampling.py +1 -1
  194. torch_geometric/utils/_normalize_edge_index.py +46 -0
  195. torch_geometric/utils/_scatter.py +37 -12
  196. torch_geometric/utils/_subgraph.py +4 -0
  197. torch_geometric/utils/_tree_decomposition.py +2 -2
  198. torch_geometric/utils/augmentation.py +1 -1
  199. torch_geometric/utils/convert.py +5 -5
  200. torch_geometric/utils/geodesic.py +24 -22
  201. torch_geometric/utils/hetero.py +1 -1
  202. torch_geometric/utils/map.py +1 -1
  203. torch_geometric/utils/smiles.py +66 -28
  204. torch_geometric/utils/sparse.py +25 -10
  205. torch_geometric/visualization/graph.py +3 -4
@@ -1,4 +1,5 @@
1
1
  import copy
2
+ import os
2
3
  import os.path as osp
3
4
  import sys
4
5
  from dataclasses import dataclass
@@ -11,6 +12,7 @@ from torch import Tensor
11
12
  import torch_geometric.typing
12
13
  from torch_geometric.data import Data
13
14
  from torch_geometric.index import index2ptr, ptr2index
15
+ from torch_geometric.io import fs
14
16
  from torch_geometric.typing import pyg_lib
15
17
  from torch_geometric.utils import index_sort, narrow, select, sort_edge_index
16
18
  from torch_geometric.utils.map import map_index
@@ -43,6 +45,8 @@ class ClusterData(torch.utils.data.Dataset):
43
45
  (default: :obj:`False`)
44
46
  save_dir (str, optional): If set, will save the partitioned data to the
45
47
  :obj:`save_dir` directory for faster re-use. (default: :obj:`None`)
48
+ filename (str, optional): Name of the stored partitioned file.
49
+ (default: :obj:`None`)
46
50
  log (bool, optional): If set to :obj:`False`, will not log any
47
51
  progress. (default: :obj:`True`)
48
52
  keep_inter_cluster_edges (bool, optional): If set to :obj:`True`,
@@ -56,6 +60,7 @@ class ClusterData(torch.utils.data.Dataset):
56
60
  num_parts: int,
57
61
  recursive: bool = False,
58
62
  save_dir: Optional[str] = None,
63
+ filename: Optional[str] = None,
59
64
  log: bool = True,
60
65
  keep_inter_cluster_edges: bool = False,
61
66
  sparse_format: Literal['csr', 'csc'] = 'csr',
@@ -69,11 +74,11 @@ class ClusterData(torch.utils.data.Dataset):
69
74
  self.sparse_format = sparse_format
70
75
 
71
76
  recursive_str = '_recursive' if recursive else ''
72
- filename = f'metis_{num_parts}{recursive_str}.pt'
73
- path = osp.join(save_dir or '', filename)
77
+ root_dir = osp.join(save_dir or '', f'part_{num_parts}{recursive_str}')
78
+ path = osp.join(root_dir, filename or 'metis.pt')
74
79
 
75
80
  if save_dir is not None and osp.exists(path):
76
- self.partition = torch.load(path)
81
+ self.partition = fs.torch_load(path)
77
82
  else:
78
83
  if log: # pragma: no cover
79
84
  print('Computing METIS partitioning...', file=sys.stderr)
@@ -82,6 +87,7 @@ class ClusterData(torch.utils.data.Dataset):
82
87
  self.partition = self._partition(data.edge_index, cluster)
83
88
 
84
89
  if save_dir is not None:
90
+ os.makedirs(root_dir, exist_ok=True)
85
91
  torch.save(self.partition, path)
86
92
 
87
93
  if log: # pragma: no cover
@@ -4,6 +4,7 @@ from typing import Optional
4
4
  import torch
5
5
  from tqdm import tqdm
6
6
 
7
+ from torch_geometric.io import fs
7
8
  from torch_geometric.typing import SparseTensor
8
9
 
9
10
 
@@ -77,7 +78,7 @@ class GraphSAINTSampler(torch.utils.data.DataLoader):
77
78
  if self.sample_coverage > 0:
78
79
  path = osp.join(save_dir or '', self._filename)
79
80
  if save_dir is not None and osp.exists(path): # pragma: no cover
80
- self.node_norm, self.edge_norm = torch.load(path)
81
+ self.node_norm, self.edge_norm = fs.torch_load(path)
81
82
  else:
82
83
  self.node_norm, self.edge_norm = self._compute_norm()
83
84
  if save_dir is not None: # pragma: no cover
@@ -1,9 +1,17 @@
1
1
  import logging
2
2
  import math
3
- from typing import Callable, Iterator, List, NamedTuple, Optional, Tuple, Union
3
+ from typing import (
4
+ Any,
5
+ Callable,
6
+ Iterator,
7
+ List,
8
+ NamedTuple,
9
+ Optional,
10
+ Tuple,
11
+ Union,
12
+ )
4
13
 
5
14
  import numpy as np
6
- import scipy.sparse
7
15
  import torch
8
16
  from torch import Tensor
9
17
  from tqdm import tqdm
@@ -281,7 +289,7 @@ def create_batchwise_out_aux_pairs(
281
289
  return loader
282
290
 
283
291
 
284
- def get_pairs(ppr_mat: scipy.sparse.csr_matrix) -> np.ndarray:
292
+ def get_pairs(ppr_mat: Any) -> np.ndarray:
285
293
  ppr_mat = ppr_mat + ppr_mat.transpose()
286
294
 
287
295
  ppr_mat = ppr_mat.tocoo()
@@ -387,7 +395,7 @@ def topk_ppr_matrix(
387
395
  output_node_indices: Union[np.ndarray, torch.LongTensor],
388
396
  topk: int,
389
397
  normalization='row',
390
- ) -> Tuple[scipy.sparse.csr_matrix, List[np.ndarray]]:
398
+ ) -> Tuple[Any, List[np.ndarray]]:
391
399
  neighbors, weights = get_ppr(edge_index, alpha, eps, output_node_indices,
392
400
  num_nodes)
393
401
 
@@ -56,7 +56,7 @@ def get_numa_nodes_cores() -> Dict[str, Any]:
56
56
  nodes[numa_node_id] = sorted([(k, sorted(v))
57
57
  for k, v in thread_siblings.items()])
58
58
 
59
- except (OSError, ValueError, IndexError, IOError):
59
+ except (OSError, ValueError, IndexError):
60
60
  Warning('Failed to read NUMA info')
61
61
  return {}
62
62
 
@@ -14,7 +14,7 @@ class NeighborLoader(NodeLoader):
14
14
  This loader allows for mini-batch training of GNNs on large-scale graphs
15
15
  where full-batch training is not feasible.
16
16
 
17
- More specifically, :obj:`num_neighbors` denotes how much neighbors are
17
+ More specifically, :obj:`num_neighbors` denotes how many neighbors are
18
18
  sampled for each node in each iteration.
19
19
  :class:`~torch_geometric.loader.NeighborLoader` takes in this list of
20
20
  :obj:`num_neighbors` and iteratively samples :obj:`num_neighbors[i]` for
@@ -72,9 +72,9 @@ class NeighborSampler(torch.utils.data.DataLoader):
72
72
  `examples/reddit.py
73
73
  <https://github.com/pyg-team/pytorch_geometric/blob/master/examples/
74
74
  reddit.py>`_ or
75
- `examples/ogbn_products_sage.py
75
+ `examples/ogbn_train.py
76
76
  <https://github.com/pyg-team/pytorch_geometric/blob/master/examples/
77
- ogbn_products_sage.py>`_.
77
+ ogbn_train.py>`_.
78
78
 
79
79
  Args:
80
80
  edge_index (Tensor or SparseTensor): A :obj:`torch.LongTensor` or a
@@ -73,7 +73,7 @@ class PrefetchLoader:
73
73
  if isinstance(batch, dict):
74
74
  return {k: self.non_blocking_transfer(v) for k, v in batch.items()}
75
75
 
76
- batch = batch.pin_memory(self.device_helper.device)
76
+ batch = batch.pin_memory()
77
77
  return batch.to(self.device_helper.device, non_blocking=True)
78
78
 
79
79
  def __iter__(self) -> Any:
@@ -0,0 +1,107 @@
1
+ from abc import abstractmethod
2
+ from typing import Any, Callable, Dict, Optional, Protocol, Tuple, Union
3
+
4
+ from torch_geometric.data import Data, FeatureStore, HeteroData
5
+ from torch_geometric.sampler import HeteroSamplerOutput, SamplerOutput
6
+ from torch_geometric.typing import InputEdges, InputNodes
7
+
8
+
9
+ class RAGFeatureStore(Protocol):
10
+ """Feature store template for remote GNN RAG backend."""
11
+ @abstractmethod
12
+ def retrieve_seed_nodes(self, query: Any, **kwargs) -> InputNodes:
13
+ """Makes a comparison between the query and all the nodes to get all
14
+ the closest nodes. Return the indices of the nodes that are to be seeds
15
+ for the RAG Sampler.
16
+ """
17
+ ...
18
+
19
+ @abstractmethod
20
+ def retrieve_seed_edges(self, query: Any, **kwargs) -> InputEdges:
21
+ """Makes a comparison between the query and all the edges to get all
22
+ the closest nodes. Returns the edge indices that are to be the seeds
23
+ for the RAG Sampler.
24
+ """
25
+ ...
26
+
27
+ @abstractmethod
28
+ def load_subgraph(
29
+ self, sample: Union[SamplerOutput, HeteroSamplerOutput]
30
+ ) -> Union[Data, HeteroData]:
31
+ """Combines sampled subgraph output with features in a Data object."""
32
+ ...
33
+
34
+
35
+ class RAGGraphStore(Protocol):
36
+ """Graph store template for remote GNN RAG backend."""
37
+ @abstractmethod
38
+ def sample_subgraph(self, seed_nodes: InputNodes, seed_edges: InputEdges,
39
+ **kwargs) -> Union[SamplerOutput, HeteroSamplerOutput]:
40
+ """Sample a subgraph using the seeded nodes and edges."""
41
+ ...
42
+
43
+ @abstractmethod
44
+ def register_feature_store(self, feature_store: FeatureStore):
45
+ """Register a feature store to be used with the sampler. Samplers need
46
+ info from the feature store in order to work properly on HeteroGraphs.
47
+ """
48
+ ...
49
+
50
+
51
+ # TODO: Make compatible with Heterographs
52
+
53
+
54
+ class RAGQueryLoader:
55
+ """Loader meant for making RAG queries from a remote backend."""
56
+ def __init__(self, data: Tuple[RAGFeatureStore, RAGGraphStore],
57
+ local_filter: Optional[Callable[[Data, Any], Data]] = None,
58
+ seed_nodes_kwargs: Optional[Dict[str, Any]] = None,
59
+ seed_edges_kwargs: Optional[Dict[str, Any]] = None,
60
+ sampler_kwargs: Optional[Dict[str, Any]] = None,
61
+ loader_kwargs: Optional[Dict[str, Any]] = None):
62
+ """Loader meant for making queries from a remote backend.
63
+
64
+ Args:
65
+ data (Tuple[RAGFeatureStore, RAGGraphStore]): Remote FeatureStore
66
+ and GraphStore to load from. Assumed to conform to the
67
+ protocols listed above.
68
+ local_filter (Optional[Callable[[Data, Any], Data]], optional):
69
+ Optional local transform to apply to data after retrieval.
70
+ Defaults to None.
71
+ seed_nodes_kwargs (Optional[Dict[str, Any]], optional): Paramaters
72
+ to pass into process for fetching seed nodes. Defaults to None.
73
+ seed_edges_kwargs (Optional[Dict[str, Any]], optional): Parameters
74
+ to pass into process for fetching seed edges. Defaults to None.
75
+ sampler_kwargs (Optional[Dict[str, Any]], optional): Parameters to
76
+ pass into process for sampling graph. Defaults to None.
77
+ loader_kwargs (Optional[Dict[str, Any]], optional): Parameters to
78
+ pass into process for loading graph features. Defaults to None.
79
+ """
80
+ fstore, gstore = data
81
+ self.feature_store = fstore
82
+ self.graph_store = gstore
83
+ self.graph_store.register_feature_store(self.feature_store)
84
+ self.local_filter = local_filter
85
+ self.seed_nodes_kwargs = seed_nodes_kwargs or {}
86
+ self.seed_edges_kwargs = seed_edges_kwargs or {}
87
+ self.sampler_kwargs = sampler_kwargs or {}
88
+ self.loader_kwargs = loader_kwargs or {}
89
+
90
+ def query(self, query: Any) -> Data:
91
+ """Retrieve a subgraph associated with the query with all its feature
92
+ attributes.
93
+ """
94
+ seed_nodes = self.feature_store.retrieve_seed_nodes(
95
+ query, **self.seed_nodes_kwargs)
96
+ seed_edges = self.feature_store.retrieve_seed_edges(
97
+ query, **self.seed_edges_kwargs)
98
+
99
+ subgraph_sample = self.graph_store.sample_subgraph(
100
+ seed_nodes, seed_edges, **self.sampler_kwargs)
101
+
102
+ data = self.feature_store.load_subgraph(sample=subgraph_sample,
103
+ **self.loader_kwargs)
104
+
105
+ if self.local_filter:
106
+ data = self.local_filter(data, query)
107
+ return data
@@ -59,6 +59,16 @@ class ZipLoader(torch.utils.data.DataLoader):
59
59
  self.loaders = loaders
60
60
  self.filter_per_worker = filter_per_worker
61
61
 
62
+ def __call__(
63
+ self,
64
+ index: Union[Tensor, List[int]],
65
+ ) -> Union[Tuple[Data, ...], Tuple[HeteroData, ...]]:
66
+ r"""Samples subgraphs from a batch of input IDs."""
67
+ out = self.collate_fn(index)
68
+ if not self.filter_per_worker:
69
+ out = self.filter_fn(out)
70
+ return out
71
+
62
72
  def collate_fn(self, index: List[int]) -> Tuple[Any, ...]:
63
73
  if not isinstance(index, Tensor):
64
74
  index = torch.tensor(index, dtype=torch.long)
@@ -1,14 +1,23 @@
1
1
  # flake8: noqa
2
2
 
3
- from .link_pred import (LinkPredPrecision, LinkPredRecall, LinkPredF1,
4
- LinkPredMAP, LinkPredNDCG)
3
+ from .link_pred import (
4
+ LinkPredMetricCollection,
5
+ LinkPredPrecision,
6
+ LinkPredRecall,
7
+ LinkPredF1,
8
+ LinkPredMAP,
9
+ LinkPredNDCG,
10
+ LinkPredMRR,
11
+ )
5
12
 
6
13
  link_pred_metrics = [
14
+ 'LinkPredMetricCollection',
7
15
  'LinkPredPrecision',
8
16
  'LinkPredRecall',
9
17
  'LinkPredF1',
10
18
  'LinkPredMAP',
11
19
  'LinkPredNDCG',
20
+ 'LinkPredMRR',
12
21
  ]
13
22
 
14
23
  __all__ = link_pred_metrics
@@ -1,4 +1,4 @@
1
- from typing import Optional, Tuple, Union
1
+ from typing import Dict, List, Optional, Tuple, Union
2
2
 
3
3
  import torch
4
4
  from torch import Tensor
@@ -43,34 +43,15 @@ class LinkPredMetric(BaseMetric):
43
43
  self.register_buffer('accum', torch.tensor(0.))
44
44
  self.register_buffer('total', torch.tensor(0))
45
45
 
46
- def update(
47
- self,
46
+ @staticmethod
47
+ def _prepare(
48
48
  pred_index_mat: Tensor,
49
49
  edge_label_index: Union[Tensor, Tuple[Tensor, Tensor]],
50
- ) -> None:
51
- r"""Updates the state variables based on the current mini-batch
52
- prediction.
53
-
54
- :meth:`update` can be repeated multiple times to accumulate the results
55
- of successive predictions, *e.g.*, inside a mini-batch training or
56
- evaluation loop.
57
-
58
- Args:
59
- pred_index_mat (torch.Tensor): The top-:math:`k` predictions of
60
- every example in the mini-batch with shape
61
- :obj:`[batch_size, k]`.
62
- edge_label_index (torch.Tensor): The ground-truth indices for every
63
- example in the mini-batch, given in COO format of shape
64
- :obj:`[2, num_ground_truth_indices]`.
65
- """
66
- if pred_index_mat.size(1) != self.k:
67
- raise ValueError(f"Expected 'pred_index_mat' to hold {self.k} "
68
- f"many indices for every entry "
69
- f"(got {pred_index_mat.size(1)})")
70
-
71
- # Compute a boolean matrix indicating if the k-th prediction is part of
72
- # the ground-truth. We do this by flattening both prediction and
73
- # target indices, and then determining overlaps via `torch.isin`.
50
+ ) -> Tuple[Tensor, Tensor]:
51
+ # Compute a boolean matrix indicating if the `k`-th prediction is part
52
+ # of the ground-truth, as well as the number of ground-truths for every
53
+ # example. We do this by flattening both prediction and ground-truth
54
+ # indices, and then determining overlaps via `torch.isin`.
74
55
  max_index = max( # type: ignore
75
56
  pred_index_mat.max() if pred_index_mat.numel() > 0 else 0,
76
57
  edge_label_index[1].max()
@@ -78,8 +59,8 @@ class LinkPredMetric(BaseMetric):
78
59
  ) + 1
79
60
  arange = torch.arange(
80
61
  start=0,
81
- end=max_index * pred_index_mat.size(0),
82
- step=max_index,
62
+ end=max_index * pred_index_mat.size(0), # type: ignore
63
+ step=max_index, # type: ignore
83
64
  device=pred_index_mat.device,
84
65
  ).view(-1, 1)
85
66
  flat_pred_index = (pred_index_mat + arange).view(-1)
@@ -88,7 +69,7 @@ class LinkPredMetric(BaseMetric):
88
69
  pred_isin_mat = torch.isin(flat_pred_index, flat_y_index)
89
70
  pred_isin_mat = pred_isin_mat.view(pred_index_mat.size())
90
71
 
91
- # Compute the number of targets per example:
72
+ # Compute the number of ground-truths per example:
92
73
  y_count = scatter(
93
74
  torch.ones_like(edge_label_index[0]),
94
75
  edge_label_index[0],
@@ -97,11 +78,41 @@ class LinkPredMetric(BaseMetric):
97
78
  reduce='sum',
98
79
  )
99
80
 
100
- metric = self._compute(pred_isin_mat, y_count)
81
+ return pred_isin_mat, y_count
101
82
 
83
+ def _update_from_prepared(
84
+ self,
85
+ pred_isin_mat: Tensor,
86
+ y_count: Tensor,
87
+ ) -> None:
88
+ metric = self._compute(pred_isin_mat[:, :self.k], y_count)
102
89
  self.accum += metric.sum()
103
90
  self.total += (y_count > 0).sum()
104
91
 
92
+ def update(
93
+ self,
94
+ pred_index_mat: Tensor,
95
+ edge_label_index: Union[Tensor, Tuple[Tensor, Tensor]],
96
+ ) -> None:
97
+ r"""Updates the state variables based on the current mini-batch
98
+ prediction.
99
+
100
+ :meth:`update` can be repeated multiple times to accumulate the results
101
+ of successive predictions, *e.g.*, inside a mini-batch training or
102
+ evaluation loop.
103
+
104
+ Args:
105
+ pred_index_mat (torch.Tensor): The top-:math:`k` predictions of
106
+ every example in the mini-batch with shape
107
+ :obj:`[batch_size, k]`.
108
+ edge_label_index (torch.Tensor): The ground-truth indices for every
109
+ example in the mini-batch, given in COO format of shape
110
+ :obj:`[2, num_ground_truth_indices]`.
111
+ """
112
+ pred_isin_mat, y_count = self._prepare(pred_index_mat,
113
+ edge_label_index)
114
+ self._update_from_prepared(pred_isin_mat, y_count)
115
+
105
116
  def compute(self) -> Tensor:
106
117
  r"""Computes the final metric value."""
107
118
  if self.total == 0:
@@ -133,6 +144,103 @@ class LinkPredMetric(BaseMetric):
133
144
  return f'{self.__class__.__name__}(k={self.k})'
134
145
 
135
146
 
147
+ class LinkPredMetricCollection(torch.nn.ModuleDict):
148
+ r"""A collection of metrics to reduce and speed-up computation of link
149
+ prediction metrics.
150
+
151
+ .. code-block:: python
152
+
153
+ from torch_geometric.metrics import (
154
+ LinkPredMAP,
155
+ LinkPredMetricCollection,
156
+ LinkPredPrecision,
157
+ LinkPredRecall,
158
+ )
159
+
160
+ metrics = LinkPredMetricCollection([
161
+ LinkPredMAP(k=10),
162
+ LinkPredPrecision(k=100),
163
+ LinkPredRecall(k=50),
164
+ ])
165
+
166
+ metrics.update(pred_index_mat, edge_label_index)
167
+ out = metrics.compute()
168
+ metrics.reset()
169
+
170
+ print(out)
171
+ >>> {'LinkPredMAP@10': tensor(0.375),
172
+ ... 'LinkPredPrecision@100': tensor(0.127),
173
+ ... 'LinkPredRecall@50': tensor(0.483)}
174
+
175
+ Args:
176
+ metrics: The link prediction metrics.
177
+ """
178
+ def __init__(
179
+ self,
180
+ metrics: Union[
181
+ List[LinkPredMetric],
182
+ Dict[str, LinkPredMetric],
183
+ ],
184
+ ) -> None:
185
+ super().__init__()
186
+
187
+ if isinstance(metrics, (list, tuple)):
188
+ metrics = {
189
+ f'{metric.__class__.__name__}@{metric.k}': metric
190
+ for metric in metrics
191
+ }
192
+ assert len(metrics) > 0
193
+ assert isinstance(metrics, dict)
194
+
195
+ for name, metric in metrics.items():
196
+ self[name] = metric
197
+
198
+ @property
199
+ def max_k(self) -> int:
200
+ r"""The maximum number of top-:math:`k` predictions to evaluate
201
+ against.
202
+ """
203
+ return max([metric.k for metric in self.values()])
204
+
205
+ def update( # type: ignore
206
+ self,
207
+ pred_index_mat: Tensor,
208
+ edge_label_index: Union[Tensor, Tuple[Tensor, Tensor]],
209
+ ) -> None:
210
+ r"""Updates the state variables based on the current mini-batch
211
+ prediction.
212
+
213
+ :meth:`update` can be repeated multiple times to accumulate the results
214
+ of successive predictions, *e.g.*, inside a mini-batch training or
215
+ evaluation loop.
216
+
217
+ Args:
218
+ pred_index_mat (torch.Tensor): The top-:math:`k` predictions of
219
+ every example in the mini-batch with shape
220
+ :obj:`[batch_size, k]`.
221
+ edge_label_index (torch.Tensor): The ground-truth indices for every
222
+ example in the mini-batch, given in COO format of shape
223
+ :obj:`[2, num_ground_truth_indices]`.
224
+ """
225
+ pred_isin_mat, y_count = LinkPredMetric._prepare(
226
+ pred_index_mat, edge_label_index)
227
+ for metric in self.values():
228
+ metric._update_from_prepared(pred_isin_mat, y_count)
229
+
230
+ def compute(self) -> Dict[str, Tensor]:
231
+ r"""Computes the final metric values."""
232
+ return {name: metric.compute() for name, metric in self.items()}
233
+
234
+ def reset(self) -> None:
235
+ r"""Reset metric state variables to their default value."""
236
+ for metric in self.values():
237
+ metric.reset()
238
+
239
+ def __repr__(self) -> str:
240
+ names = [f' {name}: {metric},\n' for name, metric in self.items()]
241
+ return f'{self.__class__.__name__}([\n{"".join(names)}])'
242
+
243
+
136
244
  class LinkPredPrecision(LinkPredMetric):
137
245
  r"""A link prediction metric to compute Precision @ :math:`k`.
138
246
 
@@ -182,8 +290,9 @@ class LinkPredMAP(LinkPredMetric):
182
290
  higher_is_better: bool = True
183
291
 
184
292
  def _compute(self, pred_isin_mat: Tensor, y_count: Tensor) -> Tensor:
185
- cum_precision = (torch.cumsum(pred_isin_mat, dim=1) /
186
- torch.arange(1, self.k + 1, device=y_count.device))
293
+ device = pred_isin_mat.device
294
+ arange = torch.arange(1, pred_isin_mat.size(1) + 1, device=device)
295
+ cum_precision = pred_isin_mat.cumsum(dim=1) / arange
187
296
  return ((cum_precision * pred_isin_mat).sum(dim=-1) /
188
297
  y_count.clamp(min=1e-7, max=self.k))
189
298
 
@@ -210,9 +319,25 @@ class LinkPredNDCG(LinkPredMetric):
210
319
  self.register_buffer('idcg', cumsum(multiplier))
211
320
 
212
321
  def _compute(self, pred_isin_mat: Tensor, y_count: Tensor) -> Tensor:
213
- dcg = (pred_isin_mat * self.multiplier.view(1, -1)).sum(dim=-1)
322
+ multiplier = self.multiplier[:pred_isin_mat.size(1)].view(1, -1)
323
+ dcg = (pred_isin_mat * multiplier).sum(dim=-1)
214
324
  idcg = self.idcg[y_count.clamp(max=self.k)]
215
325
 
216
326
  out = dcg / idcg
217
327
  out[out.isnan() | out.isinf()] = 0.0
218
328
  return out
329
+
330
+
331
+ class LinkPredMRR(LinkPredMetric):
332
+ r"""A link prediction metric to compute the MRR @ :math:`k` (Mean
333
+ Reciprocal Rank).
334
+
335
+ Args:
336
+ k (int): The number of top-:math:`k` predictions to evaluate against.
337
+ """
338
+ higher_is_better: bool = True
339
+
340
+ def _compute(self, pred_isin_mat: Tensor, y_count: Tensor) -> Tensor:
341
+ device = pred_isin_mat.device
342
+ arange = torch.arange(1, pred_isin_mat.size(1) + 1, device=device)
343
+ return (pred_isin_mat / arange).max(dim=-1)[0]
@@ -25,6 +25,7 @@ from .deep_sets import DeepSetsAggregation
25
25
  from .set_transformer import SetTransformerAggregation
26
26
  from .lcm import LCMAggregation
27
27
  from .variance_preserving import VariancePreservingAggregation
28
+ from .patch_transformer import PatchTransformerAggregation
28
29
 
29
30
  __all__ = classes = [
30
31
  'Aggregation',
@@ -53,4 +54,5 @@ __all__ = classes = [
53
54
  'SetTransformerAggregation',
54
55
  'LCMAggregation',
55
56
  'VariancePreservingAggregation',
57
+ 'PatchTransformerAggregation',
56
58
  ]
@@ -65,8 +65,6 @@ class AttentionalAggregation(Aggregation):
65
65
  ptr: Optional[Tensor] = None, dim_size: Optional[int] = None,
66
66
  dim: int = -2) -> Tensor:
67
67
 
68
- self.assert_two_dimensional_input(x, dim)
69
-
70
68
  if self.gate_mlp is not None:
71
69
  gate = self.gate_mlp(x, batch=index, batch_size=dim_size)
72
70
  else:
@@ -25,7 +25,7 @@ class Aggregation(torch.nn.Module):
25
25
  Notably, :obj:`index` does not have to be sorted (for most aggregation
26
26
  operators):
27
27
 
28
- .. code-block::
28
+ .. code-block:: python
29
29
 
30
30
  # Feature matrix holding 10 elements with 64 features each:
31
31
  x = torch.randn(10, 64)
@@ -39,7 +39,7 @@ class Aggregation(torch.nn.Module):
39
39
  called :obj:`ptr`. Here, elements within the same set need to be grouped
40
40
  together in the input, and :obj:`ptr` defines their boundaries:
41
41
 
42
- .. code-block::
42
+ .. code-block:: python
43
43
 
44
44
  # Feature matrix holding 10 elements with 64 features each:
45
45
  x = torch.randn(10, 64)
@@ -94,11 +94,9 @@ class Aggregation(torch.nn.Module):
94
94
  max_num_elements: (int, optional): The maximum number of elements
95
95
  within a single aggregation group. (default: :obj:`None`)
96
96
  """
97
- pass
98
97
 
99
98
  def reset_parameters(self):
100
99
  r"""Resets all learnable parameters of the module."""
101
- pass
102
100
 
103
101
  @disable_dynamic_shapes(required_args=['dim_size'])
104
102
  def __call__(