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

Sharded Plugin 3/n: Expose step input to DDP plugin #4686

Merged
merged 13 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 12 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
18 changes: 10 additions & 8 deletions pytorch_lightning/accelerators/ddp2_accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,23 @@ def train(self):
return self.ddp_train(process_idx=self.task_idx, mp_queue=None, model=model)

def training_step(self, args):
return self._step(args)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice. this is better haha.


def validation_step(self, args):
return self._step(args)
Borda marked this conversation as resolved.
Show resolved Hide resolved

def test_step(self, args):
return self._step(args)

def _step(self, args):
args = self.ddp_plugin.on_before_forward(self.trainer.get_model(), *args)
if self.trainer.amp_backend == AMPType.NATIVE:
with torch.cuda.amp.autocast():
output = self.trainer.model(*args)
else:
output = self.trainer.model(*args)
return output

def validation_step(self, args):
output = self.training_step(args)
return output

def test_step(self, args):
output = self.training_step(args)
return output

def barrier(self, name: Optional[str] = None):
if torch_distrib.is_initialized():
torch_distrib.barrier()
Expand Down
18 changes: 10 additions & 8 deletions pytorch_lightning/accelerators/ddp_accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,23 @@ def train(self):
return results

def training_step(self, args):
return self._step(args)

def validation_step(self, args):
return self._step(args)

def test_step(self, args):
return self._step(args)

def _step(self, args):
args = self.ddp_plugin.on_before_forward(self.trainer.get_model(), *args)
Copy link
Contributor

Choose a reason for hiding this comment

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

love it

if self.trainer.amp_backend == AMPType.NATIVE:
with torch.cuda.amp.autocast():
output = self.trainer.model(*args)
else:
output = self.trainer.model(*args)
return output

def validation_step(self, args):
output = self.training_step(args)
return output

def test_step(self, args):
output = self.training_step(args)
return output

def barrier(self, name: Optional[str] = None):
if torch_distrib.is_initialized():
torch_distrib.barrier()
Expand Down
18 changes: 10 additions & 8 deletions pytorch_lightning/accelerators/ddp_cpu_spawn_accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,23 @@ def ddp_train(self, process_idx, mp_queue, model):
torch.cuda.empty_cache()

def training_step(self, args):
return self._step(args)

def validation_step(self, args):
return self._step(args)

def test_step(self, args):
return self._step(args)

def _step(self, args):
args = self.ddp_plugin.on_before_forward(self.trainer.get_model(), *args)
if self.trainer.amp_backend == AMPType.NATIVE:
with torch.cuda.amp.autocast():
output = self.trainer.model(*args)
else:
output = self.trainer.model(*args)
return output

def validation_step(self, args):
output = self.training_step(args)
return output

def test_step(self, args):
output = self.training_step(args)
return output

def barrier(self, name: Optional[str] = None):
if torch_distrib.is_initialized():
torch_distrib.barrier()
Expand Down
18 changes: 10 additions & 8 deletions pytorch_lightning/accelerators/ddp_hpc_accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,23 @@ def get_device_ids(self):
return device_ids

def training_step(self, args):
return self._step(args)

def validation_step(self, args):
return self._step(args)

def test_step(self, args):
return self._step(args)

def _step(self, args):
args = self.ddp_plugin.on_before_forward(self.trainer.get_model(), *args)
if self.trainer.amp_backend == AMPType.NATIVE:
with torch.cuda.amp.autocast():
output = self.trainer.model(*args)
else:
output = self.trainer.model(*args)
return output

def validation_step(self, args):
output = self.training_step(args)
return output

def test_step(self, args):
output = self.training_step(args)
return output

def barrier(self, name: Optional[str] = None):
if torch_distrib.is_initialized():
torch_distrib.barrier()
Expand Down
18 changes: 10 additions & 8 deletions pytorch_lightning/accelerators/ddp_spawn_accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,23 @@ def get_device_ids(self):
return device_ids

def training_step(self, args):
return self._step(args)

def validation_step(self, args):
return self._step(args)

def test_step(self, args):
return self._step(args)

def _step(self, args):
args = self.ddp_plugin.on_before_forward(self.trainer.get_model(), *args)
if self.trainer.amp_backend == AMPType.NATIVE:
with torch.cuda.amp.autocast():
output = self.trainer.model(*args)
else:
output = self.trainer.model(*args)
return output

def validation_step(self, args):
output = self.training_step(args)
return output

def test_step(self, args):
output = self.training_step(args)
return output

def barrier(self, name: Optional[str] = None):
if torch_distrib.is_initialized():
torch_distrib.barrier()
Expand Down
18 changes: 18 additions & 0 deletions pytorch_lightning/plugins/ddp_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,21 @@ def configure_ddp(self, model, device_ids):
**self._ddp_kwargs,
)
return model

def on_before_forward(self, model: LightningModule, *args):
"""
Override to handle custom input to device logic. For DDP, no logic is required as this is handled internally
within the DDP wrapper.

Example::

def on_before_forward(self, model, *args):
batch, batch_idx = args
return batch.to(model.device)

Args:
args: Inputs to the model.
model: Model to train.
Returns: args moved to correct device if needed.
SeanNaren marked this conversation as resolved.
Show resolved Hide resolved
"""
return args