forked from pytorch/torchtune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
124 lines (98 loc) · 4.34 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
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import os
import uuid
from pathlib import Path
import pytest
import torch.distributed.launcher as pet
import torchtune
root = str(Path(torchtune.__file__).parent.parent.absolute())
CACHE_ARTIFACTS_SCRIPT_PATH = root + "/tests/cache_artifacts.sh"
def pytest_configure(config):
"""
This hook runs before each pytest invocation. Its purpose is to handle optional fetching
of remote artifacts needed for the test run and filtering across unit tests, recipe tests, and
regression tests.
When testing, you should run one of the following:
- `pytest tests`: run unit tests only
- `pytest tests --with-integration`: run unit tests and recipe tests
- `pytest tests --with-integration --with-slow-integration`: run all tests
- `pytest tests -m integration_test`: run recipe tests only
- `pytest tests -m slow_integration_test`: run regression tests only
Similar commands apply for filtering in subdirectories or individual test files.
This hook also ensures that the appropriate artifacts are available locally for all of the above cases.
Note that artifact download is determined by the CLI flags, so if you run e.g.
`pytest tests/torchtune/some_unit_test.py -m integration_test`, the integration test
artifacts will be downloaded even if your test doesn't require them.
The hook also supports optional silencing of S3 progress bars to reduce CI log spew via `--silence-s3-logs`.
"""
# To make it more convenient to run an individual unit test, we override the default
# behavior of pytest-integration to run with --without-integration --without-slow-integration
# This means that we need to manually override the values of run_integration and run_slow_integration
# whenever either set of tests is passed via the -m option.
if config.option.markexpr == "integration_test":
config.option.run_integration = True
run_regression_tests = False
if config.option.markexpr == "slow_integration_test":
config.option.run_slow_integration = True
run_recipe_tests = False
# Default is to run both integration and slow integration tests (i.e. both are None)
run_recipe_tests = (
config.option.run_integration is None or config.option.run_integration is True
)
run_regression_tests = (
config.option.run_slow_integration is None
or config.option.run_slow_integration is True
)
cmd = str(CACHE_ARTIFACTS_SCRIPT_PATH)
if run_recipe_tests:
cmd += " --run-recipe-tests"
if run_regression_tests:
cmd += " --run-regression-tests"
# Optionally silence S3 download logs (useful when running on CI)
if config.option.silence_s3_logs:
cmd += " --silence-s3-logs"
# Only need to handle artifacts for recipe and regression tests
if run_recipe_tests or run_regression_tests:
os.system(cmd)
@pytest.fixture(scope="session")
def get_pet_launch_config():
def get_pet_launch_config_fn(nproc: int) -> pet.LaunchConfig:
"""
Initialize pet.LaunchConfig for single-node, multi-rank functions.
Args:
nproc (int): The number of processes to launch.
Returns:
An instance of pet.LaunchConfig for single-node, multi-rank functions.
Example:
>>> from torch.distributed import launcher
>>> launch_config = get_pet_launch_config(nproc=8)
>>> launcher.elastic_launch(config=launch_config, entrypoint=train)()
"""
return pet.LaunchConfig(
min_nodes=1,
max_nodes=1,
nproc_per_node=nproc,
run_id=str(uuid.uuid4()),
rdzv_backend="c10d",
rdzv_endpoint="localhost:0",
max_restarts=0,
monitor_interval=1,
)
return get_pet_launch_config_fn
def pytest_addoption(parser: argparse.ArgumentParser) -> None:
parser.addoption(
"--large-scale",
type=bool,
default=False,
help="Run a larger scale integration test",
)
parser.addoption(
"--silence-s3-logs",
action="store_true",
help="Silence progress bar when fetching assets from S3",
)