-
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
Tpu save #4309
+172
−98
Merged
Tpu save #4309
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c900608
convert xla tensor to cpu before save
lezwon 8c1e3f7
move_to_cpu
lezwon 654c15b
updated CHANGELOG.md
lezwon 031af97
added on_save to accelerators
lezwon 00256b8
if accelerator is not None
lezwon 8fcf157
refactors
lezwon a9a8188
change filename to run test
lezwon 9e78274
run test_tpu_backend
lezwon 5becb7b
added xla_device_utils to tests
lezwon 1946591
added xla_device_utils to test
lezwon 502e8a1
removed tests
lezwon c6e4535
Revert "added xla_device_utils to test"
lezwon 3a35796
fixed pep
lezwon 0565e93
increase timeout and print traceback
lezwon 7321045
lazy check tpu exists
lezwon 5573d51
increased timeout
lezwon da59b4d
fixed torch_xla imports
lezwon 63c6e15
fix tests
lezwon 57029e3
define xla utils
lezwon b34d615
fix test
lezwon 9c0c567
aval
Borda e4eb7ed
chlog
Borda d06c1b2
docs
Borda b180798
aval
Borda bab6d12
Apply suggestions from code review
Borda a485e29
Merge branch 'master' into tpu_save
tchaton eef0849
Merge branch 'master' into tpu_save
SeanNaren 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 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -227,6 +227,9 @@ def hpc_save(self, folderpath: str, logger): | |
|
||
model.on_hpc_save(checkpoint) | ||
|
||
if self.trainer.accelerator_backend: | ||
checkpoint = self.trainer.accelerator_backend.on_save(checkpoint) | ||
Comment on lines
+230
to
+231
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. isn't accelerator backend always set? even on cpu? 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. I think it isnt set when |
||
|
||
# do the actual save | ||
# TODO: fix for anything with multiprocess DP, DDP, DDP2 | ||
try: | ||
|
@@ -380,6 +383,8 @@ def save_checkpoint(self, filepath, weights_only: bool = False): | |
|
||
if self.trainer.is_global_zero: | ||
# write the checkpoint dictionary on the file | ||
if self.trainer.accelerator_backend: | ||
checkpoint = self.trainer.accelerator_backend.on_save(checkpoint) | ||
try: | ||
atomic_save(checkpoint, filepath) | ||
except AttributeError as err: | ||
|
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License | ||
|
||
import pytest | ||
import torch | ||
|
||
from pytorch_lightning import Trainer | ||
from pytorch_lightning.utilities.xla_device_utils import XLADeviceUtils | ||
from tests.base.boring_model import BoringModel | ||
from tests.base.develop_utils import pl_multi_process_test | ||
|
||
|
||
@pytest.mark.skipif(not XLADeviceUtils.tpu_device_exists(), reason="test requires TPU machine") | ||
@pl_multi_process_test | ||
def test_resume_training_on_cpu(tmpdir): | ||
""" Checks if training can be resumed from a saved checkpoint on CPU""" | ||
|
||
# Train a model on TPU | ||
model = BoringModel() | ||
trainer = Trainer(checkpoint_callback=True, max_epochs=1, tpu_cores=8,) | ||
trainer.fit(model) | ||
|
||
model_path = trainer.checkpoint_callback.best_model_path | ||
|
||
# Verify saved Tensors are on CPU | ||
ckpt = torch.load(model_path) | ||
weight_tensor = list(ckpt["state_dict"].values())[0] | ||
assert weight_tensor.device == torch.device("cpu") | ||
|
||
# Verify that training is resumed on CPU | ||
trainer = Trainer(resume_from_checkpoint=model_path, checkpoint_callback=True, max_epochs=1, default_root_dir=tmpdir) | ||
result = trainer.fit(model) | ||
|
||
assert result == 1 | ||
|
||
|
||
@pytest.mark.skipif(not XLADeviceUtils.tpu_device_exists(), reason="test requires TPU machine") | ||
@pl_multi_process_test | ||
def test_if_test_works_after_train(tmpdir): | ||
""" Ensure that .test() works after .fit() """ | ||
|
||
# Train a model on TPU | ||
model = BoringModel() | ||
trainer = Trainer(checkpoint_callback=True, max_epochs=1, tpu_cores=8, default_root_dir=tmpdir) | ||
trainer.fit(model) | ||
|
||
assert trainer.test() == 1 |
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.
where did this go? cc @tchaton
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 guess useless imports.
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.
shall not be there at all in the first place, @SeanNaren