lightning/pytorch_lightning/trainer/model_hooks.py

47 lines
1.7 KiB
Python
Raw Normal View History

import inspect
from abc import ABC, abstractmethod
from pytorch_lightning.core.lightning import LightningModule
class TrainerModelHooksMixin(ABC):
2020-06-19 01:45:09 +00:00
def is_function_implemented(self, f_name, model=None):
if model is None:
model = self.get_model()
f_op = getattr(model, f_name, None)
return callable(f_op)
2020-05-07 13:25:54 +00:00
def is_overridden(self, method_name: str, model: LightningModule = None) -> bool:
Clean up dataloader logic (#926) * added get dataloaders directly using a getter * deleted decorator * added prepare_data hook * refactored dataloader init * refactored dataloader init * added dataloader reset flag and main loop * added dataloader reset flag and main loop * added dataloader reset flag and main loop * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * made changes * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed bad loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixed error in .fit with loaders * fixes #909 * fixes #909 * bug fix * Fixes #902
2020-02-25 03:23:25 +00:00
if model is None:
model = self.get_model()
super_object = LightningModule
if not hasattr(model, method_name):
# in case of calling deprecated method
return False
instance_attr = getattr(model, method_name)
if not instance_attr:
return False
super_attr = getattr(super_object, method_name)
# when code pointers are different, it was implemented
if hasattr(instance_attr, 'patch_loader_code'):
# cannot pickle __code__ so cannot verify if PatchDataloader
# exists which shows dataloader methods have been overwritten.
# so, we hack it by using the string representation
2020-05-07 13:25:54 +00:00
is_overridden = instance_attr.patch_loader_code != str(super_attr.__code__)
else:
2020-05-07 13:25:54 +00:00
is_overridden = instance_attr.__code__ is not super_attr.__code__
return is_overridden
def has_arg(self, f_name, arg_name):
model = self.get_model()
f_op = getattr(model, f_name, None)
return arg_name in inspect.signature(f_op).parameters
@abstractmethod
def get_model(self) -> LightningModule:
"""Warning: this is just empty shell for code implemented in other class."""