2021-02-26 10:09:08 +00:00
|
|
|
# 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.
|
2021-04-14 20:53:21 +00:00
|
|
|
from typing import Any, Callable, Union
|
2021-02-17 20:23:42 +00:00
|
|
|
|
|
|
|
import torch
|
2021-04-14 20:53:21 +00:00
|
|
|
from torch.optim import Optimizer
|
2021-02-17 20:23:42 +00:00
|
|
|
|
2021-04-14 20:53:21 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-02-17 20:23:42 +00:00
|
|
|
from pytorch_lightning.plugins.precision.precision_plugin import PrecisionPlugin
|
2021-04-06 13:27:37 +00:00
|
|
|
from pytorch_lightning.utilities import GradClipAlgorithmType
|
2021-02-17 20:23:42 +00:00
|
|
|
from pytorch_lightning.utilities.model_helpers import is_overridden
|
|
|
|
from pytorch_lightning.utilities.warnings import WarningCache
|
|
|
|
|
|
|
|
warning_cache = WarningCache()
|
|
|
|
|
|
|
|
|
|
|
|
class DeepSpeedPrecisionPlugin(PrecisionPlugin):
|
2021-04-14 20:53:21 +00:00
|
|
|
""" Precision plugin for DeepSpeed integration. """
|
2021-02-17 20:23:42 +00:00
|
|
|
|
2021-02-26 13:27:16 +00:00
|
|
|
def __init__(self, precision: int) -> None:
|
2021-02-17 20:23:42 +00:00
|
|
|
super().__init__()
|
|
|
|
self.precision = precision
|
|
|
|
|
|
|
|
def pre_optimizer_step(
|
2021-02-26 13:27:16 +00:00
|
|
|
self,
|
2021-04-14 20:53:21 +00:00
|
|
|
pl_module: 'pl.LightningModule',
|
|
|
|
optimizer: Optimizer,
|
2021-02-26 13:27:16 +00:00
|
|
|
optimizer_idx: int,
|
|
|
|
lambda_closure: Callable,
|
|
|
|
**kwargs: Any,
|
2021-02-17 20:23:42 +00:00
|
|
|
) -> bool:
|
|
|
|
deepspeed_engine = pl_module.trainer.model
|
|
|
|
# DeepSpeed not support closures.
|
|
|
|
lambda_closure()
|
|
|
|
|
|
|
|
if not pl_module.automatic_optimization:
|
|
|
|
pl_module.trainer.call_hook("on_after_backward")
|
|
|
|
|
|
|
|
deepspeed_engine.step()
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def backward(
|
|
|
|
self,
|
2021-04-14 20:53:21 +00:00
|
|
|
model: 'pl.LightningModule',
|
2021-02-17 20:23:42 +00:00
|
|
|
closure_loss: torch.Tensor,
|
2021-04-14 20:53:21 +00:00
|
|
|
optimizer: Optimizer,
|
2021-02-17 20:23:42 +00:00
|
|
|
opt_idx: int,
|
|
|
|
should_accumulate: bool,
|
2021-02-26 13:27:16 +00:00
|
|
|
*args: Any,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> torch.Tensor:
|
|
|
|
if is_overridden('backward', model):
|
2021-02-17 20:23:42 +00:00
|
|
|
warning_cache.warn(
|
|
|
|
"Overridden backward hook in the LightningModule will be ignored since DeepSpeed handles"
|
|
|
|
"backward logic outside of the LightningModule"
|
|
|
|
)
|
|
|
|
# todo: hack around for deepspeed engine to call backward
|
2021-02-26 13:27:16 +00:00
|
|
|
deepspeed_engine = model.trainer.model
|
|
|
|
deepspeed_engine.backward(closure_loss, *args, **kwargs)
|
2021-02-17 20:23:42 +00:00
|
|
|
# once backward has been applied, release graph
|
|
|
|
closure_loss = closure_loss.detach()
|
|
|
|
|
|
|
|
return closure_loss
|
|
|
|
|
2021-03-31 11:36:48 +00:00
|
|
|
def clip_gradients(
|
|
|
|
self,
|
2021-04-14 20:53:21 +00:00
|
|
|
model: 'pl.LightningModule',
|
|
|
|
optimizer: Optimizer,
|
2021-03-31 11:36:48 +00:00
|
|
|
clip_val: Union[int, float],
|
2021-04-06 13:27:37 +00:00
|
|
|
gradient_clip_algorithm: GradClipAlgorithmType = GradClipAlgorithmType.NORM,
|
2021-03-31 11:36:48 +00:00
|
|
|
) -> None:
|
2021-02-17 20:23:42 +00:00
|
|
|
"""
|
|
|
|
DeepSpeed handles clipping gradients via the training type plugin.
|
|
|
|
"""
|
|
|
|
pass
|