Repository navigation
[rl] Set the trainer's CPU thread count after NUMA binding - #5447
Open
felipemello1 wants to merge 1 commit into
Open
felipemello1 wants to merge 1 commit into
felipemello1 wants to merge 1 commit into
Conversation
NUMA binding limits the trainer to one node's CPUs, but torch keeps one thread per physical core of the host. Each per-tensor CPU copy in the weight push's TorchStore put then oversubscribes the node. Give each rank the node's physical cores divided by the trainer ranks on that node.
tianyu-l
reviewed
Oct 17, 2026
| engine = self.engine | ||
| self._controller_state = MirroredState() | ||
| maybe_apply_numa_binding(engine.device.index, engine.device.type) | ||
| if engine.device.type == "cuda": |
Contributor
There was a problem hiding this comment.
can we put all these into maybe_apply_numa_binding function one line above
This branch has not been deployed
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.
TLDR: The RL trainer binds itself to one NUMA node but keeps torch's thread count for the whole host, so the weight push's CPU copies oversubscribe the node. This PR gives each trainer rank its share of the node's cores. The push drops from 1.05 s to 0.08 s at Qwen3-0.6B; in a 35B microbench the put drops from 13.4 s to 0.76 s per rank.
Problem
Trainer.__init__callsmaybe_apply_numa_binding, which limits the trainer process to the CPUs of its GPU's NUMA node: 96 of the host's 192 cores on our 2-socket H100 hosts.Solution
Trainer.__init__callstorch.set_num_threads(_num_cpu_threads_per_rank(device_index=...))._num_cpu_threads_per_rankdivides the physical cores the process is bound to by the trainer ranks on its NUMA node (one rank per visible GPU, astrain.pylaunches them).__init__. Each thread applies torch's thread count on its first parallel CPU op (ATen'slazy_init_num_threadsis thread-local). The put runs in anasyncio.to_threadworker. Once that worker has run a copy, a laterset_num_threadsdoesn't reach it.Results
Numerics: unchanged. Training math runs on the GPU; this PR changes only how many CPU threads the copies use.
forward_backwardthen started during the push, and the push could reply only after it (2.98-3.22 s). A follow-up PR awaits the push beforeforward_backward; that is only cheap with this fix.forward_backwardof the PR run spent 16.6 s in its firstpreprocess_inputs(main: 0.04 s). Two more runs with this change took 0.036 and 0.047 s, and no compile cache was written during it. I think it was a host stall, not this PR.You may ask:
torch.cuda.device_count()instead of the GPUs on the node? A trainer on all 8 GPUs would get 12 threads instead of 24.AffinityMode.EXCLUSIVE? It narrows each rank's CPUs further but leaves torch at 192 threads.maybe_apply_numa_bindingare the RL generator, where vLLM already sets torch to 1 thread, andexperiments/graph_trainer. The core trainer doesn't bind.NOTE:
_num_cpu_threads_per_rankreads the GPU's NUMA node from sysfs, like the binding does. The binding falls back silently when that fails; this helper raises.Follow up
experiments/graph_trainerbinds the same way and likely has the same oversubscription for its CPU work.Test plan
timing/weight_sync/trainer_push_model_state_dict, trainer spans).CUDA_VISIBLE_DEVICES=0-> 96,0,1,2,3-> 24,0-7-> 24,4,5-> 48.pytest tests/rl/unit_tests/cpu: same results as main (7 failures and 2 collection errors in verifiers/search-r1 tests that also fail on main in my env).