Skip to content

Commit 8b4abe4

Browse files
Update tests/models/*.py to use devices instead of gpus or ipus (#11470)
1 parent 92a2a6e commit 8b4abe4

File tree

7 files changed

+63
-42
lines changed

7 files changed

+63
-42
lines changed

tests/models/test_amp.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ def _assert_autocast_enabled(self):
7777
],
7878
)
7979
@pytest.mark.parametrize("precision", [16, "bf16"])
80-
@pytest.mark.parametrize("num_processes", [1, 2])
81-
def test_amp_cpus(tmpdir, strategy, precision, num_processes):
80+
@pytest.mark.parametrize("devices", [1, 2])
81+
def test_amp_cpus(tmpdir, strategy, precision, devices):
8282
"""Make sure combinations of AMP and training types work if supported."""
8383
tutils.reset_seed()
8484

8585
trainer = Trainer(
86-
default_root_dir=tmpdir, num_processes=num_processes, max_epochs=1, strategy=strategy, precision=precision
86+
default_root_dir=tmpdir,
87+
accelerator="cpu",
88+
devices=devices,
89+
max_epochs=1,
90+
strategy=strategy,
91+
precision=precision,
8792
)
8893

8994
model = AMPTestModel()
@@ -97,12 +102,19 @@ def test_amp_cpus(tmpdir, strategy, precision, num_processes):
97102
@RunIf(min_gpus=2, min_torch="1.10")
98103
@pytest.mark.parametrize("strategy", [None, "dp", "ddp_spawn"])
99104
@pytest.mark.parametrize("precision", [16, "bf16"])
100-
@pytest.mark.parametrize("gpus", [1, 2])
101-
def test_amp_gpus(tmpdir, strategy, precision, gpus):
105+
@pytest.mark.parametrize("devices", [1, 2])
106+
def test_amp_gpus(tmpdir, strategy, precision, devices):
102107
"""Make sure combinations of AMP and training types work if supported."""
103108
tutils.reset_seed()
104109

105-
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, gpus=gpus, strategy=strategy, precision=precision)
110+
trainer = Trainer(
111+
default_root_dir=tmpdir,
112+
max_epochs=1,
113+
accelerator="gpu",
114+
devices=devices,
115+
strategy=strategy,
116+
precision=precision,
117+
)
106118

107119
model = AMPTestModel()
108120
trainer.fit(model)
@@ -141,7 +153,8 @@ def test_amp_gpu_ddp_slurm_managed(tmpdir):
141153
trainer = Trainer(
142154
default_root_dir=tmpdir,
143155
max_epochs=1,
144-
gpus=[0],
156+
accelerator="gpu",
157+
devices=[0],
145158
strategy="ddp_spawn",
146159
precision=16,
147160
callbacks=[checkpoint],
@@ -195,7 +208,9 @@ def configure_optimizers(self):
195208
model = CustomModel()
196209
model.training_epoch_end = None
197210

198-
trainer = Trainer(default_root_dir=tmpdir, max_steps=5, precision=16, amp_backend="apex", gpus=1)
211+
trainer = Trainer(
212+
default_root_dir=tmpdir, max_steps=5, precision=16, amp_backend="apex", accelerator="gpu", devices=1
213+
)
199214
assert str(trainer.amp_backend) == "AMPType.APEX"
200215
trainer.fit(model)
201216
assert trainer.state.finished, f"Training failed with {trainer.state}"

tests/models/test_cpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def test_multi_cpu_model_ddp(tmpdir):
136136
max_epochs=1,
137137
limit_train_batches=0.4,
138138
limit_val_batches=0.2,
139-
gpus=None,
140-
num_processes=2,
139+
accelerator="cpu",
140+
devices=2,
141141
strategy="ddp_spawn",
142142
)
143143

tests/models/test_gpu.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def test_multi_gpu_none_backend(tmpdir):
4747
max_epochs=1,
4848
limit_train_batches=0.2,
4949
limit_val_batches=0.2,
50-
gpus=2,
50+
accelerator="gpu",
51+
devices=2,
5152
)
5253

5354
dm = ClassifDataModule()
@@ -56,16 +57,17 @@ def test_multi_gpu_none_backend(tmpdir):
5657

5758

5859
@RunIf(min_gpus=2)
59-
@pytest.mark.parametrize("gpus", [1, [0], [1]])
60-
def test_single_gpu_model(tmpdir, gpus):
60+
@pytest.mark.parametrize("devices", [1, [0], [1]])
61+
def test_single_gpu_model(tmpdir, devices):
6162
"""Make sure single GPU works (DP mode)."""
6263
trainer_options = dict(
6364
default_root_dir=tmpdir,
6465
enable_progress_bar=False,
6566
max_epochs=1,
6667
limit_train_batches=0.1,
6768
limit_val_batches=0.1,
68-
gpus=gpus,
69+
accelerator="gpu",
70+
devices=devices,
6971
)
7072

7173
model = BoringModel()
@@ -94,7 +96,7 @@ def device_count():
9496

9597
# Asking for a gpu when non are available will result in a MisconfigurationException
9698
@pytest.mark.parametrize(
97-
["gpus", "expected_root_gpu", "strategy"],
99+
["devices", "expected_root_gpu", "strategy"],
98100
[
99101
(1, None, "ddp"),
100102
(3, None, "ddp"),
@@ -105,13 +107,13 @@ def device_count():
105107
("-1", None, "ddp"),
106108
],
107109
)
108-
def test_root_gpu_property_0_raising(mocked_device_count_0, gpus, expected_root_gpu, strategy):
110+
def test_root_gpu_property_0_raising(mocked_device_count_0, devices, expected_root_gpu, strategy):
109111
with pytest.raises(MisconfigurationException):
110-
Trainer(gpus=gpus, strategy=strategy)
112+
Trainer(accelerator="gpu", devices=devices, strategy=strategy)
111113

112114

113115
@pytest.mark.parametrize(
114-
["gpus", "expected_root_gpu"],
116+
["devices", "expected_root_gpu"],
115117
[
116118
pytest.param(None, None, id="No gpus, expect gpu root device to be None"),
117119
pytest.param([0], 0, id="Oth gpu, expect gpu root device to be 0."),
@@ -120,12 +122,12 @@ def test_root_gpu_property_0_raising(mocked_device_count_0, gpus, expected_root_
120122
pytest.param([1, 2], 1, id="[1, 2] gpus, expect gpu root device to be 1."),
121123
],
122124
)
123-
def test_determine_root_gpu_device(gpus, expected_root_gpu):
124-
assert device_parser.determine_root_gpu_device(gpus) == expected_root_gpu
125+
def test_determine_root_gpu_device(devices, expected_root_gpu):
126+
assert device_parser.determine_root_gpu_device(devices) == expected_root_gpu
125127

126128

127129
@pytest.mark.parametrize(
128-
["gpus", "expected_gpu_ids"],
130+
["devices", "expected_gpu_ids"],
129131
[
130132
(None, None),
131133
(0, None),
@@ -143,31 +145,31 @@ def test_determine_root_gpu_device(gpus, expected_root_gpu):
143145
pytest.param("-1", list(range(PRETEND_N_OF_GPUS)), id="'-1' - use all gpus"),
144146
],
145147
)
146-
def test_parse_gpu_ids(mocked_device_count, gpus, expected_gpu_ids):
147-
assert device_parser.parse_gpu_ids(gpus) == expected_gpu_ids
148+
def test_parse_gpu_ids(mocked_device_count, devices, expected_gpu_ids):
149+
assert device_parser.parse_gpu_ids(devices) == expected_gpu_ids
148150

149151

150-
@pytest.mark.parametrize("gpus", [0.1, -2, False, [-1], [None], ["0"], [0, 0]])
151-
def test_parse_gpu_fail_on_unsupported_inputs(mocked_device_count, gpus):
152+
@pytest.mark.parametrize("devices", [0.1, -2, False, [-1], [None], ["0"], [0, 0]])
153+
def test_parse_gpu_fail_on_unsupported_inputs(mocked_device_count, devices):
152154
with pytest.raises(MisconfigurationException):
153-
device_parser.parse_gpu_ids(gpus)
155+
device_parser.parse_gpu_ids(devices)
154156

155157

156-
@pytest.mark.parametrize("gpus", [[1, 2, 19], -1, "-1"])
157-
def test_parse_gpu_fail_on_non_existent_id(mocked_device_count_0, gpus):
158+
@pytest.mark.parametrize("devices", [[1, 2, 19], -1, "-1"])
159+
def test_parse_gpu_fail_on_non_existent_id(mocked_device_count_0, devices):
158160
with pytest.raises(MisconfigurationException):
159-
device_parser.parse_gpu_ids(gpus)
161+
device_parser.parse_gpu_ids(devices)
160162

161163

162164
def test_parse_gpu_fail_on_non_existent_id_2(mocked_device_count):
163165
with pytest.raises(MisconfigurationException):
164166
device_parser.parse_gpu_ids([1, 2, 19])
165167

166168

167-
@pytest.mark.parametrize("gpus", [-1, "-1"])
168-
def test_parse_gpu_returns_none_when_no_devices_are_available(mocked_device_count_0, gpus):
169+
@pytest.mark.parametrize("devices", [-1, "-1"])
170+
def test_parse_gpu_returns_none_when_no_devices_are_available(mocked_device_count_0, devices):
169171
with pytest.raises(MisconfigurationException):
170-
device_parser.parse_gpu_ids(gpus)
172+
device_parser.parse_gpu_ids(devices)
171173

172174

173175
@mock.patch.dict(
@@ -198,7 +200,7 @@ def test_torchelastic_gpu_parsing(mocked_device_count, mocked_is_available, gpus
198200

199201
@RunIf(min_gpus=1)
200202
def test_single_gpu_batch_parse():
201-
trainer = Trainer(gpus=1)
203+
trainer = Trainer(accelerator="gpu", devices=1)
202204

203205
# non-transferrable types
204206
primitive_objects = [None, {}, [], 1.0, "x", [None, 2], {"x": (1, 2), "y": None}]

tests/models/test_hooks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def train_dataloader(self):
203203
max_epochs=1,
204204
enable_model_summary=False,
205205
strategy="ddp",
206-
gpus=2,
206+
accelerator="gpu",
207+
devices=2,
207208
)
208209
trainer.fit(model)
209210

tests/models/test_onnx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def test_model_saves_on_multi_gpu(tmpdir):
9696
max_epochs=1,
9797
limit_train_batches=10,
9898
limit_val_batches=10,
99-
gpus=[0, 1],
99+
accelerator="gpu",
100+
devices=[0, 1],
100101
strategy="ddp_spawn",
101102
enable_progress_bar=False,
102103
)

tests/models/test_restore.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ def test_running_test_pretrained_model_distrib_dp(tmpdir):
399399
limit_val_batches=5,
400400
callbacks=[checkpoint],
401401
logger=logger,
402-
gpus=[0, 1],
402+
accelerator="gpu",
403+
devices=[0, 1],
403404
strategy="dp",
404405
default_root_dir=tmpdir,
405406
)
@@ -445,7 +446,8 @@ def test_running_test_pretrained_model_distrib_ddp_spawn(tmpdir):
445446
limit_val_batches=2,
446447
callbacks=[checkpoint],
447448
logger=logger,
448-
gpus=[0, 1],
449+
accelerator="gpu",
450+
devices=[0, 1],
449451
strategy="ddp_spawn",
450452
default_root_dir=tmpdir,
451453
)
@@ -564,7 +566,7 @@ def test_dp_resume(tmpdir):
564566
model = CustomClassificationModelDP(lr=0.1)
565567
dm = ClassifDataModule()
566568

567-
trainer_options = dict(max_epochs=1, gpus=2, strategy="dp", default_root_dir=tmpdir)
569+
trainer_options = dict(max_epochs=1, accelerator="gpu", devices=2, strategy="dp", default_root_dir=tmpdir)
568570

569571
# get logger
570572
logger = tutils.get_default_logger(tmpdir)

tests/models/test_tpu.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def val_dataloader(self):
5050

5151
@RunIf(tpu=True)
5252
@pl_multi_process_test
53-
def test_model_tpu_cores_1(tmpdir):
53+
def test_model_tpu_devices_1(tmpdir):
5454
"""Make sure model trains on TPU."""
5555
tutils.reset_seed()
5656
trainer_options = dict(
@@ -90,7 +90,7 @@ def test_model_tpu_index(tmpdir, tpu_core):
9090

9191
@RunIf(tpu=True)
9292
@pl_multi_process_test
93-
def test_model_tpu_cores_8(tmpdir):
93+
def test_model_tpu_devices_8(tmpdir):
9494
"""Make sure model trains on TPU."""
9595
tutils.reset_seed()
9696
trainer_options = dict(
@@ -110,7 +110,7 @@ def test_model_tpu_cores_8(tmpdir):
110110

111111
@RunIf(tpu=True)
112112
@pl_multi_process_test
113-
def test_model_16bit_tpu_cores_1(tmpdir):
113+
def test_model_16bit_tpu_devices_1(tmpdir):
114114
"""Make sure model trains on TPU."""
115115
tutils.reset_seed()
116116
trainer_options = dict(
@@ -152,7 +152,7 @@ def test_model_16bit_tpu_index(tmpdir, tpu_core):
152152

153153
@RunIf(tpu=True)
154154
@pl_multi_process_test
155-
def test_model_16bit_tpu_cores_8(tmpdir):
155+
def test_model_16bit_tpu_devices_8(tmpdir):
156156
"""Make sure model trains on TPU."""
157157
tutils.reset_seed()
158158
trainer_options = dict(

0 commit comments

Comments
 (0)