|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import os |
16 | 15 | import time |
17 | 16 | from typing import Type |
18 | 17 |
|
|
21 | 20 |
|
22 | 21 | from pytorch_lightning import seed_everything, Trainer |
23 | 22 | from pytorch_lightning.plugins import DDPSpawnShardedPlugin |
24 | | -from tests.accelerators import DDPLauncher |
25 | 23 | from tests.helpers.boring_model import BoringModel, RandomDataset |
26 | 24 | from tests.helpers.runif import RunIf |
27 | 25 |
|
28 | 26 |
|
29 | | -@RunIf(min_gpus=1, skip_windows=True, fairscale=True) |
30 | | -def test_ddp_sharded_plugin_correctness_one_gpu(): |
31 | | - plugin_parity_test( |
32 | | - gpus=1, |
33 | | - model_cls=SeedTrainLoaderModel, |
34 | | - ) |
35 | | - |
36 | | - |
37 | | -@RunIf(min_gpus=1, skip_windows=True, fairscale=True, amp_native=True) |
38 | | -def test_ddp_sharded_plugin_correctness_amp_one_gpu(): |
39 | | - plugin_parity_test( |
40 | | - gpus=1, |
41 | | - precision=16, |
42 | | - model_cls=SeedTrainLoaderModel, |
43 | | - ) |
44 | | - |
45 | | - |
46 | | -@pytest.mark.skip(reason="Not a critical test, skip till drone CI performance improves.") |
47 | | -@RunIf(min_gpus=2, skip_windows=True, fairscale=True) |
48 | | -def test_ddp_sharded_plugin_correctness_multi_gpu(): |
49 | | - plugin_parity_test( |
50 | | - gpus=2, |
51 | | - model_cls=SeedTrainLoaderModel, |
52 | | - max_percent_speed_diff=0.25, # todo: Increase speed diff since only 2 GPUs sharding 2 optimizers |
53 | | - ) |
54 | | - |
55 | | - |
56 | | -@RunIf(min_gpus=2, skip_windows=True, fairscale=True, amp_native=True) |
57 | | -def test_ddp_sharded_plugin_correctness_amp_multi_gpu(): |
58 | | - plugin_parity_test( |
59 | | - gpus=2, |
60 | | - precision=16, |
61 | | - model_cls=SeedTrainLoaderModel, |
62 | | - max_percent_speed_diff=0.25, # todo: Increase speed diff since only 2 GPUs sharding 2 optimizers |
63 | | - ) |
64 | | - |
65 | | - |
66 | | -@RunIf(min_gpus=2, skip_windows=True, fairscale=True, amp_native=True) |
67 | | -def test_ddp_string_sharded_plugin_correctness_amp_multi_gpu(): |
68 | | - plugin_parity_test( |
69 | | - gpus=2, |
70 | | - precision=16, |
71 | | - model_cls=SeedTrainLoaderModel, |
72 | | - max_percent_speed_diff=0.25, # todo: Increase speed diff since only 2 GPUs sharding 2 optimizers |
73 | | - ) |
74 | | - |
75 | | - |
76 | | -@RunIf(min_gpus=2, fairscale=True) |
77 | | -@pytest.mark.skipif( |
78 | | - not os.getenv("PL_RUNNING_SPECIAL_TESTS", '0') == '1', reason="test should be run outside of pytest" |
79 | | -) |
80 | | -@DDPLauncher.run("--accelerator ddp --gpus 2 --precision 32") |
81 | | -def test_ddp_sharded_plugin_correctness_multi_gpu_ddp(tmpdir, args=None): |
82 | | - plugin_parity_test( |
83 | | - gpus=args.gpus, |
84 | | - precision=args.precision, |
85 | | - model_cls=SeedTrainLoaderModel, |
86 | | - ) |
87 | | - |
88 | | - |
89 | | -@RunIf(min_gpus=2, fairscale=True) |
90 | | -@pytest.mark.skipif( |
91 | | - not os.getenv("PL_RUNNING_SPECIAL_TESTS", '0') == '1', reason="test should be run outside of pytest" |
92 | | -) |
93 | | -@DDPLauncher.run("--accelerator ddp --gpus 2 --precision 16") |
94 | | -def test_ddp_sharded_plugin_correctness_amp_multi_gpu_ddp(tmpdir, args=None): |
95 | | - plugin_parity_test( |
96 | | - gpus=args.gpus, |
97 | | - precision=args.precision, |
98 | | - model_cls=SeedTrainLoaderModel, |
99 | | - ) |
100 | | - |
101 | | - |
102 | | -@pytest.mark.skip(reason="Current issue with multiple optimizers and FairScale.") |
103 | | -@RunIf(min_gpus=2, skip_windows=True, fairscale=True) |
104 | | -def test_ddp_sharded_plugin_correctness_multi_gpu_multi_optim(): |
105 | | - """ |
106 | | - Ensures same results using multiple optimizers across multiple GPUs |
107 | | - """ |
108 | | - plugin_parity_test( |
109 | | - gpus=2, |
110 | | - model_cls=SeedTrainLoaderMultipleOptimizersModel, |
111 | | - max_percent_speed_diff=0.25, # todo: Increase speed diff since only 2 GPUs sharding 2 optimizers |
112 | | - ) |
113 | | - |
114 | | - |
115 | | -@pytest.mark.skip(reason="Current issue with multiple optimizers and FairScale.") |
116 | | -@RunIf(min_gpus=2, skip_windows=True, fairscale=True) |
117 | | -def test_ddp_sharded_plugin_correctness_multi_gpu_multi_optim_manual(tmpdir): |
118 | | - """ |
119 | | - Ensures using multiple optimizers across multiple GPUs with manual optimization |
120 | | - """ |
121 | | - plugin_parity_test( |
122 | | - gpus=2, |
123 | | - model_cls=SeedTrainLoaderManualModel, |
124 | | - max_percent_speed_diff=0.25, # todo: Increase speed diff since only 2 GPUs sharding 2 optimizers |
125 | | - ) |
126 | | - |
127 | | - |
128 | 27 | class SeedTrainLoaderModel(BoringModel): |
129 | 28 | """ |
130 | | - Overrides training loader to ensure we enforce the same seed for all DDP processes. |
| 29 | + Overrides training loader to ensure we enforce the same seed for all DDP processes. |
131 | 30 | """ |
132 | 31 |
|
133 | 32 | def train_dataloader(self): |
@@ -177,7 +76,7 @@ class SeedTrainLoaderMultipleOptimizersModel(SeedTrainLoaderModel): |
177 | 76 | def training_step(self, batch, batch_idx, optimizer_idx): |
178 | 77 | output = self.layer(batch) |
179 | 78 | loss = self.loss(batch, output) |
180 | | - return {"loss": loss} |
| 79 | + return {'loss': loss} |
181 | 80 |
|
182 | 81 | def training_epoch_end(self, outputs) -> None: |
183 | 82 | # outputs should be an array with an entry per optimizer |
@@ -279,11 +178,48 @@ def plugin_parity_test( |
279 | 178 | # Assert speed parity by ensuring percentage difference between custom/ddp is below threshold |
280 | 179 | percent_diff = (custom_model_time - ddp_time) / custom_model_time |
281 | 180 |
|
282 | | - assert percent_diff <= max_percent_speed_diff, \ |
283 | | - f'Custom DDP plugin was too slow compared to DDP, Custom Plugin Time: {custom_model_time}, DDP Time: {ddp_time}' |
| 181 | + assert ( |
| 182 | + percent_diff <= max_percent_speed_diff |
| 183 | + ), f'Custom DDP plugin was too slow compared to DDP, Custom Plugin Time: {custom_model_time}, DDP Time: {ddp_time}' |
284 | 184 |
|
285 | 185 | if use_cuda: |
286 | 186 | # Assert CUDA memory parity |
287 | | - assert max_memory_custom <= max_memory_ddp, \ |
288 | | - f'Custom plugin used too much memory compared to DDP,' \ |
| 187 | + assert max_memory_custom <= max_memory_ddp, ( |
| 188 | + 'Custom plugin used too much memory compared to DDP, ' |
289 | 189 | f'Custom Mem: {max_memory_custom}, DDP Mem: {max_memory_ddp}' |
| 190 | + ) |
| 191 | + |
| 192 | + |
| 193 | +@RunIf(skip_windows=True, fairscale=True) |
| 194 | +@pytest.mark.parametrize( |
| 195 | + 'kwargs', |
| 196 | + [ |
| 197 | + pytest.param(dict(gpus=1, model_cls=SeedTrainLoaderModel), marks=RunIf(min_gpus=1)), |
| 198 | + pytest.param( |
| 199 | + dict(gpus=1, precision=16, model_cls=SeedTrainLoaderModel), marks=RunIf(min_gpus=1, amp_native=True) |
| 200 | + ), |
| 201 | + pytest.param(dict(gpus=2, model_cls=SeedTrainLoaderModel), marks=RunIf(min_gpus=2)), |
| 202 | + pytest.param( |
| 203 | + dict(gpus=2, precision=16, model_cls=SeedTrainLoaderModel), marks=RunIf(min_gpus=2, amp_native=True) |
| 204 | + ), |
| 205 | + pytest.param( |
| 206 | + dict(gpus=2, model_cls=SeedTrainLoaderMultipleOptimizersModel), |
| 207 | + marks=[ |
| 208 | + RunIf(min_gpus=2), |
| 209 | + pytest.mark.skip(reason='TODO: Current issue with multiple optimizers and FairScale.'), |
| 210 | + ], |
| 211 | + ), |
| 212 | + pytest.param( |
| 213 | + dict(gpus=2, model_cls=SeedTrainLoaderManualModel), |
| 214 | + marks=[ |
| 215 | + RunIf(min_gpus=2), |
| 216 | + pytest.mark.skip(reason='TODO: Current issue with multiple optimizers and FairScale.'), |
| 217 | + ], |
| 218 | + ), |
| 219 | + ], |
| 220 | +) |
| 221 | +def test_ddp_spawn_sharded_plugin(kwargs): |
| 222 | + if kwargs['gpus'] > 1: |
| 223 | + # TODO: decrease speed diff since only 2 GPUs sharding 2 optimizers |
| 224 | + kwargs['max_percent_speed_diff'] = 0.25 |
| 225 | + plugin_parity_test(**kwargs) |
0 commit comments