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

fix dp reduction test #6404

Merged
merged 4 commits into from
Mar 8, 2021
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
6 changes: 3 additions & 3 deletions pytorch_lightning/plugins/training_type/dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def reduce(self, tensor, *args, **kwargs):

else:

def _reduce(tensor: torch.Tensor):
dtype_tensor = tensor.dtype
return tensor.float().mean().type(dtype_tensor)
def _reduce(t: torch.Tensor):
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
dtype_tensor = t.dtype
return t.float().mean().type(dtype_tensor)

tensor = apply_to_collection(tensor, torch.Tensor, _reduce)

Expand Down
54 changes: 47 additions & 7 deletions tests/accelerators/test_dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
# limitations under the License.
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader

import pytorch_lightning as pl
import tests.helpers.pipelines as tpipes
import tests.helpers.utils as tutils
from pytorch_lightning.callbacks import EarlyStopping
from pytorch_lightning.core import memory
from tests.helpers import BoringModel
from tests.helpers import BoringModel, RandomDataset
from tests.helpers.datamodules import ClassifDataModule
from tests.helpers.runif import RunIf
from tests.helpers.simple_models import ClassificationModel
Expand Down Expand Up @@ -127,17 +128,56 @@ def test_dp_test(tmpdir):

@RunIf(min_gpus=2)
def test_dp_training_step_dict(tmpdir):
"""
This test verify dp properly reduce dictionaries
"""
""" This test verifies that dp properly reduces dictionaries """

model = BoringModel()
class TestModel(BoringModel):

def train_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=2)

def val_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=2)

def test_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=2)
awaelchli marked this conversation as resolved.
Show resolved Hide resolved

def add_outputs(self, output, device):
output.update({
"reduce_int": torch.tensor(device.index, dtype=torch.int, device=device),
"reduce_float": torch.tensor(device.index, dtype=torch.float, device=device),
})

def training_step(self, batch, batch_idx):
output = super().training_step(batch, batch_idx)
self.add_outputs(output, batch.device)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw, using batch.device here because self.device seems to be broken for DP

Copy link
Member

Choose a reason for hiding this comment

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

yes it is, since only a copy of the model is moved in DP. Maybe we can still set the device to the root device in the plugin/wrapper?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, already looking into that. I think it should be possible.

return output

def validation_step(self, batch, batch_idx):
output = super().validation_step(batch, batch_idx)
self.add_outputs(output, batch.device)
return output

def test_step(self, batch, batch_idx):
output = super().test_step(batch, batch_idx)
self.add_outputs(output, batch.device)
return output

def training_epoch_end(self, outputs):
assert outputs[0]["loss"].shape == torch.Size([])
assert outputs[0]["reduce_int"].item() == 0 # mean([0, 1]) = 0
assert outputs[0]["reduce_float"].item() == 0.5 # mean([0., 1.]) = 0.5

model = TestModel()
model.training_step_end = None
model.validation_step_end = None
model.test_step_end = None

trainer = pl.Trainer(
default_root_dir=tmpdir,
max_epochs=1,
limit_train_batches=2,
limit_val_batches=0,
limit_train_batches=1,
limit_val_batches=1,
limit_test_batches=1,
gpus=2,
accelerator='dp',
)
Expand Down