Skip to content

GraphDataBatch

GraphDataBatch(graphs: list[GraphData], **kwargs)

Bases: GraphData

Concatenates multiple GraphData into a single unified GraphDataBatch for efficient computation and parallelization over multiple graphs.

All graphs remain disconnected in the batch, meaning that any pairs of graphs have no nodes or edges in common. GraphDataBatch can be especially used to speed up graph classification tasks, where multiple graphs can easily fit into memory and be processed in parallel.

Parameters:

Name Type Description Default
graphs list[GraphData]

List of GraphData objects to batch together

required

Example:

from mlx_graphs.data.batch import GraphDataBatch

graphs = [
    GraphData(
        edge_index=mx.array([[0, 0, 0], [1, 1, 1]]),
        node_features=mx.zeros((3, 1)),
    ),
    GraphData(
        edge_index=mx.array([[1, 1, 1], [2, 2, 2]]),
        node_features=mx.ones((3, 1)),
    ),
    GraphData(
        edge_index=mx.array([[3, 3, 3], [4, 4, 4]]),
        node_features=mx.ones((3, 1)) * 2,
    )
]
batch = GraphDataBatch(graphs)
>>> GraphDataBatch(
    edge_index(shape=[2, 9], int32)
    node_features(shape=[9, 1], float32))

batch.num_graphs
>>> 3

batch[1]
>>> GraphData(
    edge_index(shape=[2, 3], int32)
    node_features(shape=[3, 1], float32))

batch[1:]
>>> [
        GraphData(
            edge_index(shape=[2, 3], int32)
            node_features(shape=[3, 1], float32)),
        GraphData(
            edge_index(shape=[2, 3], int32)
            node_features(shape=[3, 1], float32))
    ]

Methods:

Name Description
batch_indices

Mask indicating for each node its corresponding batch index.

batch

PyG-style alias of batch_indices: the index of the graph of

ptr

Offsets of the nodes of each graph in the batch, as in PyG: the nodes

Attributes:

Name Type Description
num_graphs int

Number of graphs in the batch.

Source code in mlx_graphs/data/batch.py
def __init__(self, graphs: list[GraphData], **kwargs):
    batch_kwargs = collate(graphs)
    super().__init__(_num_graphs=len(graphs), **batch_kwargs, **kwargs)

num_graphs property

num_graphs: int

Number of graphs in the batch.

batch_indices

batch_indices()

Mask indicating for each node its corresponding batch index.

Graphs that carry their own batch_indices (e.g. the outputs of ShaDowKHopSampler, one subgraph per seed) keep it, offset per graph so that ids stay distinct, as PyG does with batch.

Source code in mlx_graphs/data/batch.py
@_BatchAlias
def batch_indices(self):
    """Mask indicating for each node its corresponding batch index.

    Graphs that carry their own ``batch_indices`` (e.g. the outputs of
    :class:`~mlx_graphs.loaders.ShaDowKHopSampler`, one subgraph per seed)
    keep it, offset per graph so that ids stay distinct, as PyG does with
    ``batch``."""
    return self._batch_indices  # type: ignore - provided via collate

batch

batch() -> array

PyG-style alias of batch_indices: the index of the graph of each node.

Source code in mlx_graphs/data/batch.py
@_BatchAlias
def batch(self) -> mx.array:
    """PyG-style alias of :attr:`batch_indices`: the index of the graph of
    each node."""
    return self.batch_indices

ptr

ptr() -> array

Offsets of the nodes of each graph in the batch, as in PyG: the nodes of graph i are ptr[i] to ptr[i + 1] - 1. Has num_graphs + 1 entries (int32).

Source code in mlx_graphs/data/batch.py
@_BatchAlias
def ptr(self) -> mx.array:
    """Offsets of the nodes of each graph in the batch, as in PyG: the nodes
    of graph ``i`` are ``ptr[i]`` to ``ptr[i + 1] - 1``. Has
    ``num_graphs + 1`` entries (int32)."""
    cumsum = getattr(self, "_cumsum_edge_index", None)
    if cumsum is not None:
        return cumsum.astype(mx.int32)
    counts = np.bincount(np.asarray(self.batch_indices), minlength=self.num_graphs)
    ptr = np.zeros(self.num_graphs + 1, dtype=np.int32)
    np.cumsum(counts, out=ptr[1:])
    return mx.array(ptr)

from mlx_graphs.data.batch import GraphDataBatch · mlx_graphs/data/batch.py