-
Notifications
You must be signed in to change notification settings - Fork 413
/
conftest.py
76 lines (61 loc) · 2.31 KB
/
conftest.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
# Copyright The 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 contextlib
import os
import sys
import pytest
import torch
from torch.multiprocessing import Pool, set_sharing_strategy, set_start_method
with contextlib.suppress(RuntimeError):
set_start_method("spawn")
set_sharing_strategy("file_system")
NUM_PROCESSES = 2 # torch.cuda.device_count() if torch.cuda.is_available() else 2
NUM_BATCHES = 2 * NUM_PROCESSES # Need to be divisible with the number of processes
BATCH_SIZE = 32
NUM_CLASSES = 5
EXTRA_DIM = 3
THRESHOLD = 0.5
MAX_PORT = 8100
START_PORT = 8088
CURRENT_PORT = START_PORT
USE_PYTEST_POOL = os.getenv("USE_PYTEST_POOL", "0") == "1"
@pytest.fixture()
def use_deterministic_algorithms(): # noqa: PT004
"""Set deterministic algorithms for the test."""
torch.use_deterministic_algorithms(True)
yield
torch.use_deterministic_algorithms(False)
def setup_ddp(rank, world_size):
"""Initialize ddp environment."""
global CURRENT_PORT
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = str(CURRENT_PORT)
CURRENT_PORT += 1
if CURRENT_PORT > MAX_PORT:
CURRENT_PORT = START_PORT
if torch.distributed.is_available() and sys.platform not in ("win32", "cygwin"):
torch.distributed.init_process_group("gloo", rank=rank, world_size=world_size)
def pytest_sessionstart():
"""Global initialization of multiprocessing pool; runs before any test."""
if not USE_PYTEST_POOL:
return
pool = Pool(processes=NUM_PROCESSES)
pool.starmap(setup_ddp, [(rank, NUM_PROCESSES) for rank in range(NUM_PROCESSES)])
pytest.pool = pool
def pytest_sessionfinish():
"""Correctly closes the global multiprocessing pool; runs after all tests."""
if not USE_PYTEST_POOL:
return
pytest.pool.close()
pytest.pool.join()