pytorch-lightning

Форк
0
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.
14
from unittest import mock
15

16
import pytest
17
from lightning.fabric.utilities import device_parser
18
from 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
    [
26
        pytest.param(None, None, id="No gpus, expect gpu root device to be None"),
27
        pytest.param([0], 0, id="Oth gpu, expect gpu root device to be 0."),
28
        pytest.param([1], 1, id="1st gpu, expect gpu root device to be 1."),
29
        pytest.param([3], 3, id="3rd gpu, expect gpu root device to be 3."),
30
        pytest.param([1, 2], 1, id="[1, 2] gpus, expect gpu root device to be 1."),
31
    ],
32
)
33
def test_determine_root_gpu_device(devices, expected_root_gpu):
34
    assert 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]),
44
        pytest.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]),
52
        pytest.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)
56
def test_parse_gpu_ids(_, devices, expected_gpu_ids):
57
    assert 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)
62
def test_parse_gpu_fail_on_unsupported_inputs(_, devices):
63
    with pytest.raises((TypeError, MisconfigurationException)):
64
        device_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)
69
def test_parse_gpu_fail_on_non_existent_id(_, devices):
70
    with pytest.raises((TypeError, MisconfigurationException)):
71
        device_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)
75
def test_parse_gpu_fail_on_non_existent_id_2(_):
76
    with pytest.raises((TypeError, MisconfigurationException)):
77
        device_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)
82
def test_parse_gpu_returns_none_when_no_devices_are_available(_, devices):
83
    with pytest.raises(MisconfigurationException):
84
        device_parser._parse_gpu_ids(devices, include_cuda=True)
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.