|
16 | 16 | import pytest |
17 | 17 | import torch |
18 | 18 |
|
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) |
20 | 103 |
|
21 | 104 |
|
22 | 105 | @pytest.mark.skipif( |
|
27 | 110 | def test_num_cuda_devices_without_forking(*_): |
28 | 111 | """This merely tests that on platforms without fork support our helper functions fall back to the default |
29 | 112 | 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 |
0 commit comments