Skip to content

Clarify stream-only dedupe check#6280

Open
kashif wants to merge 5 commits into
huggingface:mainfrom
kashif:clarify-stream-storage-dedupe-check
Open

Clarify stream-only dedupe check#6280
kashif wants to merge 5 commits into
huggingface:mainfrom
kashif:clarify-stream-storage-dedupe-check

Conversation

@kashif

@kashif kashif commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Makes the storage dedupe check explicitly streams-only. Follow up from #6241

This matches the existing comment: fwd_stash gives streams mode a clear release point, while single-stream mode does not.

Selective activation checkpointing (SAC) for SFT

Also adds selective activation checkpointing to SFT. When gradient checkpointing is on, the attention output is saved during the forward pass instead of being recomputed in the backward pass. At long context this recovers most of the checkpointing slowdown for one extra hidden-state sized tensor per layer, and it runs fully eager with no torch.compile needed.

Turn it on with selective_activation_checkpointing=True on SFTConfig (needs gradient_checkpointing=True, which SFT already defaults to). It works by wrapping the model's gradient_checkpointing_enable to inject a policy that saves attention and recomputes the rest, and it forces non-reentrant checkpointing which SAC relies on. This is the same eager SAC approach torchtitan uses under FSDP2. PEFT plus DeepSpeed ZeRO-3 is rejected up front since that combo needs reentrant checkpointing.

Checked that SAC gradients match full checkpointing exactly and that attention is no longer recomputed in backward. Added a test for both.

This is the SFT-only first step. DPO, KTO, reward and GRPO can follow the same two lines in a later PR.


Note

Medium Risk
SAC changes the gradient-checkpointing path for all SFT runs that opt in; incorrect policy could affect training correctness, though tests compare gradients to full checkpointing. The offloading dedupe tweak is narrow but touches hot memory paths.

Overview
Adds selective activation checkpointing (SAC) for SFT: SFTConfig.selective_activation_checkpointing wraps the model’s gradient_checkpointing_enable so checkpointing saves attention outputs and recomputes other ops (non-reentrant, eager policy via PyTorch’s selective checkpoint contexts). SFTTrainer enables this before the base trainer init, requires gradient_checkpointing=True, and rejects SAC together with PEFT + DeepSpeed ZeRO-3.

Exports enable_selective_activation_checkpointing from trl.models. New tests assert SAC gradients match full checkpointing and that SDPA runs fewer times under SAC.

Separately, activation offloading only skips duplicate storage offloads when use_streams is true, matching the intended fwd_stash release behavior in single-stream mode.

Reviewed by Cursor Bugbot for commit 600adf1. Bugbot is set up for automated code reviews on this repo. Configure here.

🤖 Generated with Claude Code

@kashif
kashif marked this pull request as ready for review July 3, 2026 19:18
@kashif
kashif requested a review from Copilot July 3, 2026 19:19

Copilot AI left a comment

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.

Pull request overview

This PR makes the activation offload storage-deduplication guard in OffloadActivations.pack_tensor explicitly apply only in streams mode, aligning the condition with the intended lifecycle management of GPU references via fwd_stash.

Changes:

  • Restricts the “already-offloaded storage” dedupe fast-path to use_streams=True by gating the membership check with self.use_streams.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread trl/models/activation_offloading.py Outdated
@qgallouedec

Copy link
Copy Markdown
Member

cc @winglian

@kashif
kashif force-pushed the clarify-stream-storage-dedupe-check branch from 37c71cf to 4b76032 Compare July 3, 2026 20:48
@bot-ci-comment

bot-ci-comment Bot commented Jul 3, 2026

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

kashif and others added 2 commits July 17, 2026 14:57
Save the attention output during the forward pass instead of recomputing
it in backward. Adds `selective_activation_checkpointing` to SFTConfig.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit aa6b8d0. Configure here.

Comment thread trl/trainer/sft_trainer.py Outdated
kashif and others added 2 commits July 17, 2026 15:51
Install the gradient_checkpointing_enable wrap before the Trainer init so
the SAC context function is in place before train() enables checkpointing.
Move the checkpoint imports to module level.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants