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

[WIP] Reduction when batch size < num gpus #1609

Merged
merged 4 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added callback for logging learning rates ([#1498](https://github.com/PyTorchLightning/pytorch-lightning/pull/1498))

### Changed

- Reduction when `batch_size < num_gpus` ([#1609](https://github.com/PyTorchLightning/pytorch-lightning/pull/1609))

### Deprecated

Expand Down
8 changes: 4 additions & 4 deletions pytorch_lightning/trainer/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def reduce_distributed_output(self, output, num_gpus):
elif isinstance(output[k], torch.Tensor) and output[k].dim() == 0:
pass

# reduce only metrics that have the same number of gpus
elif output[k].size(0) == num_gpus:
reduced = torch.mean(output[k])
output[k] = reduced
# do not reduce metrics that have batch size > num gpus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

batch size has nothing to do with dp.... why is this fix even needed?

size(0) should be the number of GPUs in DP... NOT batch size

Copy link
Contributor Author

@awaelchli awaelchli May 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simple example: batch_size = 2, num_gpus = 3. Lightning will forward the batch with only 2 gpus, so the number of outputs is 2, so size(0) = 2. Therefore Lightning will not reduce the output and we get a problem later when the progress bar metrics call .item on that tensor.

Is my explanation correct or not?

elif output[k].size(0) <= num_gpus:
output[k] = torch.mean(output[k])

return output
45 changes: 45 additions & 0 deletions tests/trainer/test_dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import pytest
import torch
from torch.utils.data.dataloader import DataLoader
from torch.utils.data.dataset import Subset

import tests.base.utils as tutils
from pytorch_lightning import Trainer
Expand Down Expand Up @@ -494,3 +496,46 @@ class CustomDummyObj:
assert isinstance(result, torch.utils.data.DataLoader)
assert isinstance(result, CustomDataLoader)
assert hasattr(result, 'dummy_kwarg')


@pytest.mark.skipif(torch.cuda.device_count() < 3, reason='Test requires multiple GPUs')
def test_batch_size_smaller_than_num_gpus():
# we need at least 3 gpus for this test
num_gpus = 3
batch_size = 3

class CurrentTestModel(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@awaelchli @Borda this needs the new test syntax not mixins...

LightTrainDataloader,
TestModelBase,
):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.c_d1_bn = torch.nn.ReLU()

def train_dataloader(self):
dataloader = super().train_dataloader()
# construct a dataset with a size that is not divisible by num_gpus
# therefore the last batch will have a size < num_gpus
size = num_gpus * batch_size + (num_gpus - 1)
dataset = Subset(dataloader.dataset, range(size))
dataloader = DataLoader(
dataset,
batch_size=self.hparams.batch_size,
drop_last=False,
)
return dataloader

hparams = tutils.get_default_hparams()
hparams.batch_size = batch_size
model = CurrentTestModel(hparams)

trainer = Trainer(
max_epochs=1,
gpus=num_gpus,
)

# we expect the reduction for the metrics also to happen on the last batch
# where we will get fewer metrics than gpus
result = trainer.fit(model)
assert 1 == result