Fix FSDP activation recompute prefetch - #6153
Draft
shjwudp wants to merge 1 commit into
Draft
Conversation
shjwudp
force-pushed
the
fix/mfsdp-activation-recompute
branch
from
July 30, 2026 15:51
2b6e98a to
10d38be
Compare
Signed-off-by: Jianbin Chang <jianbinc@nvidia.com>
shjwudp
force-pushed
the
fix/mfsdp-activation-recompute
branch
from
July 31, 2026 02:16
10d38be to
e639bb5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Bug
Activation recomputation executes module forward hooks while autograd is already running backward. Megatron FSDP v2 did not distinguish these recomputed forwards from the original forward pass, so
FsdpModule.pre_forward()continued using the static forward order to prefetch the next module.For two consecutive FSDP modules, the failure sequence is:
In the downstream optimizer path, this can leave stale gradients on empty local DTensor shards. The next
zero_grad()clears nonempty optimizer shards but cannot reach those empty sharded parameter objects while the model exposes its unsharded parameters. Gradient reduction then observes a mixture of set and missing gradients and raises:FSDP sharded gradients must be either all set or all None.Fix
The root FSDP context now enters backward state from its root pre-backward hook until the autograd final callback runs.
While in this state:
post_backward()performs the matching gradient reduction and reshard;Regression test
The test activation-checkpoints two independently sharded linear layers and is parameterized over
use_reentrant=Trueanduse_reentrant=False.Without the fix, both modes leave the second layer as an unsharded
Parameterafter backward. With the fix, both layers return to shardedDTensorparameters and the root context exits backward state.Tests
test_fully_shard.pysuite: 22 passed, 3 deselected.The complete
test_fully_shard.pyrun reaches an unrelated test-container profiler incompatibility:FunctionEvent.linked_correlation_idis unavailable. The activation-recompute regression and all non-profiler cases pass.