rl icon indicating copy to clipboard operation
rl copied to clipboard

[Feature] Add Learner primitive (LocalLearner, FSDP2Learner)

Open theap06 opened this issue 5 months ago • 1 comments

Summary

Introduces torchrl.trainers.Learner: a backend-agnostic entry point for taking one optimization step on a tensordict batch with a given LossModule. LocalLearner is the single-process reference implementation; FSDP2Learner shards the same model with fully_shard and reuses the training step unchanged.

Design

  • Learner.update() is concrete, in the base class, and touches only self.model / self.optimizer / self.clip_grad_norm / self.grad_accum_steps: zero_grad -> forward -> sum the loss_module's "loss"-prefixed output keys -> backward -> optional grad-norm clip -> optimizer step. This is what lets FSDP2Learner reuse the exact same step as LocalLearner -- sharded training only changes model construction and weight gathering, not the step itself.
  • get_weights() is the one place sharding is not transparent: it must return plain tensors (for WeightSyncScheme.send, which already accepts a TensorDictBase), even when the learner's parameters are sharded. FSDP2Learner.get_weights() gathers every DTensor leaf via full_tensor().
  • FSDP2Learner does not decide sharding granularity or device mesh -- it accepts a model the caller has already wrapped with fully_shard, exactly as bare FSDP2 usage works. Keeping that decision in caller code avoids FSDP2Learner becoming a second place those choices are made.

Planned follow-ups (not in this PR)

  1. Multi-rank FSDP2Learner verification on real multi-GPU hardware -- the one thing I could not test here.
  2. Refactor an existing recipe's hand-rolled training loop (e.g. the reward-model recipe, once #3922 lands) onto LocalLearner, as the first real consumer.
  3. A RemoteLearner design writeup scoping one external backend (TorchTitan is the most PyTorch-native of the candidates and the most plausible first integration target) before any implementation.

theap06 avatar Jul 02 '26 08:07 theap06

:link: Helpful Links

:test_tube: See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3926

Note: Links to docs will display an error until the docs builds have been completed.

:x: 2 New Failures

As of commit 132f0bc57611f045f655cf59ecb2b8cb64e8c703 with merge base 99daa1c21494f5a5ea9859467e0a785266bfc69f (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

pytorch-bot[bot] avatar Jul 02 '26 08:07 pytorch-bot[bot]

Reviewed the rebased head 3614a44bf1a6, with particular attention to the latest optimizer-coverage/checkpoint contract changes and the final FSDP2 checkpoint-cloning fix. The base and FSDP2 checkpoint paths now clone tensor state before returning it, accumulation restores safely reset partial windows, and the optimizer-coverage check runs before the first step. I ran the Learner/FSDP2-focused test selection locally: 27 passed, including the FSDP2 checkpoint round trip, accumulation/reference comparison, and plain-tensor weight gather. I did not find a blocking issue in the current code. The meaningful residual risk is unchanged from the PR description: these checks use world_size=1 gloo/CPU, so actual multi-rank collective behavior and rank-0-only checkpoint/weight gathering still need real distributed coverage.

vmoens avatar Aug 10 '26 08:08 vmoens

Coordinated this branch with #4140 and force-pushed the rebased stack.

The shared contract is now:

  • Learner owns execution placement and weight materialization.
  • OptimizationStepper is the single owner of loss reduction, backward, gradient accumulation, clipping, mixed precision, optimizer stepping, and optimizer checkpoint state.
  • LocalLearner and FSDP2Learner receive the model, loss module, and stepper at construction, then expose update(batch). This is the same stepper contract used by Trainer and GRPOTrainer.

The reconciliation also moved optimizer-coverage and scalar-loss validation into MixedPrecisionOptimizationStepper, added the FSDP gradient-sync seam there, and made Learner checkpoints compose stepper state instead of maintaining a second accumulation/optimizer implementation.

I replaced the prior Learner test expansion with behavior-focused parity, weight snapshot, checkpoint, optimizer coverage, and real FSDP2/DTensor tests. Relative to #4140, test/test_trainer.py now adds 233 lines instead of 526.

Local validation: the complete test/test_trainer.py suite passes (154 passed, 19 dependency-based skips), including two FSDP2 tests. The full changed-file pre-commit suite passes. I also added ci/olddeps because this feature touches the recent FSDP2 API.

vmoens avatar Aug 28 '26 07:08 vmoens

Rebased #3926 onto current main after #4140 merged. Because #4140 was squash-merged, I dropped its duplicated pre-squash commit sequence and replayed only the seven Learner commits. The new head is 132f0bc57; GitHub reports the PR mergeable and its diff is limited to the Learner/OptimizationStepper files. Local validation: 154 trainer tests passed with 19 dependency-based skips, and the changed-file pre-commit suite passed.

vmoens avatar Aug 28 '26 08:08 vmoens