Skip to content

Add precomputed routing for weight sync - #5061

Merged
tushar00jain merged 1 commit into
mainfrom
fs-head/draft/3
Oct 6, 2026
Merged

tushar00jain merged 1 commit into
mainfrom
fs-head/draft/3

Conversation

@tushar00jain

@tushar00jain tushar00jain commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack (top to bottom):


Integrate Titan to optionally use the new locally cached routing plan. Currently, we look up and creating the routing plan every time weights need to be pulled on the generator. This change speeds up weight transfer by dispatching the requests faster. This also unblocks features in Titan e.g. pipeline parallel that need merged state_dict's from multiple trainers and dtype conversion inside torchstore.

API Changes

  • Pass an enum to ts.initialize() that initializes torchstore in the new mode - a lightweight API that spawns ts actors
  • Have Titan explicitly initialize ts client with ts.client(role=...) - this tells ts who stores the data and who needs to get the data

How it works

There is some metadata exchange that happens lazily on the first get_state_dict and put_state_dict

First put_state_dict

The trainer ts clients send state dict metadata to a coordinator

First get_state_dict

The generator ts clients

  • Wait for all trainers to register their metadata with the coordinator
  • Build and cache a routing plan after they get all the metadata from trainers

@pytorch-bot pytorch-bot Bot added the ciflow/rl label Oct 5, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 5, 2026
@tushar00jain
tushar00jain force-pushed the fs-head/draft/3 branch 2 times, most recently from 42357b7 to 665426f Compare October 5, 2026 17:46
@tushar00jain
tushar00jain marked this pull request as ready for review October 5, 2026 17:51
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 5, 2026
Comment thread torchtitan/rl/controller.py Resolved
Comment thread torchtitan/rl/controller.py Resolved
@tushar00jain
tushar00jain force-pushed the fs-head/draft/3 branch 2 times, most recently from 891cef0 to da9e951 Compare October 6, 2026 01:50
@tushar00jain
tushar00jain requested a review from tianyu-l October 6, 2026 01:53
Comment thread torchtitan/rl/trainer.py Resolved
Comment thread torchtitan/rl/controller.py Resolved
wwwjn added a commit that referenced this pull request Oct 6, 2026
Stacked on #5061 (TorchStore routing weight sync).

- Lift the RL trainer PP restriction; context parallelism raises a
  ValueError until a follow-up adds support.
- Group RL microbatches into PP schedule steps (num_pp_microbatches is
  plumbed into the batcher) and broadcast loss metrics from the last stage.
- TrainingEngine packs per-microbatch loss kwargs (e.g. advantages,
  generator logprobs) into PP targets and keeps per-microbatch loss
  metrics, dropping the calls made by first-step metadata inference.
- Every PP stage publishes its partial state dict into the single
  "model_state_dict" TorchStore key, so generators do not depend on the
  trainer PP layout. This requires weight_sync.mode="routing" (#5061):
  the routing client merges every stage's MAPPING and checks full
  coverage, while controller mode reads MAPPING from a single volume and
  would silently drop the other stages. The controller rejects trainer PP
  without routing.
- Generators no longer request the vLLM attention scale buffers, which
  the trainer never publishes; routing mode errors on unpublished keys.
- Add CPU unit tests and an 8-GPU PP=2 x FSDP=2 integration test.

@tianyu-l tianyu-l left a comment

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.

neat

@tushar00jain
tushar00jain merged commit 92b40ce into main Oct 6, 2026
@tushar00jain
tushar00jain deleted the fs-head/draft/3 branch October 6, 2026 17:37
wwwjn added a commit that referenced this pull request Oct 6, 2026
- Lift the RL trainer PP restriction; context parallelism raises a
  ValueError until a follow-up adds support.
- Group RL microbatches into PP schedule steps (num_pp_microbatches is
  plumbed into the batcher) and broadcast loss metrics from the last stage.
- TrainingEngine packs per-microbatch loss kwargs (e.g. advantages,
  generator logprobs) into PP targets and keeps per-microbatch loss
  metrics, dropping the calls made by first-step metadata inference.
- Every PP stage publishes its partial state dict into the single
  "model_state_dict" TorchStore key, so generators do not depend on the
  trainer PP layout. The TorchStore routing client (#5061) merges every
  stage's publication and checks that the requested keys are covered.
- Generators no longer request the vLLM attention scale buffers, which
  the trainer never publishes; routing errors on unpublished keys.
- Add CPU unit tests and an 8-GPU PP=2 x FSDP=2 integration test.
wwwjn added a commit that referenced this pull request Oct 8, 2026
- Lift the RL trainer PP restriction; context parallelism raises a
  ValueError until a follow-up adds support.
- Group RL microbatches into PP schedule steps (num_pp_microbatches is
  plumbed into the batcher) and gather loss metrics from the rank that
  holds the last stage.
- Keep the shared TrainingEngine unchanged. Under PP the RL trainer
  builds PipelineTrainingEngine (torchtitan/rl/pipeline_parallel.py),
  which packs the per-token loss arguments (advantages, generator
  logprobs, loss mask, temperature) into the PP targets. PackedTargetLoss
  unpacks them inside ChunkedLossWrapper and records metrics, which are
  regrouped into one dict per microbatch; the leading calls made by
  first-step metadata inference are skipped.
- Every PP stage publishes its partial state dict into the single
  "model_state_dict" TorchStore key, so generators do not depend on the
  trainer PP layout. The TorchStore routing client (#5061) merges every
  stage's publication and checks that the requested keys are covered.
- Generators no longer request the vLLM attention scale buffers, which
  the trainer never publishes; routing errors on unpublished keys.
- Add CPU unit tests and an 8-GPU PP=2 x FSDP=2 integration test.
wwwjn added a commit that referenced this pull request Oct 19, 2026
**Depends on pytorch/pytorch#200238**, which adds
`step(loss_kwarg_mbs=...)` to pipeline schedules. Every PP run in
TorchTitan now passes it, so CI will fail until that change reaches the
torch nightly.

## Why

The RL trainer rejected pipeline parallelism for two reasons:

1. TorchStore could not publish a complete model from stage-local state
dicts. The routing client from #5061 fixes this: every stage publishes
into the same key.
2. The RL loss takes per-token arguments (advantages, generator
logprobs, loss mask, temperature) that differ per microbatch, and
pipeline schedules passed the same `loss_kwargs` to every microbatch.
pytorch/pytorch#200238 adds `loss_kwarg_mbs` for this, so TorchTitan no
longer needs an RL-side workaround.

## What changes

- **Engine (shared):** under PP, the engine copies
`global_loss_token_counts` into each microbatch's loss arguments and
passes them to the schedule as `loss_kwarg_mbs`, instead of raising "not
supported with pipeline parallelism yet". It also collects one loss
metrics dict per microbatch: the schedule's scalar loss wrapper fills a
per-microbatch `loss_metrics` dict, replacing it on each call because
the schedule also calls the loss on microbatch 0 while inferring stage
metadata. `ForwardBackwardResult.loss_metrics` is now nested like
`microbatch_groups` (one list per group, one dict per microbatch). The
pretraining trainers do not read these metrics.
- **GraphTrainer:** its traced loss takes one shared set of loss
arguments. It overrides `_pp_forward_backward_microbatch_group` to keep
passing the shared `loss_kwargs` to its runtime, and its existing
rejection of per-microbatch loss arguments now also covers its PP path.
- **RL trainer:**
- Lifts the PP restriction. Context parallelism still raises, because
per-token loss arguments are not sharded along the sequence yet.
- The batcher returns `TrainerStepBatch.microbatch_groups`
(`[num_groups][num_pp_microbatches][dp_degree]`), so each group runs as
one pipeline step. The controller sends each DP rank its grouped column;
the Monarch actor system now slices the trainer ranks as `(pp, dp, cp,
tp)` so a DP rank's slice spans every pipeline stage.
- Ranks without the last stage return no metrics, and the controller
takes the result from a rank that has it. V-style schedules (ZBV,
DualPipeV) put the last stage on the first PP rank, so this does not
assume a fixed rank.
- RDMA microbatch transfer (`async_loop.batcher.rdma_min_tensor_bytes`,
off by default) keeps the group structure, but is rejected together with
PP for now: its prefetcher reads one microbatch per group.
- Every stage publishes its partial state dict into the single
`model_state_dict` key, so generators do not depend on the trainer PP
layout.
- **CI:** new 8-GPU integration test
`rl_grpo_pp2_interleaved1f1b_fsdp2_gen_tp2`: Qwen3-0.6B, trainer PP=2
with Interleaved1F1B (two stages per rank) x FSDP=2, two TP=2
generators, untied embeddings.

## Test plan

- `tests/unit_tests/cpu/test_trainer.py`,
`tests/unit_tests/cpu/test_pipeline_parallel.py`, and GraphTrainer's
`test_trace_module.py` and `test_graph_pp_runner.py` pass on the
2026-10-07 CPU nightly with pytorch/pytorch#200238 applied. One GraphPP
test fails because the optional `dist_moe` package is not installed,
which is unrelated.
- RL CPU unit tests pass locally with vLLM stubbed out (no real vLLM on
that host); the per-file failure set matches `main`. The tests that
spawn Monarch processes (`test_batcher_actor.py`, the multi-rank RDMA
test) time out on that host on `main` too, so CI covers them.
- Not yet run on this version: the 8-GPU integration test. It needs vLLM
plus the PyTorch change; CI covers it once #200238 is in the nightly.
- An earlier version of this PR, which used an RL-side workaround
instead of the PyTorch change, ran an 8-step deterministic
batch-invariant run where PP=2 x FSDP=2 matched no-PP FSDP=2 bit for bit
on loss, entropy, and reward, with `logprob_diff/max = 0`. grad_norm
differed by 1 ULP at one step. This needs re-running on this version.
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