2020-08-20 02:03:22 +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-02-18 14:59:54 +00:00
|
|
|
from abc import ABC
|
2021-03-08 14:27:07 +00:00
|
|
|
from typing import Optional
|
2019-12-04 15:57:32 +00:00
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-06-21 16:51:53 +00:00
|
|
|
from pytorch_lightning.utilities import rank_zero_deprecation
|
2021-05-07 20:19:36 +00:00
|
|
|
from pytorch_lightning.utilities.signature_utils import is_param_in_hook_signature
|
2019-10-22 01:16:51 +00:00
|
|
|
|
|
|
|
|
2019-12-04 15:57:32 +00:00
|
|
|
class TrainerModelHooksMixin(ABC):
|
2021-05-07 20:19:36 +00:00
|
|
|
"""
|
|
|
|
TODO: Remove this class in v1.6.
|
|
|
|
|
|
|
|
Use the utilities from ``pytorch_lightning.utilities.signature_utils`` instead.
|
|
|
|
"""
|
2021-02-03 09:25:42 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
lightning_module: "pl.LightningModule"
|
2021-02-18 14:59:54 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def is_function_implemented(self, f_name: str, model: Optional["pl.LightningModule"] = None) -> bool:
|
2021-05-07 20:19:36 +00:00
|
|
|
rank_zero_deprecation(
|
|
|
|
"Internal: TrainerModelHooksMixin.is_function_implemented is deprecated in v1.4"
|
|
|
|
" and will be removed in v1.6."
|
|
|
|
)
|
2021-03-08 14:27:07 +00:00
|
|
|
# note: currently unused - kept as it is public
|
2020-06-19 01:45:09 +00:00
|
|
|
if model is None:
|
2021-02-18 14:59:54 +00:00
|
|
|
model = self.lightning_module
|
2019-10-22 01:16:51 +00:00
|
|
|
f_op = getattr(model, f_name, None)
|
|
|
|
return callable(f_op)
|
|
|
|
|
2021-03-08 14:27:07 +00:00
|
|
|
def has_arg(self, f_name: str, arg_name: str) -> bool:
|
2021-05-07 20:19:36 +00:00
|
|
|
rank_zero_deprecation(
|
|
|
|
"Internal: TrainerModelHooksMixin.is_function_implemented is deprecated in v1.4"
|
|
|
|
" and will be removed in v1.6."
|
|
|
|
" Use `pytorch_lightning.utilities.signature_utils.is_param_in_hook_signature` instead."
|
|
|
|
)
|
2021-02-18 14:59:54 +00:00
|
|
|
model = self.lightning_module
|
2019-12-04 12:32:47 +00:00
|
|
|
f_op = getattr(model, f_name, None)
|
2021-05-07 20:19:36 +00:00
|
|
|
if not f_op:
|
|
|
|
return False
|
|
|
|
return is_param_in_hook_signature(f_op, arg_name)
|