[release/2.10] Cherry-pick: [FSDP2] support dataclass args/kwargs output without memory leakage (#174692)#3146
Draft
chinmaydk99 wants to merge 1 commit intoROCm:release/2.10from
Draft
Conversation
…ytorch#174692) support dataclass output. reland pytorch#173415 `pytest -s test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py -k test_dataclass_input_output` Pull Request resolved: pytorch#174692 Approved by: https://github.com/mori360
|
Jenkins build for ef9bb3215728bf956a13bc30e0896e330356373d commit finished as FAILURE Detected error during Pytorch building: |
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.
Motivation
Addressing this JIRA ticket: https://amd-hub.atlassian.net/browse/AIPYTORCH-396
FSDP2 with reshard_after_forward=True crashes during the backward pass with RuntimeError: setStorage: sizes [...] are inconsistent with scalar type Float and target storage of size 0 when the wrapped module's forward() returns a dataclass containing tensors (rather than a plain tensor or tuple).
This is a cherry-pick of upstream fix pytorch#174692 (reland of pytorch#173415).
Technical Details
FSDP2's hook registration uses tree_flatten to find grad-requiring tensors in forward outputs/inputs, but tree_flatten does not traverse dataclass instances. It treats them as opaque leaves. This means no pre-backward hooks get registered, so parameters are never unsharded before gradient computation, causing the crash.
The fix adds two dataclass-aware traversal utilities (collect_grad_tensors, replace_grad_tensors) in _common_utils.py and uses them in place of tree_flatten/tree_unflatten in:
Upstream commit: ab1d15e