pytorch-lightning
84 строки · 3.4 Кб
1# Copyright The Lightning AI team.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14from unittest import mock
15
16import pytest
17from lightning.fabric.utilities import device_parser
18from lightning.fabric.utilities.exceptions import MisconfigurationException
19
20_PRETEND_N_OF_GPUS = 16
21
22
23@pytest.mark.parametrize(
24("devices", "expected_root_gpu"),
25[
26pytest.param(None, None, id="No gpus, expect gpu root device to be None"),
27pytest.param([0], 0, id="Oth gpu, expect gpu root device to be 0."),
28pytest.param([1], 1, id="1st gpu, expect gpu root device to be 1."),
29pytest.param([3], 3, id="3rd gpu, expect gpu root device to be 3."),
30pytest.param([1, 2], 1, id="[1, 2] gpus, expect gpu root device to be 1."),
31],
32)
33def test_determine_root_gpu_device(devices, expected_root_gpu):
34assert device_parser._determine_root_gpu_device(devices) == expected_root_gpu
35
36
37@pytest.mark.parametrize(
38("devices", "expected_gpu_ids"),
39[
40(0, None),
41([], None),
42(1, [0]),
43(3, [0, 1, 2]),
44pytest.param(-1, list(range(_PRETEND_N_OF_GPUS)), id="-1 - use all gpus"),
45([0], [0]),
46([1, 3], [1, 3]),
47((1, 3), [1, 3]),
48("0", None),
49("3", [0, 1, 2]),
50("1, 3", [1, 3]),
51("2,", [2]),
52pytest.param("-1", list(range(_PRETEND_N_OF_GPUS)), id="'-1' - use all gpus"),
53],
54)
55@mock.patch("lightning.fabric.accelerators.cuda.num_cuda_devices", return_value=_PRETEND_N_OF_GPUS)
56def test_parse_gpu_ids(_, devices, expected_gpu_ids):
57assert device_parser._parse_gpu_ids(devices, include_cuda=True) == expected_gpu_ids
58
59
60@pytest.mark.parametrize("devices", [0.1, -2, False, [-1], [None], ["0"], [0, 0]])
61@mock.patch("lightning.fabric.accelerators.cuda.num_cuda_devices", return_value=_PRETEND_N_OF_GPUS)
62def test_parse_gpu_fail_on_unsupported_inputs(_, devices):
63with pytest.raises((TypeError, MisconfigurationException)):
64device_parser._parse_gpu_ids(devices, include_cuda=True)
65
66
67@pytest.mark.parametrize("devices", [[1, 2, 19], -1, "-1"])
68@mock.patch("lightning.fabric.accelerators.cuda.num_cuda_devices", return_value=0)
69def test_parse_gpu_fail_on_non_existent_id(_, devices):
70with pytest.raises((TypeError, MisconfigurationException)):
71device_parser._parse_gpu_ids(devices, include_cuda=True)
72
73
74@mock.patch("lightning.fabric.accelerators.cuda.num_cuda_devices", return_value=_PRETEND_N_OF_GPUS)
75def test_parse_gpu_fail_on_non_existent_id_2(_):
76with pytest.raises((TypeError, MisconfigurationException)):
77device_parser._parse_gpu_ids([1, 2, 19], include_cuda=True)
78
79
80@pytest.mark.parametrize("devices", [-1, "-1"])
81@mock.patch("lightning.fabric.accelerators.cuda.num_cuda_devices", return_value=0)
82def test_parse_gpu_returns_none_when_no_devices_are_available(_, devices):
83with pytest.raises(MisconfigurationException):
84device_parser._parse_gpu_ids(devices, include_cuda=True)
85