gigl.distributed.utils.partition_routing#

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.

Functions#

group_by_partition(ids, partition_ids, num_partitions)

Splits ids by owning partition with one stable sort.

Module Contents#

gigl.distributed.utils.partition_routing.group_by_partition(ids, partition_ids, num_partitions)[source]#

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])]
Parameters:
  • ids (jaxtyping.Integer[torch.Tensor, num_ids]) – Ids to route.

  • partition_ids (jaxtyping.Integer[torch.Tensor, num_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 (int) – Number of partitions.

Returns:

The ids of each partition, and the positions of those ids in ids. Both lists are indexed by partition.

Return type:

tuple[list[jaxtyping.Integer[torch.Tensor, _num_selected]], list[jaxtyping.Int64[torch.Tensor, _num_selected]]]