pytorch-lightning

Форк
0
87 строк · 3.1 Кб
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 os
15
from unittest import mock
16

17
import pytest
18
from lightning.fabric.plugins.environments import LightningEnvironment
19

20

21
@mock.patch.dict(os.environ, {}, clear=True)
22
def test_default_attributes():
23
    """Test the default attributes when no environment variables are set."""
24
    env = LightningEnvironment()
25
    assert not env.creates_processes_externally
26
    assert env.main_address == "127.0.0.1"
27
    assert isinstance(env.main_port, int)
28
    assert env.world_size() == 1
29
    assert env.local_rank() == 0
30
    assert env.node_rank() == 0
31

32

33
@mock.patch.dict(os.environ, {"MASTER_ADDR": "1.2.3.4", "MASTER_PORT": "500", "LOCAL_RANK": "2", "NODE_RANK": "3"})
34
def test_attributes_from_environment_variables():
35
    """Test that the default cluster environment takes the attributes from the environment variables."""
36
    env = LightningEnvironment()
37
    assert env.main_address == "1.2.3.4"
38
    assert env.main_port == 500
39
    assert env.world_size() == 1
40
    assert env.global_rank() == 0
41
    assert env.local_rank() == 2
42
    assert env.node_rank() == 3
43
    env.set_global_rank(100)
44
    assert env.global_rank() == 100
45
    env.set_world_size(100)
46
    assert env.world_size() == 100
47

48

49
@pytest.mark.parametrize(
50
    ("environ", "creates_processes_externally"), [({}, False), ({"LOCAL_RANK": "2"}, True), ({"NODE_RANK": "1"}, False)]
51
)
52
def test_manual_user_launch(environ, creates_processes_externally):
53
    """Test that the environment switches to manual user mode when LOCAL_RANK env variable detected."""
54
    with mock.patch.dict(os.environ, environ):
55
        env = LightningEnvironment()
56
        assert env.creates_processes_externally == creates_processes_externally
57

58

59
@mock.patch.dict(os.environ, {"GROUP_RANK": "1"})
60
def test_node_rank_from_group_rank():
61
    """Test that the GROUP_RANK substitutes NODE_RANK."""
62
    env = LightningEnvironment()
63
    assert "NODE_RANK" not in os.environ
64
    assert env.node_rank() == 1
65

66

67
@mock.patch.dict(os.environ, {}, clear=True)
68
def test_random_main_port():
69
    """Test randomly chosen main port when no main port was given by user."""
70
    env = LightningEnvironment()
71
    port = env.main_port
72
    assert isinstance(port, int)
73
    # repeated calls do not generate a new port number
74
    assert env.main_port == port
75

76

77
@mock.patch.dict(os.environ, {"WORLD_SIZE": "1"})
78
def test_teardown():
79
    """Test that the GROUP_RANK substitutes NODE_RANK."""
80
    env = LightningEnvironment()
81
    assert "WORLD_SIZE" in os.environ
82
    env.teardown()
83
    assert "WORLD_SIZE" not in os.environ
84

85

86
def test_detect():
87
    assert LightningEnvironment.detect()
88

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

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

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

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