Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto convert to contiguous format for all_gather #4907

Merged
merged 9 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Fixed

- Auto convert tensors to contiguous format when `gather_all` ([#4907](https://github.com/PyTorchLightning/pytorch-lightning/pull/4907))


## [1.0.8] - 2020-11-24
Expand Down
6 changes: 6 additions & 0 deletions pytorch_lightning/utilities/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def gather_all_tensors(result: Union[torch.Tensor], group: Optional[Any] = None)
if group is None:
group = torch.distributed.group.WORLD

if not result.is_contiguous():
rank_zero_warn('Syncing with `gather_all` requires input to be contiguous'
' memory allocated. Will convert to contiguous format.',
UserWarning)
result = result.contiguous()
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved

world_size = torch.distributed.get_world_size(group)

gathered_result = [torch.zeros_like(result) for _ in range(world_size)]
Expand Down
32 changes: 32 additions & 0 deletions tests/metrics/test_ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import torch

from pytorch_lightning.metrics import Metric
from tests.metrics.test_metric import Dummy
from tests.metrics.utils import setup_ddp

Expand Down Expand Up @@ -43,3 +44,34 @@ def _test_ddp_sum_cat(rank, worldsize):
@pytest.mark.parametrize("process", [_test_ddp_cat, _test_ddp_sum, _test_ddp_sum_cat])
def test_ddp(process):
torch.multiprocessing.spawn(process, args=(2,), nprocs=2)


def _test_memory_warning(rank, worldsize):
setup_ddp(rank, worldsize)

class DummyMetric(Metric):
def __init__(self):
super().__init__()
self.add_state("x", default=[], dist_reduce_fx=None)

def update(self, x):
self.x.append(x)

def compute(self):
x = torch.cat(self.x, dim=0)
return x.sum()

metric = DummyMetric()
metric.update(torch.randn(10, 5)[:, 0])

with pytest.warns(
UserWarning,
match="Syncing with `gather_all` requires input to be contiguous"
" memory allocated. Will convert to contiguous format.",
):
Borda marked this conversation as resolved.
Show resolved Hide resolved
val = metric.compute()


@pytest.mark.skipif(sys.platform == "win32", reason="DDP not available on windows")
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved
def test_not_contiguous_memory_warning():
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved
torch.multiprocessing.spawn(_test_memory_warning, args=(2,), nprocs=2)