-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
williamFalcon
merged 4 commits into
Lightning-AI:master
from
awaelchli:bugfix/reduce_batch
May 2, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?