-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
test_cli.py
157 lines (135 loc) · 5.88 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from unittest import mock
from unittest.mock import Mock
import pytest
import torch.distributed.run
from tests_fabric.helpers.runif import RunIf
from lightning_fabric.cli import _run_model
from lightning_fabric.utilities.imports import _IS_WINDOWS, _TORCH_GREATER_EQUAL_1_13
@pytest.fixture
def fake_script(tmp_path):
script = tmp_path / "script.py"
script.touch()
return str(script)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_cli_env_vars_defaults(monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script])
assert e.value.code == 0
assert os.environ["LT_CLI_USED"] == "1"
assert os.environ["LT_ACCELERATOR"] == "cpu"
assert "LT_STRATEGY" not in os.environ
assert os.environ["LT_DEVICES"] == "1"
assert os.environ["LT_NUM_NODES"] == "1"
assert os.environ["LT_PRECISION"] == "32"
@pytest.mark.parametrize("accelerator", ["cpu", "gpu", "cuda", pytest.param("mps", marks=RunIf(mps=True))])
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
@mock.patch("lightning_fabric.accelerators.cuda.num_cuda_devices", return_value=2)
def test_cli_env_vars_accelerator(_, accelerator, monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--accelerator", accelerator])
assert e.value.code == 0
assert os.environ["LT_ACCELERATOR"] == accelerator
@pytest.mark.parametrize("strategy", ["dp", "ddp", "deepspeed"])
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
@mock.patch("lightning_fabric.accelerators.cuda.num_cuda_devices", return_value=2)
def test_cli_env_vars_strategy(_, strategy, monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--strategy", strategy])
assert e.value.code == 0
assert os.environ["LT_STRATEGY"] == strategy
@pytest.mark.parametrize("devices", ["1", "2", "0,", "1,0", "-1"])
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
@mock.patch("lightning_fabric.accelerators.cuda.num_cuda_devices", return_value=2)
def test_cli_env_vars_devices_cuda(_, devices, monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--accelerator", "cuda", "--devices", devices])
assert e.value.code == 0
assert os.environ["LT_DEVICES"] == devices
@RunIf(mps=True)
@pytest.mark.parametrize("accelerator", ["mps", "gpu"])
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_cli_env_vars_devices_mps(accelerator, monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--accelerator", accelerator])
assert e.value.code == 0
assert os.environ["LT_DEVICES"] == "1"
@pytest.mark.parametrize("num_nodes", ["1", "2", "3"])
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_cli_env_vars_num_nodes(num_nodes, monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--num-nodes", num_nodes])
assert e.value.code == 0
assert os.environ["LT_NUM_NODES"] == num_nodes
@pytest.mark.parametrize("precision", ["64", "32", "16", "bf16"])
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_cli_env_vars_precision(precision, monkeypatch, fake_script):
monkeypatch.setattr(torch.distributed, "run", Mock())
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--precision", precision])
assert e.value.code == 0
assert os.environ["LT_PRECISION"] == precision
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_cli_torchrun_defaults(monkeypatch, fake_script):
torchrun_mock = Mock()
monkeypatch.setattr(torch.distributed, "run", torchrun_mock)
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script])
assert e.value.code == 0
torchrun_mock.main.assert_called_with(
[
"--nproc_per_node=1",
"--nnodes=1",
"--node_rank=0",
"--master_addr=127.0.0.1",
"--master_port=29400",
fake_script,
]
)
@pytest.mark.parametrize(
"devices,expected",
[
("1", 1),
("2", 2),
("0,", 1),
("1,0,2", 3),
("-1", 5),
],
)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
@mock.patch("lightning_fabric.accelerators.cuda.num_cuda_devices", return_value=5)
def test_cli_torchrun_num_processes_launched(_, devices, expected, monkeypatch, fake_script):
torchrun_mock = Mock()
monkeypatch.setattr(torch.distributed, "run", torchrun_mock)
with pytest.raises(SystemExit) as e:
_run_model.main([fake_script, "--accelerator", "cuda", "--devices", devices])
assert e.value.code == 0
torchrun_mock.main.assert_called_with(
[
f"--nproc_per_node={expected}",
"--nnodes=1",
"--node_rank=0",
"--master_addr=127.0.0.1",
"--master_port=29400",
fake_script,
]
)