@@ -86,7 +86,6 @@ def test_accelerator_choice_ddp_spawn(cuda_available_mock, device_count_mock):
86
86
assert isinstance (trainer .training_type_plugin .cluster_environment , LightningEnvironment )
87
87
88
88
89
- @RunIf (min_gpus = 2 )
90
89
@mock .patch .dict (
91
90
os .environ ,
92
91
{
@@ -98,8 +97,10 @@ def test_accelerator_choice_ddp_spawn(cuda_available_mock, device_count_mock):
98
97
"SLURM_LOCALID" : "1" ,
99
98
},
100
99
)
100
+ @mock .patch ("torch.cuda.set_device" )
101
+ @mock .patch ("torch.cuda.device_count" , return_value = 2 )
101
102
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
102
- def test_accelerator_choice_ddp_slurm (setup_distributed_mock ):
103
+ def test_accelerator_choice_ddp_slurm (set_device_mock , device_count_mock , setup_distributed_mock ):
103
104
class CB (Callback ):
104
105
def on_fit_start (self , trainer , pl_module ):
105
106
assert trainer ._accelerator_connector ._is_slurm_managing_tasks
@@ -111,13 +112,13 @@ def on_fit_start(self, trainer, pl_module):
111
112
raise SystemExit ()
112
113
113
114
model = BoringModel ()
114
- trainer = Trainer (fast_dev_run = True , accelerator = "ddp" , gpus = 2 , callbacks = [CB ()])
115
+ with pytest .deprecated_call (match = r"accelerator='ddp'\)` has been deprecated in v1.5" ):
116
+ trainer = Trainer (fast_dev_run = True , accelerator = "ddp" , gpus = 2 , callbacks = [CB ()])
115
117
116
118
with pytest .raises (SystemExit ):
117
119
trainer .fit (model )
118
120
119
121
120
- @RunIf (min_gpus = 2 )
121
122
@mock .patch .dict (
122
123
os .environ ,
123
124
{
@@ -129,9 +130,10 @@ def on_fit_start(self, trainer, pl_module):
129
130
"SLURM_LOCALID" : "1" ,
130
131
},
131
132
)
133
+ @mock .patch ("torch.cuda.set_device" )
132
134
@mock .patch ("torch.cuda.device_count" , return_value = 2 )
133
135
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
134
- def test_accelerator_choice_ddp2_slurm (device_count_mock , setup_distributed_mock ):
136
+ def test_accelerator_choice_ddp2_slurm (set_device_mock , device_count_mock , setup_distributed_mock ):
135
137
class CB (Callback ):
136
138
def on_fit_start (self , trainer , pl_module ):
137
139
assert trainer ._accelerator_connector ._is_slurm_managing_tasks
@@ -143,13 +145,15 @@ def on_fit_start(self, trainer, pl_module):
143
145
raise SystemExit ()
144
146
145
147
model = BoringModel ()
146
- trainer = Trainer (fast_dev_run = True , accelerator = "ddp2" , gpus = 2 , callbacks = [CB ()])
148
+ with pytest .deprecated_call (match = r"accelerator='ddp2'\)` has been deprecated in v1.5" ):
149
+ trainer = Trainer (fast_dev_run = True , accelerator = "ddp2" , gpus = 2 , callbacks = [CB ()])
147
150
148
151
with pytest .raises (SystemExit ):
149
152
trainer .fit (model )
150
153
154
+ set_device_mock .assert_called_once ()
155
+
151
156
152
- @RunIf (min_gpus = 1 )
153
157
@mock .patch .dict (
154
158
os .environ ,
155
159
{
@@ -161,9 +165,10 @@ def on_fit_start(self, trainer, pl_module):
161
165
"GROUP_RANK" : "0" ,
162
166
},
163
167
)
164
- @mock .patch ("torch.cuda.device_count" , return_value = 2 )
168
+ @mock .patch ("torch.cuda.set_device" )
169
+ @mock .patch ("torch.cuda.device_count" , return_value = 1 )
165
170
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
166
- def test_accelerator_choice_ddp_te (device_count_mock , setup_distributed_mock ):
171
+ def test_accelerator_choice_ddp_te (set_device_mock , device_count_mock , setup_distributed_mock ):
167
172
class CB (Callback ):
168
173
def on_fit_start (self , trainer , pl_module ):
169
174
assert isinstance (trainer .accelerator , GPUAccelerator )
@@ -174,13 +179,15 @@ def on_fit_start(self, trainer, pl_module):
174
179
raise SystemExit ()
175
180
176
181
model = BoringModel ()
177
- trainer = Trainer (fast_dev_run = True , accelerator = "ddp" , gpus = 2 , callbacks = [CB ()])
182
+ with pytest .deprecated_call (match = r"accelerator='ddp'\)` has been deprecated in v1.5" ):
183
+ trainer = Trainer (fast_dev_run = True , accelerator = "ddp" , gpus = 2 , callbacks = [CB ()])
178
184
179
185
with pytest .raises (SystemExit ):
180
186
trainer .fit (model )
181
187
188
+ set_device_mock .assert_called_once ()
189
+
182
190
183
- @RunIf (min_gpus = 1 )
184
191
@mock .patch .dict (
185
192
os .environ ,
186
193
{
@@ -192,9 +199,10 @@ def on_fit_start(self, trainer, pl_module):
192
199
"GROUP_RANK" : "0" ,
193
200
},
194
201
)
195
- @mock .patch ("torch.cuda.device_count" , return_value = 2 )
202
+ @mock .patch ("torch.cuda.set_device" )
203
+ @mock .patch ("torch.cuda.device_count" , return_value = 1 )
196
204
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
197
- def test_accelerator_choice_ddp2_te (device_count_mock , setup_distributed_mock ):
205
+ def test_accelerator_choice_ddp2_te (set_device_mock , device_count_mock , setup_distributed_mock ):
198
206
class CB (Callback ):
199
207
def on_fit_start (self , trainer , pl_module ):
200
208
assert isinstance (trainer .accelerator , GPUAccelerator )
@@ -205,11 +213,14 @@ def on_fit_start(self, trainer, pl_module):
205
213
raise SystemExit ()
206
214
207
215
model = BoringModel ()
208
- trainer = Trainer (fast_dev_run = True , accelerator = "ddp2" , gpus = 2 , callbacks = [CB ()])
216
+ with pytest .deprecated_call (match = r"accelerator='ddp2'\)` has been deprecated in v1.5" ):
217
+ trainer = Trainer (fast_dev_run = True , accelerator = "ddp2" , gpus = 2 , callbacks = [CB ()])
209
218
210
219
with pytest .raises (SystemExit ):
211
220
trainer .fit (model )
212
221
222
+ set_device_mock .assert_called_once ()
223
+
213
224
214
225
@mock .patch .dict (
215
226
os .environ , {"WORLD_SIZE" : "2" , "LOCAL_WORLD_SIZE" : "2" , "RANK" : "1" , "LOCAL_RANK" : "1" , "GROUP_RANK" : "0" }
@@ -233,7 +244,6 @@ def on_fit_start(self, trainer, pl_module):
233
244
trainer .fit (model )
234
245
235
246
236
- @RunIf (min_gpus = 1 )
237
247
@mock .patch .dict (
238
248
os .environ ,
239
249
{
@@ -245,9 +255,10 @@ def on_fit_start(self, trainer, pl_module):
245
255
"RANK" : "1" ,
246
256
},
247
257
)
258
+ @mock .patch ("torch.cuda.set_device" )
248
259
@mock .patch ("torch.cuda.device_count" , return_value = 1 )
249
260
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
250
- def test_accelerator_choice_ddp_kubeflow (device_count_mock , setup_distributed_mock ):
261
+ def test_accelerator_choice_ddp_kubeflow (set_device_mock , device_count_mock , setup_distributed_mock ):
251
262
class CB (Callback ):
252
263
def on_fit_start (self , trainer , pl_module ):
253
264
assert isinstance (trainer .accelerator , GPUAccelerator )
@@ -258,11 +269,14 @@ def on_fit_start(self, trainer, pl_module):
258
269
raise SystemExit ()
259
270
260
271
model = BoringModel ()
261
- trainer = Trainer (fast_dev_run = True , accelerator = "ddp" , gpus = 1 , callbacks = [CB ()])
272
+ with pytest .deprecated_call (match = r"accelerator='ddp'\)` has been deprecated in v1.5" ):
273
+ trainer = Trainer (fast_dev_run = True , accelerator = "ddp" , gpus = 1 , callbacks = [CB ()])
262
274
263
275
with pytest .raises (SystemExit ):
264
276
trainer .fit (model )
265
277
278
+ set_device_mock .assert_called_once ()
279
+
266
280
267
281
@mock .patch .dict (
268
282
os .environ ,
@@ -323,29 +337,28 @@ def on_fit_start(self, trainer, pl_module):
323
337
trainer .fit (model )
324
338
325
339
326
- @RunIf (special = True )
327
- def test_accelerator_choice_ddp_cpu_and_plugin (tmpdir ):
340
+ @RunIf (skip_windows = True , special = True )
341
+ def test_accelerator_choice_ddp_cpu_and_strategy (tmpdir ):
328
342
"""Test that accelerator="ddp_cpu" can work together with an instance of DDPPlugin."""
329
- _test_accelerator_choice_ddp_cpu_and_plugin (tmpdir , ddp_plugin_class = DDPPlugin )
343
+ _test_accelerator_choice_ddp_cpu_and_strategy (tmpdir , ddp_strategy_class = DDPPlugin )
330
344
331
345
332
- @RunIf (special = True )
333
- def test_accelerator_choice_ddp_cpu_and_plugin_spawn (tmpdir ):
346
+ @RunIf (skip_windows = True )
347
+ def test_accelerator_choice_ddp_cpu_and_strategy_spawn (tmpdir ):
334
348
"""Test that accelerator="ddp_cpu" can work together with an instance of DDPPSpawnPlugin."""
335
- _test_accelerator_choice_ddp_cpu_and_plugin (tmpdir , ddp_plugin_class = DDPSpawnPlugin )
336
-
349
+ _test_accelerator_choice_ddp_cpu_and_strategy (tmpdir , ddp_strategy_class = DDPSpawnPlugin )
337
350
338
- def _test_accelerator_choice_ddp_cpu_and_plugin (tmpdir , ddp_plugin_class ):
339
351
352
+ def _test_accelerator_choice_ddp_cpu_and_strategy (tmpdir , ddp_strategy_class ):
340
353
model = BoringModel ()
341
354
trainer = Trainer (
342
355
default_root_dir = tmpdir ,
343
- plugins = [ ddp_plugin_class (find_unused_parameters = True )] ,
356
+ strategy = ddp_strategy_class (find_unused_parameters = True ),
344
357
fast_dev_run = True ,
345
358
accelerator = "ddp_cpu" ,
346
359
num_processes = 2 ,
347
360
)
348
- assert isinstance (trainer .training_type_plugin , ddp_plugin_class )
361
+ assert isinstance (trainer .training_type_plugin , ddp_strategy_class )
349
362
assert isinstance (trainer .accelerator , CPUAccelerator )
350
363
assert trainer .training_type_plugin .num_processes == 2
351
364
assert trainer .training_type_plugin .parallel_devices == [torch .device ("cpu" )] * 2
@@ -793,7 +806,6 @@ def on_fit_start(self, trainer, pl_module):
793
806
trainer .fit (model )
794
807
795
808
796
- @RunIf (min_gpus = 2 )
797
809
@mock .patch .dict (
798
810
os .environ ,
799
811
{
@@ -805,10 +817,11 @@ def on_fit_start(self, trainer, pl_module):
805
817
"SLURM_LOCALID" : "1" ,
806
818
},
807
819
)
820
+ @mock .patch ("torch.cuda.set_device" )
808
821
@mock .patch ("torch.cuda.device_count" , return_value = 2 )
809
822
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
810
823
@pytest .mark .parametrize ("strategy" , ["ddp2" , DDP2Plugin ()])
811
- def test_strategy_choice_ddp2_slurm (device_count_mock , setup_distributed_mock , strategy ):
824
+ def test_strategy_choice_ddp2_slurm (set_device_mock , device_count_mock , setup_distributed_mock , strategy ):
812
825
class CB (Callback ):
813
826
def on_fit_start (self , trainer , pl_module ):
814
827
assert trainer ._accelerator_connector ._is_slurm_managing_tasks
@@ -825,8 +838,9 @@ def on_fit_start(self, trainer, pl_module):
825
838
with pytest .raises (SystemExit ):
826
839
trainer .fit (model )
827
840
841
+ set_device_mock .assert_called_once ()
842
+
828
843
829
- @RunIf (min_gpus = 1 )
830
844
@mock .patch .dict (
831
845
os .environ ,
832
846
{
@@ -838,9 +852,10 @@ def on_fit_start(self, trainer, pl_module):
838
852
"GROUP_RANK" : "0" ,
839
853
},
840
854
)
855
+ @mock .patch ("torch.cuda.set_device" )
841
856
@mock .patch ("torch.cuda.device_count" , return_value = 2 )
842
857
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
843
- def test_strategy_choice_ddp_te (device_count_mock , setup_distributed_mock ):
858
+ def test_strategy_choice_ddp_te (set_device_mock , device_count_mock , setup_distributed_mock ):
844
859
class CB (Callback ):
845
860
def on_fit_start (self , trainer , pl_module ):
846
861
assert isinstance (trainer .accelerator , GPUAccelerator )
@@ -856,8 +871,9 @@ def on_fit_start(self, trainer, pl_module):
856
871
with pytest .raises (SystemExit ):
857
872
trainer .fit (model )
858
873
874
+ set_device_mock .assert_called_once ()
875
+
859
876
860
- @RunIf (min_gpus = 1 )
861
877
@mock .patch .dict (
862
878
os .environ ,
863
879
{
@@ -869,9 +885,10 @@ def on_fit_start(self, trainer, pl_module):
869
885
"GROUP_RANK" : "0" ,
870
886
},
871
887
)
888
+ @mock .patch ("torch.cuda.set_device" )
872
889
@mock .patch ("torch.cuda.device_count" , return_value = 2 )
873
890
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
874
- def test_strategy_choice_ddp2_te (device_count_mock , setup_distributed_mock ):
891
+ def test_strategy_choice_ddp2_te (set_device_mock , device_count_mock , setup_distributed_mock ):
875
892
class CB (Callback ):
876
893
def on_fit_start (self , trainer , pl_module ):
877
894
assert isinstance (trainer .accelerator , GPUAccelerator )
@@ -887,6 +904,8 @@ def on_fit_start(self, trainer, pl_module):
887
904
with pytest .raises (SystemExit ):
888
905
trainer .fit (model )
889
906
907
+ set_device_mock .assert_called_once ()
908
+
890
909
891
910
@mock .patch .dict (
892
911
os .environ , {"WORLD_SIZE" : "2" , "LOCAL_WORLD_SIZE" : "2" , "RANK" : "1" , "LOCAL_RANK" : "1" , "GROUP_RANK" : "0" }
@@ -910,7 +929,6 @@ def on_fit_start(self, trainer, pl_module):
910
929
trainer .fit (model )
911
930
912
931
913
- @RunIf (min_gpus = 1 )
914
932
@mock .patch .dict (
915
933
os .environ ,
916
934
{
@@ -922,9 +940,10 @@ def on_fit_start(self, trainer, pl_module):
922
940
"RANK" : "1" ,
923
941
},
924
942
)
943
+ @mock .patch ("torch.cuda.set_device" )
925
944
@mock .patch ("torch.cuda.device_count" , return_value = 1 )
926
945
@mock .patch ("pytorch_lightning.plugins.DDPPlugin.setup_distributed" , autospec = True )
927
- def test_strategy_choice_ddp_kubeflow (device_count_mock , setup_distributed_mock ):
946
+ def test_strategy_choice_ddp_kubeflow (set_device_mock , device_count_mock , setup_distributed_mock ):
928
947
class CB (Callback ):
929
948
def on_fit_start (self , trainer , pl_module ):
930
949
assert isinstance (trainer .accelerator , GPUAccelerator )
@@ -940,6 +959,8 @@ def on_fit_start(self, trainer, pl_module):
940
959
with pytest .raises (SystemExit ):
941
960
trainer .fit (model )
942
961
962
+ set_device_mock .assert_called_once ()
963
+
943
964
944
965
@mock .patch .dict (
945
966
os .environ ,
0 commit comments