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-27 11:34:02 +00:00
|
|
|
from typing import Any, Callable, Optional, Union
|
2021-02-17 20:23:42 +00:00
|
|
|
|
2021-04-15 16:48:16 +00:00
|
|
|
from torch import Tensor
|
2021-04-27 11:34:02 +00:00
|
|
|
from torch.nn import Module
|
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-09-07 11:52:20 +00:00
|
|
|
from pytorch_lightning.utilities.exceptions import MisconfigurationException
|
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-07-26 11:37:35 +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-07-26 11:37:35 +00:00
|
|
|
model: "pl.LightningModule",
|
2021-04-14 20:53:21 +00:00
|
|
|
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:
|
2021-07-09 11:30:52 +00:00
|
|
|
"""Hook to do something before each optimizer step."""
|
2021-09-07 11:52:20 +00:00
|
|
|
result = lambda_closure() # DeepSpeed does not support closures
|
2021-07-09 11:30:52 +00:00
|
|
|
super().pre_optimizer_step(model, optimizer, optimizer_idx, lambda_closure, **kwargs)
|
2021-09-07 11:52:20 +00:00
|
|
|
# in manual optimization, the closure does not return a value
|
|
|
|
if model.automatic_optimization and result is None:
|
|
|
|
raise MisconfigurationException(
|
|
|
|
"Skipping backward by returning `None` from your `training_step` is not supported by `DeepSpeed`"
|
|
|
|
)
|
2021-07-09 11:30:52 +00:00
|
|
|
# the following should be in a `optimizer_step` hook but we don't have one in the precision plugin.
|
|
|
|
deepspeed_engine = model.trainer.model
|
2021-02-17 20:23:42 +00:00
|
|
|
deepspeed_engine.step()
|
|
|
|
return False
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def backward(self, model: "pl.LightningModule", closure_loss: Tensor, *args: Any, **kwargs: Any) -> None:
|
|
|
|
if is_overridden("backward", model):
|
2021-02-17 20:23:42 +00:00
|
|
|
warning_cache.warn(
|
2021-07-05 10:50:01 +00:00
|
|
|
"You have overridden the `LightningModule.backward` hook but it will be ignored since DeepSpeed handles"
|
|
|
|
" the backward logic internally."
|
2021-02-17 20:23:42 +00:00
|
|
|
)
|
|
|
|
# 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
|
|
|
|
2021-03-31 11:36:48 +00:00
|
|
|
def clip_gradients(
|
|
|
|
self,
|
2021-04-14 20:53:21 +00:00
|
|
|
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-04-27 11:34:02 +00:00
|
|
|
model: Optional[Module] = None,
|
2021-03-31 11:36:48 +00:00
|
|
|
) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""DeepSpeed handles clipping gradients internally via the training type plugin."""
|
2021-02-17 20:23:42 +00:00
|
|
|
pass
|