pytorch-lightning

Форк
0
93 строки · 3.2 Кб
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
import logging
15
import os
16
import re
17
from unittest import mock
18

19
import pytest
20
from lightning.fabric.plugins.environments import TorchElasticEnvironment
21

22

23
@mock.patch.dict(os.environ, {}, clear=True)
24
def test_default_attributes():
25
    """Test the default attributes when no environment variables are set."""
26
    env = TorchElasticEnvironment()
27
    assert env.creates_processes_externally
28
    assert env.main_address == "127.0.0.1"
29
    assert env.main_port == 12910
30
    with pytest.raises(KeyError):
31
        # world size is required to be passed as env variable
32
        env.world_size()
33
    with pytest.raises(KeyError):
34
        # local rank is required to be passed as env variable
35
        env.local_rank()
36
    assert env.node_rank() == 0
37

38

39
@mock.patch.dict(
40
    os.environ,
41
    {
42
        "MASTER_ADDR": "1.2.3.4",
43
        "MASTER_PORT": "500",
44
        "WORLD_SIZE": "20",
45
        "RANK": "1",
46
        "LOCAL_RANK": "2",
47
        "GROUP_RANK": "3",
48
    },
49
)
50
def test_attributes_from_environment_variables(caplog):
51
    """Test that the torchelastic cluster environment takes the attributes from the environment variables."""
52
    env = TorchElasticEnvironment()
53
    assert env.main_address == "1.2.3.4"
54
    assert env.main_port == 500
55
    assert env.world_size() == 20
56
    assert env.global_rank() == 1
57
    assert env.local_rank() == 2
58
    assert env.node_rank() == 3
59
    # setter should be no-op
60
    with caplog.at_level(logging.DEBUG, logger="lightning.fabric.plugins.environments"):
61
        env.set_global_rank(100)
62
    assert env.global_rank() == 1
63
    assert "setting global rank is not allowed" in caplog.text
64

65
    caplog.clear()
66

67
    with caplog.at_level(logging.DEBUG, logger="lightning.fabric.plugins.environments"):
68
        env.set_world_size(100)
69
    assert env.world_size() == 20
70
    assert "setting world size is not allowed" in caplog.text
71

72

73
def test_detect():
74
    """Test the detection of a torchelastic environment configuration."""
75
    with mock.patch.dict(os.environ, {}, clear=True):
76
        assert not TorchElasticEnvironment.detect()
77

78
    with mock.patch.dict(
79
        os.environ,
80
        {
81
            "TORCHELASTIC_RUN_ID": "",
82
        },
83
    ):
84
        assert TorchElasticEnvironment.detect()
85

86

87
@mock.patch.dict(os.environ, {"WORLD_SIZE": "8"})
88
def test_validate_user_settings():
89
    """Test that the environment can validate the number of devices and nodes set in Fabric/Trainer."""
90
    env = TorchElasticEnvironment()
91
    env.validate_settings(num_devices=4, num_nodes=2)
92
    with pytest.raises(ValueError, match=re.escape("the product (2 * 2) does not match the world size (8)")):
93
        env.validate_settings(num_devices=2, num_nodes=2)
94

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

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

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

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