-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
test_distributed_logging.py
59 lines (52 loc) · 1.92 KB
/
test_distributed_logging.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
import pytest
import torch
from tests.base import BoringModel
import platform
from distutils.version import LooseVersion
from pytorch_lightning import Trainer, Callback
from unittest import mock
class TestModel(BoringModel):
def on_pretrain_routine_end(self) -> None:
with mock.patch('pytorch_lightning.loggers.base.LightningLoggerBase.agg_and_log_metrics') as m:
self.trainer.logger_connector.log_metrics({'a': 2}, {})
logged_times = m.call_count
expected = 1 if self.global_rank == 0 else 0
assert logged_times == expected, 'actual logger called from non-global zero'
@pytest.mark.skipif(platform.system() == "Windows",
reason="Distributed training is not supported on Windows")
@pytest.mark.skipif((platform.system() == "Darwin" and
LooseVersion(torch.__version__) < LooseVersion("1.3.0")),
reason="Distributed training is not supported on MacOS before Torch 1.3.0")
def test_global_zero_only_logging_ddp_cpu(tmpdir):
"""
Makes sure logging only happens from root zero
"""
model = TestModel()
model.training_epoch_end = None
trainer = Trainer(
distributed_backend='ddp_cpu',
num_processes=2,
default_root_dir=tmpdir,
limit_train_batches=1,
limit_val_batches=1,
max_epochs=1,
weights_summary=None,
)
trainer.fit(model)
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
def test_global_zero_only_logging_ddp_spawn(tmpdir):
"""
Makes sure logging only happens from root zero
"""
model = TestModel()
model.training_epoch_end = None
trainer = Trainer(
distributed_backend='ddp_spawn',
gpus=2,
default_root_dir=tmpdir,
limit_train_batches=1,
limit_val_batches=1,
max_epochs=1,
weights_summary=None,
)
trainer.fit(model)