-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generalize Optimizer
validation to accommodate both FSDP 1.x and 2.x
#16733
Merged
awaelchli
merged 14 commits into
Lightning-AI:master
from
speediedan:allow_fsdp_use_orig_params
Mar 28, 2023
Merged
Generalize Optimizer
validation to accommodate both FSDP 1.x and 2.x
#16733
awaelchli
merged 14 commits into
Lightning-AI:master
from
speediedan:allow_fsdp_use_orig_params
Mar 28, 2023
Conversation
This file contains 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
github-actions
bot
added
fabric
lightning.fabric.Fabric
pl
Generic label for PyTorch Lightning package
labels
Feb 12, 2023
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patience on this!
awaelchli
reviewed
Mar 26, 2023
… allow_fsdp_use_orig_params
…ition scope and adjusting test definition format
awaelchli
approved these changes
Mar 28, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
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>
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
Optimizer
validation 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_params
params mode.Currently, false-positive "The optimizer does not seem to reference any FSDP parameters"
ValueError
s will be triggered when theuse_orig_params
FSDP configuration is used because such a configuration does not requireFlatParameter
instances 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.13
and2.0
both ensure FSDP flattened parameters have the_fsdp_flattened
attribute 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.12
is 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_params
in 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_params
to accommodate FSDP 1.x and 2.0 usage._is_fsdp_flattened
method and protected constant (which have both changed locations between versions), I've included the relevant constant and attribute check infabric/strategies/fsdp.py
directly rather than relying on a protected attribute whose location is version dependent.auto_wrap_policy
test_FSDPPolicy
-based test that usesuse_orig_params
to validate the multi-versionOptimizer
validation while also testing the preferred_FSDPPolicy
-based auto-wrapping approach moving forwardauto_wrap_policy
behind the necessarymax_torch
markTested 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_flattened
attribute 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