-
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
fix dtype/device property not getting updated in submodules #2657
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
499ca64
recursive dtype device apply
awaelchli 9de24ed
simplify
awaelchli 95478ce
simple test
awaelchli cc5bf5b
submodule test
awaelchli 27e15da
rename
awaelchli db8cb0a
explicit
awaelchli 38bdd23
type hints
awaelchli 2f510dc
test for dp backend
awaelchli b955923
fix test skip
awaelchli a494523
rename
awaelchli 1c56e13
add ddp_spawn test
awaelchli 1eb776a
fix None index in test
awaelchli 565e4cb
try fix ddp_spawn test
awaelchli aa9306a
changelog
awaelchli 34a6805
move _dtype and _device to mixin
awaelchli 9bf20a0
additional doctest
awaelchli 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
|
||
from pytorch_lightning import Trainer, Callback | ||
from pytorch_lightning.utilities.device_dtype_mixin import DeviceDtypeModuleMixin | ||
from tests.base import EvalModelTemplate | ||
|
||
|
||
class SubSubModule(DeviceDtypeModuleMixin): | ||
pass | ||
|
||
|
||
class SubModule(nn.Module): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.module = SubSubModule() | ||
|
||
|
||
class TopModule(EvalModelTemplate): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.module = SubModule() | ||
|
||
|
||
class DeviceAssertCallback(Callback): | ||
|
||
def on_batch_start(self, trainer, model): | ||
rank = trainer.local_rank | ||
assert isinstance(model, TopModule) | ||
# index = None also means first device | ||
assert (model.device.index is None and rank == 0) or model.device.index == rank | ||
assert model.device == model.module.module.device | ||
|
||
|
||
@pytest.mark.parametrize(['dst_dtype'], [ | ||
pytest.param(torch.float), | ||
pytest.param(torch.double), | ||
pytest.param(torch.half), | ||
]) | ||
@pytest.mark.parametrize(['dst_device'], [ | ||
pytest.param(torch.device('cpu')), | ||
pytest.param(torch.device('cuda')), | ||
pytest.param(torch.device('cuda', 0)), | ||
]) | ||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine") | ||
def test_submodules_device_and_dtype(dst_device, dst_dtype): | ||
""" | ||
Test that the device and dtype property updates propagate through mixed nesting of regular | ||
nn.Modules and the special modules of type DeviceDtypeModuleMixin (e.g. Metric or LightningModule). | ||
""" | ||
|
||
model = TopModule() | ||
assert model.device == torch.device('cpu') | ||
model = model.to(device=dst_device, dtype=dst_dtype) | ||
# nn.Module does not have these attributes | ||
assert not hasattr(model.module, '_device') | ||
assert not hasattr(model.module, '_dtype') | ||
# device and dtype change should propagate down into all children | ||
assert model.device == model.module.module.device == dst_device | ||
assert model.dtype == model.module.module.dtype == dst_dtype | ||
|
||
|
||
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") | ||
def test_submodules_multi_gpu_dp(tmpdir): | ||
model = TopModule() | ||
trainer = Trainer( | ||
default_root_dir=tmpdir, | ||
distributed_backend='dp', | ||
gpus=2, | ||
callbacks=[DeviceAssertCallback()], | ||
max_steps=1, | ||
) | ||
trainer.fit(model) | ||
|
||
|
||
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") | ||
def test_submodules_multi_gpu_ddp_spawn(tmpdir): | ||
model = TopModule() | ||
trainer = Trainer( | ||
default_root_dir=tmpdir, | ||
distributed_backend='dpp_spawn', | ||
gpus=2, | ||
callbacks=[DeviceAssertCallback()], | ||
max_steps=1, | ||
) | ||
trainer.fit(model) |
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.
what is the reason of using
apply
?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.
part of the answer is here in my comment.
apply in contrast to "to" works recursively on all modules and allows us to update our custom properties.
I'm writing a test right now to make sure it fixes what failed before.