Skip to content

Commit e1f5eac

Browse files
authored
fix dp reduction test (#6404)
* fix * update * fix * move the class outside
1 parent efd272a commit e1f5eac

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

pytorch_lightning/plugins/training_type/dp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def reduce(self, tensor, *args, **kwargs):
4949

5050
else:
5151

52-
def _reduce(tensor: torch.Tensor):
53-
dtype_tensor = tensor.dtype
54-
return tensor.float().mean().type(dtype_tensor)
52+
def _reduce(t: torch.Tensor):
53+
dtype_tensor = t.dtype
54+
return t.float().mean().type(dtype_tensor)
5555

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

tests/accelerators/test_dp.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
# limitations under the License.
1414
import torch
1515
import torch.nn.functional as F
16+
from torch.utils.data import DataLoader
1617

1718
import pytorch_lightning as pl
1819
import tests.helpers.pipelines as tpipes
1920
import tests.helpers.utils as tutils
2021
from pytorch_lightning.callbacks import EarlyStopping
2122
from pytorch_lightning.core import memory
22-
from tests.helpers import BoringModel
23+
from tests.helpers import BoringModel, RandomDataset
2324
from tests.helpers.datamodules import ClassifDataModule
2425
from tests.helpers.runif import RunIf
2526
from tests.helpers.simple_models import ClassificationModel
@@ -125,19 +126,58 @@ def test_dp_test(tmpdir):
125126
assert torch.all(torch.eq(old_weights, new_weights))
126127

127128

129+
class ReductionTestModel(BoringModel):
130+
131+
def train_dataloader(self):
132+
return DataLoader(RandomDataset(32, 64), batch_size=2)
133+
134+
def val_dataloader(self):
135+
return DataLoader(RandomDataset(32, 64), batch_size=2)
136+
137+
def test_dataloader(self):
138+
return DataLoader(RandomDataset(32, 64), batch_size=2)
139+
140+
def add_outputs(self, output, device):
141+
output.update({
142+
"reduce_int": torch.tensor(device.index, dtype=torch.int, device=device),
143+
"reduce_float": torch.tensor(device.index, dtype=torch.float, device=device),
144+
})
145+
146+
def training_step(self, batch, batch_idx):
147+
output = super().training_step(batch, batch_idx)
148+
self.add_outputs(output, batch.device)
149+
return output
150+
151+
def validation_step(self, batch, batch_idx):
152+
output = super().validation_step(batch, batch_idx)
153+
self.add_outputs(output, batch.device)
154+
return output
155+
156+
def test_step(self, batch, batch_idx):
157+
output = super().test_step(batch, batch_idx)
158+
self.add_outputs(output, batch.device)
159+
return output
160+
161+
def training_epoch_end(self, outputs):
162+
assert outputs[0]["loss"].shape == torch.Size([])
163+
assert outputs[0]["reduce_int"].item() == 0 # mean([0, 1]) = 0
164+
assert outputs[0]["reduce_float"].item() == 0.5 # mean([0., 1.]) = 0.5
165+
166+
128167
@RunIf(min_gpus=2)
129168
def test_dp_training_step_dict(tmpdir):
130-
"""
131-
This test verify dp properly reduce dictionaries
132-
"""
133-
134-
model = BoringModel()
169+
""" This test verifies that dp properly reduces dictionaries """
170+
model = ReductionTestModel()
135171
model.training_step_end = None
172+
model.validation_step_end = None
173+
model.test_step_end = None
174+
136175
trainer = pl.Trainer(
137176
default_root_dir=tmpdir,
138177
max_epochs=1,
139-
limit_train_batches=2,
140-
limit_val_batches=0,
178+
limit_train_batches=1,
179+
limit_val_batches=1,
180+
limit_test_batches=1,
141181
gpus=2,
142182
accelerator='dp',
143183
)

0 commit comments

Comments
 (0)