Repository navigation
[core] Apply NUMA binding in TrainingEngine - #5991
Conversation
|
@claude review |
|
Claude finished @anijain2305's task in 3m 12s —— View job Approve, with items to address before merging: see the review. |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. Binding right after set_device is the right layer and the right time. One binding site in the engine replaces two late ones. Core already has pinned host memory consumers today: FSDP CPU offload, EMA (components/optim/ema.py:96), pinned collator buffers, and async_with_pinned_mem checkpoint staging. A maintainer still needs to review, because this touches core.
What this PR does. TrainingEngine._initialize_distributed_runtime now binds the process to its GPU's NUMA node right after set_device. This happens before init_distributed, model build, FSDP CPU offload, or any pinned allocation. The PR deletes the late calls in GraphTrainingEngine._initialize_forward_backward and in the RL Trainer.__init__. The vLLM generator keeps its own call.
Example. GB300, rank on GPU0, fsdp_enable_cpu_offloading=True:
main, core Trainer: no binding. Pinned shards land on whichever node the thread ran on.
D2H can drop to about 97 GB/s (remote node).
main, graph_trainer: binds in _initialize_forward_backward, after FSDP has pinned its shards.
Those shards may already sit on the remote node.
this PR, all three: binds in __init__ before any pinned allocation. Shards are on the local node.
D2H about 193 GB/s.
Must fix
- The PR description has no end-to-end check for the main path. The PR now changes CPU affinity for every CUDA run of the core
Trainer, not only offload users. The only evidence is a 1 GiB copy microbenchmark, and "Testing: CI" cannot exercise NUMA. Please add a short tps comparison, before and after, for one standard run. For example, Llama3 8B with FSDP on an 8-GPU 2-socket x86 host, plus GB300. This shows that throughput is unchanged or better, including dataloader workers, which inherit the narrower affinity.
Should consider
torchtitan/training_engine.py:317: the core path now depends on a private upstream API, and there is no TODO for it (see the inline comment).
Questions
- What happens if the launcher already sets an affinity, such as
torchrun --numa-binding=core-complexor SLURM--cpu-bind=cores? DoesAffinityMode.NODEwiden that affinity back to the whole node? If it does, should the engine skip binding when the process is already bound to a subset of the node? Unverified: I could not read the upstream implementation in this environment. - #5447 (felipemello1) calls
torch.set_num_threads(...)right after the RL trainer'smaybe_apply_numa_bindingline, which this PR deletes. The two PRs will conflict. That PR's call should move to right afterTrainingEngine(...)is constructed, which is still after the binding.
Details
- Callsite audit.
GraphTrainingEngine,FaultTolerantTrainingEngine(torchft) andSDCReplayMismatchTrainingEngineall reach_initialize_distributed_runtimethroughTrainingEngine.__init__, and none of them override it. So every engine binds exactly once. Thecpu_offload.pymodule docstring ("Trainer automatically applies NUMA binding ... at init") is still accurate. - Non-CUDA. XPU/NPU/CPU devices are still a no-op through the
device_type != "cuda"check, so CPU unit tests that build engines are unaffected. - Threads. On the main path, torchrun defaults
OMP_NUM_THREADS=1whennproc_per_node > 1. So the intra-op oversubscription that #5447 describes for Monarch-launched RL trainers should not newly appear here. A launcher that does not set it would hit the same problem (unverified). - Numerics. Only CPU affinity changes, so loss and grad_norm should be bitwise unchanged.
- Test edit. Removing the patch in
test_dist_moe.pyis mechanical:_initialize_forward_backwardno longer callsmaybe_apply_numa_binding.
Not verified: GPU or NUMA behavior, and the upstream _maybe_apply_numa_binding_to_current_process semantics.
| device_module.set_device(self.device) | ||
| # Before anything allocates pinned host memory (FSDP CPU offload | ||
| # shards, pinned batches, EMA copies), so it lands on the GPU's NUMA node. | ||
| maybe_apply_numa_binding(self.device.index, device_type) |
There was a problem hiding this comment.
With this change, every CUDA run of the core Trainer calls torch.numa.binding._maybe_apply_numa_binding_to_current_process (torchtitan/distributed/offloading.py:21-27). That is a private upstream API, and there is no TODO for it. Before, only graph_trainer, RL and the vLLM generator used it. Now it is on the main training path, so if upstream renames or changes it, every CUDA run breaks. CLAUDE.md principle 3 asks for a TODO with an upstream issue link here: either ask upstream for a public in-process binding API, or say why the torchrun-level --numa-binding is not enough. The TODO is one line in offloading.py.
| device_module.set_device(self.device) | ||
| # Before anything allocates pinned host memory (FSDP CPU offload | ||
| # shards, pinned batches, EMA copies), so it lands on the GPU's NUMA node. | ||
| maybe_apply_numa_binding(self.device.index, device_type) |
There was a problem hiding this comment.
I feel RL generators also need it? today we could be using CPU staging for generators during weight sync
There was a problem hiding this comment.
Agent says generators already bind.
▎ Generators already bind: VLLMEngine (the only GenerationEngine) calls maybe_apply_numa_bindingin vllm/model_wrapper.py right before constructing ModelStateDictLoader, which is
▎ where the cpu_prefetch pinned staging buffers are allocated (generator/model.py, _create_cpu_state_dict(..., pin_memory=True)). So weight-sync staging lands on the GPU-local node.
▎ This PR doesn't touch that call. Generators don't go through TrainingEngine, so it stays there; only thetrainer-side calls (RL trainer, graph_trainer), which ran after FSDP CPU
▎ offload had already pinned its shards, move into the engine.
…tions Bind each training process to the NUMA node of its GPU right after set_device in TrainingEngine, before anything allocates pinned host memory, so first touch places that memory on the GPU-local node. Why core: core now has pinned-memory users whose bandwidth depends on NUMA placement -- FSDP2 CPU offload shards (CPUOffloadPolicy pins by default), EMA host copies, pinned batches, and the upcoming eager activation offloading. On GB300 (2 Grace CPUs, 2 GPUs each), a 1 GiB pinned copy from GPU0 measures: pinned memory on D2H H2D local node 193 GB/s 211 GB/s remote node 97 GB/s 100 GB/s Without binding, which node a rank's pinned memory lands on depends on where the scheduler ran the allocating thread, so a rank can silently get half the bandwidth. This also moves the existing calls into the engine. GraphTrainingEngine and the RL Trainer bound only after the model was built and sharded, i.e. after FSDP CPU offload had already pinned its shards. The vLLM generator keeps its own call (separate process, no TrainingEngine). The helper now logs the resulting affinity instead of claiming success unconditionally, and has a TODO for its private torch.numa dependency. AffinityMode.NODE intersects the node's CPUs with the current affinity, so it never widens a narrower launcher binding (torchrun --numa-binding=core-complex, SLURM --cpu-bind=cores); if the launcher bound to another node, torch.numa warns and leaves the affinity alone. Test plan: llama3 8B, FSDP over 4 GB300 GPUs, seq 8192, 2 x 8192 tokens per rank, 30 steps, runs interleaved on a shared host; tps over steps 6-30 (main = 98a8ce0): run mean tps median tps stdev median MFU main #1 22,022 22,055 721 51.09% this PR #1 22,182 22,146 745 51.30% this PR #2 22,179 22,167 750 51.35% main #2 22,024 21,949 735 50.85% Throughput is unchanged (+0.7%, within noise); the Grain loader's prefetch threads run in-process and are rebound with it. This revives #3519, which was redirected to graph_trainer (#3489) until core had a use for it.
d2c2448 to
0159faa
Compare
<!--
Reviewers need to be convinced, not just informed. Delete sections you
don't need.
Agents: follow .claude/skills/pr-description/SKILL.md. Never fill in the
Summary.
-->
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
this approach, and what you checked yourself. -->
This revives pytorch#3519, which was redirected to graph_trainer (pytorch#3489) until
core had a use for it.
Bind each training process to the NUMA node of its GPU right after
set_device in TrainingEngine, before anything allocates pinned host
memory, so first touch places that memory on the GPU-local node.
Why core: On GB300 (2 Grace CPUs, 2 GPUs each), a 1 GiB pinned copy from
GPU0 measures:
pinned memory on D2H H2D
local node 193 GB/s 211 GB/s
remote node 97 GB/s 100 GB/s
Without binding, which node a rank's pinned memory lands on depends on
where the scheduler ran the allocating thread, so a rank can silently
get half the bandwidth.
We are working on activation offloading, and this will ensure that there
is appropriate core affinity.
This also moves the existing calls into the engine. GraphTrainingEngine
and the RL Trainer bound only after the model was built and sharded,
i.e. after FSDP CPU offload had already pinned its shards. The vLLM
generator keeps its own call (separate process, no TrainingEngine).
## Testing
<!-- What you ran, and the regression each new test catches. -->
CI
(cherry picked from commit dae9de6)
Summary (human-written)
This revives #3519, which was redirected to graph_trainer (#3489) until core had a use for it.
Bind each training process to the NUMA node of its GPU right after set_device in TrainingEngine, before anything allocates pinned host memory, so first touch places that memory on the GPU-local node.
Why core: On GB300 (2 Grace CPUs, 2 GPUs each), a 1 GiB pinned copy from GPU0 measures:
pinned memory on D2H H2D
local node 193 GB/s 211 GB/s
remote node 97 GB/s 100 GB/s
Without binding, which node a rank's pinned memory lands on depends on where the scheduler ran the allocating thread, so a rank can silently get half the bandwidth.
We are working on activation offloading, and this will ensure that there is appropriate core affinity.
This also moves the existing calls into the engine. GraphTrainingEngine and the RL Trainer bound only after the model was built and sharded, i.e. after FSDP CPU offload had already pinned its shards. The vLLM generator keeps its own call (separate process, no TrainingEngine).
Testing
CI