feat(shmem): bind ranks to GPU-local CPUs by default#401
Open
jhchouuu wants to merge 1 commit into
Open
Conversation
The shmem/EP path never pinned its rank thread, so kernel-launch / CQ-polling / staging-copy work (and later-spawned threads such as IO workers) could float onto a CPU on a NUMA node remote from the rank's GPU and NIC. Following NCCL: read the GPU's local CPU set (PCI BDF -> sysfs local_cpulist), intersect it with the current cpuset (so cgroup limits and any outer numactl/ torchrun binding are respected), and pin the calling thread to it; skip if the intersection is empty. Enabled by default; set MORI_IGNORE_CPU_AFFINITY=1 to opt out. New threads inherit the affinity. Unlike NCCL (a sub-library that restores the caller's affinity after init), mori keeps it for the rank lifetime since a rank is dedicated to one GPU. Bind at a single site: the top of ShmemInit, which every init entry funnels through and which runs before any buffer allocation or worker-thread spawn (so buffers land on the local node and children inherit the affinity). Per the SPMT contract the caller has already hipSetDevice()'d its GPU before ShmemInit, so the calling thread's current device is the rank's GPU in both one-rank-per-GPU and SPMT (one process, one such thread per GPU) layouts -- no build-time special casing needed. The helper (application/utils/cpu_affinity.hpp) is thread-local-once and reads the current device, so re-init is a no-op.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The shmem/EP path never pinned its rank thread. Kernel-launch, CQ-polling and staging-copy work — plus any later-spawned threads (e.g. IO workers, which inherit the parent's affinity) — could float onto a CPU on a NUMA node remote from the rank's GPU and NIC, adding cross-socket hops on the host side of every transfer.
This pins each rank to the CPUs local to its GPU, on by default, following NCCL.
How
At the top of
ShmemInit, read the GPU's local CPU set (PCI BDF → sysfslocal_cpulist), intersect it with the current cpuset, and pin the calling thread to the result:numactl/torchrunbinding or a cgroup CPU limit is respected; we only narrow within what's already allowed.MORI_IGNORE_CPU_AFFINITY=1leaves placement entirely to the caller.Single bind site
All init entries (
ShmemMpiInit,ShmemInit,ShmemTorchProcessGroupInit, the socket-bootstrap path) funnel throughShmemInit(BootstrapNetwork*), and it runs before any buffer allocation or worker-thread spawn — so buffers land on the local node and children inherit the affinity.Per the existing SPMT contract, the caller has already
hipSetDevice()'d its GPU beforeShmemInit, so the calling thread's current device is the rank's GPU in both layouts — one-rank-per-GPU and SPMT (single process, one init thread per GPU). No build-time special-casing is needed: SPMT and non-SPMT use the same code path, each thread pinning to its own GPU's NUMA node.Scope
shmem/EP only. IO / UMBP go through
RdmaContextdirectly (notShmemInit) and have their own affinity handling, so they are unaffected.Testing
MORI_MULTITHREAD_SUPPORT=ON(SPMT) configs.thread bound to N CPUs local to GPU X (numa node Y), pinned to the correct NUMA node; new threads inherit it.MORI_IGNORE_CPU_AFFINITY=1: no binding applied.--bind-to corethe intersection correctly collapses to the caller-provided CPU (existing binding respected).Env vars
MORI_IGNORE_CPU_AFFINITY1to disable GPU-local CPU pinning.