2019-12-04 12:32:47 +00:00
|
|
|
import inspect
|
2019-12-04 15:57:32 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
from pytorch_lightning.core.lightning import LightningModule
|
2019-10-22 01:16:51 +00:00
|
|
|
|
|
|
|
|
2019-12-04 15:57:32 +00:00
|
|
|
class TrainerModelHooksMixin(ABC):
|
2019-10-22 01:16:51 +00:00
|
|
|
|
|
|
|
def is_function_implemented(self, f_name):
|
|
|
|
model = self.get_model()
|
|
|
|
f_op = getattr(model, f_name, None)
|
|
|
|
return callable(f_op)
|
|
|
|
|
|
|
|
def is_overriden(self, f_name):
|
|
|
|
model = self.get_model()
|
|
|
|
super_object = LightningModule
|
|
|
|
|
|
|
|
# when code pointers are different, it was overriden
|
|
|
|
is_overriden = getattr(model, f_name).__code__ is not getattr(super_object, f_name).__code__
|
|
|
|
return is_overriden
|
2019-12-04 12:32:47 +00:00
|
|
|
|
|
|
|
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
|
2019-12-04 15:57:32 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_model(self):
|
|
|
|
# this is just empty shell for code from other class
|
|
|
|
pass
|