Skip to content

Commit 5be6720

Browse files
Update tests in strategies directory in preparation for #11040 (#12467)
1 parent 204c5cd commit 5be6720

8 files changed

+41
-23
lines changed

tests/strategies/test_bagua_strategy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ def test_bagua_not_available(monkeypatch):
118118
monkeypatch.setattr(imports, "_BAGUA_AVAILABLE", False)
119119
with mock.patch("torch.cuda.device_count", return_value=1):
120120
with pytest.raises(MisconfigurationException, match="you must have `Bagua` installed"):
121-
Trainer(strategy="bagua", gpus=1)
121+
Trainer(strategy="bagua", accelerator="gpu", devices=1)

tests/strategies/test_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
@pytest.mark.parametrize(
2727
"trainer_kwargs",
2828
(
29-
pytest.param(dict(gpus=1), marks=RunIf(min_gpus=1)),
30-
pytest.param(dict(strategy="dp", gpus=2), marks=RunIf(min_gpus=2)),
31-
pytest.param(dict(strategy="ddp_spawn", gpus=2), marks=RunIf(min_gpus=2)),
29+
pytest.param(dict(accelerator="gpu", devices=1), marks=RunIf(min_gpus=1)),
30+
pytest.param(dict(strategy="dp", accelerator="gpu", devices=2), marks=RunIf(min_gpus=2)),
31+
pytest.param(dict(strategy="ddp_spawn", accelerator="gpu", devices=2), marks=RunIf(min_gpus=2)),
3232
),
3333
)
3434
def test_evaluate(tmpdir, trainer_kwargs):

tests/strategies/test_ddp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def setup(self, stage: Optional[str] = None) -> None:
109109
raise SystemExit()
110110

111111
model = TestModel()
112-
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, strategy="ddp", gpus=1)
112+
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, strategy="ddp", accelerator="gpu", devices=1)
113113
with pytest.deprecated_call(match="Environment variable `PL_TORCH_DISTRIBUTED_BACKEND` was deprecated in v1.6"):
114114
with pytest.raises(SystemExit):
115115
trainer.fit(model)

tests/strategies/test_ddp_fully_sharded_with_full_state_dict.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def test_invalid_on_cpu(tmpdir):
3434
@RunIf(fairscale_fully_sharded=True)
3535
def test_fsdp_with_sharded_amp(device_count_mock, mock_cuda_available, tmpdir):
3636
"""Test to ensure that plugin native amp plugin is correctly chosen when using sharded."""
37-
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, strategy="fsdp", gpus=1, precision=16)
37+
trainer = Trainer(
38+
default_root_dir=tmpdir, fast_dev_run=True, strategy="fsdp", accelerator="gpu", devices=1, precision=16
39+
)
3840
assert isinstance(trainer.strategy, DDPFullyShardedStrategy)
3941
assert isinstance(trainer.strategy.precision_plugin, FullyShardedNativeMixedPrecisionPlugin)
4042

tests/strategies/test_ddp_spawn_strategy.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_ddp_cpu():
6969
def test_ddp_spawn_extra_parameters(tmpdir):
7070
"""Tests if device is set correctly when training for DDPSpawnStrategy and tests add_to_queue/get_from_queue
7171
with Lightning Module (deprecated way)."""
72-
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, gpus=2, strategy="ddp_spawn")
72+
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, accelerator="gpu", devices=2, strategy="ddp_spawn")
7373

7474
assert isinstance(trainer.strategy, DDPSpawnStrategy)
7575
assert trainer.strategy.root_device == torch.device("cuda:0")
@@ -103,7 +103,9 @@ def test_ddp_spawn_add_get_queue(tmpdir):
103103
"""Tests add_to_queue/get_from_queue with DDPSpawnStrategy."""
104104

105105
ddp_spawn_strategy = TestDDPSpawnStrategy()
106-
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, num_processes=2, strategy=ddp_spawn_strategy)
106+
trainer = Trainer(
107+
default_root_dir=tmpdir, fast_dev_run=True, accelerator="cpu", devices=2, strategy=ddp_spawn_strategy
108+
)
107109

108110
val: float = 1.0
109111
val_name: str = "val_acc"
@@ -138,7 +140,7 @@ def on_predict_start(self) -> None:
138140
@RunIf(skip_windows=True, skip_49370=True, skip_hanging_spawn=True)
139141
def test_ddp_spawn_configure_ddp(tmpdir):
140142
"""Tests with ddp spawn strategy."""
141-
trainer = Trainer(default_root_dir=tmpdir, num_processes=2, strategy="ddp_spawn", fast_dev_run=True)
143+
trainer = Trainer(default_root_dir=tmpdir, accelerator="cpu", devices=2, strategy="ddp_spawn", fast_dev_run=True)
142144

143145
model = BoringModelDDP()
144146

tests/strategies/test_ddp_strategy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def creates_processes_externally(self):
8787
trainer = Trainer(
8888
default_root_dir=tmpdir,
8989
strategy="ddp",
90-
num_processes=2,
90+
accelerator="cpu",
91+
devices=2,
9192
plugins=[WronglyImplementedEnvironment()],
9293
)
9394
with pytest.raises(
@@ -138,7 +139,7 @@ def test_ddp_dont_configure_sync_batchnorm(trainer_fn):
138139
model = BoringModelGPU()
139140
model.layer = torch.nn.BatchNorm1d(10)
140141
ddp_strategy = DDPStrategy()
141-
trainer = Trainer(gpus=1, strategy=ddp_strategy, sync_batchnorm=True)
142+
trainer = Trainer(accelerator="gpu", devices=1, strategy=ddp_strategy, sync_batchnorm=True)
142143
trainer.state.fn = trainer_fn
143144
trainer.strategy.connect(model)
144145
trainer.lightning_module.trainer = trainer

tests/strategies/test_dp.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def test_multi_gpu_early_stop_dp(tmpdir):
7272
max_epochs=50,
7373
limit_train_batches=10,
7474
limit_val_batches=10,
75-
gpus=[0, 1],
75+
accelerator="gpu",
76+
devices=[0, 1],
7677
strategy="dp",
7778
)
7879

@@ -88,7 +89,8 @@ def test_multi_gpu_model_dp(tmpdir):
8889
max_epochs=1,
8990
limit_train_batches=10,
9091
limit_val_batches=10,
91-
gpus=[0, 1],
92+
accelerator="gpu",
93+
devices=[0, 1],
9294
strategy="dp",
9395
enable_progress_bar=False,
9496
)
@@ -166,7 +168,7 @@ def transfer_batch_to_device(self, batch, device, dataloader_idx):
166168
batch = batch.to(device)
167169
return batch
168170

169-
trainer_options = dict(default_root_dir=tmpdir, max_steps=7, gpus=[0, 1], strategy="dp")
171+
trainer_options = dict(default_root_dir=tmpdir, max_steps=7, accelerator="gpu", devices=[0, 1], strategy="dp")
170172

171173
trainer = Trainer(**trainer_options)
172174
model = CustomModel()
@@ -208,7 +210,8 @@ def test_dp_training_step_dict(tmpdir):
208210
trainer = pl.Trainer(
209211
default_root_dir=tmpdir,
210212
fast_dev_run=True,
211-
gpus=2,
213+
accelerator="gpu",
214+
devices=2,
212215
strategy="dp",
213216
)
214217
trainer.fit(model)

tests/strategies/test_sharded_strategy.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
def test_ddp_sharded_precision_16_clip_gradients(mock_oss_clip_grad_norm, clip_val, tmpdir):
2323
"""Ensure that clip gradients is only called if the value is greater than 0."""
2424
model = BoringModel()
25-
trainer = Trainer(strategy="ddp_sharded", gpus=1, precision=16, fast_dev_run=True, gradient_clip_val=clip_val)
25+
trainer = Trainer(
26+
strategy="ddp_sharded",
27+
accelerator="gpu",
28+
devices=1,
29+
precision=16,
30+
fast_dev_run=True,
31+
gradient_clip_val=clip_val,
32+
)
2633
trainer.fit(model)
2734
if clip_val > 0:
2835
mock_oss_clip_grad_norm.assert_called()
@@ -46,15 +53,15 @@ def test_sharded_ddp_choice(tmpdir, strategy, expected):
4653
)
4754
def test_ddp_choice_sharded_amp(tmpdir, strategy, expected):
4855
"""Test to ensure that plugin native amp plugin is correctly chosen when using sharded."""
49-
trainer = Trainer(fast_dev_run=True, gpus=1, precision=16, strategy=strategy)
56+
trainer = Trainer(fast_dev_run=True, accelerator="gpu", devices=1, precision=16, strategy=strategy)
5057
assert isinstance(trainer.strategy, expected)
5158

5259

5360
@RunIf(skip_windows=True, fairscale=True)
5461
def test_ddp_sharded_strategy_checkpoint_cpu(tmpdir):
5562
"""Test to ensure that checkpoint is saved correctly."""
5663
model = BoringModel()
57-
trainer = Trainer(strategy="ddp_sharded_spawn", num_processes=2, fast_dev_run=True)
64+
trainer = Trainer(strategy="ddp_sharded_spawn", accelerator="cpu", devices=2, fast_dev_run=True)
5865

5966
trainer.fit(model)
6067

@@ -103,7 +110,7 @@ def test_ddp_sharded_strategy_finetune(tmpdir):
103110
def test_ddp_sharded_strategy_fit_ckpt_path(tmpdir):
104111
"""Test to ensure that resuming from checkpoint works."""
105112
model = BoringModel()
106-
trainer = Trainer(strategy="ddp_sharded_spawn", num_processes=2, fast_dev_run=True)
113+
trainer = Trainer(strategy="ddp_sharded_spawn", accelerator="cpu", devices=2, fast_dev_run=True)
107114

108115
trainer.fit(model)
109116

@@ -112,7 +119,7 @@ def test_ddp_sharded_strategy_fit_ckpt_path(tmpdir):
112119

113120
model = BoringModel()
114121

115-
trainer = Trainer(strategy="ddp_sharded_spawn", num_processes=2, fast_dev_run=True)
122+
trainer = Trainer(strategy="ddp_sharded_spawn", accelerator="cpu", devices=2, fast_dev_run=True)
116123

117124
trainer.fit(model, ckpt_path=checkpoint_path)
118125

@@ -141,7 +148,7 @@ def test_ddp_sharded_strategy_fit_ckpt_path_downsize_gpus(tmpdir):
141148
def test_ddp_sharded_strategy_fit_ckpt_path_gpu_to_cpu(tmpdir):
142149
"""Test to ensure that resuming from checkpoint works when going from GPUs- > CPU."""
143150
model = BoringModel()
144-
trainer = Trainer(strategy="ddp_sharded_spawn", gpus=1, fast_dev_run=True)
151+
trainer = Trainer(strategy="ddp_sharded_spawn", accelerator="gpu", devices=1, fast_dev_run=True)
145152

146153
trainer.fit(model)
147154

@@ -150,13 +157,16 @@ def test_ddp_sharded_strategy_fit_ckpt_path_gpu_to_cpu(tmpdir):
150157

151158
model = BoringModel()
152159

153-
trainer = Trainer(strategy="ddp_sharded_spawn", num_processes=2, fast_dev_run=True)
160+
trainer = Trainer(strategy="ddp_sharded_spawn", accelerator="cpu", devices=2, fast_dev_run=True)
154161

155162
trainer.fit(model, ckpt_path=checkpoint_path)
156163

157164

158165
@RunIf(skip_windows=True, standalone=True, fairscale=True)
159-
@pytest.mark.parametrize("trainer_kwargs", (dict(num_processes=2), pytest.param(dict(gpus=2), marks=RunIf(min_gpus=2))))
166+
@pytest.mark.parametrize(
167+
"trainer_kwargs",
168+
(dict(accelerator="cpu", devices=2), pytest.param(dict(accelerator="gpu", devices=2), marks=RunIf(min_gpus=2))),
169+
)
160170
def test_ddp_sharded_strategy_test_multigpu(tmpdir, trainer_kwargs):
161171
"""Test to ensure we can use validate and test without fit."""
162172
model = BoringModel()

0 commit comments

Comments
 (0)