gigl.distributed.utils.native_neighbor_lookup#
Serve one-hop neighbor lookups, local and remote, with a native lookup instead of GLT’s sampler and Python callee.
GLT sends each remote neighbor request as a Python RPC (graphlearn_torch.distributed.rpc.rpc_request_async). torch
RPC holds the interpreter lock while it unpickles such a request, runs the callee and pickles the reply, so a sampling
worker serves them one at a time, and slower while its own sampling holds the lock. A native request is instead sent as
a TorchScript RPC function that calls torch.ops.gigl_core.lookup_neighbors, a C++ lookup over the partition’s
CSR (gigl-core/core/sampling/neighbor_lookup.cpp). The serving side runs C++ on the RPC thread with no Python
and no interpreter lock; TorchScript is only the transport.
The same operator serves the local partition: lookup_local calls it from Python, and a torch operator call
releases the interpreter lock while it runs. With the flag on, local and remote nodes go through the same kernel, so
one hop uses one kernel and one random stream.
On by default; GIGL_NATIVE_NEIGHBOR_LOOKUP=off (NATIVE_NEIGHBOR_LOOKUP_ENV) turns it off, identically on
every process.
How the pieces fit together#
Registering a partition and reading it live in different files, joined by the sampler’s RPC sampling callee id and an edge-type key:
Setup, once per sampler
BaseDistNeighborSampler.__init__ gigl/distributed/base_sampler.py
-> rpc_register(callee) returns the callee id, identical on every rank
-> _enable_native_neighbor_lookup_on_all_ranks
base_sampler.py (a collective)
-> register_partition_csr this module; compiles _serve_remote_lookup in this process
-> registerCsr (C++) neighbor_lookup.{h,cpp}: process-global registry
keyed by (callee id, edge-type key)
-> all_gather every rank registered, or every rank raises
Local read (nodes this rank owns)
_sample_one_hop -> lookup_local -> torch.ops.gigl_core.lookup_neighbors python_neighbor_lookup.cpp
-> registry lookup by (callee id, edge-type key) -> kernel neighbor_lookup.{h,cpp}
Remote read (nodes a peer owns)
_sample_one_hop -> lookup_remote -> torch RPC ---------------------> peer's RPC thread
-> TorchScript _serve_remote_lookup compiled in that process by its own registration; found by qualified name
-> torch.ops.gigl_core.lookup_neighbors -> registry -> kernel no Python, no interpreter lock
reply tuple <- torch RPC <------------------------------------------ back on the caller:
NeighborOutput(*reply) on the caller's event-loop thread, after ``collect_all``
The registry is keyed by the callee id because the serving process has no other handle to the sampler: the request
names the sampler only by that id, and rpc_register guarantees the id is the same on every rank. The files are
gigl/distributed/base_sampler.py (the caller and the all-ranks check), this module (the Python API),
gigl-core/core/sampling/python_neighbor_lookup.cpp (the torch operator and the pybind functions) and
gigl-core/core/sampling/neighbor_lookup.{h,cpp} (the registry and the kernel).
Registration keeps references to the dataset’s tensors; nothing is copied. Entries are removed only if registration fails, so they live as long as the process.
Sampled neighbors are identical to GLT’s whenever a neighbor list is no longer than the requested fan-out. A longer list is sampled uniformly with replacement on both paths, but from different random generators.
Attributes#
Functions#
|
Looks up neighbors in this process's partition now, and returns them. |
|
Asks |
|
Registers this partition's CSR for native neighbor lookups, local and remote, addressed to |
Module Contents#
- gigl.distributed.utils.native_neighbor_lookup.lookup_local(sampler_id, etype, ids, req_num)[source]#
Looks up neighbors in this process’s partition now, and returns them.
For nodes another process owns, see
lookup_remote, which returns a future instead.Samplers call this for local nodes, with the lookup a native request runs on the server. A torch operator call releases the interpreter lock while it runs.
A negative
req_num, or one above the int32 maximum, means all neighbors. An id outside the partition’s rows has 0 neighbors. A row with at mostreq_numneighbors is copied in stored order. A longer row is sampled uniformly with replacement from a per-thread generator.- Parameters:
sampler_id (int) – The sampler’s RPC sampling callee id.
etype (Optional[EdgeType]) – The edge type to sample along, or
Noneon a homogeneous graph.ids (torch.Tensor) – Global ids of the nodes to sample from, int64 on the CPU.
req_num (int) – Requested neighbors per node.
- Returns:
The flat neighbor ids, the neighbor count per id and the edge ids, or
Noneforedgewhen the CSR was registered withwith_edge=False.- Return type:
NeighborOutput
- Raises:
RuntimeError – If no CSR is registered for
sampler_idandetype, ifidsis not a 1-D int64 CPU tensor, or if the registration wants edge ids but the graph has none.
- gigl.distributed.utils.native_neighbor_lookup.lookup_remote(worker_name, sampler_id, etype, ids, req_num)[source]#
Asks
worker_name’s process to look up neighbors in its partition, and returns a future of the reply.For nodes this process owns, see
lookup_local, which returns the neighbors directly.The request is a TorchScript RPC function, so the server needs no Python to answer it.
- Parameters:
worker_name (str) – The RPC worker that owns
ids.sampler_id (int) – The sampler’s RPC sampling callee id.
etype (Optional[EdgeType]) – The edge type to sample along, or
Noneon a homogeneous graph.ids (torch.Tensor) – Global ids of the nodes to sample from, int64 on the CPU.
req_num (int) – Requested neighbors per node.
- Returns:
A future of
(neighbors, counts, edge_ids or None). The reply is a tuple, which the caller wraps withNeighborOutput(*reply)on its own event loop, not in a Python callback on the future: a callback would take the interpreter lock on an RPC thread, which also serves peers.- Return type:
torch.futures.Future[NativeLookupReply]
- gigl.distributed.utils.native_neighbor_lookup.register_partition_csr(sampler_id, local_graph, with_edge, with_weight, device, seed)[source]#
Registers this partition’s CSR for native neighbor lookups, local and remote, addressed to
sampler_id.Never raises: a failure is returned as a reason, so that the caller’s
all_gatheracross ranks still runs on every rank (GLT’sall_gatherwaits with no timeout, so a rank that raised first would hang the others).- Parameters:
sampler_id (int) – The sampler’s RPC sampling callee id, identical on every rank.
local_graph (Union[Graph, dict[EdgeType, Graph]]) – The partition’s graph, or one graph per edge type.
with_edge (bool) – Whether replies carry edge ids; if not, they carry
None.with_weight (bool) – Whether the sampler samples by edge weight, which the C++ lookup does not do.
device (torch.device) – The sampler’s device; the lookup runs on the CPU only.
seed (Optional[int]) – The sampler’s random seed, or
None. Seeds the lookup’s sub-sampling as GLT’s sampler seeds its own.
- Returns:
Noneif every edge type was registered, else the reason this rank cannot register. When a reason is returned, nothing is registered.- Return type:
Optional[str]