[Feature] Add Learner primitive (LocalLearner, FSDP2Learner)
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 onlyself.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 letsFSDP2Learnerreuse the exact same step asLocalLearner-- 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 (forWeightSyncScheme.send, which already accepts aTensorDictBase), even when the learner's parameters are sharded.FSDP2Learner.get_weights()gathers everyDTensorleaf viafull_tensor().FSDP2Learnerdoes not decide sharding granularity or device mesh -- it accepts a model the caller has already wrapped withfully_shard, exactly as bare FSDP2 usage works. Keeping that decision in caller code avoidsFSDP2Learnerbecoming a second place those choices are made.
Planned follow-ups (not in this PR)
- Multi-rank
FSDP2Learnerverification on real multi-GPU hardware -- the one thing I could not test here. - 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. - A
RemoteLearnerdesign writeup scoping one external backend (TorchTitan is the most PyTorch-native of the candidates and the most plausible first integration target) before any implementation.
:link: Helpful Links
:test_tube: See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3926
- :page_facing_up: Preview Python docs built from this PR
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 ():
NEW FAILURES - The following jobs have failed:
- Unit-tests on Linux / tests-olddeps (3.10, 11.8, remainder) / linux-job (gh)
RuntimeError: Command docker exec -t 0c97b6dcd037c4025728d693447d9b04372b9fe099675b4cdd452b7432097837 /exec failed with exit code 2 - Unit-tests on Linux / tests-stable-gpu (3.12, 13.0, 3) / linux-job (gh)
AssertionError: Tensor-likes are not close!
This comment was automatically generated by Dr. CI and updates every 15 minutes.
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.
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.
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.