Add activation checkpointing to DPO and GRPO
Feature request
Currently TRL only implemented activation checkpointing for SFT (https://github.com/huggingface/trl/pull/2954), can we also add it to DPO and GRPO? Will the same code changes from SFT also work for those?
Motivation
Since SFT has activation checkpointing, it would be good that other training algorithms have the same feature.
Your contribution
If the same code changes from SFT also work for DPO and GRPO, I can submit a PR.
Activation offloading is now supported in DPO see #3906
Hey @mistysesame, sorry for the slow follow-up; we're catching up on the backlog.
Two features are easy to mix up here: plain gradient/activation checkpointing (gradient_checkpointing) has always been available for both DPO and GRPO (it's a shared training arg, default True). What #2954 added for SFT is activation offloading (moving activations to CPU, from TorchTune, PR name is misleading). That offloading has since landed in DPO (#3906) and KTO (#5901), so the DPO half of your request is done.
GRPO just doesn't have an activation_offloading flag yet. The wiring is small and mirrors DPO almost line-for-line: a config flag plus wrapping training_step in the offload context, see https://github.com/huggingface/trl/blob/82f6b2c9cf5313bb85c3d3c473cf2a4e527e76d7/trl/trainer/dpo_trainer.py#L1753-L1756 and https://github.com/huggingface/trl/blob/82f6b2c9cf5313bb85c3d3c473cf2a4e527e76d7/trl/trainer/dpo_trainer.py#L878-L882
Since you offered to contribute, a draft PR porting that to GRPO would be very welcome and I'm happy to review. Anyone else who'd use GRPO activation offloading, a 👍 here helps us prioritize.
Hi, I'd like to take this on if it's still open.
The SFT activation checkpointing PR (#2954) uses model.enable_input_require_grads() + model.gradient_checkpointing_enable() — the same pattern should apply to DPOTrainer and GRPOTrainer. The key thing to be careful about: the reference model in DPO runs inference-only forward passes, so checkpointing should be enabled only on the policy model, not on model_ref.
My plan:
- Mirror the SFT approach for
DPOTrainer(policy model only, not the ref model) - Apply to
GRPOTrainer, accounting for the vLLM-colocate path - Add tests covering memory reduction and ensuring
model_refis unaffected
Could I be assigned? Happy to submit a PR.
@qgallouedec — thanks for the clarification that the remaining open scope is activation offloading for GRPOTrainer specifically (not plain gradient checkpointing, which already works). I've looked at the DPO offloading wiring in #3906 — the GRPO port would add the config flag and wrap training_step in the offload context mirroring https://github.com/huggingface/trl/blob/82f6b2c9cf5313bb85c3d3c473cf2a4e527e76d7/trl/trainer/dpo_trainer.py#L1753-L1756. Happy to submit a draft PR along those lines if that's useful.
Hi @qgallouedec and @priyanka25aug — I reviewed current main and the closed #6551/#6552. Since neither PR was merged and there is no open implementation, I’d like to work on the remaining activation-offloading scope.
To comply with TRL’s duplicated-trainer consistency policy, I plan to:
- add the opt-in config and training-step context to both GRPO and RLOO;
- mirror the existing DPO/KTO/SFT initialization block exactly while preserving the GRPO/RLOO step-time logic;
- add paired regression tests and update the memory-usage documentation;
- report accelerator validation, including distributed/FSDP coverage where the available hardware permits.
I’ll open a draft PR so the implementation can be reviewed early. Please let me know if @priyanka25aug is still continuing this work, or if you would prefer the RLOO consistency update to be scoped differently.
Hi @YAO-001 — thanks for checking. Yes, I'm still on this. I scoped the GRPO port with @qgallouedec above and have the implementation in progress: the opt-in activation_offloading config flag plus wrapping training_step in the offload context, mirroring the DPO wiring from #3906, with tests covering memory reduction and the vLLM-colocate path. I'll have a draft PR up within the next few days so it can be reviewed early.
Re: RLOO — that's a fair point on the duplicated-trainer consistency policy. I'm happy to include the RLOO mirror in the same PR, or if you'd like to collaborate, you'd be welcome to take the RLOO side + FSDP/distributed validation as a follow-up on top of my draft. Either way I'll tag you when the draft is up.
Thank you for the update, @priyanka25aug — I really appreciate it. I’d be very happy to collaborate. When your draft PR is ready, would you mind tagging me? I’d be glad to help with the RLOO side and FSDP/distributed validation wherever it would be most useful.
@YAO-001 — tagging you as promised! My draft PR is ready to go up now — it covers GRPOTrainer + RLOOTrainer (both config flag + training_step context), paired slow tests for each using trl-internal-testing/zen and trl-internal-testing/tiny-Qwen2ForSequenceClassification-2.5 as the reward model, and the RLOOConfig docstring update mirroring the memory-optimization section already in GRPOConfig.
I see you also have a draft open in #6615 — happy to coordinate. A few options for @qgallouedec to weigh in on:
- Keep both drafts open briefly so reviewers can compare approaches and pick one to iterate on
- Fold any differences from #6615 into mine (or vice versa) and one of us closes their draft
Either way, @YAO-001, I'd love your help on the FSDP/distributed validation path since you mentioned you have the hardware for it — that's the one gap I couldn't cover locally. Will have the PR link here shortly.
As promised — draft PR is up: #6621
It adds activation_offloading to both GRPOTrainer and RLOOTrainer, mirroring the existing DPO/KTO pattern exactly:
activation_offloading: bool = Falsefield inGRPOConfigandRLOOConfigget_act_offloading_ctx_managerinitialized once in__init__(falls back tonullcontext()when disabled)- Context manager wrapping each
training_step test_train_with_activation_offloadingadded toTestGRPOTrainerSlowand a newTestRLOOTrainerSlow, parametrized over Llama-3.2 and Mistral-0.2 tiny models
Marked as draft until CI passes. @YAO-001 — happy to coordinate on the FSDP/distributed validation you mentioned if you want to contribute tests there.