Skip to content

[rl] Optionally bound the generators' weight fetch: concurrent fetches and per-module requests - #5585

Draft
wwwjn wants to merge 2 commits into
pytorch:mainfrom
wwwjn:rl-bounded-weight-fetch
Draft

wwwjn wants to merge 2 commits into
pytorch:mainfrom
wwwjn:rl-bounded-weight-fetch

Conversation

@wwwjn

@wwwjn wwwjn commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

TLDR

Two opt-in knobs bound the trainer-host memory burst of the generators' weight fetch: InterGeneratorRouter.Config.max_concurrent_prefetches caps how many generators fetch from TorchStore at once, and Generator.Config.fetch_weights_per_module splits each generator's fetch into one request per top-level module. Defaults keep today's behavior.

Problem

  1. InterGeneratorRouter.pull_model_state_dict starts every generator's prefetch_model_state_dict at once, and each generator rank fetches its whole weight shard with one ts.get_state_dict request.
  2. With TorchStore's NIXL transport, a trainer storage volume serving a batched get makes a contiguous copy of every non-contiguous slice in it up front and frees the copies only after the whole batch is written. So each in-flight get holds up to a copy of the slices it asked for on the trainer host.
  3. Cost: (non-contiguous bytes per get) x (gets in flight). With a large model and several generators this exceeds trainer host RAM at the first weight sync.

Solution

  1. InterGeneratorRouter.Config.max_concurrent_prefetches (default 0 = all): a semaphore around each generator's TorchStore fetch. Each generator still applies its weights CPU-to-GPU as soon as its own fetch ends. Replaces the TODO in pull_model_state_dict that asked for this stagger.
  2. Generator.Config.fetch_weights_per_module (default False): the same reads as get_state_dict, written in place into the same pinned receive buffers, but one get_batch per top-level module (each layers.<i>, then the remaining modules). Keys this rank has no buffer for are skipped, as with strict=False.

Unchanged: with both defaults, the fetch path is exactly today's single get_state_dict per generator rank, all generators at once.

Results

  • Muse Spark 816B on GB300 (one generator of 16 ranks, trainer on 64 hosts): runs on torchtitan main lost a trainer node about 25 s into the first weight pull; the same run with fetch_weights_per_module=True and max_concurrent_prefetches=1 pulled weights and trained.
  • Mechanism, reproduced on one GB300 host (one storage volume holding 64 bf16 tensors of 256 MiB; the client reads half of each): one batched get of 64 non-contiguous column slices (8 GiB) raised the volume's peak RSS by 7.93 GiB over NIXL; 64 gets of one slice each raised it by 0.02 GiB; contiguous row slices, 0.00 GiB.
  • Kimi K3 reduced-layer RL runs (12 GB300 hosts) use both knobs: weight sync pull 1.5-1.9 s per step.

NOTE: no A/B of fetch time with and without the knobs; per-module requests add round trips, so measure before turning them on for small models, where the burst is harmless.

NOTE: this bounds the burst from the generator side. A TorchStore-side change (copying non-contiguous slices in bounded groups inside the NIXL get handler) would bound it independently of how requests are split.

Test plan

  • New: test_max_concurrent_prefetches_staggers_weight_fetches (third generator waits for a fetch slot), test_group_fqns_by_module_splits_each_layer, test_prefetch_model_state_dict_per_module_fetches_in_place (one request per module, written into the pinned buffers themselves, unknown keys skipped). All three fail without the change.
  • pytest tests/rl/unit_tests/cpu/test_inter_generator_router.py tests/rl/unit_tests/cpu/test_generator.py tests/rl/unit_tests/cpu/test_engine_loop.py: 114 passed, 1 skipped (GB300 venv).
  • pre-commit run --files on the touched files: clean (Pyrefly skipped locally).

wwwjn added 2 commits October 22, 2026 11:28
Each generator's prefetch reads the trainer's CPU-staged weights through
TorchStore, and pull_model_state_dict starts every generator's prefetch at
once. A TorchStore NIXL get copies every non-contiguous slice it serves into a
staging buffer up front, so the trainer hosts briefly hold a copy of each
requesting generator's whole shard; with a large model and several generators,
concurrent fetches multiply that host RAM.

InterGeneratorRouter.Config.max_concurrent_prefetches caps the concurrent
TorchStore fetches with a semaphore; each generator's local CPU-to-GPU apply
still runs as soon as its own fetch ends. The default, 0, keeps fetching from
every generator at once. Replaces the TODO that asked for this stagger.
A generator's prefetch reads its whole weight shard with one TorchStore
get_state_dict request, so the trainer-side storage volumes stage and serve
all of it at once. For very large models that single burst can exhaust
trainer host RAM at the first weight sync.

Generator.Config.fetch_weights_per_module splits the fetch into one request
per top-level module (each layers.<i>, then the remaining modules), written in
place into the same pinned receive buffers. Keys this rank has no buffer for
are skipped, as with strict=False. The default keeps the single
get_state_dict request.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 22, 2026

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/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.

1 participant