Skip to content

[rl] Set the trainer's CPU thread count after NUMA binding - #5447

Open
felipemello1 wants to merge 1 commit into
mainfrom
felipemello1/weight-push/0-trainer-num-threads
Open

felipemello1 wants to merge 1 commit into
mainfrom
felipemello1/weight-push/0-trainer-num-threads

Conversation

@felipemello1

Copy link
Copy Markdown
Contributor

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

  1. Binding. Trainer.__init__ calls maybe_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.
  2. Threads. torch keeps its default thread count, one per physical core of the whole host: 192.
  3. The push. After each optimizer step, the trainer's TorchStore put copies every tensor of its CPU snapshot into shared memory. Each copy starts 192 threads on the node's 96 cores.
  4. Cost. The push takes 1.05 s at Qwen3-0.6B, and only 0.03 s of it is the GPU -> CPU copy. At 35B with 3 trainer ranks on a node, the put takes 13.4 s per rank.

Solution

  1. Right after binding, Trainer.__init__ calls torch.set_num_threads(_num_cpu_threads_per_rank(device_index=...)).
  2. _num_cpu_threads_per_rank divides the physical cores the process is bound to by the trainer ranks on its NUMA node (one rank per visible GPU, as train.py launches them).
2 sockets x 96 cores x 2 threads; GPUs 0-3 on node 0, GPUs 4-7 on node 1
trainer on GPU 0       192 logical CPUs / 2 per core / 1 rank on node 0   = 96 threads
trainer on GPUs 0-3    192 logical CPUs / 2 per core / 4 ranks on node 0  = 24 threads
trainer on GPUs 0-7    192 logical CPUs / 2 per core / 4 ranks per node   = 24 threads
  1. Why in __init__. Each thread applies torch's thread count on its first parallel CPU op (ATen's lazy_init_num_threads is thread-local). The put runs in an asyncio.to_thread worker. Once that worker has run a copy, a later set_num_threads doesn't reach it.
  2. Unchanged: trainers on other devices (binding is a no-op there), the generator, and the other trainers.

Results

Numerics: unchanged. Training math runs on the GPU; this PR changes only how many CPU threads the copies use.

Qwen3-0.6B RL run: multi-turn debug env (12 turns), 4 prompts x 16 samples, trainer and generator on 1 H100 each,
TorchStore over TCP fallback, 6 steps, same upstream main for both arms. Trainer spans from the structured log.

                                                         main              this PR
TorchStore put (its own log line), warm pushes           0.91-1.13 s       0.036-0.063 s
push_model_state_dict span, pushes that ran alone        0.95-1.18 s (6)   0.077-0.087 s (3)
first push (allocates the shared memory)                 1.97 s            0.82 s
timing/step/forward_backward, median of steps 2-6        2.967 s           2.974 s
TorchStore put microbench: real routing put from CPU-only Monarch actors, NUMA-bound like the trainer,
warm medians. Qwen3-0.6B: 1 rank on node 0. 35B: Qwen3.5-35B-A3B shapes (69 GB bf16), 4 ranks with dim-0
shards (17.3 GB each) pushing at once, 3 on node 0 and 1 on node 1; max over ranks.

                          default threads   this PR's count
0.6B put                  1.60 s            0.046 s   (96 threads)
35B put per rank          13.4 s            0.76 s    (32 threads on node 0)
  • In 3 of the 5 measured steps of the RL run with this PR, the next batch was ready before the push ended. forward_backward then started during the push, and the push could reply only after it (2.98-3.22 s). A follow-up PR awaits the push before forward_backward; that is only cheap with this fix.
  • The first forward_backward of the PR run spent 16.6 s in its first preprocess_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:

  • Why not a fixed count, like 16? The right count depends on the host. At 35B, 16 threads put in 0.85 s against 0.76 s for 32.
  • Why not count torch.cuda.device_count() instead of the GPUs on the node? A trainer on all 8 GPUs would get 12 threads instead of 24.
  • Why not bind with AffinityMode.EXCLUSIVE? It narrows each rank's CPUs further but leaves torch at 192 threads.
  • Why only the RL trainer? The other callers of maybe_apply_numa_binding are the RL generator, where vLLM already sets torch to 1 thread, and experiments/graph_trainer. The core trainer doesn't bind.

NOTE:

  • Measured on H100 hosts (AMD EPYC, 2 x 96 cores, 2 threads per core). Needs retesting on Blackwell hosts (Grace CPUs, no SMT, 2 GPUs per NUMA node), where the formula gives 72 / 2 = 36 threads.
  • _num_cpu_threads_per_rank reads 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_trainer binds the same way and likely has the same oversubscription for its CPU work.
  • The generator has the opposite problem: vLLM runs torch on 1 thread, so its TorchStore get copies on one thread (estimated ~4 s at 35B).

Test plan

  • GPU: the Results RL run, main vs this PR (timing/weight_sync/trainer_push_model_state_dict, trainer spans).
  • The helper on this host: 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).

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.
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 16, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 16, 2026
engine = self.engine
self._controller_state = MirroredState()
maybe_apply_numa_binding(engine.device.index, engine.device.type)
if engine.device.type == "cuda":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put all these into maybe_apply_numa_binding function one line above

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants