Skip to content

[rl] Checkpoint + resume the RL training loop (#3735) - #3751

Merged
yichuan-w merged 4 commits into
pytorch:mainfrom
yichuan-w:yichuan/rl-checkpoint-resume
Jun 24, 2026
Merged

yichuan-w merged 4 commits into
pytorch:mainfrom
yichuan-w:yichuan/rl-checkpoint-resume

Conversation

@yichuan-w

@yichuan-w yichuan-w commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Wire up mid-run checkpointing and resume for the RL trainer
(torchtitan/experiments/rl/). Fixes #3735.

Why

PolicyTrainer.save_checkpoint() existed but was never called, and the loop ran
range(1, num_steps + 1), so a preempted RL job restarted from step 1 and
discarded all progress (hit on a multi-host Search-R1 run that lost ~6h after a
MAST restart).

What

  • Save: call trainer.save_checkpoint(step, last_step) each step, after the
    divergence check (so a NaN step is not checkpointed). CheckpointManager
    writes only on its interval and the final step; intermediate saves are full
    (model + optimizer + LR scheduler + policy_version), hence resumable.
  • Resume: PolicyTrainer.__init__ already runs CheckpointManager.load().
    A new get_policy_version endpoint lets the controller read the restored step,
    resume the loop from start_step + 1, and pull the generator weights at the
    matching policy version -- the RL-specific wrinkle from the issue: the
    generators must return to the trainer's restored version, not just reload the
    trainer.
  • Recipe: search_r1 enables mid-run checkpoints (interval=50, full last
    save, keep_latest_k=3). The interval / keep-last-N are existing
    CheckpointManager.Config knobs -- no new config surface.
  • Also fixes a pre-existing search_r1 bug: the generator was built with
    ParallelismConfig instead of InferenceParallelismConfig, so the recipe
    raised AttributeError at build. Folded in since this PR makes that recipe
    resumable and it should build.

policy_version == completed optim steps == checkpoint step, so it doubles as
the resume counter and the generator sync version. A fresh run (empty folder)
loads HF weights as before; once a DCP checkpoint exists in the folder, restart
resumes from it.

Test

New rl_grpo_checkpoint_resume integration test (4 GPU): two runs over a shared
dump_folder -- the first trains 2 steps and writes a full checkpoint at step 2;
the second resumes from it and trains through step 4 (the second run errors if
resume is broken).

Manually verified on alphabet_sort rl_grpo_qwen3_0_6b_flex: run A saved
step-2 (DCP metadata confirms model + optimizer + lr_scheduler +
train_state.policy_version); run B logged Resuming RL training from step 2,
trained steps 3-4, saved step-4.

The RL trainer could save but never did: PolicyTrainer.save_checkpoint was dead
code and the loop always ran range(1, num_steps + 1), so a preempted run
restarted from step 1 and threw away all progress.

Wire it up:
- Call trainer.save_checkpoint(step, last_step) each step after the divergence
  check. CheckpointManager honors its own interval and the final step
  (off-interval steps no-op); intermediate saves are full
  (model + optimizer + LR scheduler + policy_version), so they are resumable.
- The trainer's __init__ already runs CheckpointManager.load(); add a
  get_policy_version endpoint so the controller reads the restored step, resumes
  the loop from start_step + 1, and pulls the generator weights at the matching
  policy version (the RL wrinkle: generators must return to the trainer's
  version, not just reload the trainer).
- search_r1 recipe: enable mid-run checkpoints (interval=50, full last save,
  keep_latest_k=3) so the motivating preemptible run can resume.

policy_version == completed optim steps == checkpoint step, so it doubles as the
resume counter and the generator sync version.

Verified on alphabet_sort rl_grpo_qwen3_0_6b_flex (4 GPU): run A trained steps
1-2 and saved step-2 (DCP metadata confirms model + optimizer + lr_scheduler +
train_state.policy_version); run B loaded step-2, logged "Resuming RL training
from step 2", trained steps 3-4, and saved step-4.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 23, 2026
The generator was built with ParallelismConfig, but
VLLMGenerator.Config.parallelism is an InferenceParallelismConfig
(data_parallel_degree / tensor_parallel_degree). Building the recipe therefore
raised AttributeError: 'ParallelismConfig' object has no attribute
'data_parallel_degree' in VLLMGenerator.Config.__post_init__. Use the right
type. Pre-existing bug, surfaced while enabling checkpoint/resume for this
recipe.
@yichuan-w

Copy link
Copy Markdown
Member Author

Added a second commit fixing a pre-existing bug in the search_r1 recipe: the generator was built with ParallelismConfig but VLLMGenerator.Config.parallelism expects InferenceParallelismConfig, so the recipe raised AttributeError: 'ParallelismConfig' object has no attribute 'data_parallel_degree' at build. Folded in here since this PR makes that recipe resumable and it should actually build.

Add an rl_grpo_checkpoint_resume integration test: two runs over a shared
dump_folder where the first writes a checkpoint at step 2 and the second resumes
from it through step 4 (the second run errors if resume is broken). Tighten the
comments added in this PR.
[
"--module alphabet_sort",
"--config rl_grpo_qwen3_0_6b_varlen",
"--num_steps 2",

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.

"--module alphabet_sort",
"--config rl_grpo_qwen3_0_6b_varlen",
"--num_steps 2",
"--trainer.parallelism.tensor_parallel_degree 2",

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.

If just 4 gpus:

  • can we test trainer DP2 -> trainer TP2 DCP resharding? generator can stay TP2 as it's the only thing we support today

What's better:

  • let's use 8 gpus
  • first run: trainer DP2, 2 generators each TP2
  • second run: trainer TP2, one generator with TP4

This will test

  1. trainer DCP checkpoint resharding
  2. trainer -> generator torchstore resharding
  3. mutli-generator vs. one generator robustness

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

true, but is that common to have two different (parallel) config in the same run?

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.

what do you mean by "same run"

Comment thread torchtitan/experiments/rl/trainer.py Outdated
await self.trainer.get_policy_version.call()
)
if self.start_step > 0:
logger.info("Resuming RL training from step %d", self.start_step)

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.

nit: use f"" string formatting

Comment thread torchtitan/experiments/rl/trainer.py Outdated
logger.info(f"Pre-training validation; then {num_steps} steps of RL training")
start_step = self.start_step # 0 fresh, >0 when resuming a checkpoint
logger.info(
"Pre-training validation; then RL training steps %d..%d",

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.

similar

…otal_steps)

Per review:
- Make the resume integration test 8-GPU and exercise resharding: run 1 trainer
  DP=2 + 2 generators (TP=2) writes the checkpoint; run 2 trainer TP=2 + 1
  generator (TP=4) resumes from it. Covers DCP checkpoint resharding (DP->TP),
  trainer->generator TorchStore resharding, and multi- vs single-generator.
- Pin lr_scheduler.total_steps=4 in both runs so the LR is identical across
  save/load (the resumed run continues the same curve).
- f-string formatting for the two resume log lines.

Validated locally on 8 GPUs: run 1 saved a 2-shard DP=2 checkpoint at step 2;
run 2 loaded it into the TP=2 layout, resumed from step 2 (lr continuing the
4-step decay 2e-6 -> 1e-6), trained steps 3-4, saved step 4.
@yichuan-w
yichuan-w requested a review from tianyu-l June 24, 2026 00:19
@yichuan-w

Copy link
Copy Markdown
Member Author

also, DCP is verified to work

@yichuan-w
yichuan-w merged commit c6c2fb2 into pytorch:main Jun 24, 2026
8 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl ciflow/8gpu 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.

[RL] No mid-run checkpoint/resume in the RL training loop -- preemption loses all progress

2 participants