Skip to content

FSDP2: count a sharded activation checkpoint wrapper as its block unit (fsdp2 red since accelerate 1.16) - #18025

Merged
danielhanchen merged 1 commit into
mainfrom
ci-fsdp2-checkpoint-wrapper-units
Nov 16, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci-fsdp2-checkpoint-wrapper-units

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Repo tests (CPU, fsdp2) fails on main since accelerate 1.16.0 reached PyPI today (seen on #18012 and #18017, both on 20d05c20):

tests/test_fsdp2_wrap_gloo.py::test_activation_checkpointed_blocks_keep_the_last_block_gathered
    assert r["blocks"] == N_LAYERS
E   assert 0 == 3

Cause

With activation checkpointing on, accelerate wraps each decoder block in a CheckpointWrapper and then calls fully_shard. Up to 1.15 it sharded the block inside the wrapper. 1.16 shards the wrapper instead and skips the block inside ("Shard the wrapper to keep FSDP hooks outside compiled code", fsdp2_prepare_model).

after_fsdp2_wrap finds the blocks with _decoder_blocks(model) and keeps the ones that are FSDPModules. On 1.16 the block is no longer one, and the wrapper's class is not a decoder layer class, so it found no blocks. The fused LoRA gate does not depend on that list (it checks that each fused layer's owning unit sits in the layer list, which the wrapper does). What was lost is the per-block tuning: the last block was no longer kept gathered after forward (set_reshard_after_forward(False)), so with activation checkpointing every step gathered it again at the start of backward.

The test is right. accelerate's change is reasonable, and Unsloth's block lookup assumed the old layout.

Change

  • unsloth/models/fsdp2.py: new _block_units, which returns each decoder block's FSDP unit: the block itself, or the checkpoint wrapper around it when that wrapper is the FSDPModule (_checkpoint_wrapped_module is block). after_fsdp2_wrap uses it for the block list, the block count and the last-block reshard setting.
  • New tests/test_fsdp2_block_units.py (in the fsdp2 shard): both layouts with stand-in classes, so the 1.16 layout is checked whatever accelerate CI installs, plus a case where neither the block nor the wrapper is sharded.

Verification

  • tests/test_fsdp2_wrap_gloo.py on accelerate 1.16.0: the checkpointed test fails on main (0 == 3) and all 5 pass with this change. On accelerate 1.15.0: 5 passed with this change.
  • tests/test_fsdp2_block_units.py: 3 passed. Without the wrapper lookup, the wrapper case fails.
  • The whole fsdp2 shard file set on accelerate 1.16.0: 247 passed, 16 skipped.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T21:34:40.801020Z 82ae557 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 82ae55740e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 94c1c65 into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the ci-fsdp2-checkpoint-wrapper-units branch November 16, 2026 21:40
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.

1 participant