Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ module = [
"pytorch_lightning.plugins.environments.lsf_environment",
"pytorch_lightning.plugins.environments.slurm_environment",
"pytorch_lightning.plugins.environments.torchelastic_environment",
"pytorch_lightning.plugins.precision.deepspeed",
"pytorch_lightning.plugins.precision.native_amp",
"pytorch_lightning.plugins.precision.precision_plugin",
"pytorch_lightning.plugins.training_type.ddp",
"pytorch_lightning.plugins.training_type.ddp2",
"pytorch_lightning.plugins.training_type.ddp_spawn",
Expand Down
4 changes: 3 additions & 1 deletion pytorch_lightning/plugins/precision/deepspeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def backward(self, model: "pl.LightningModule", closure_loss: Tensor, *args: Any
deepspeed_engine: DeepSpeedEngine = model.trainer.model
deepspeed_engine.backward(closure_loss, *args, **kwargs)

def _run_backward(self, tensor: Tensor, model: Module, *args: Any, **kwargs: Any) -> None:
def _run_backward(self, tensor: Tensor, model: Optional["DeepSpeedEngine"], *args: Any, **kwargs: Any) -> None:
if model is None:
raise ValueError("Please provide the model as input to `backward`.")
model.backward(tensor, *args, **kwargs)

def optimizer_step(
Expand Down
12 changes: 6 additions & 6 deletions pytorch_lightning/plugins/precision/native_amp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from pytorch_lightning.utilities.exceptions import MisconfigurationException

if _TORCH_GREATER_EQUAL_1_10:
from torch import autocast
from torch import autocast as new_autocast
else:
from torch.cuda.amp import autocast
from torch.cuda.amp import autocast as old_autocast


class NativeMixedPrecisionPlugin(MixedPrecisionPlugin):
Expand Down Expand Up @@ -62,7 +62,7 @@ def pre_backward(self, model: "pl.LightningModule", closure_loss: torch.Tensor)
closure_loss = self.scaler.scale(closure_loss)
return super().pre_backward(model, closure_loss)

def _run_backward(self, tensor: Tensor, model: Module, *args: Any, **kwargs: Any) -> None:
def _run_backward(self, tensor: Tensor, model: Optional[Module], *args: Any, **kwargs: Any) -> None:
if self.scaler is not None:
tensor = self.scaler.scale(tensor)
super()._run_backward(tensor, model, *args, **kwargs)
Expand Down Expand Up @@ -93,12 +93,12 @@ def optimizer_step(
self.scaler.step(optimizer, **kwargs)
self.scaler.update()

def autocast_context_manager(self) -> autocast:
def autocast_context_manager(self) -> Union["old_autocast", "new_autocast"]:
if _TORCH_GREATER_EQUAL_1_10:
# the dtype could be automatically inferred but we need to manually set it due to a bug upstream
# https://github.com/pytorch/pytorch/issues/67233
return autocast(self.device, dtype=torch.bfloat16 if self.precision == "bf16" else torch.half)
return autocast()
return new_autocast(self.device, dtype=torch.bfloat16 if self.precision == "bf16" else torch.half)
return old_autocast()

@contextmanager
def forward_context(self) -> Generator[None, None, None]:
Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/plugins/precision/precision_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def optimizer_step(
"""Hook to run the optimizer step."""
if isinstance(model, pl.LightningModule):
closure = partial(self._wrap_closure, model, optimizer, optimizer_idx, closure)
optimizer.step(closure=closure, **kwargs)
optimizer.step(closure=closure, **kwargs) # type: ignore[call-arg]

def _track_grad_norm(self, trainer: "pl.Trainer") -> None:
if trainer.track_grad_norm == -1:
Expand Down