-
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
updated sync bn #2838
Merged
+163
−227
Merged
updated sync bn #2838
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b23ea28
updated sync bn
ananyahjha93 2d260a0
updated sync bn
ananyahjha93 b3523d9
updated sync bn
ananyahjha93 9dadb29
updated sync bn
ananyahjha93 5a88a61
updated sync bn
ananyahjha93 07aa2ec
updated sync bn
ananyahjha93 ed391e1
updated sync bn
ananyahjha93 66f948b
updated sync bn
ananyahjha93 984c5db
added ddp_spawn test
ananyahjha93 b01d78b
rebase
ananyahjha93 1a587a9
updated test
ananyahjha93 2c9d8b4
clean
Borda 6ace358
clean
Borda 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
added ddp_spawn test
commit 984c5db0681dad31454f29a1690edc4623162276
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import os | ||
import math | ||
import numpy as np | ||
from argparse import ArgumentParser | ||
|
||
import pytest | ||
from collections import namedtuple | ||
import tests.base.develop_utils as tutils | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import pytorch_lightning as pl | ||
from pytorch_lightning import Trainer | ||
from tests.base.datamodules import MNISTDataModule | ||
|
||
|
||
pl.seed_everything(234) | ||
FLOAT16_EPSILON = np.finfo(np.float16).eps | ||
|
||
|
||
class SyncBNModule(pl.LightningModule): | ||
def __init__(self, gpu_count=1, **kwargs): | ||
super().__init__() | ||
|
||
self.gpu_count = gpu_count | ||
self.bn_targets = None | ||
if 'bn_targets' in kwargs: | ||
self.bn_targets = kwargs['bn_targets'] | ||
|
||
self.linear = nn.Linear(28 * 28, 10) | ||
self.bn_layer = nn.BatchNorm1d(28 * 28) | ||
|
||
def forward(self, x, batch_idx): | ||
with torch.no_grad(): | ||
out_bn = self.bn_layer(x.view(x.size(0), -1)) | ||
|
||
if self.bn_targets: | ||
bn_target = self.bn_targets[batch_idx] | ||
|
||
# executes on both GPUs | ||
bn_target = bn_target[self.trainer.local_rank::self.gpu_count] | ||
bn_target = bn_target.to(out_bn.device) | ||
assert torch.sum(torch.abs(bn_target - out_bn)) < FLOAT16_EPSILON | ||
|
||
out = self.linear(out_bn) | ||
|
||
return out, out_bn | ||
|
||
def training_step(self, batch, batch_idx): | ||
x, y = batch | ||
|
||
y_hat, _ = self(x, batch_idx) | ||
loss = F.cross_entropy(y_hat, y) | ||
|
||
return pl.TrainResult(loss) | ||
|
||
def configure_optimizers(self): | ||
return torch.optim.Adam(self.linear.parameters(), lr=0.02) | ||
|
||
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") | ||
def test_sync_batchnorm_ddp(tmpdir): | ||
tutils.set_random_master_port() | ||
|
||
parent_parser = ArgumentParser(add_help=False) | ||
|
||
# define datamodule and dataloader | ||
dm = MNISTDataModule() | ||
dm.prepare_data() | ||
dm.setup(stage=None) | ||
|
||
train_dataloader = dm.train_dataloader() | ||
model = SyncBNModule() | ||
|
||
bn_outputs = [] | ||
|
||
# shuffle is false by default | ||
for batch_idx, batch in enumerate(train_dataloader): | ||
x, _ = batch | ||
|
||
_, out_bn = model.forward(x, batch_idx) | ||
bn_outputs.append(out_bn) | ||
|
||
# get 3 steps | ||
if batch_idx == 2: | ||
break | ||
|
||
bn_outputs = [x.cuda() for x in bn_outputs] | ||
|
||
# reset datamodule | ||
# batch-size = 16 because 2 GPUs in DDP | ||
dm = MNISTDataModule(batch_size=16, dist_sampler=True) | ||
dm.prepare_data() | ||
dm.setup(stage=None) | ||
|
||
model = SyncBNModule(gpu_count=2, bn_targets=bn_outputs) | ||
|
||
trainer = Trainer( | ||
gpus=2, | ||
num_nodes=1, | ||
distributed_backend='ddp_spawn', | ||
max_epochs=1, | ||
max_steps=3, | ||
sync_batchnorm=True, | ||
num_sanity_val_steps=0, | ||
replace_sampler_ddp=False, | ||
) | ||
|
||
result = trainer.fit(model, dm) | ||
assert result == 1, "Sync batchnorm failing with DDP" |
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.
I thought the Trainer / LightningModule will call these automatically? @nateraw