Repository navigation
Generalize Optimizer validation to accommodate both FSDP 1.x and 2.x - #16733
Merged
awaelchli merged 14 commits intoMar 28, 2023
Merged
Conversation
speediedan
marked this pull request as ready for review
February 12, 2023 18:54
speediedan
requested review from
awaelchli,
carmocca,
justusschock and
williamFalcon
as code owners
February 12, 2023 18:54
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #16733 +/- ##
=========================================
- Coverage 69% 59% -9%
=========================================
Files 433 408 -25
Lines 31560 31269 -291
=========================================
- Hits 21625 18556 -3069
- Misses 9935 12713 +2778 |
carmocca
reviewed
Feb 13, 2023
Borda
approved these changes
Mar 24, 2023
awaelchli
approved these changes
Mar 26, 2023
awaelchli
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the patience on this!
awaelchli
reviewed
Mar 26, 2023
… allow_fsdp_use_orig_params
…ition scope and adjusting test definition format
awaelchli
enabled auto-merge (squash)
March 28, 2023 12:14
carmocca
pushed a commit
that referenced
this pull request
Mar 30, 2023
#16733) Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
lantiga
pushed a commit
that referenced
this pull request
Mar 30, 2023
#16733) Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
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 does this PR do?
Resolves #16732
Generalize
Optimizervalidation to accommodate both FSDP 1.x and 2.xIn preparing the downstream package I maintain (finetuning-scheduler) to support PyTorch 2.0's version of FSDP, I noticed that Lightning has yet to be adapted to accommodate FSDP 2.x's useful
use_orig_paramsparams mode.Currently, false-positive "The optimizer does not seem to reference any FSDP parameters"
ValueErrors will be triggered when theuse_orig_paramsFSDP configuration is used because such a configuration does not requireFlatParameterinstances be present in theOptimizer.https://github.com/Lightning-AI/lightning/blob/c35fee92f4ffdcfe5137c762f614b44ca7f8b7d0/src/lightning/fabric/strategies/fsdp.py#L344-L347
https://github.com/Lightning-AI/lightning/blob/c35fee92f4ffdcfe5137c762f614b44ca7f8b7d0/src/lightning/pytorch/strategies/fsdp.py#L282-L287
FSDP versions
1.13and2.0both ensure FSDP flattened parameters have the_fsdp_flattenedattribute set, allowing for a single check to work across both versions (this is the check used internally by PyTorch FSDP). For BC, we need to keep the existingFlatParameter-based check for versions below1.13, but we should be able to remove that version guard once1.12is deprecated.I'm sure support for this (along with other FSDP 2.x features) will be arriving as the team's priorities dictate but I'm hoping this PR will allow PyTorch 2.x development/testing to continue for
use_orig_paramsin a BC-compatible manner while also adding some useful 1.x and 2.x FSDP tests.This PR makes 4 minor changes:
_optimizer_has_flat_paramsto accommodate FSDP 1.x and 2.0 usage._is_fsdp_flattenedmethod and protected constant (which have both changed locations between versions), I've included the relevant constant and attribute check infabric/strategies/fsdp.pydirectly rather than relying on a protected attribute whose location is version dependent.auto_wrap_policytest_FSDPPolicy-based test that usesuse_orig_paramsto validate the multi-versionOptimizervalidation while also testing the preferred_FSDPPolicy-based auto-wrapping approach moving forwardauto_wrap_policybehind the necessarymax_torchmarkTested with: PyTorch 1.12.1, 1.13.1 and 2.0.0.dev20230211
Noted Alternatives
One could use an FSDP instance-based check but that seems both more complicated and brittle. The
_fsdp_flattenedattribute is used extensively internally and was the best candidate attribute for this check IMHO.Before submitting
PR review
Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:
Reviewer checklist