"""Routing of ids to the partitions that own them, for distributed sampling and feature lookups.
GLT routes ids with one mask and two ``masked_select`` calls per partition. Each of those torch ops releases and
reacquires the GIL, so with many partitions the routing dominates the sampler's event-loop thread. Grouping the ids
once with a stable sort does the same routing in a handful of ops and yields the same ids in the same order for
integer partition books.
``group_by_partition`` replaces the per-partition mask loops of two GLT functions, which GiGL copies and edits:
- ``graphlearn_torch.distributed.dist_neighbor_sampler.DistNeighborSampler._sample_one_hop``, copied as
``gigl.distributed.base_sampler.BaseDistNeighborSampler._sample_one_hop``.
- ``graphlearn_torch.distributed.dist_feature.DistFeature._remote_selecting_get``, copied as
``gigl.distributed.utils.fused_feature.FusedDistFeature._remote_selecting_get``.
The other mask loops in GLT's ``DistFeature`` (``async_get``, its all-to-all path) are not replaced.
"""
import torch
from jaxtyping import Int64, Integer
[docs]
def group_by_partition(
ids: Integer[torch.Tensor, "num_ids"],
partition_ids: Integer[torch.Tensor, "num_ids"],
num_partitions: int,
) -> tuple[
list[Integer[torch.Tensor, "_num_selected"]],
list[Int64[torch.Tensor, "_num_selected"]],
]:
"""Splits ``ids`` by owning partition with one stable sort.
For every partition ``p`` the result equals ``masked_select(ids, partition_ids == p)`` and
``masked_select(arange(len(ids)), partition_ids == p)``, in the same order. The ids of a partition have a storage
of their own, of the same size as ``masked_select`` gives, whereas the positions are views of one sorted
permutation. A partition that owns none of the ids gets empty tensors.
Example:
>>> ids = torch.tensor([9, 4, 9, 7, 1])
>>> partition_ids = torch.tensor([2, 0, 2, 1, 0])
>>> ids_by_partition, index_by_partition = group_by_partition(ids, partition_ids, num_partitions=3)
>>> ids_by_partition
[tensor([4, 1]), tensor([7]), tensor([9, 9])]
>>> index_by_partition
[tensor([1, 4]), tensor([3]), tensor([0, 2])]
Args:
ids: Ids to route.
partition_ids: Owning partition of each id, in ``[0, num_partitions)``. We do not expect other values:
GiGL's partition books do not produce them, but a range partition book gives ``num_partitions`` for an
id past its last bound. Ids with any other value are dropped, to mirror GLT's mask loop. Dropped ids
cost the same as any other id, whatever their value.
num_partitions: Number of partitions.
Returns:
The ids of each partition, and the positions of those ids in ``ids``. Both lists are indexed by partition.
"""
# Every id that no partition owns is sent to the extra partition ``num_partitions``, which is dropped below.
# This bounds the work by ``num_partitions`` instead of by the largest id. The conversion to ``long`` has to
# come first: ``bincount`` and ``clamp_max`` reject the unsigned dtypes of partition books (``uint16``,
# ``uint32``, ``uint64``) on CPU. A ``uint64`` value above the ``int64`` maximum wraps to a negative value and
# is dropped, as the mask drops it.
partition_ids = partition_ids.to(torch.long)
partition_ids = partition_ids.masked_fill(
(partition_ids < 0) | (partition_ids > num_partitions), num_partitions
)
perm: Int64[torch.Tensor, "num_ids"] = torch.argsort(partition_ids, stable=True)
counts: list[int] = torch.bincount(
partition_ids, minlength=num_partitions + 1
).tolist()
# Torch RPC pickles the whole storage of a view, and TensorPipe only re-copies a storage that is at least
# twice the bytes in use. Without a storage of their own, the ids of a partition could put up to twice their
# bytes on the wire, e.g. all of the sorted ids when there are two partitions.
ids_by_partition: list[Integer[torch.Tensor, "_num_selected"]] = []
for chunk in ids[perm.to(ids.device)].split(counts)[:num_partitions]:
ids_by_partition.append(chunk.clone())
index_by_partition: list[Int64[torch.Tensor, "_num_selected"]] = list(
perm.split(counts)
)[:num_partitions]
return ids_by_partition, index_by_partition