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

Accelerator Refactor: Precision Plugins #5718

Merged
merged 14 commits into from
Jan 31, 2021
Prev Previous commit
Next Next commit
add native amp
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
justusschock and awaelchli committed Jan 31, 2021
commit a287567031d2d787bcc8b8a1301c4e8193423ef2
79 changes: 79 additions & 0 deletions pytorch_lightning/plugins/precision/native_amp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from contextlib import contextmanager
from typing import Generator

import torch

from pytorch_lightning.core import LightningModule
from pytorch_lightning.plugins.precision.mixed import MixedPrecisionPlugin
from pytorch_lightning.utilities import AMPType
from pytorch_lightning.utilities.exceptions import MisconfigurationException


class NativeMixedPrecisionPlugin(MixedPrecisionPlugin):
def __init__(self):
self.backend = AMPType.NATIVE
self.scaler = torch.cuda.amp.GradScaler()

def pre_optimizer_step(self, optimizer: torch.optim.Optimizer, optimizer_idx: int) -> None:
"""always called before the optimizer step.
Checks that the optimizer is not LBFGS, as this one is not supported by native amp
"""
if isinstance(optimizer, torch.optim.LBFGS):
raise MisconfigurationException(
f"native PyTorch amp and lbfgs are not compatible (optimizer {optimizer_idx})."
" To request, please file a Github issue in PyTorch and tag @mcarilli"
)

def post_optimizer_step(self, optimizer: torch.optim.Optimizer, optimizer_idx: int) -> None:
"""Updates the GradScaler"""
self.scaler.update()

def backward(
self,
model: LightningModule,
closure_loss: torch.Tensor,
optimizer: torch.optim.Optimizer,
opt_idx: int,
should_accumulate: bool,
*args,
**kwargs,
) -> torch.Tensor:
"""performs the actual backpropagation

Args:
model: the model to be optimized
closure_loss: the loss value obtained from the closure
optimizer: the optimizer to perform the step lateron
opt_idx: the optimizer's index
should_accumulate: whether to accumulate gradients or not

"""
closure_loss = self.scaler.scale(closure_loss)

automatic_optimization = model.automatic_optimization

closure_loss = super().backward(model, closure_loss, optimizer, opt_idx, should_accumulate, *args, **kwargs)

# unscale gradient to allow analyze within `on_after_backward`
if not should_accumulate and automatic_optimization:
self.scaler.unscale_(optimizer)

return closure_loss

@contextmanager
def train_step_context(self) -> Generator[torch.cuda.amp.autocast, None, None]:
"""Enable autocast context"""
yield torch.cuda.amp.autocast()