|
| 1 | +# Copyright The Lightning AI team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from unittest import mock |
| 15 | +from unittest.mock import PropertyMock |
| 16 | + |
| 17 | +import torch |
| 18 | +from torch import nn |
| 19 | + |
| 20 | +from lightning.pytorch.strategies.ddp import MultiModelDDPStrategy |
| 21 | + |
| 22 | + |
| 23 | +def test_multi_model_ddp_setup_and_register_hooks(): |
| 24 | + class Parent(nn.Module): |
| 25 | + def __init__(self): |
| 26 | + super().__init__() |
| 27 | + self.gen = nn.Linear(1, 1) |
| 28 | + self.dis = nn.Linear(1, 1) |
| 29 | + |
| 30 | + model = Parent() |
| 31 | + original_children = [model.gen, model.dis] |
| 32 | + |
| 33 | + strategy = MultiModelDDPStrategy(parallel_devices=[torch.device("cpu")]) |
| 34 | + |
| 35 | + wrapped_modules = [] |
| 36 | + wrapped_device_ids = [] |
| 37 | + |
| 38 | + class DummyDDP(nn.Module): |
| 39 | + def __init__(self, module: nn.Module, device_ids=None, **kwargs): |
| 40 | + super().__init__() |
| 41 | + self.module = module |
| 42 | + wrapped_modules.append(module) |
| 43 | + wrapped_device_ids.append(device_ids) |
| 44 | + |
| 45 | + with mock.patch("lightning.pytorch.strategies.ddp.DistributedDataParallel", DummyDDP): |
| 46 | + returned_model = strategy._setup_model(model) |
| 47 | + assert returned_model is model |
| 48 | + assert isinstance(model.gen, DummyDDP) |
| 49 | + assert isinstance(model.dis, DummyDDP) |
| 50 | + assert wrapped_modules == original_children |
| 51 | + assert wrapped_device_ids == [None, None] |
| 52 | + |
| 53 | + strategy.model = model |
| 54 | + with mock.patch("lightning.pytorch.strategies.ddp._register_ddp_comm_hook") as register_hook: |
| 55 | + with mock.patch.object(MultiModelDDPStrategy, "root_device", new_callable=PropertyMock) as root_device: |
| 56 | + root_device.return_value = torch.device("cuda", 0) |
| 57 | + strategy._register_ddp_hooks() |
| 58 | + |
| 59 | + assert register_hook.call_count == 2 |
| 60 | + register_hook.assert_any_call( |
| 61 | + model=model.gen, |
| 62 | + ddp_comm_state=strategy._ddp_comm_state, |
| 63 | + ddp_comm_hook=strategy._ddp_comm_hook, |
| 64 | + ddp_comm_wrapper=strategy._ddp_comm_wrapper, |
| 65 | + ) |
| 66 | + register_hook.assert_any_call( |
| 67 | + model=model.dis, |
| 68 | + ddp_comm_state=strategy._ddp_comm_state, |
| 69 | + ddp_comm_hook=strategy._ddp_comm_hook, |
| 70 | + ddp_comm_wrapper=strategy._ddp_comm_wrapper, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def test_multi_model_ddp_register_hooks_cpu_noop(): |
| 75 | + class Parent(nn.Module): |
| 76 | + def __init__(self) -> None: |
| 77 | + super().__init__() |
| 78 | + self.gen = nn.Linear(1, 1) |
| 79 | + self.dis = nn.Linear(1, 1) |
| 80 | + |
| 81 | + model = Parent() |
| 82 | + strategy = MultiModelDDPStrategy(parallel_devices=[torch.device("cpu")]) |
| 83 | + |
| 84 | + class DummyDDP(nn.Module): |
| 85 | + def __init__(self, module: nn.Module, device_ids=None, **kwargs): |
| 86 | + super().__init__() |
| 87 | + self.module = module |
| 88 | + |
| 89 | + with mock.patch("lightning.pytorch.strategies.ddp.DistributedDataParallel", DummyDDP): |
| 90 | + strategy.model = strategy._setup_model(model) |
| 91 | + |
| 92 | + with mock.patch("lightning.pytorch.strategies.ddp._register_ddp_comm_hook") as register_hook: |
| 93 | + strategy._register_ddp_hooks() |
| 94 | + |
| 95 | + register_hook.assert_not_called() |
0 commit comments