2020-10-13 11:18:07 +00:00
|
|
|
# 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.
|
2020-08-06 00:42:09 +00:00
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
|
|
|
import torch.multiprocessing as mp
|
2021-01-13 06:48:37 +00:00
|
|
|
|
2021-02-08 10:52:02 +00:00
|
|
|
import tests.helpers.utils as tutils
|
2021-06-10 12:09:01 +00:00
|
|
|
from pytorch_lightning.trainer.connectors.logger_connector.result import _Sync
|
2021-06-08 20:20:17 +00:00
|
|
|
from pytorch_lightning.utilities.distributed import sync_ddp_if_available
|
2021-03-02 09:36:01 +00:00
|
|
|
from tests.helpers.runif import RunIf
|
2020-08-06 00:42:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _setup_ddp(rank, worldsize):
|
|
|
|
import os
|
|
|
|
|
|
|
|
os.environ["MASTER_ADDR"] = "localhost"
|
|
|
|
|
|
|
|
# initialize the process group
|
|
|
|
dist.init_process_group("gloo", rank=rank, world_size=worldsize)
|
|
|
|
|
|
|
|
|
2021-05-24 12:13:55 +00:00
|
|
|
def _ddp_test_fn(rank, worldsize):
|
2020-08-06 00:42:09 +00:00
|
|
|
_setup_ddp(rank, worldsize)
|
|
|
|
tensor = torch.tensor([1.0])
|
2021-11-10 04:42:27 +00:00
|
|
|
sync = _Sync(sync_ddp_if_available, _should=True, _op="SUM")
|
2021-06-08 20:20:17 +00:00
|
|
|
actual = sync(tensor)
|
2021-05-24 12:13:55 +00:00
|
|
|
assert actual.item() == dist.get_world_size(), "Result-Log does not work properly with DDP and Tensors"
|
2020-08-06 00:42:09 +00:00
|
|
|
|
|
|
|
|
2021-03-02 09:36:01 +00:00
|
|
|
@RunIf(skip_windows=True)
|
2021-03-09 11:27:15 +00:00
|
|
|
def test_result_reduce_ddp():
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Make sure result logging works with DDP."""
|
2021-10-25 12:09:05 +00:00
|
|
|
tutils.set_random_main_port()
|
2020-08-06 00:42:09 +00:00
|
|
|
worldsize = 2
|
2021-07-26 11:37:35 +00:00
|
|
|
mp.spawn(_ddp_test_fn, args=(worldsize,), nprocs=worldsize)
|