mlx-graphs 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. mlx-graphs-0.0.1/LICENSE +21 -0
  2. mlx-graphs-0.0.1/PKG-INFO +120 -0
  3. mlx-graphs-0.0.1/README.md +67 -0
  4. mlx-graphs-0.0.1/mlx_graphs/__init__.py +0 -0
  5. mlx-graphs-0.0.1/mlx_graphs/data/__init__.py +6 -0
  6. mlx-graphs-0.0.1/mlx_graphs/data/batch.py +201 -0
  7. mlx-graphs-0.0.1/mlx_graphs/data/collate.py +62 -0
  8. mlx-graphs-0.0.1/mlx_graphs/data/data.py +117 -0
  9. mlx-graphs-0.0.1/mlx_graphs/nn/__init__.py +5 -0
  10. mlx-graphs-0.0.1/mlx_graphs/nn/conv/__init__.py +4 -0
  11. mlx-graphs-0.0.1/mlx_graphs/nn/conv/gat_conv.py +174 -0
  12. mlx-graphs-0.0.1/mlx_graphs/nn/conv/gcn_conv.py +63 -0
  13. mlx-graphs-0.0.1/mlx_graphs/nn/conv/gin_conv.py +99 -0
  14. mlx-graphs-0.0.1/mlx_graphs/nn/conv/sage_conv.py +97 -0
  15. mlx-graphs-0.0.1/mlx_graphs/nn/graph_network_block.py +90 -0
  16. mlx-graphs-0.0.1/mlx_graphs/nn/linear.py +24 -0
  17. mlx-graphs-0.0.1/mlx_graphs/nn/message_passing.py +140 -0
  18. mlx-graphs-0.0.1/mlx_graphs/nn/pooling/__init__.py +5 -0
  19. mlx-graphs-0.0.1/mlx_graphs/nn/pooling/global_pooling.py +79 -0
  20. mlx-graphs-0.0.1/mlx_graphs/utils/__init__.py +8 -0
  21. mlx-graphs-0.0.1/mlx_graphs/utils/array_ops.py +47 -0
  22. mlx-graphs-0.0.1/mlx_graphs/utils/scatter.py +208 -0
  23. mlx-graphs-0.0.1/mlx_graphs/utils/sorting.py +46 -0
  24. mlx-graphs-0.0.1/mlx_graphs/utils/topology.py +103 -0
  25. mlx-graphs-0.0.1/mlx_graphs/utils/transformations.py +274 -0
  26. mlx-graphs-0.0.1/mlx_graphs/utils/validators.py +92 -0
  27. mlx-graphs-0.0.1/mlx_graphs.egg-info/PKG-INFO +120 -0
  28. mlx-graphs-0.0.1/mlx_graphs.egg-info/SOURCES.txt +31 -0
  29. mlx-graphs-0.0.1/mlx_graphs.egg-info/dependency_links.txt +1 -0
  30. mlx-graphs-0.0.1/mlx_graphs.egg-info/requires.txt +22 -0
  31. mlx-graphs-0.0.1/mlx_graphs.egg-info/top_level.txt +1 -0
  32. mlx-graphs-0.0.1/pyproject.toml +46 -0
  33. mlx-graphs-0.0.1/setup.cfg +4 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright © 2024 mlx-graphs contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,120 @@
1
+ Metadata-Version: 2.1
2
+ Name: mlx-graphs
3
+ Version: 0.0.1
4
+ Summary: Graph Neural Network library made for Apple Silicon
5
+ Author: mlx-graphs contributors
6
+ License: MIT License
7
+
8
+ Copyright © 2024 mlx-graphs contributors.
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Repository, https://github.com/TristanBilot/mlx-graphs
29
+ Project-URL: Issues, https://github.com/TristanBilot/mlx-graphs/issues
30
+ Classifier: Development Status :: 3 - Alpha
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Operating System :: MacOS
33
+ Classifier: Programming Language :: Python
34
+ Requires-Python: >=3.8
35
+ Description-Content-Type: text/markdown
36
+ License-File: LICENSE
37
+ Requires-Dist: mlx==0.0.11.*; sys_platform == "darwin"
38
+ Requires-Dist: numpy==1.26.3
39
+ Provides-Extra: dev
40
+ Requires-Dist: pre-commit==3.6.0; extra == "dev"
41
+ Provides-Extra: test
42
+ Requires-Dist: pytest==7.4.4; extra == "test"
43
+ Provides-Extra: docs
44
+ Requires-Dist: sphinx==7.2.6; extra == "docs"
45
+ Requires-Dist: sphinx-book-theme==1.1.0; extra == "docs"
46
+ Requires-Dist: sphinx-autodoc-typehints==1.25.2; extra == "docs"
47
+ Provides-Extra: benchmarks
48
+ Requires-Dist: scipy==1.12.0; extra == "benchmarks"
49
+ Requires-Dist: torch==2.1.2; extra == "benchmarks"
50
+ Requires-Dist: torch_geometric==2.4.0; extra == "benchmarks"
51
+ Requires-Dist: torch_scatter==2.1.2; extra == "benchmarks"
52
+ Requires-Dist: tqdm==4.66.1; extra == "benchmarks"
53
+
54
+ # MLX-graphs
55
+
56
+ [Documentation](https://tristanbilot.github.io/mlx-graphs/)
57
+
58
+ MLX-graphs is a library for Graph Neural Networks (GNNs) built upon Apple's MLX.
59
+
60
+ ## Work in progress 🚧
61
+
62
+ We just started the development of this lib, with the aim to integrate it within [ml-explore](https://github.com/ml-explore).
63
+
64
+ The lib follows the Message Passing Neural Network ([MPNN](https://proceedings.mlr.press/v70/gilmer17a/gilmer17a.pdf)) architecture to build arbitrary GNNs on top of it, similarly as in [PyG](https://github.com/pyg-team/pytorch_geometric).
65
+
66
+ ### Installation (build from source)
67
+ To build and install `mlx-graphs` from source start by cloning the github repo
68
+ ```
69
+ git clone git@github.com:TristanBilot/mlx-graphs.git && cd mlx-graphs
70
+ ```
71
+ Create a new virtual environment and install the requirements
72
+ ```
73
+ pip install -e .
74
+ ```
75
+
76
+ ### Usage
77
+ #### Graph data model
78
+ A graph is defined by a set of (optional) attributes
79
+ 1. `edge_index`: an array of size `[2, num_edges]` which specifies the topology of the graph. The i-th column in `edge_index` defines the source and destination nodes of the i-th edge
80
+ 2. `node_features`: an array of size `[num_nodes, num_node_features]` defining the features associated to each node (if any). The i-th row contains the features of the i-th node
81
+ 3. `edge_features`: an array of size `[num_edges, num_edge_features]` defining the features associated to each edge (if any). The i-th row contains the features of the i-th edge
82
+ 4. `graph_features`: an array of size `[num_graph_features]` defining the features associated to the graph itself
83
+
84
+ We adopt the above convention across the entire library both in terms of shapes of the attributes and the order in which they're provided to functions.
85
+
86
+ ### Contributing
87
+ #### Installing test, dev, benchmaks, docs dependencies
88
+ Extra dependencies are specified in the `pyproject.toml`.
89
+ To install those required for testing, development and building documentation, you can run any of the following
90
+ ```
91
+ pip install -e '.[test]'
92
+ pip install -e '.[dev]'
93
+ pip install -e '.[benchmarks]'
94
+ pip install -e '.[docs]'
95
+ ```
96
+ For dev purposes you may want to install the current version of `mlx` via `pip install mlx @ git+https://github.com/ml-explore/mlx.git`
97
+
98
+ #### Testing
99
+ We encourage to write tests for all components. CI is currently not in place as runners with Apple Silicon are required.
100
+ Please run `pytest` to ensure breaking changes are not introduced.
101
+
102
+
103
+ #### Pre-commit hooks (optional)
104
+ To ensure code quality you can run [pre-commit](https://pre-commit.com) hooks. Simply install them by running
105
+ ```
106
+ pre-commit install
107
+ ```
108
+ and run via `pre-commit run --all-files`.
109
+
110
+ > Note: CI is in place to verify code quality, so pull requests that don't meet those requirements won't pass CI tests.
111
+
112
+
113
+ ## Why running GNNs on my Mac?
114
+
115
+ By leveraging Apple Silicon, running GNNs enjoys accelerated GPU computation ⚡️ and unified memory architecture. This eliminates device data transfers and enables leveraging all available memory to store large graphs directly on your Mac's GPU.
116
+
117
+ ## Why contributing?
118
+
119
+ We are at an early stage of the development of the lib, which means your contributions can have a large impact!
120
+ Everyone is welcome to contribute, just open an issue 📝 with your idea 💡 and we'll work together on the implementation ✨.
@@ -0,0 +1,67 @@
1
+ # MLX-graphs
2
+
3
+ [Documentation](https://tristanbilot.github.io/mlx-graphs/)
4
+
5
+ MLX-graphs is a library for Graph Neural Networks (GNNs) built upon Apple's MLX.
6
+
7
+ ## Work in progress 🚧
8
+
9
+ We just started the development of this lib, with the aim to integrate it within [ml-explore](https://github.com/ml-explore).
10
+
11
+ The lib follows the Message Passing Neural Network ([MPNN](https://proceedings.mlr.press/v70/gilmer17a/gilmer17a.pdf)) architecture to build arbitrary GNNs on top of it, similarly as in [PyG](https://github.com/pyg-team/pytorch_geometric).
12
+
13
+ ### Installation (build from source)
14
+ To build and install `mlx-graphs` from source start by cloning the github repo
15
+ ```
16
+ git clone git@github.com:TristanBilot/mlx-graphs.git && cd mlx-graphs
17
+ ```
18
+ Create a new virtual environment and install the requirements
19
+ ```
20
+ pip install -e .
21
+ ```
22
+
23
+ ### Usage
24
+ #### Graph data model
25
+ A graph is defined by a set of (optional) attributes
26
+ 1. `edge_index`: an array of size `[2, num_edges]` which specifies the topology of the graph. The i-th column in `edge_index` defines the source and destination nodes of the i-th edge
27
+ 2. `node_features`: an array of size `[num_nodes, num_node_features]` defining the features associated to each node (if any). The i-th row contains the features of the i-th node
28
+ 3. `edge_features`: an array of size `[num_edges, num_edge_features]` defining the features associated to each edge (if any). The i-th row contains the features of the i-th edge
29
+ 4. `graph_features`: an array of size `[num_graph_features]` defining the features associated to the graph itself
30
+
31
+ We adopt the above convention across the entire library both in terms of shapes of the attributes and the order in which they're provided to functions.
32
+
33
+ ### Contributing
34
+ #### Installing test, dev, benchmaks, docs dependencies
35
+ Extra dependencies are specified in the `pyproject.toml`.
36
+ To install those required for testing, development and building documentation, you can run any of the following
37
+ ```
38
+ pip install -e '.[test]'
39
+ pip install -e '.[dev]'
40
+ pip install -e '.[benchmarks]'
41
+ pip install -e '.[docs]'
42
+ ```
43
+ For dev purposes you may want to install the current version of `mlx` via `pip install mlx @ git+https://github.com/ml-explore/mlx.git`
44
+
45
+ #### Testing
46
+ We encourage to write tests for all components. CI is currently not in place as runners with Apple Silicon are required.
47
+ Please run `pytest` to ensure breaking changes are not introduced.
48
+
49
+
50
+ #### Pre-commit hooks (optional)
51
+ To ensure code quality you can run [pre-commit](https://pre-commit.com) hooks. Simply install them by running
52
+ ```
53
+ pre-commit install
54
+ ```
55
+ and run via `pre-commit run --all-files`.
56
+
57
+ > Note: CI is in place to verify code quality, so pull requests that don't meet those requirements won't pass CI tests.
58
+
59
+
60
+ ## Why running GNNs on my Mac?
61
+
62
+ By leveraging Apple Silicon, running GNNs enjoys accelerated GPU computation ⚡️ and unified memory architecture. This eliminates device data transfers and enables leveraging all available memory to store large graphs directly on your Mac's GPU.
63
+
64
+ ## Why contributing?
65
+
66
+ We are at an early stage of the development of the lib, which means your contributions can have a large impact!
67
+ Everyone is welcome to contribute, just open an issue 📝 with your idea 💡 and we'll work together on the implementation ✨.
File without changes
@@ -0,0 +1,6 @@
1
+ # from .data import GraphData # noqa
2
+ # from .batch import (GraphDataBatch, batch, unbatch) # noqa
3
+ # from .collate import collate # noqa
4
+
5
+ # TODO: when uncommenting, this generates a circular import error, need tovfix that
6
+ # to import GraphData with from mlx_graphs.data import GraphData
@@ -0,0 +1,201 @@
1
+ from typing import Union
2
+
3
+ import mlx.core as mx
4
+ import numpy as np
5
+
6
+ from mlx_graphs.data.data import GraphData
7
+ from mlx_graphs.data.collate import collate
8
+
9
+
10
+ class GraphDataBatch(GraphData):
11
+ """Concatenates multiple `GraphData` into a single unified `GraphDataBatch`
12
+ for efficient computation and parallelization over multiple graphs.
13
+
14
+ All graphs remain disconnected in the batch, meaning that any pairs of graphs
15
+ have no nodes or edges in common.
16
+ `GraphDataBatch` can be especially used to speed up graph classification tasks, where
17
+ multiple graphs can easily fit into memory and be processed in parallel.
18
+
19
+ Args:
20
+ graphs: List of `GraphData` objects to batch together
21
+
22
+ Example:
23
+
24
+ .. code-block:: python
25
+
26
+ graphs = [
27
+ GraphData(
28
+ edge_index=mx.array([[0, 0, 0], [1, 1, 1]]),
29
+ node_features=mx.zeros((3, 1)),
30
+ ),
31
+ GraphData(
32
+ edge_index=mx.array([[1, 1, 1], [2, 2, 2]]),
33
+ node_features=mx.ones((3, 1)),
34
+ ),
35
+ GraphData(
36
+ edge_index=mx.array([[3, 3, 3], [4, 4, 4]]),
37
+ node_features=mx.ones((3, 1)) * 2,
38
+ )
39
+ ]
40
+ batch = GraphDataBatch(graphs)
41
+
42
+ >>> batch.num_graphs
43
+ 3
44
+
45
+ >>> batch
46
+ GraphDataBatch(
47
+ edge_index=array([[0, 0, 0, 4, 4, 4, 9, 9, 9], [1, 1, 1, 5, 5, 5, 10, 10, 10]], dtype=int32),
48
+ node_features=array([[0.0], [0.0], [0.0], [1.0], [1.0], [1.0], [2.0], [2.0], [2.0]], dtype=float32))
49
+
50
+ >>> batch[1]
51
+ GraphData(
52
+ edge_index=array([[1, 1, 1], [2, 2, 2]], dtype=int32),
53
+ node_features=array([[1], [1], [1]], dtype=float32))
54
+
55
+ >>> batch[1:]
56
+ [
57
+ GraphData(
58
+ edge_index=array([[1, 1, 1], [2, 2, 2]], dtype=int32),
59
+ node_features=array([[1], [1], [1]], dtype=float32)),
60
+ GraphData(
61
+ edge_index=array([[3, 3, 3], [4, 4, 4]], dtype=int32),
62
+ node_features=array([[2], [2], [2]], dtype=float32))
63
+ ]
64
+ """
65
+
66
+ def __init__(self, graphs: list[GraphData], **kwargs) -> None:
67
+ batch_kwargs = collate(graphs)
68
+ super().__init__(_num_graphs=len(graphs), **batch_kwargs, **kwargs)
69
+
70
+ @property
71
+ def num_graphs(self):
72
+ """Number of graphs in the batch."""
73
+ return self._num_graphs # provided via collate
74
+
75
+ @property
76
+ def batch_indices(self):
77
+ """Mask indicating for each node its corresponding batch index."""
78
+ return self._batch_indices # provided via collate
79
+
80
+ def __getitem__(self, idx: Union[int, slice]) -> Union[GraphData, list[GraphData]]:
81
+ if isinstance(idx, int):
82
+ idx = self._handle_neg_index_if_needed(idx)
83
+ return self._get_graph(idx)
84
+
85
+ elif isinstance(idx, slice):
86
+ start, stop, step = (
87
+ idx.start or 0,
88
+ idx.stop or self._num_graphs,
89
+ idx.step or 1,
90
+ )
91
+ start = self._handle_neg_index_if_needed(start)
92
+ stop = self._handle_neg_index_if_needed(stop)
93
+ if stop < start:
94
+ raise IndexError(
95
+ "Batch slicing out of range (stop is greater than start)."
96
+ )
97
+
98
+ return [self._get_graph(i) for i in range(start, stop, step)]
99
+
100
+ elif isinstance(idx, (mx.array, list)):
101
+ if isinstance(idx, list):
102
+ idx = mx.array(idx)
103
+
104
+ if idx.ndim != 1:
105
+ raise ValueError(
106
+ "Batch indexing with mx.array only supports 1D index array."
107
+ )
108
+
109
+ idx = self._handle_neg_index_if_needed(idx)
110
+ return [self._get_graph(i) for i in idx]
111
+
112
+ raise TypeError("GraphDataBatch indices should be int or slice.")
113
+
114
+ def _get_graph(self, idx: int) -> GraphData:
115
+ """Returns a `GraphData` from the batch with its original attributes and edge index."""
116
+ large_graph_dict = {
117
+ attr: getattr(self, attr)
118
+ for attr in self.to_dict()
119
+ if not attr.startswith("_")
120
+ }
121
+
122
+ single_graph_dict = {}
123
+ for attr in large_graph_dict:
124
+ from_idx, upto_idx = self._get_attr_slice_at_index(attr, idx)
125
+ attr_array = getattr(self, attr)
126
+
127
+ # Using mx.take_along_axis required indices with same number of dimensions as the values
128
+ # Singleton dimensions are thus added to all dimensions except the __concat__ dimension
129
+ gather_dim = self.to_dict()[f"_cat_dim_{attr}"]
130
+ broadcast_dim = [
131
+ -1 if dim == gather_dim else 1 for dim in range(attr_array.ndim)
132
+ ]
133
+ range_at_dim = mx.arange(from_idx, upto_idx).reshape(broadcast_dim)
134
+
135
+ # Gather values at the current range on the __concat__ dimension
136
+ original_value = mx.take_along_axis(attr_array, range_at_dim, gather_dim)
137
+
138
+ # If __inc__ is True, we get back the original values by subtracting
139
+ # the cumulative sum at the batch index
140
+ if f"_inc_{attr}" in self.to_dict():
141
+ original_value -= self.to_dict()[f"_cumsum_{attr}"][idx]
142
+
143
+ single_graph_dict[attr] = original_value
144
+
145
+ return GraphData(**single_graph_dict)
146
+
147
+ def _get_attr_slice_at_index(self, attr: str, idx: int):
148
+ """Returns the starting and ending index to retrieve the original attribute `attr`
149
+ from the batch, at the given index `idx`.
150
+ """
151
+ attr_sizes = self.to_dict()[f"_size_{attr}"]
152
+ cum_attr_counts = mx.cumsum(mx.concatenate([mx.array([0]), attr_sizes]))
153
+
154
+ from_idx = cum_attr_counts[idx].item()
155
+ upto_idx = cum_attr_counts[idx + 1].item()
156
+
157
+ return from_idx, upto_idx
158
+
159
+ def _handle_neg_index_if_needed(self, index: Union[mx.array, list, int]):
160
+ """Returns the corresponsing positive index or indices of negative batch indices.
161
+ Raises an index error if indices are incorrect. This method accepts either an mx.array,
162
+ a list or a single int index.
163
+ """
164
+ if isinstance(index, int):
165
+ index = mx.array([index])
166
+ elif isinstance(index, mx.array):
167
+ index = index.tolist()
168
+
169
+ # Should be changed once boolean indexing exists in mlx
170
+ index = np.asarray(index)
171
+ index[index < 0] += self.num_graphs
172
+
173
+ if np.any((index < 0) | (index > self.num_graphs)):
174
+ raise IndexError("Batch indexing out of range.")
175
+
176
+ index = index.tolist()
177
+ return index[0] if len(index) == 1 else index
178
+
179
+
180
+ def batch(graphs: list[GraphData]) -> GraphDataBatch:
181
+ """Constructs a `GraphDataBatch` object from a list of `GraphData`.
182
+
183
+ Args:
184
+ batch: List of `GraphData` to merge into a single batch
185
+
186
+ Returns:
187
+ `GraphDataBatch` storing a large batched graph
188
+ """
189
+ return GraphDataBatch(graphs)
190
+
191
+
192
+ def unbatch(batch: GraphDataBatch) -> list[GraphData]:
193
+ """Reconstructs a list of `GraphData` objects from a `GraphDataBatch`.
194
+
195
+ Args:
196
+ batch: `GraphDataBatch` to unbatch
197
+
198
+ Returns:
199
+ List of unbatched `GraphData`
200
+ """
201
+ return [batch[idx] for idx in range(batch.num_graphs)]
@@ -0,0 +1,62 @@
1
+ import mlx.core as mx
2
+
3
+ from mlx_graphs.data.data import GraphData
4
+ from mlx_graphs.utils.validators import validate_list_of_graph_data
5
+
6
+
7
+ @validate_list_of_graph_data
8
+ def collate(graph_list: list[GraphData]) -> dict:
9
+ """Concatenates attributes of multiple graphs based on the specifications
10
+ of each `GraphData`.
11
+
12
+ By default, concatenates all default array attributes in dim 0
13
+ apart from `edge_index` which is concatenated along dim 1.
14
+ Each graph remains independent in the final graph by incrementing
15
+ the indices in `edge_index` based on the cumsum of previous number
16
+ of nodes per graph.
17
+
18
+ Args:
19
+ graph_list: List of `GraphData` objects to collate
20
+
21
+ Returns:
22
+ Dict containing all the attributes of the unified and disconnected big graph as
23
+ well as the "private" attributes used behind the hood by the batching. These private
24
+ attributes start with an underscore "_" and can be ignore by the user.
25
+ """
26
+ batch_attr_dict = {}
27
+
28
+ for attr in graph_list[0].to_dict():
29
+ __inc__ = graph_list[0].__inc__(key=attr)
30
+ __cat_dim__ = graph_list[0].__cat_dim__(key=attr)
31
+
32
+ cumsum: mx.array = None
33
+ if __inc__:
34
+ num_attr_list = [getattr(graph, "__inc__")(attr) for graph in graph_list]
35
+ cumsum = mx.cumsum(mx.array([0] + num_attr_list))
36
+
37
+ attr_list, attr_sizes = [], []
38
+ for i, graph in enumerate(graph_list):
39
+ attr_array = getattr(graph, attr)
40
+
41
+ if __inc__:
42
+ attr_array = attr_array + cumsum[i]
43
+
44
+ attr_list.append(attr_array)
45
+ attr_sizes.append(attr_array.shape[__cat_dim__])
46
+
47
+ # Private attributes are used later in batching for indexing
48
+ batch_attr_dict[f"_size_{attr}"] = mx.array(attr_sizes)
49
+ batch_attr_dict[f"_cat_dim_{attr}"] = __cat_dim__
50
+ if __inc__:
51
+ batch_attr_dict[f"_inc_{attr}"] = True
52
+ batch_attr_dict[f"_cumsum_{attr}"] = cumsum
53
+
54
+ if attr == "edge_index":
55
+ batch_indices = []
56
+ for i in range(len(cumsum) - 1):
57
+ batch_indices.extend([i] * (cumsum[i + 1] - cumsum[i]).item())
58
+ batch_attr_dict["_batch_indices"] = mx.array(batch_indices)
59
+
60
+ batch_attr_dict[attr] = mx.concatenate(attr_list, axis=__cat_dim__)
61
+
62
+ return batch_attr_dict
@@ -0,0 +1,117 @@
1
+ from typing import Optional, Union
2
+
3
+ import mlx.core as mx
4
+
5
+
6
+ class GraphData:
7
+ """
8
+ Represents a graph data object with optional features and labels.
9
+
10
+ Args:
11
+ edge_index: edge index representing the topology of
12
+ the graph, with shape [2, num_edges].
13
+ node_features: Array of shape [num_nodes, num_node_features]
14
+ containing the features of each node.
15
+ edge_features: Array of shape [num_edges, num_edge_features]
16
+ containing the features of each edge.
17
+ graph_features: Array of shape [num_graph_features]
18
+ containing the global features of the graph.
19
+ node_labels: Array of shape [num_nodes, num_node_labels]
20
+ containing the labels of each node.
21
+ edge_labels: Array of shape [num_edges, num_edge_labels]
22
+ containing the labels of each edge.
23
+ graph_labels: Array of shape [num_graph_labels]
24
+ containing the global labels of the graph.
25
+ **kwargs: Additional keyword arguments to store any other custom attributes.
26
+ """
27
+
28
+ def __init__(
29
+ self,
30
+ edge_index: Optional[mx.array] = None,
31
+ node_features: Optional[mx.array] = None,
32
+ edge_features: Optional[mx.array] = None,
33
+ graph_features: Optional[mx.array] = None,
34
+ node_labels: Optional[mx.array] = None,
35
+ edge_labels: Optional[mx.array] = None,
36
+ graph_labels: Optional[mx.array] = None,
37
+ **kwargs,
38
+ ):
39
+ self.edge_index = edge_index
40
+ self.node_features = node_features
41
+ self.edge_features = edge_features
42
+ self.graph_features = graph_features
43
+ self.node_labels = node_labels
44
+ self.edge_labels = edge_labels
45
+ self.graph_labels = graph_labels
46
+ for key, value in kwargs.items():
47
+ setattr(self, key, value)
48
+
49
+ def __repr__(self):
50
+ strings = []
51
+ for k, v in vars(self).items():
52
+ if v is not None and not k.startswith("_"):
53
+ if isinstance(v, mx.array):
54
+ strings.append(
55
+ f"{k}(shape={v.shape}, {str(v.dtype).split('.')[-1]})"
56
+ )
57
+ else:
58
+ strings.append(f"{k}={v}")
59
+
60
+ prefix = "\n\t"
61
+ return f"{type(self).__name__}({prefix + prefix.join(strings)})"
62
+
63
+ def to_dict(self) -> dict:
64
+ """Converts the Data object to a dictionary.
65
+
66
+ Returns:
67
+ A dictionary with all attributes of the `GraphData` object.
68
+ """
69
+ return {k: v for k, v in self.__dict__.items() if v is not None}
70
+
71
+ @property
72
+ def num_nodes(self) -> Union[int, None]:
73
+ """Number of nodes in the graph."""
74
+ if self.node_features:
75
+ return self.node_features.shape[0]
76
+ return None
77
+
78
+ def __cat_dim__(self, key: str, *args, **kwargs) -> int:
79
+ """This method can be overriden when batching is used with custom attributes.
80
+ Given the name of a custom attribute `key`, returns the dimension where the
81
+ concatenation happens during batching.
82
+
83
+ By default, all attribute names containing "index" will be concatenated on axis 1,
84
+ e.g. `edge_index`. Other attributes are concatenated on axis 0, e.g. node features.
85
+
86
+ Args:
87
+ key: Name of the attribute on which change the default concatenation dimension
88
+ while using batching.
89
+
90
+ Returns:
91
+ The dimension where concatenation will happen when batching.
92
+ """
93
+ if "index" in key:
94
+ return 1
95
+ return 0
96
+
97
+ def __inc__(self, key: str, *args, **kwargs) -> Union[int, None]:
98
+ """This method can be overriden when batching is used with custom attributes.
99
+ Given the name of a custom attribute `key`, returns an integer indicating the
100
+ incrementing value to apply to the elemnts of the attribute.
101
+
102
+ By default, all attribute names containing "index" will be incremented based on
103
+ the number of nodes in previous batches to avoid duplicate nodes in the index,
104
+ e.g. `edge_index`. Other attributes are cnot incremented and keep their original
105
+ values, e.g. node features.
106
+ If incrementation is not used, the return value should be set to `None`.
107
+
108
+ Args:
109
+ key: Name of the attribute on which change the default incrementation behavior
110
+ while using batching.
111
+
112
+ Returns:
113
+ Incrementing value for the given attribute or None.
114
+ """
115
+ if "index" in key:
116
+ return len(self.node_features) if self.node_features is not None else None
117
+ return None
@@ -0,0 +1,5 @@
1
+ from .conv import * # noqa
2
+ from .linear import Linear # noqa
3
+ from .graph_network_block import GraphNetworkBlock # noqa
4
+ from .message_passing import MessagePassing # noqa
5
+ from .pooling import * # noqa
@@ -0,0 +1,4 @@
1
+ from .gat_conv import GATConv # noqa
2
+ from .gcn_conv import GCNConv # noqa
3
+ from .gin_conv import GINConv # noqa
4
+ from .sage_conv import SAGEConv # noqa