forked from Lightning-AI/pytorch-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_hook_validator.py
231 lines (185 loc) · 7.39 KB
/
callback_hook_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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.
from pytorch_lightning.utilities.exceptions import MisconfigurationException
class CallbackHookNameValidator:
@staticmethod
def check_logging_in_callbacks(
current_hook_fx_name: str = None, on_step: bool = None, on_epoch: bool = None
) -> None:
if current_hook_fx_name is None:
return
internal_func = getattr(CallbackHookNameValidator, f"_{current_hook_fx_name}_log", None)
if internal_func is None:
return
current_callback_hook_auth_args = internal_func()
if current_callback_hook_auth_args is not None:
m = "{} function supports only {} in {}. Provided {}"
if on_step not in current_callback_hook_auth_args["on_step"]:
msg = m.format(current_hook_fx_name, "on_step", current_callback_hook_auth_args["on_step"], on_step)
raise MisconfigurationException(msg)
if on_epoch not in current_callback_hook_auth_args["on_epoch"]:
msg = m.format(current_hook_fx_name, "on_epoch", current_callback_hook_auth_args["on_epoch"], on_epoch)
raise MisconfigurationException(msg)
else:
raise MisconfigurationException(
f"{current_hook_fx_name} function doesn't support logging using self.log() yet."
)
@staticmethod
def _on_before_accelerator_backend_setup_log():
"""Called before accelerator is being setup"""
return None
@staticmethod
def _setup_log():
"""Called when fit or test begins"""
return None
@staticmethod
def _teardown_log():
"""Called at the end of fit and test"""
return None
@staticmethod
def _on_init_start_log():
"""Called when the trainer initialization begins, model has not yet been set."""
return None
@staticmethod
def _on_init_end_log():
"""Called when the trainer initialization ends, model has not yet been set."""
return None
@staticmethod
def _on_fit_start_log():
"""Called when the trainer initialization begins, model has not yet been set."""
return None
@staticmethod
def _on_fit_end_log():
"""Called when the trainer initialization begins, model has not yet been set."""
return None
@staticmethod
def _on_sanity_check_start_log():
"""Called when the validation sanity check starts."""
return None
@staticmethod
def _on_sanity_check_end_log():
"""Called when the validation sanity check ends."""
return None
@staticmethod
def _on_train_epoch_start_log():
"""Called when the epoch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_train_epoch_end_log():
"""Called when the epoch ends."""
return {"on_step": [False], "on_epoch": [False, True]}
@staticmethod
def _on_train_epoch_final_end_log():
"""Called when at the very end of train epoch."""
return {"on_step": [False], "on_epoch": [False, True]}
@staticmethod
def _on_validation_epoch_start_log():
"""Called when the epoch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_validation_epoch_end_log():
"""Called when the epoch ends."""
return {"on_step": [False], "on_epoch": [False, True]}
@staticmethod
def _on_test_epoch_start_log():
"""Called when the epoch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_test_epoch_end_log():
"""Called when the epoch ends."""
return {"on_step": [False], "on_epoch": [False, True]}
@staticmethod
def _on_epoch_start_log():
"""Called when the epoch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_epoch_end_log():
"""Called when the epoch ends."""
return {"on_step": [False], "on_epoch": [False, True]}
@staticmethod
def _on_train_start_log():
"""Called when the train begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_train_end_log():
"""Called when the train ends."""
return None
@staticmethod
def _on_pretrain_routine_start_log():
"""Called when the train begins."""
return None
@staticmethod
def _on_pretrain_routine_end_log():
"""Called when the train ends."""
return None
@staticmethod
def _on_batch_start_log():
"""Called when the training batch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_batch_end_log():
"""Called when the training batch ends."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_train_batch_start_log():
"""Called when the training batch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_train_batch_end_log():
"""Called when the training batch ends."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_validation_batch_start_log():
"""Called when the validation batch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_validation_batch_end_log():
"""Called when the validation batch ends."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_test_batch_start_log():
"""Called when the test batch begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_test_batch_end_log():
"""Called when the test batch ends."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_validation_start_log():
"""Called when the validation loop begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_validation_end_log():
"""Called when the validation loop ends."""
return None
@staticmethod
def _on_test_start_log():
"""Called when the test begins."""
return {"on_step": [False, True], "on_epoch": [False, True]}
@staticmethod
def _on_test_end_log():
"""Called when the test ends."""
return None
@staticmethod
def _on_keyboard_interrupt_log():
"""Called when the training is interrupted by KeyboardInterrupt."""
return None
@staticmethod
def _on_save_checkpoint_log():
"""Called when saving a model checkpoint."""
return None
@staticmethod
def _on_load_checkpoint_log():
"""Called when loading a model checkpoint."""
return None