|
| 1 | +from typing import Callable |
| 2 | +from abc import ABC |
| 3 | + |
| 4 | +from pytorch_lightning.callbacks import Callback |
| 5 | + |
| 6 | + |
| 7 | +class TrainerCallbackHookMixin(ABC): |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + # this is just a summary on variables used in this abstract class, |
| 11 | + # the proper values/initialisation should be done in child class |
| 12 | + self.callbacks: list[Callback] = [] |
| 13 | + self.get_model: Callable = ... |
| 14 | + |
| 15 | + def on_init_start(self): |
| 16 | + """Called when the trainer initialization begins.""" |
| 17 | + for callback in self.callbacks: |
| 18 | + callback.on_init_start(self, None) |
| 19 | + |
| 20 | + def on_init_end(self): |
| 21 | + """Called when the trainer initialization ends.""" |
| 22 | + for callback in self.callbacks: |
| 23 | + callback.on_init_end(self, self.get_model()) |
| 24 | + |
| 25 | + def on_fit_start(self): |
| 26 | + """Called when the fit begins.""" |
| 27 | + for callback in self.callbacks: |
| 28 | + callback.on_fit_start(self, self.get_model()) |
| 29 | + |
| 30 | + def on_fit_end(self): |
| 31 | + """Called when the fit ends.""" |
| 32 | + for callback in self.callbacks: |
| 33 | + callback.on_fit_end(self, self.get_model()) |
| 34 | + |
| 35 | + def on_epoch_start(self): |
| 36 | + """Called when the epoch begins.""" |
| 37 | + for callback in self.callbacks: |
| 38 | + callback.on_epoch_start(self, self.get_model()) |
| 39 | + |
| 40 | + def on_epoch_end(self): |
| 41 | + """Called when the epoch ends.""" |
| 42 | + for callback in self.callbacks: |
| 43 | + callback.on_epoch_end(self, self.get_model()) |
| 44 | + |
| 45 | + def on_train_start(self): |
| 46 | + """Called when the train begins.""" |
| 47 | + for callback in self.callbacks: |
| 48 | + callback.on_train_start(self, self.get_model()) |
| 49 | + |
| 50 | + def on_train_end(self): |
| 51 | + """Called when the train ends.""" |
| 52 | + for callback in self.callbacks: |
| 53 | + callback.on_train_end(self, self.get_model()) |
| 54 | + |
| 55 | + def on_batch_start(self): |
| 56 | + """Called when the training batch begins.""" |
| 57 | + for callback in self.callbacks: |
| 58 | + callback.on_batch_start(self, self.get_model()) |
| 59 | + |
| 60 | + def on_batch_end(self): |
| 61 | + """Called when the training batch ends.""" |
| 62 | + for callback in self.callbacks: |
| 63 | + callback.on_batch_end(self, self.get_model()) |
| 64 | + |
| 65 | + def on_validation_start(self): |
| 66 | + """Called when the validation loop begins.""" |
| 67 | + for callback in self.callbacks: |
| 68 | + callback.on_validation_start(self, self.get_model()) |
| 69 | + |
| 70 | + def on_validation_end(self): |
| 71 | + """Called when the validation loop ends.""" |
| 72 | + for callback in self.callbacks: |
| 73 | + callback.on_validation_end(self, self.get_model()) |
| 74 | + |
| 75 | + def on_test_start(self): |
| 76 | + """Called when the test begins.""" |
| 77 | + for callback in self.callbacks: |
| 78 | + callback.on_test_start(self, self.get_model()) |
| 79 | + |
| 80 | + def on_test_end(self): |
| 81 | + """Called when the test ends.""" |
| 82 | + for callback in self.callbacks: |
| 83 | + callback.on_test_end(self, self.get_model()) |
0 commit comments