pytorch-lightning
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.
14import os
15from unittest import mock
16
17import pytest
18from lightning.fabric.plugins.environments import LightningEnvironment
19
20
21@mock.patch.dict(os.environ, {}, clear=True)
22def test_default_attributes():
23"""Test the default attributes when no environment variables are set."""
24env = LightningEnvironment()
25assert not env.creates_processes_externally
26assert env.main_address == "127.0.0.1"
27assert isinstance(env.main_port, int)
28assert env.world_size() == 1
29assert env.local_rank() == 0
30assert 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"})
34def test_attributes_from_environment_variables():
35"""Test that the default cluster environment takes the attributes from the environment variables."""
36env = LightningEnvironment()
37assert env.main_address == "1.2.3.4"
38assert env.main_port == 500
39assert env.world_size() == 1
40assert env.global_rank() == 0
41assert env.local_rank() == 2
42assert env.node_rank() == 3
43env.set_global_rank(100)
44assert env.global_rank() == 100
45env.set_world_size(100)
46assert 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)
52def test_manual_user_launch(environ, creates_processes_externally):
53"""Test that the environment switches to manual user mode when LOCAL_RANK env variable detected."""
54with mock.patch.dict(os.environ, environ):
55env = LightningEnvironment()
56assert env.creates_processes_externally == creates_processes_externally
57
58
59@mock.patch.dict(os.environ, {"GROUP_RANK": "1"})
60def test_node_rank_from_group_rank():
61"""Test that the GROUP_RANK substitutes NODE_RANK."""
62env = LightningEnvironment()
63assert "NODE_RANK" not in os.environ
64assert env.node_rank() == 1
65
66
67@mock.patch.dict(os.environ, {}, clear=True)
68def test_random_main_port():
69"""Test randomly chosen main port when no main port was given by user."""
70env = LightningEnvironment()
71port = env.main_port
72assert isinstance(port, int)
73# repeated calls do not generate a new port number
74assert env.main_port == port
75
76
77@mock.patch.dict(os.environ, {"WORLD_SIZE": "1"})
78def test_teardown():
79"""Test that the GROUP_RANK substitutes NODE_RANK."""
80env = LightningEnvironment()
81assert "WORLD_SIZE" in os.environ
82env.teardown()
83assert "WORLD_SIZE" not in os.environ
84
85
86def test_detect():
87assert LightningEnvironment.detect()
88