Repository navigation
[rl] Checkpoint + resume the RL training loop (#3735) - #3751
Conversation
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.
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.
|
Added a second commit fixing a pre-existing bug in the |
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", |
There was a problem hiding this comment.
maybe also set https://github.com/pytorch/torchtitan/blob/main/torchtitan/components/lr_scheduler.py#L57 to 4 so that across save / load the lr doesn't change.
| "--module alphabet_sort", | ||
| "--config rl_grpo_qwen3_0_6b_varlen", | ||
| "--num_steps 2", | ||
| "--trainer.parallelism.tensor_parallel_degree 2", |
There was a problem hiding this comment.
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
- trainer DCP checkpoint resharding
- trainer -> generator torchstore resharding
- mutli-generator vs. one generator robustness
There was a problem hiding this comment.
true, but is that common to have two different (parallel) config in the same run?
There was a problem hiding this comment.
what do you mean by "same run"
| await self.trainer.get_policy_version.call() | ||
| ) | ||
| if self.start_step > 0: | ||
| logger.info("Resuming RL training from step %d", self.start_step) |
There was a problem hiding this comment.
nit: use f"" string formatting
| 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", |
…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.
|
also, DCP is verified to work |
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 ranrange(1, num_steps + 1), so a preempted RL job restarted from step 1 anddiscarded all progress (hit on a multi-host Search-R1 run that lost ~6h after a
MAST restart).
What
trainer.save_checkpoint(step, last_step)each step, after thedivergence check (so a NaN step is not checkpointed).
CheckpointManagerwrites only on its
intervaland the final step; intermediate saves are full(model + optimizer + LR scheduler +
policy_version), hence resumable.PolicyTrainer.__init__already runsCheckpointManager.load().A new
get_policy_versionendpoint lets the controller read the restored step,resume the loop from
start_step + 1, and pull the generator weights at thematching policy version -- the RL-specific wrinkle from the issue: the
generators must return to the trainer's restored version, not just reload the
trainer.
search_r1enables mid-run checkpoints (interval=50, full lastsave,
keep_latest_k=3). The interval / keep-last-N are existingCheckpointManager.Configknobs -- no new config surface.search_r1bug: the generator was built withParallelismConfiginstead ofInferenceParallelismConfig, so the reciperaised
AttributeErrorat build. Folded in since this PR makes that reciperesumable and it should build.
policy_version== completed optim steps == checkpoint step, so it doubles asthe 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_resumeintegration test (4 GPU): two runs over a shareddump_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 savedstep-2(DCP metadata confirms model + optimizer + lr_scheduler +train_state.policy_version); run B loggedResuming RL training from step 2,trained steps 3-4, saved
step-4.