2020-07-25 18:38:51 +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.
|
|
|
|
|
2020-08-07 22:33:51 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2019-12-04 15:57:32 +00:00
|
|
|
|
2019-10-22 01:16:51 +00:00
|
|
|
import torch
|
2020-03-19 13:24:45 +00:00
|
|
|
from torch import Tensor
|
2020-01-20 19:50:31 +00:00
|
|
|
|
2020-03-17 22:44:00 +00:00
|
|
|
from pytorch_lightning import _logger as log
|
2019-10-22 01:16:51 +00:00
|
|
|
from pytorch_lightning.callbacks import GradientAccumulationScheduler
|
2020-08-07 22:33:51 +00:00
|
|
|
from pytorch_lightning.core.lightning import LightningModule
|
2019-10-22 01:16:51 +00:00
|
|
|
|
2020-08-05 17:42:21 +00:00
|
|
|
try:
|
|
|
|
from apex import amp
|
|
|
|
except ImportError:
|
2020-08-08 09:07:32 +00:00
|
|
|
amp = None
|
2020-08-05 17:42:21 +00:00
|
|
|
|
2020-02-27 20:46:47 +00:00
|
|
|
EPSILON = 1e-6
|
|
|
|
EPSILON_FP16 = 1e-5
|
|
|
|
|
2019-10-22 01:16:51 +00:00
|
|
|
|
2019-12-04 15:57:32 +00:00
|
|
|
class TrainerTrainingTricksMixin(ABC):
|
|
|
|
|
2020-02-27 21:21:14 +00:00
|
|
|
# this is just a summary on variables used in this abstract class,
|
|
|
|
# the proper values/initialisation should be done in child class
|
2020-05-24 22:59:08 +00:00
|
|
|
default_root_dir: str
|
|
|
|
progress_bar_callback: ...
|
2020-05-09 12:28:36 +00:00
|
|
|
on_gpu: bool
|
2019-12-04 15:57:32 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
2020-06-12 15:23:18 +00:00
|
|
|
def get_model(self) -> LightningModule:
|
2020-02-27 21:21:14 +00:00
|
|
|
"""Warning: this is just empty shell for code implemented in other class."""
|
2019-10-22 01:16:51 +00:00
|
|
|
|
2020-03-19 13:24:45 +00:00
|
|
|
def print_nan_gradients(self) -> None:
|
2019-10-22 01:16:51 +00:00
|
|
|
model = self.get_model()
|
|
|
|
for param in model.parameters():
|
2019-12-04 12:04:58 +00:00
|
|
|
if (param.grad is not None) and torch.isnan(param.grad.float()).any():
|
2020-02-01 20:47:58 +00:00
|
|
|
log.info(param, param.grad)
|
2019-10-22 01:16:51 +00:00
|
|
|
|
2020-03-19 13:24:45 +00:00
|
|
|
def detect_nan_tensors(self, loss: Tensor) -> None:
|
|
|
|
model = self.get_model()
|
|
|
|
|
|
|
|
# check if loss is nan
|
|
|
|
if not torch.isfinite(loss).all():
|
|
|
|
raise ValueError(
|
|
|
|
'The loss returned in `training_step` is nan or inf.'
|
|
|
|
)
|
|
|
|
# check if a network weight is nan
|
|
|
|
for name, param in model.named_parameters():
|
|
|
|
if not torch.isfinite(param).all():
|
|
|
|
self.print_nan_gradients()
|
|
|
|
raise ValueError(
|
|
|
|
f'Detected nan and/or inf values in `{name}`.'
|
|
|
|
' Check your forward pass for numerically unstable operations.'
|
|
|
|
)
|