fix(fsdp): call module() instead of module.forward() to preserve hooks - #2
Open
Jackie2049 wants to merge 2 commits into
Open
fix(fsdp): call module() instead of module.forward() to preserve hooks#2Jackie2049 wants to merge 2 commits into
Jackie2049 wants to merge 2 commits into
Conversation
In StorageResizeBasedBucketAllocator.free(), temporary all-gather bucket storage was freed without calling record_stream() on the consuming CUDA stream. This causes a use-after-free race condition when overlapped param gather operates on a dedicated stream: 1. Megatron-FSDP allocates all-gather buckets on a parameter-gather CUDA stream 2. Compute kernels consume these parameters on the default stream 3. free() returns storage to the CUDA caching allocator without record_stream() 4. The caching allocator may recycle the memory immediately 5. If an in-flight compute kernel is still reading → use-after-free Add record_stream(current_stream()) before _free_storage() to ensure the caching allocator waits until all kernels on the consuming stream have completed before recycling the memory. This is the same pattern family as: - DeepSpeed #8061 (overlap_comm NaN, resolved via #8080) - vLLM #45552 (CuMem sleep/wake illegal memory access) - vLLM #46125 (stale KV cache after weight update) Refs: NVIDIA#5788 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
In MegatronFSDP.forward(), calling self.module.forward(*inputs, **kwargs) bypasses any registered forward pre/post hooks on the wrapped module. This includes hooks registered by PyTorch's own nn.Module hook system (register_forward_pre_hook, register_forward_hook) and any custom hooks. Fix: use self.module(*inputs, **kwargs) which properly invokes hooks before and after the module's forward method. Impact: Any code relying on forward hooks on modules wrapped by MegatronFSDP (e.g., gradient checkpointing, profiling, custom monitoring) will now work correctly. Refs: NVIDIA#5789 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Owner
Author
Upstream Validation Update (July 15)This fix has been validated by the official upstream merge: NVIDIA/Megatron-LM NVIDIA#5808 — "Fix MegatronFSDP root module hook dispatch" — MERGED July 15, 2026 Our PR #2 independently identified and fixed the same bug ( This is the 2nd Jackie2049 fork PR validated by an official upstream merge:
Pattern: We identify bugs independently, then official fixes validate our analysis. |
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.
Summary
In
MegatronFSDP.forward()(line 1490), callingself.module.forward(*inputs, **kwargs)bypasses any registered forward pre/post hooks on the wrapped module. This includes:nn.Module.register_forward_pre_hook()/register_forward_hook()Fix: Use
self.module(*inputs, **kwargs)instead, which properly invokes all registered hooks before and after the module's forward method.Root Cause (issue NVIDIA#5789)
nn.Module.__call__()invokes hooks in this order:forward_pre_hooks → modify inputsforward()→ compute outputsforward_hooks → modify outputsBy calling
.forward()directly, step 1 and 3 are skipped entirely.Changes
megatron_fsdp.py: +3/-1 linesself.module.forward(*inputs, **kwargs)→self.module(*inputs, **kwargs)Cross-Framework Connection
This is part of the FSDP bug cluster identified in NVIDIA#5788/NVIDIA#5789/NVIDIA#5790:
StorageResizeBasedBucketAllocator.freeresizes gather storage to zero withoutrecord_stream(use-after-free race) NVIDIA/Megatron-LM#5788: StorageResize use-after-free (norecord_stream)MegatronFSDP.forwardcallsself.module.forward(...)directly, bypassing the wrapped module's registered forward hooks NVIDIA/Megatron-LM#5789: Forward hook bypass (this fix)Parameterstate (_megatron_fsdp_model,_is_shared) is stored as plain Python attributes that do not survive post-wrap parameter rebuilds NVIDIA/Megatron-LM#5790: Parameter state loss during FSDP lifecycleAll three share the same root cause: MegatronFSDP's implementation doesn't properly integrate with PyTorch's nn.Module lifecycle (hooks, streams, state management).
Refs: NVIDIA#5789
🤖 Generated with Claude Code