Repository navigation
Conversation
[ghstack-poisoned]
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - Ready for human review:
Findings (1): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/204113
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit c035ad3 with merge base 02a1dc0 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
There was a problem hiding this comment.
Automated review of c035ad3: nothing blocking; ready for human review.
For host (CPU) memory, the PR changes the Mooncake registration location from "cpu:0" to "" so Mooncake picks NICs from the buffer's NUMA node, and updates the test to check that "" is passed. Recommendation: Approve. The only point is a non-blocking request to document or check the minimum Mooncake version.
⚪ 1 non-blocking finding
Non-blocking: the Mooncake >= 0.3.14 requirement is not checked at runtime and is not in any user-facing doc. The version floor appears only in this inline comment. _load_backend() and supported() only check that mooncake.engine imports and has TransferEngine, the MooncakeTransport docstring says only that Mooncake is an optional dependency, the pyproject.toml entry point declares no version, and docs/source/distributed/transport.md has no Mooncake section. The PR body says versions before 0.3.14 may resolve "*" before pinning and treat every NIC as preferred. On those versions registration still succeeds with no error or warning, so host buffers on NUMA node 0, which used node 0's NICs before this change, can be spread across the socket interconnect. That is a silent throughput regression with no correctness impact. The module is private and experimental, so this is not a BC break. Suggested fix: state the minimum version (and that the NUMA node comes from the buffer's first page) in the MooncakeTransport docstring. Optionally, read importlib.metadata.version("mooncake-transfer-engine") once in __init__ and keep "cpu:0" below 0.3.14.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: staging (cast and restage on GPU, D2H into the staging buffer) runs on the push stream from the actor thread, once per optimizer step, by whichever of `push` and the next `forward_backward` reaches each rank first. Restaging is a collective, so this keeps it in the same order relative to training's collectives on every rank regardless of message order. `push` then awaits the stream in a worker thread.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0`.
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +218/-18 (incl. 6 lines in `scripts/install_rl_env.sh`), docs +101/-8, tests +65 (a trainer test checking `forward_backward` and `push` stage once per optimizer step on the push stream from the actor thread and `push` synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: 92c1efb
Pull-Request: #5997
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: staging (cast and restage on GPU, D2H into the staging buffer) runs on the push stream from the actor thread, once per optimizer step, by whichever of `push` and the next `forward_backward` reaches each rank first. Restaging is a collective, so this keeps it in the same order relative to training's collectives on every rank regardless of message order. `push` then awaits the stream in a worker thread.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0`.
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +218/-18 (incl. 6 lines in `scripts/install_rl_env.sh`), docs +101/-8, tests +65 (a trainer test checking `forward_backward` and `push` stage once per optimizer step on the push stream from the actor thread and `push` synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: 4c5d209
Pull-Request: #5997
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: staging (cast and restage on GPU, D2H into the staging buffer) runs on the push stream from the actor thread, once per optimizer step, by whichever of `push` and the next `forward_backward` reaches each rank first. Restaging is a collective, so this keeps it in the same order relative to training's collectives on every rank regardless of message order. `push` then awaits the stream in a worker thread.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0`.
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +218/-18 (incl. 6 lines in `scripts/install_rl_env.sh`), docs +102/-8, tests +65 (a trainer test checking `forward_backward` and `push` stage once per optimizer step on the push stream from the actor thread and `push` synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: 95da8a1
Pull-Request: #5997
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: staging (cast and restage on GPU, D2H into the staging buffer) runs on the push stream from the actor thread, once per optimizer step, by whichever of `push` and the next `forward_backward` reaches each rank first. Restaging is a collective, so this keeps it in the same order relative to training's collectives on every rank regardless of message order. `push` then awaits the stream in a worker thread.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0`.
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +218/-18 (incl. 6 lines in `scripts/install_rl_env.sh`), docs +102/-8, tests +65 (a trainer test checking `forward_backward` and `push` stage once per optimizer step on the push stream from the actor thread and `push` synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: 1024af6
Pull-Request: #5997
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: staging (cast and, where needed, restage on GPU, D2H into the staging buffer) runs on the push stream from the actor thread, once per optimizer step, by whichever of `push` and the next `forward_backward` reaches each rank first. Restaging is a collective, so this keeps it in the same order relative to training's collectives on every rank regardless of message order. `push` then awaits the stream in a worker thread.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0` with `--no-deps` (nixl-cu13 requires torch).
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
Trainer staging, same 27B runs (trainer rank 0). Staging runs on the push stream overlapped with the next `forward_backward`; the trainer blocks only on `push`'s wait (`timing/step/blocking_trainer_push_model_state_dict`). This layout (FSDP 2 x TP 2 -> TP 1) still restages row-parallel weights.
| | TorchStore + NIXL | This PR (Mooncake / NIXL) |
| --- | --- | --- |
| Trainer blocking per step | 0-3 ms | 0-3 ms |
| Full stage, not overlapped | 0.39-0.41 s | 0.12 s |
| `forward_backward` incl. overlapped staging | 44.8-45.3 s | 44.5-45.3 s |
| Init push | 2.19 s | 0.17-0.19 s |
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +220/-18 (incl. 8 lines in `scripts/install_rl_env.sh`), docs +104/-8, tests +65 (a trainer test checking `forward_backward` and `push` stage once per optimizer step on the push stream from the actor thread and `push` synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: b50b9bf
Pull-Request: #5997
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: staging (cast and, where needed, restage on GPU, D2H into the staging buffer) runs on the push stream from the actor thread, once per optimizer step, by whichever of `push` and the next `forward_backward` reaches each rank first. Restaging is a collective, so this keeps it in the same order relative to training's collectives on every rank regardless of message order. `push` then awaits the stream in a worker thread.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0` with `--no-deps` (nixl-cu13 requires torch).
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
Trainer staging, same 27B runs (trainer rank 0). Staging runs on the push stream overlapped with the next `forward_backward`; the trainer blocks only on `push`'s wait (`timing/step/blocking_trainer_push_model_state_dict`). This layout (FSDP 2 x TP 2 -> TP 1) still restages row-parallel weights.
| | TorchStore + NIXL | This PR (Mooncake / NIXL) |
| --- | --- | --- |
| Trainer blocking per step | 0-3 ms | 0-3 ms |
| Full stage, not overlapped | 0.39-0.41 s | 0.12 s |
| `forward_backward` incl. overlapped staging | 44.8-45.3 s | 44.5-45.3 s |
| Init push | 2.19 s | 0.17-0.19 s |
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +229/-18 (incl. 8 lines in `scripts/install_rl_env.sh`), docs +104/-8, tests +65 (a trainer test checking `forward_backward` and `push` stage once per optimizer step on the push stream from the actor thread and `push` synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: c360b8f
Pull-Request: #5997
…config flag
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it).
## What changed
- `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time.
- `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads.
- Trainer: `push` stages (cast and, where needed, restage on GPU, D2H into the staging buffer) on the push stream from the actor thread before its first `await`, then awaits the stream in a worker thread. Restaging is a collective, so the controller sends the push before the next `forward_backward`; Monarch starts concurrent endpoints from one sender in send order, so every rank stages before its training collectives.
- `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0` with `--no-deps` (nixl-cu13 requires torch).
- Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged.
- `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle.
## User-facing changes
| Before | After |
| --- | --- |
| TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` |
## Numerics
The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged.
`scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps.
## Performance
14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL.
| | TorchStore + NIXL | This PR (Mooncake) |
| --- | --- | --- |
| Generator pull | 10.0-13.2 s | 0.62-0.90 s |
| of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) |
| of which H2D load | - | 0.37-0.49 s |
| Trainer push | 3.2-4.0 s | 0.5-0.9 s |
| blocking the trainer | 2.1-3.2 s | ~0 |
| Throughput | 60-74 tok/s | 610-880 tok/s |
| Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB |
| Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB |
| Init span | 63.3 s | 62.5 s |
Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator.
| | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) |
| --- | --- | --- | --- |
| First pull | 22.5 s | 4.6 s | 0.95 s |
| Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s |
| Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) |
| Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 |
| tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k |
| Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB |
| Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB |
The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS.
Trainer staging, same 27B runs (trainer rank 0). Staging runs on the push stream overlapped with the next `forward_backward`; the trainer blocks only on `push`'s wait (`timing/step/blocking_trainer_push_model_state_dict`). This layout (FSDP 2 x TP 2 -> TP 1) still restages row-parallel weights.
| | TorchStore + NIXL | This PR (Mooncake / NIXL) |
| --- | --- | --- |
| Trainer blocking per step | 0-3 ms | 0-3 ms |
| Full stage, not overlapped | 0.39-0.41 s | 0.12 s |
| `forward_backward` incl. overlapped staging | 44.8-45.3 s | 44.5-45.3 s |
| Init push | 2.19 s | 0.17-0.19 s |
The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s).
## Size
Implementation +230/-22 (incl. 8 lines in `scripts/install_rl_env.sh`), docs +104/-8, tests +46 (a trainer test checking `push` stages on the push stream from the actor thread before its first `await` and synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan.
## Testing
- 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0.
- 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`.
- Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only).
- The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113.
- `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass).
- `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`).
## Known issues / follow-ups
- Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it.
- The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later.
- No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging.
- Delta sync: stage and read only the changed pieces.
- The H2D load from the prefetch buffers (0.4 s) now dominates the pull.
- Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP.
- Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way.
- Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas.
AI-assisted (Claude).
ghstack-source-id: 7d03b40
Pull-Request: #5997
…config flag (#5997) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.18.0) (oldest at bottom): * #6097 * __->__ #5997 ## Summary (human-written) <!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why this approach, and what you checked yourself. --> ## Why Wires the previous PR's `ModelStateDictTransport` into the RL loop as an opt-in replacement for TorchStore, so it can be rolled out per job. With 14B on one 8xB200 (183 GB) host it cuts the generator pull from 10-13 s to 0.6-0.9 s with pytorch/pytorch#204113 (1.7-2.35 s without it). ## What changed - `Controller.Config.model_state_dict_transport: ModelStateDictTransport.Config | None`. None (the default) keeps TorchStore. Setting it requires `generator.model_state_dict_load_mode="cpu_prefetch"`, which is validated at config time. - `ActorSystem.initialize_model_state_dict_transport` (Monarch impl): every generator reports its ranks' `shard_layout`s (which must match), the trainer ranks register staging buffers for it, and every generator rank connects and plans its reads. - Trainer: `push` stages (cast and, where needed, restage on GPU, D2H into the staging buffer) on the push stream from the actor thread before its first `await`, then awaits the stream in a worker thread. Restaging is a collective, so the controller sends the push before the next `forward_backward`; Monarch starts concurrent endpoints from one sender in send order, so every rank stages before its training collectives. - `scripts/install_rl_env.sh` installs `mooncake-transfer-engine-cuda13==0.3.14`, `nixl==1.5.0` and `nixl-cu13==1.5.0` with `--no-deps` (nixl-cu13 requires torch). - Generator prefetch: `read()` into the registered prefetch buffers replaces the TorchStore get. The load into the model is unchanged. - `torchtitan/rl/MODEL_STATE_DICT_TRANSPORT.md`: layout diagram and lifecycle. ## User-facing changes | Before | After | | --- | --- | | TorchStore only | `config.model_state_dict_transport = ModelStateDictTransport.Config(backend="mooncake" or "nixl")` | ## Numerics The trainer casts every tensor not in `preserve_dtype_keys` to `transfer_dtype` before the transfer; buffers and `preserve_dtype_params` keep their dtype. A dtype mismatch with the generator raises at init. The generator load is unchanged. `scripts/rl/loss_compare.py` (Qwen3-0.6B, deterministic, seed 42, 10 steps) with this flag matches TorchStore exactly: `--assert-equal` passes on loss for all 10 steps. ## Performance 14B (29.6 GB bf16), one 8xB200 (183 GB) host with 8x400G NICs, trainer FSDP 4 (GPUs 0,2,3,4), one generator TP 2 (GPUs 5,6), 10 steps. Baseline: `main` 8a33dbf + TorchStore 804ae43 over NIXL. | | TorchStore + NIXL | This PR (Mooncake) | | --- | --- | --- | | Generator pull | 10.0-13.2 s | 0.62-0.90 s | | of which RDMA read | 9-12 s | 0.20-0.35 s (~60 GB/s per generator rank, ~120 GB/s total) | | of which H2D load | - | 0.37-0.49 s | | Trainer push | 3.2-4.0 s | 0.5-0.9 s | | blocking the trainer | 2.1-3.2 s | ~0 | | Throughput | 60-74 tok/s | 610-880 tok/s | | Peak GPU, trainer / generator | 85.0 / 37.4 GB | 78.3 / 37.4 GB | | Peak RSS, trainer / generator | 10.4 / 38.6 GiB | 11.2 / 25.4 GiB | | Init span | 63.3 s | 62.5 s | Qwen3.5 27B (53.8 GB bf16) on MAST, 2 GB300 hosts (4 NICs each), DAPO-Math recipe: trainer FSDP 2 x TP 2 on one host, 4 TP 1 vLLM generators on the other (each pulls the full model), 6 steps of 4 prompts x 16 samples. Baseline: same recipe on TorchStore over NIXL. Pull times are for the slowest generator. | | TorchStore + NIXL | This PR (Mooncake) | This PR (NIXL) | | --- | --- | --- | --- | | First pull | 22.5 s | 4.6 s | 0.95 s | | Steady pulls (pulls 2-7) | 15.6-16.8 s | 2.2-2.8 s (fastest generator 1.2-1.4 s) | 0.96-1.08 s | | Effective rate, all 4 generators | ~13 GB/s | 77-98 GB/s | ~200-225 GB/s (50-56 GB/s per generator) | | Blocking pull / step time (steps 2-6) | 0.20-0.28 | 0-0.064 | 0-0.034 | | tok/s full step, mean of steps 2-6 | 13.0k | 16.9k | 15.7k | | Trainer model GPU memory | 31.75 GiB | 31.75 GiB | 31.75 GiB | | Generator KV cache | 196.22 GiB | 196.22 GiB | 196.22 GiB | The tok/s values vary with rollout length (baseline 7.8k-15.8k per step, Mooncake 11.2k-20.8k, NIXL 3.2k-20.1k). The MAST logs do not record host RSS. Trainer staging, same 27B runs (trainer rank 0). Staging runs on the push stream overlapped with the next `forward_backward`; the trainer blocks only on `push`'s wait (`timing/step/blocking_trainer_push_model_state_dict`). This layout (FSDP 2 x TP 2 -> TP 1) still restages row-parallel weights. | | TorchStore + NIXL | This PR (Mooncake / NIXL) | | --- | --- | --- | | Trainer blocking per step | 0-3 ms | 0-3 ms | | Full stage, not overlapped | 0.39-0.41 s | 0.12 s | | `forward_backward` incl. overlapped staging | 44.8-45.3 s | 44.5-45.3 s | | Init push | 2.19 s | 0.17-0.19 s | The Mooncake numbers depend on pytorch/pytorch#203623 (poller race; without it the run hangs) and pytorch/pytorch#204113 (register host memory with a NUMA-detected location; without it numa1 buffers use numa0 NICs and the read takes 1.3-1.7 s). ## Size Implementation +230/-22 (incl. 8 lines in `scripts/install_rl_env.sh`), docs +104/-8, tests +46 (a trainer test checking `push` stages on the push stream from the actor thread before its first `await` and synchronizes in a worker thread, plus a new attribute in existing tests); the unit tests in the previous PR cover the layout and read plan. ## Testing - 10-step 14B runs on 8xB200 with both configurations (table above); the transport run completed with rc=0. - 6-step 27B runs on MAST with all three configurations (table above); all completed 6 steps. The NIXL run used the same torch and this stack's `model_state_dict_transport.py`, nixl 1.5.0 over UCX with `UCX_NET_DEVICES` set to the 4 NICs, `UCX_TLS=rc,cuda_copy,self` and `UCX_MAX_RMA_RAILS=4`. - Loss compare above on 8xB200 (TorchStore arm exported, transport arm imported with `--assert-equal`), on a revision whose `torchtitan/` code is identical to this stack (later changes are tests and docs only). - The 14B runs used an earlier revision of this stack whose staging buffer used `pin_memory=True` (rounded up to a power of two) instead of cudaHostRegister; later review fixes (names, read timeout, hardcoded backend, docs/tests) do not change the transfer path. The 27B MAST run uses a revision with the same staging, read and stream-sync code, differing only in names, config plumbing and the generator-layout check, with torch 2.16.0.dev20261030+cu132 plus pytorch/pytorch#203623 and pytorch/pytorch#204113. - `pytest tests/rl/unit_tests/cpu/{test_generator,test_model_state_dict_transport,test_trainer,test_engine_loop,test_async_controller,test_shutdown}.py` (188 pass). - `pre-commit run` on the changed files passes (pyrefly skipped; it also fails on `main`). ## Known issues / follow-ups - Only `model_state_dict_load_mode="cpu_prefetch"` is supported; `gpu_fetch` still needs TorchStore, so the TorchStore path stays until this covers it. - The torch pinned by `scripts/install_rl_env.sh` (via vLLM, 2026-11-07) predates pytorch/pytorch#203623, so the stock env can time out on a read until the pin moves to 2026-11-10 or later. - No CI job runs the flag end to end (it needs RDMA NICs); the unit tests cover the layout, read plan and trainer staging. - Delta sync: stage and read only the changed pieces. - The H2D load from the prefetch buffers (0.4 s) now dominates the pull. - Restaging a tensor whose first sharded dim is not the outermost sharded mesh axis (e.g. MoE experts `(Shard(1), Shard(0))` -> `(Shard(0), Shard(0))`) can make DTensor gather it over that axis on the GPU during staging (untested at scale), costing transient memory at large EP. - Reads per pull scale with keys x trainer ranks (one per contiguous run); a batched read upstream in `torch.distributed._transport` would cut the per-read overhead. Init metadata (every trainer rank's pieces for every generator rank) scales the same way. - Every trainer DP rank keeps its own pinned staging copy of its shard; with replicated parameters (HSDP replicas, EP replicas of dense weights) this duplicates host memory across replicas. AI-assisted (Claude).
Stack from ghstack (oldest at bottom):
Mooncake picks the NICs for a registered buffer from its location. The transport passed "cpu:0" for all host memory, so buffers on other NUMA nodes used node 0's NICs and crossed the socket interconnect. With "", Mooncake detects the buffer's NUMA node from its first page. Requires Mooncake >= 0.3.14 (kvcache-ai/Mooncake#4333); older versions may resolve "" before pinning and treat every NIC as preferred.
Measured on an 8xH100 host with 4x400G NICs per socket: a reader on numa1 pulling from pinned numa1 memory went from ~9 GB/s to ~30 GB/s, ~42 GB/s across numa1 readers. In TorchTitan RL weight sync (14B, 2 generator ranks on numa1) the RDMA read dropped from 1.3-1.7 s to 0.20-0.35 s per rank.
Implementation +3/-1, test +6/-3.
Test plan:
python test/distributed/test_transport_mooncake.pyon an 8xH100 host with Mooncake. The updatedtest_registration_reuse_and_validationasserts host memory registers with "*" and fails on the old "cpu:0".AI-assisted (Claude).