Skip to content

Commit 9537444

Browse files
authored
Move device parser tests inside Lite (#14586)
1 parent 59fcabf commit 9537444

File tree

2 files changed

+86
-63
lines changed

2 files changed

+86
-63
lines changed

tests/tests_lite/utilities/test_device_parser.py

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,90 @@
1616
import pytest
1717
import torch
1818

19-
import lightning_lite.utilities.device_parser
19+
from lightning_lite.utilities import device_parser
20+
from lightning_lite.utilities.exceptions import MisconfigurationException
21+
22+
_PRETEND_N_OF_GPUS = 16
23+
24+
25+
@pytest.fixture
26+
def mocked_device_count(monkeypatch):
27+
def device_count():
28+
return _PRETEND_N_OF_GPUS
29+
30+
def is_available():
31+
return True
32+
33+
monkeypatch.setattr(device_parser, "is_cuda_available", is_available)
34+
monkeypatch.setattr(device_parser, "num_cuda_devices", device_count)
35+
36+
37+
@pytest.fixture
38+
def mocked_device_count_0(monkeypatch):
39+
def device_count():
40+
return 0
41+
42+
monkeypatch.setattr(device_parser, "num_cuda_devices", device_count)
43+
44+
45+
@pytest.mark.parametrize(
46+
["devices", "expected_root_gpu"],
47+
[
48+
pytest.param(None, None, id="No gpus, expect gpu root device to be None"),
49+
pytest.param([0], 0, id="Oth gpu, expect gpu root device to be 0."),
50+
pytest.param([1], 1, id="1st gpu, expect gpu root device to be 1."),
51+
pytest.param([3], 3, id="3rd gpu, expect gpu root device to be 3."),
52+
pytest.param([1, 2], 1, id="[1, 2] gpus, expect gpu root device to be 1."),
53+
],
54+
)
55+
def test_determine_root_gpu_device(devices, expected_root_gpu):
56+
assert device_parser.determine_root_gpu_device(devices) == expected_root_gpu
57+
58+
59+
@pytest.mark.parametrize(
60+
["devices", "expected_gpu_ids"],
61+
[
62+
(None, None),
63+
(0, None),
64+
([], None),
65+
(1, [0]),
66+
(3, [0, 1, 2]),
67+
pytest.param(-1, list(range(_PRETEND_N_OF_GPUS)), id="-1 - use all gpus"),
68+
([0], [0]),
69+
([1, 3], [1, 3]),
70+
((1, 3), [1, 3]),
71+
("0", None),
72+
("3", [0, 1, 2]),
73+
("1, 3", [1, 3]),
74+
("2,", [2]),
75+
pytest.param("-1", list(range(_PRETEND_N_OF_GPUS)), id="'-1' - use all gpus"),
76+
],
77+
)
78+
def test_parse_gpu_ids(mocked_device_count, devices, expected_gpu_ids):
79+
assert device_parser.parse_gpu_ids(devices, include_cuda=True) == expected_gpu_ids
80+
81+
82+
@pytest.mark.parametrize("devices", [0.1, -2, False, [-1], [None], ["0"], [0, 0]])
83+
def test_parse_gpu_fail_on_unsupported_inputs(mocked_device_count, devices):
84+
with pytest.raises(MisconfigurationException):
85+
device_parser.parse_gpu_ids(devices, include_cuda=True)
86+
87+
88+
@pytest.mark.parametrize("devices", [[1, 2, 19], -1, "-1"])
89+
def test_parse_gpu_fail_on_non_existent_id(mocked_device_count_0, devices):
90+
with pytest.raises(MisconfigurationException):
91+
device_parser.parse_gpu_ids(devices, include_cuda=True)
92+
93+
94+
def test_parse_gpu_fail_on_non_existent_id_2(mocked_device_count):
95+
with pytest.raises(MisconfigurationException):
96+
device_parser.parse_gpu_ids([1, 2, 19], include_cuda=True)
97+
98+
99+
@pytest.mark.parametrize("devices", [-1, "-1"])
100+
def test_parse_gpu_returns_none_when_no_devices_are_available(mocked_device_count_0, devices):
101+
with pytest.raises(MisconfigurationException):
102+
device_parser.parse_gpu_ids(devices, include_cuda=True)
20103

21104

22105
@pytest.mark.skipif(
@@ -27,5 +110,5 @@
27110
def test_num_cuda_devices_without_forking(*_):
28111
"""This merely tests that on platforms without fork support our helper functions fall back to the default
29112
implementation for determining cuda availability."""
30-
assert lightning_lite.utilities.device_parser.is_cuda_available()
31-
assert lightning_lite.utilities.device_parser.num_cuda_devices() == 2
113+
assert device_parser.is_cuda_available()
114+
assert device_parser.num_cuda_devices() == 2

tests/tests_pytorch/models/test_gpu.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -109,66 +109,6 @@ def test_root_gpu_property_0_raising(mocked_device_count_0, devices, expected_ro
109109
Trainer(accelerator="gpu", devices=devices, strategy=strategy)
110110

111111

112-
@pytest.mark.parametrize(
113-
["devices", "expected_root_gpu"],
114-
[
115-
pytest.param(None, None, id="No gpus, expect gpu root device to be None"),
116-
pytest.param([0], 0, id="Oth gpu, expect gpu root device to be 0."),
117-
pytest.param([1], 1, id="1st gpu, expect gpu root device to be 1."),
118-
pytest.param([3], 3, id="3rd gpu, expect gpu root device to be 3."),
119-
pytest.param([1, 2], 1, id="[1, 2] gpus, expect gpu root device to be 1."),
120-
],
121-
)
122-
def test_determine_root_gpu_device(devices, expected_root_gpu):
123-
assert device_parser.determine_root_gpu_device(devices) == expected_root_gpu
124-
125-
126-
@pytest.mark.parametrize(
127-
["devices", "expected_gpu_ids"],
128-
[
129-
(None, None),
130-
(0, None),
131-
([], None),
132-
(1, [0]),
133-
(3, [0, 1, 2]),
134-
pytest.param(-1, list(range(PRETEND_N_OF_GPUS)), id="-1 - use all gpus"),
135-
([0], [0]),
136-
([1, 3], [1, 3]),
137-
((1, 3), [1, 3]),
138-
("0", None),
139-
("3", [0, 1, 2]),
140-
("1, 3", [1, 3]),
141-
("2,", [2]),
142-
pytest.param("-1", list(range(PRETEND_N_OF_GPUS)), id="'-1' - use all gpus"),
143-
],
144-
)
145-
def test_parse_gpu_ids(mocked_device_count, devices, expected_gpu_ids):
146-
assert device_parser.parse_gpu_ids(devices, include_cuda=True) == expected_gpu_ids
147-
148-
149-
@pytest.mark.parametrize("devices", [0.1, -2, False, [-1], [None], ["0"], [0, 0]])
150-
def test_parse_gpu_fail_on_unsupported_inputs(mocked_device_count, devices):
151-
with pytest.raises(MisconfigurationException):
152-
device_parser.parse_gpu_ids(devices, include_cuda=True)
153-
154-
155-
@pytest.mark.parametrize("devices", [[1, 2, 19], -1, "-1"])
156-
def test_parse_gpu_fail_on_non_existent_id(mocked_device_count_0, devices):
157-
with pytest.raises(MisconfigurationException):
158-
device_parser.parse_gpu_ids(devices, include_cuda=True)
159-
160-
161-
def test_parse_gpu_fail_on_non_existent_id_2(mocked_device_count):
162-
with pytest.raises(MisconfigurationException):
163-
device_parser.parse_gpu_ids([1, 2, 19], include_cuda=True)
164-
165-
166-
@pytest.mark.parametrize("devices", [-1, "-1"])
167-
def test_parse_gpu_returns_none_when_no_devices_are_available(mocked_device_count_0, devices):
168-
with pytest.raises(MisconfigurationException):
169-
device_parser.parse_gpu_ids(devices, include_cuda=True)
170-
171-
172112
@mock.patch.dict(
173113
os.environ,
174114
{

0 commit comments

Comments
 (0)