-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[TPU] Add Trainer support for PyTorch XLA FSDP #18746
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
Open
gkroiz
wants to merge
54
commits into
Lightning-AI:master
Choose a base branch
from
gkroiz:pytorch_xla_fsdp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 45 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
ba66f96
Basic skeletal code for XLAFSDP support for PyTorch Trainer
gkroiz 26238d8
fix import issue
gkroiz 53703e2
Fixed leaking env vars in tests + minor cleanup
gkroiz bbed985
Merge branch 'master' into pytorch_xla_fsdp
gkroiz 0132427
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d4ae668
accidentally deleted line in merge
gkroiz b538f43
fix mypy error
gkroiz 9778505
Minor fixes
carmocca 4098016
Reuse code
carmocca 5ca74df
More minor fixes
carmocca 4bcb2e9
Reorder methods to reduce diff with Fabric
carmocca 957866b
Go through precision plugin
carmocca 222896c
Consistent with regular FSDP
carmocca cd70e0d
minor fixes, limited checkpointing support, testing for trainer.test …
gkroiz d7d78f1
mypy fix
gkroiz 2f51e4e
Merge branch 'master' into pytorch_xla_fsdp
gkroiz 7a680e3
consistent setup optimizer
carmocca 46927f4
Merge branch 'master' into pytorch_xla_fsdp
carmocca c4e295a
Import pbar force fn
carmocca 0cd0cb6
Launched and overrides
carmocca e0b353f
Multiple optimizers should be fine?
carmocca 60eecd1
Test fixes
carmocca 1150e90
Minor FSDP fixes
carmocca 1ae4957
Fix
carmocca ef05276
Other xla strategies too
carmocca 1cc6d84
More fixes
carmocca 64ed7fb
Fixes
carmocca 42ea9ac
Restore minor snippet with full checkpointing
gkroiz be1b509
ignores
carmocca f9ba3f0
Fixes
carmocca 196960c
mypy
carmocca fdf21d3
_is_sharded
carmocca 483b93d
Bring over changes from #18774
carmocca f4c1ba7
Optimizer fix
carmocca 22629f4
Restore checkpoint after setup
carmocca 10253f7
load_checkpoint
carmocca e391d38
Warning
carmocca f92de54
CHANGELOG
carmocca e6fd2ce
Additional changes for Trainer XLAFSDP strategy ckpting
gkroiz 1305faa
add assertions for mypy
gkroiz bd8fb12
test manual wrap separately from ckpting test
gkroiz 58d43e2
syntax changes in strings from FSDP to XLAFSDP
gkroiz c264801
Minor fix to tests
gkroiz 3263d98
Merge branch 'master' into pytorch_xla_fsdp
gkroiz 82f9f2f
Merge branch 'master' into pytorch_xla_fsdp
carmocca 80c3faf
Add manual wrapping guard for fabric xlafsdp
carmocca 8f775f4
mypy
carmocca e5b695a
Merge branch 'master' into pytorch_xla_fsdp
carmocca 7bf9904
Apply suggestions from code review
gkroiz a00f8fe
Merge branch 'master' into pytorch_xla_fsdp
gkroiz 4280cf4
Apply formatting suggestions
gkroiz ddda55f
[XLAFSDP] add test for automatic strategy selection
gkroiz 904b50d
fix `test_tpu_invalid_raises` test
gkroiz 0c79282
only run test_xla_fsdp_automatic_strategy_selection when on TPU
gkroiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,10 @@ | |
# limitations under the License. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we extend the tests in plugins/precision/test_xla.py in a meaningful way? |
||
import os | ||
from functools import partial | ||
from typing import Any, Callable | ||
from typing import Any, Callable, Union | ||
|
||
import torch | ||
from torch.optim import Optimizer | ||
from typing_extensions import get_args | ||
|
||
import lightning.pytorch as pl | ||
|
@@ -59,6 +60,9 @@ def __init__(self, precision: _PRECISION_INPUT = "32-true") -> None: | |
else: | ||
self._desired_dtype = torch.float32 | ||
|
||
# boolean flag for simplicity over an entirely new class | ||
self._using_fsdp = False | ||
|
||
def optimizer_step( # type: ignore[override] | ||
self, | ||
optimizer: Optimizable, | ||
|
@@ -68,7 +72,8 @@ def optimizer_step( # type: ignore[override] | |
) -> Any: | ||
import torch_xla.core.xla_model as xm | ||
|
||
closure = partial(self._xla_wrap_closure, optimizer, closure) | ||
if not self._using_fsdp: | ||
closure = partial(self._reduce_gradients, optimizer, closure) | ||
closure = partial(self._wrap_closure, model, optimizer, closure) | ||
closure_result = optimizer.step(closure=closure, **kwargs) | ||
xm.mark_step() | ||
|
@@ -87,9 +92,22 @@ def teardown(self) -> None: | |
os.environ.pop("XLA_USE_BF16", None) | ||
os.environ.pop("XLA_USE_F16", None) | ||
|
||
def _xla_wrap_closure(self, optimizer: Optimizable, closure: Callable[[], Any]) -> Any: | ||
def _reduce_gradients(self, optimizer: Optimizable, closure: Callable[[], Any]) -> Any: | ||
import torch_xla.core.xla_model as xm | ||
|
||
closure_result = closure() | ||
xm.reduce_gradients(optimizer) | ||
return closure_result | ||
|
||
def clip_grad_by_norm(self, optimizer: Optimizer, clip_val: Union[int, float]) -> None: | ||
if self._using_fsdp: | ||
# Not supported by us because we need a module reference, this would need to go through the Strategy | ||
# as in Fabric | ||
raise NotImplementedError("XLA's FSDP strategy does not support to clip gradients by norm.") | ||
return super().clip_grad_by_value(optimizer, clip_val) | ||
|
||
def clip_grad_by_value(self, optimizer: Optimizer, clip_val: Union[int, float]) -> None: | ||
if self._using_fsdp: | ||
# Not supported by XLA | ||
raise NotImplementedError("XLA's FSDP strategy does not support to clip gradients by value.") | ||
return super().clip_grad_by_value(optimizer, clip_val) |
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
Oops, something went wrong.
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.
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.
I wonder what happens if you save a state where the key for the model is not "model"
This would be totally valid in any other setting, but I think here it would fail since the XLA format expects these keys.
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.
iirc this won't work when trying to consolidate the checkpoint using
consolidate_sharded_model_checkpoints
https://github.com/Lightning-AI/lightning/pull/18746/files/e5b695aff64bc37ae1a67fba4aac4981200eecfd#diff-3908a573abf00ae5f37061f214f2a3c2616b6591e0c96206b9f48b4c7ab49ea4R457. I'm not sure how this works for individual shards.