-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix wrong behavior of DDPStrategy
option with simple GAN training using DDP
#20936
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
samsara-ku
wants to merge
18
commits into
Lightning-AI:master
Choose a base branch
from
samsara-ku:bugfix/gan-ddp-training
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 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
00727e8
add: `MultiModelDDPStrategy` and its execution codes
samsara-ku e6b061a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aa9b027
refactor: extract block helper in GAN example
samsara-ku 5503d3a
Merge pull request #1 from samsara-ku/codex/add-tests-for-multimodeld…
samsara-ku dc128b4
Merge branch 'master' into bugfix/gan-ddp-training
Borda 1fb4027
with
Borda ec62397
Apply suggestions from code review
Borda ece7d38
formating
Borda 8b1fe23
misc: resolve some review comments for product consistency
samsara-ku 4b22284
misc: merge gan training example, add docstring of MultiModelDDPStrategy
samsara-ku 97dabf8
misc: add docstring of MultiModelDDPStrategy
samsara-ku 033e8e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 57e864a
update
Borda f157f59
Merge branch 'master' into bugfix/gan-ddp-training
SkafteNicki 24872e7
long line
Borda 3891102
Merge branch 'master' into bugfix/gan-ddp-training
SkafteNicki 8121337
add: set base test case and __init__py for MultiModelDDPStrategy
samsara-ku c442fc3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright The Lightning AI 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. | ||
from unittest import mock | ||
from unittest.mock import PropertyMock | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from lightning.pytorch.strategies.ddp import MultiModelDDPStrategy | ||
|
||
|
||
def test_multi_model_ddp_setup_and_register_hooks(): | ||
samsara-ku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Parent(nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
self.gen = nn.Linear(1, 1) | ||
self.dis = nn.Linear(1, 1) | ||
|
||
model = Parent() | ||
original_children = [model.gen, model.dis] | ||
|
||
strategy = MultiModelDDPStrategy(parallel_devices=[torch.device("cpu")]) | ||
|
||
wrapped_modules = [] | ||
wrapped_device_ids = [] | ||
|
||
class DummyDDP(nn.Module): | ||
def __init__(self, module: nn.Module, device_ids=None, **kwargs): | ||
super().__init__() | ||
self.module = module | ||
wrapped_modules.append(module) | ||
wrapped_device_ids.append(device_ids) | ||
|
||
with mock.patch("lightning.pytorch.strategies.ddp.DistributedDataParallel", DummyDDP): | ||
returned_model = strategy._setup_model(model) | ||
assert returned_model is model | ||
assert isinstance(model.gen, DummyDDP) | ||
assert isinstance(model.dis, DummyDDP) | ||
assert wrapped_modules == original_children | ||
assert wrapped_device_ids == [None, None] | ||
|
||
strategy.model = model | ||
with ( | ||
mock.patch("lightning.pytorch.strategies.ddp._register_ddp_comm_hook") as register_hook, | ||
mock.patch.object(MultiModelDDPStrategy, "root_device", new_callable=PropertyMock) as root_device, | ||
): | ||
root_device.return_value = torch.device("cuda", 0) | ||
strategy._register_ddp_hooks() | ||
|
||
assert register_hook.call_count == 2 | ||
register_hook.assert_any_call( | ||
model=model.gen, | ||
ddp_comm_state=strategy._ddp_comm_state, | ||
ddp_comm_hook=strategy._ddp_comm_hook, | ||
ddp_comm_wrapper=strategy._ddp_comm_wrapper, | ||
) | ||
register_hook.assert_any_call( | ||
model=model.dis, | ||
ddp_comm_state=strategy._ddp_comm_state, | ||
ddp_comm_hook=strategy._ddp_comm_hook, | ||
ddp_comm_wrapper=strategy._ddp_comm_wrapper, | ||
) | ||
|
||
|
||
def test_multi_model_ddp_register_hooks_cpu_noop(): | ||
samsara-ku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Parent(nn.Module): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.gen = nn.Linear(1, 1) | ||
self.dis = nn.Linear(1, 1) | ||
|
||
model = Parent() | ||
strategy = MultiModelDDPStrategy(parallel_devices=[torch.device("cpu")]) | ||
|
||
class DummyDDP(nn.Module): | ||
def __init__(self, module: nn.Module, device_ids=None, **kwargs): | ||
super().__init__() | ||
self.module = module | ||
|
||
with mock.patch("lightning.pytorch.strategies.ddp.DistributedDataParallel", DummyDDP): | ||
strategy.model = strategy._setup_model(model) | ||
|
||
with mock.patch("lightning.pytorch.strategies.ddp._register_ddp_comm_hook") as register_hook: | ||
strategy._register_ddp_hooks() | ||
|
||
register_hook.assert_not_called() | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.