Skip to content

Aggregation

Bases: Module

Base class of the aggregation operators, mirroring torch_geometric.nn.aggr.Aggregation.

An aggregation reduces a set of elements (e.g. the messages received by each node, or the nodes of each graph) to a single vector per set. The sets are given by an index array mapping every element of x to its set, and the reduction is always done along the first axis of x:

x = mx.random.normal((10, 64))
index = mx.array([0, 0, 1, 0, 2, 0, 2, 1, 0, 2])

out = aggr(x, index)  # shape [3, 64]

index does not need to be sorted. Unlike PyG, compressed ptr inputs and a dim argument are not supported: elements are always stacked along axis 0, which is the mlx-graphs convention.

Aggregations can be used on their own (e.g. as a global readout with index = batch_indices) or passed as aggr to any MessagePassing layer.

Subclasses implement aggregate(), which receives a validated index and dim_size.

Methods:

Name Description
__call__

Aggregates the elements of x according to index.

aggregate

Computes the aggregation. Implemented by subclasses.

reduce

Reduces x with scatter().

__call__

__call__(
    x: array,
    index: Optional[array] = None,
    dim_size: Optional[int] = None,
    **kwargs: Any,
) -> array

Aggregates the elements of x according to index.

Parameters:

Name Type Description Default
x array

Array of shape [num_elements, ...] with the elements to aggregate (e.g. messages or node features).

required
index Optional[array]

Array of shape [num_elements] with the set of each element (e.g. the destination node of each message, or batch_indices). If None, all the elements form a single set.

None
dim_size Optional[int]

Number of sets, i.e. size of the first axis of the output. Defaults to index.max() + 1 (1 without index).

None
**kwargs Any

Additional arguments of the aggregation.

{}

Returns:

Type Description
array

An array of shape [dim_size, ...].

Source code in mlx_graphs/nn/aggr/base.py
def __call__(
    self,
    x: mx.array,
    index: Optional[mx.array] = None,
    dim_size: Optional[int] = None,
    **kwargs: Any,
) -> mx.array:
    """Aggregates the elements of ``x`` according to ``index``.

    Args:
        x: Array of shape ``[num_elements, ...]`` with the elements to
            aggregate (e.g. messages or node features).
        index: Array of shape ``[num_elements]`` with the set of each
            element (e.g. the destination node of each message, or
            ``batch_indices``). If ``None``, all the elements form a
            single set.
        dim_size: Number of sets, i.e. size of the first axis of the
            output. Defaults to ``index.max() + 1`` (``1`` without
            ``index``).
        **kwargs: Additional arguments of the aggregation.

    Returns:
        An array of shape ``[dim_size, ...]``.
    """
    if x.ndim == 0:
        raise ValueError("Aggregations need at least a one-dimensional input")
    if index is None:
        index = mx.zeros((x.shape[0],), dtype=mx.int32)
        dim_size = 1 if dim_size is None else dim_size
    elif dim_size is None:
        dim_size = int(index.max().item()) + 1 if index.size > 0 else 0
    return self.aggregate(x, index, dim_size, **kwargs)

aggregate

aggregate(x: array, index: array, dim_size: int, **kwargs: Any) -> array

Computes the aggregation. Implemented by subclasses.

Parameters:

Name Type Description Default
x array

Array of shape [num_elements, ...].

required
index array

Array of shape [num_elements] with the set of each element.

required
dim_size int

Number of sets.

required
Source code in mlx_graphs/nn/aggr/base.py
def aggregate(
    self, x: mx.array, index: mx.array, dim_size: int, **kwargs: Any
) -> mx.array:
    """Computes the aggregation. Implemented by subclasses.

    Args:
        x: Array of shape ``[num_elements, ...]``.
        index: Array of shape ``[num_elements]`` with the set of each
            element.
        dim_size: Number of sets.
    """
    raise NotImplementedError

reduce

reduce(
    x: array, index: array, dim_size: int, reduce: ScatterAggregations = "add"
) -> array

Reduces x with scatter().

Parameters:

Name Type Description Default
x array

Array of shape [num_elements, ...].

required
index array

Array of shape [num_elements].

required
dim_size int

Number of sets.

required
reduce ScatterAggregations

Any reduction supported by scatter(). Default: "add"

'add'
Source code in mlx_graphs/nn/aggr/base.py
def reduce(
    self,
    x: mx.array,
    index: mx.array,
    dim_size: int,
    reduce: ScatterAggregations = "add",
) -> mx.array:
    """Reduces ``x`` with :func:`~mlx_graphs.utils.scatter`.

    Args:
        x: Array of shape ``[num_elements, ...]``.
        index: Array of shape ``[num_elements]``.
        dim_size: Number of sets.
        reduce: Any reduction supported by
            :func:`~mlx_graphs.utils.scatter`. Default: ``"add"``
    """
    return scatter(x, index, dim_size, aggr=reduce)

from mlx_graphs.nn.aggr import Aggregation · mlx_graphs/nn/aggr/base.py