Skip to content

Commit 01f2c4b

Browse files
SkafteNickiBordapre-commit-ci[bot]
authored
Description on how to run tests & build docs (#2500)
* readme tests * add note on make * add pool skipper * update Make docs * Apply suggestions from code review --------- Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3d52192 commit 01f2c4b

File tree

8 files changed

+58
-26
lines changed

8 files changed

+58
-26
lines changed

.github/workflows/docs-build.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,12 @@ jobs:
5656
sudo apt-get install -y \
5757
texlive-latex-extra texlive-pictures texlive-fonts-recommended dvipng cm-super
5858
59-
- name: Pull sphinx template
60-
run: |
61-
pip install awscli
62-
aws s3 sync --no-sign-request s3://sphinx-packages/ dist/
63-
pip install lai-sphinx-theme -U -f dist/
6459
- name: Install package & dependencies
6560
run: |
66-
pip --version
61+
make get-sphinx-template
6762
pip install . -U -r requirements/_docs.txt \
68-
--find-links="dist/" --find-links="${PYPI_CACHE}" --find-links="${TORCH_URL}"
69-
pip list
70-
63+
--find-links="${PYPI_CACHE}" --find-links="${TORCH_URL}"
64+
- run: pip list
7165
- name: Full build for deployment
7266
if: github.event_name != 'pull_request'
7367
run: echo "SPHINX_FETCH_ASSETS=1" >> $GITHUB_ENV

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: clean test pull-template docs env data
1+
.PHONY: clean test get-sphinx-template docs env data
22

33
export FREEZE_REQUIREMENTS=1
44
# assume you have installed need packages
@@ -28,12 +28,13 @@ test: clean env data
2828
cd tests && python -m pytest unittests -v --cov=torchmetrics
2929
cd tests && python -m coverage report
3030

31-
pull-template:
31+
get-sphinx-template:
3232
pip install -q awscli
3333
aws s3 sync --no-sign-request s3://sphinx-packages/ dist/
34+
pip install lai-sphinx-theme -q -U -f dist/
3435

35-
docs: clean pull-template
36-
pip install -e . --quiet -r requirements/_docs.txt -f dist/
36+
docs: clean get-sphinx-template
37+
pip install -e . --quiet -r requirements/_docs.txt
3738
# apt-get install -y texlive-latex-extra dvipng texlive-pictures texlive-fonts-recommended cm-super
3839
cd docs && make html --debug --jobs $(nproc) SPHINXOPTS="-W --keep-going"
3940

tests/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Running tests locally
2+
3+
To run the tests locally, you need to have the full development environment set up. This can be setup by running
4+
the following command in the root directory of the project:
5+
6+
```bash
7+
pip install . -r requirements/_devel.txt
8+
```
9+
10+
Then for windows users, to execute the tests (unit tests and integration tests) run the following command (will only run non-DDP tests):
11+
12+
```bash
13+
pytest tests/
14+
```
15+
16+
For linux/Mac users you will need to provide the `-m` argument to indicate if `ddp` tests should also be executed:
17+
18+
```bash
19+
pytest -m DDP tests/ # to run only DDP tests
20+
pytest -m "not DDP" tests/ # to run all tests except DDP tests
21+
```
22+
23+
## Simply Make
24+
25+
Alternatively, for Unix with `make` installed, simply running `make test` from the root of the project will install
26+
all requirements and run the full test suit.
27+
28+
## Test particular domain
29+
30+
To run only unittests, point the command only to the `tests/unittests` directory. Similarly, to only run a subset of the
31+
unittests, like all tests related to the regression domain, run the following command:
32+
33+
```bash
34+
pytest tests/unittests/regression/
35+
```

tests/unittests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
NUM_CLASSES,
1515
NUM_PROCESSES,
1616
THRESHOLD,
17+
USE_PYTEST_POOL,
1718
setup_ddp,
1819
)
1920

@@ -60,5 +61,6 @@ class _GroupInput(NamedTuple):
6061
"NUM_CLASSES",
6162
"NUM_PROCESSES",
6263
"THRESHOLD",
64+
"USE_PYTEST_POOL",
6365
"setup_ddp",
6466
]

tests/unittests/bases/test_ddp.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from torchmetrics.utilities.exceptions import TorchMetricsUserError
2525
from torchmetrics.utilities.imports import _TORCH_GREATER_EQUAL_2_1
2626

27-
from unittests import NUM_PROCESSES
27+
from unittests import NUM_PROCESSES, USE_PYTEST_POOL
2828
from unittests._helpers import seed_all
2929
from unittests._helpers.testers import DummyListMetric, DummyMetric, DummyMetricSum
3030

@@ -88,6 +88,7 @@ def _test_ddp_compositional_tensor(rank: int, worldsize: int = NUM_PROCESSES) ->
8888

8989
@pytest.mark.DDP()
9090
@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
91+
@pytest.mark.skipif(not USE_PYTEST_POOL, reason="DDP pool is not available.")
9192
@pytest.mark.parametrize(
9293
"process",
9394
[
@@ -125,6 +126,7 @@ def compute(self):
125126

126127
@pytest.mark.DDP()
127128
@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
129+
@pytest.mark.skipif(not USE_PYTEST_POOL, reason="DDP pool is not available.")
128130
def test_non_contiguous_tensors():
129131
"""Test that gather_all operation works for non-contiguous tensors."""
130132
pytest.pool.map(_test_non_contiguous_tensors, range(NUM_PROCESSES))
@@ -232,6 +234,7 @@ def reload_state_dict(state_dict, expected_x, expected_c):
232234

233235
@pytest.mark.DDP()
234236
@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
237+
@pytest.mark.skipif(not USE_PYTEST_POOL, reason="DDP pool is not available.")
235238
def test_state_dict_is_synced(tmpdir):
236239
"""Tests that metrics are synced while creating the state dict but restored after to continue accumulation."""
237240
pytest.pool.map(partial(_test_state_dict_is_synced, tmpdir=tmpdir), range(NUM_PROCESSES))
@@ -260,6 +263,7 @@ def _test_sync_on_compute_list_state(rank, sync_on_compute):
260263

261264
@pytest.mark.DDP()
262265
@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
266+
@pytest.mark.skipif(not USE_PYTEST_POOL, reason="DDP pool is not available.")
263267
@pytest.mark.parametrize("sync_on_compute", [True, False])
264268
@pytest.mark.parametrize("test_func", [_test_sync_on_compute_list_state, _test_sync_on_compute_tensor_state])
265269
def test_sync_on_compute(sync_on_compute, test_func):
@@ -276,6 +280,7 @@ def _test_sync_with_empty_lists(rank):
276280
@pytest.mark.DDP()
277281
@pytest.mark.skipif(not _TORCH_GREATER_EQUAL_2_1, reason="test only works on newer torch versions")
278282
@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
283+
@pytest.mark.skipif(not USE_PYTEST_POOL, reason="DDP pool is not available.")
279284
def test_sync_with_empty_lists():
280285
"""Test that synchronization of states can be enabled and disabled for compute."""
281286
pytest.pool.map(_test_sync_with_empty_lists, range(NUM_PROCESSES))

tests/unittests/conftest.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ def setup_ddp(rank, world_size):
6060

6161

6262
def pytest_sessionstart():
63-
"""Global initialization of multiprocessing pool.
64-
65-
Runs before any test.
66-
67-
"""
63+
"""Global initialization of multiprocessing pool; runs before any test."""
6864
if not USE_PYTEST_POOL:
6965
return
7066
pool = Pool(processes=NUM_PROCESSES)
@@ -73,11 +69,7 @@ def pytest_sessionstart():
7369

7470

7571
def pytest_sessionfinish():
76-
"""Correctly closes the global multiprocessing pool.
77-
78-
Runs after all tests.
79-
80-
"""
72+
"""Correctly closes the global multiprocessing pool; runs after all tests."""
8173
if not USE_PYTEST_POOL:
8274
return
8375
pytest.pool.close()

tests/unittests/text/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from torch import Tensor
2323
from torchmetrics import Metric
2424

25-
from unittests import NUM_PROCESSES, _reference_cachier
25+
from unittests import NUM_PROCESSES, USE_PYTEST_POOL, _reference_cachier
2626
from unittests._helpers import seed_all
2727
from unittests._helpers.testers import (
2828
MetricTester,
@@ -369,6 +369,8 @@ def run_class_metric_test(
369369
if ddp:
370370
if sys.platform == "win32":
371371
pytest.skip("DDP not supported on windows")
372+
if not USE_PYTEST_POOL:
373+
pytest.skip("DDP pool is not available.")
372374
pytest.pool.starmap(
373375
partial(_class_test, **common_kwargs, **kwargs_update),
374376
[(rank, NUM_PROCESSES) for rank in range(NUM_PROCESSES)],

tests/unittests/wrappers/test_running.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from torchmetrics.regression import MeanAbsoluteError, MeanSquaredError, PearsonCorrCoef
2323
from torchmetrics.wrappers import Running
2424

25-
from unittests import NUM_PROCESSES
25+
from unittests import NUM_PROCESSES, USE_PYTEST_POOL
2626

2727

2828
def test_errors_on_wrong_input():
@@ -145,6 +145,7 @@ def _test_ddp_running(rank, dist_sync_on_step, expected):
145145

146146
@pytest.mark.DDP()
147147
@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
148+
@pytest.mark.skipif(not USE_PYTEST_POOL, reason="DDP pool is not available.")
148149
@pytest.mark.parametrize(("dist_sync_on_step", "expected"), [(False, 1), (True, 2)])
149150
def test_ddp_running(dist_sync_on_step, expected):
150151
"""Check that the dist_sync_on_step gets correctly passed to base metric."""

0 commit comments

Comments
 (0)