|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any, Dict |
| 16 | + |
| 17 | +import lightning.pytorch as pl |
| 18 | +import torch |
| 19 | +from lightning.pytorch import callbacks as pl_callbacks |
| 20 | +from lightning.pytorch.utilities.types import STEP_OUTPUT |
| 21 | +from typing_extensions import override |
| 22 | + |
| 23 | +from ml_flashpoint.core.mlf_logging import get_logger |
| 24 | + |
| 25 | +_LOGGER = get_logger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class EventLoggingCallback(pl_callbacks.Callback): |
| 29 | + """ |
| 30 | + A comprehensive logging callback to record timestamps for all key PyTorch Lightning |
| 31 | + lifecycle events to monitor execution flow. |
| 32 | + """ |
| 33 | + |
| 34 | + def _log_event(self, hook_name: str) -> None: |
| 35 | + _LOGGER.info(f"event={hook_name}") |
| 36 | + |
| 37 | + @override |
| 38 | + def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: |
| 39 | + """Called when the train begins.""" |
| 40 | + self._log_event("on_train_start") |
| 41 | + |
| 42 | + @override |
| 43 | + def on_train_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: |
| 44 | + """Called when the train ends.""" |
| 45 | + self._log_event("on_train_end") |
| 46 | + |
| 47 | + @override |
| 48 | + def on_train_batch_start( |
| 49 | + self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", batch: Any, batch_idx: int |
| 50 | + ) -> None: |
| 51 | + """Called when the train batch begins.""" |
| 52 | + self._log_event("on_train_batch_start") |
| 53 | + |
| 54 | + @override |
| 55 | + def on_train_batch_end( |
| 56 | + self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", outputs: STEP_OUTPUT, batch: Any, batch_idx: int |
| 57 | + ) -> None: |
| 58 | + """Called when the train batch ends.""" |
| 59 | + self._log_event("on_train_batch_end") |
| 60 | + |
| 61 | + @override |
| 62 | + def on_validation_batch_start( |
| 63 | + self, |
| 64 | + trainer: "pl.Trainer", |
| 65 | + pl_module: "pl.LightningModule", |
| 66 | + batch: Any, |
| 67 | + batch_idx: int, |
| 68 | + dataloader_idx: int = 0, |
| 69 | + ) -> None: |
| 70 | + """Called when the validation batch begins.""" |
| 71 | + self._log_event("on_validation_batch_start") |
| 72 | + |
| 73 | + @override |
| 74 | + def on_validation_batch_end( |
| 75 | + self, |
| 76 | + trainer: "pl.Trainer", |
| 77 | + pl_module: "pl.LightningModule", |
| 78 | + outputs: STEP_OUTPUT, |
| 79 | + batch: Any, |
| 80 | + batch_idx: int, |
| 81 | + dataloader_idx: int = 0, |
| 82 | + ) -> None: |
| 83 | + """Called when the validation batch ends.""" |
| 84 | + self._log_event("on_validation_batch_end") |
| 85 | + |
| 86 | + @override |
| 87 | + def on_test_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: |
| 88 | + """Called when the test epoch begins.""" |
| 89 | + self._log_event("on_test_epoch_start") |
| 90 | + |
| 91 | + @override |
| 92 | + def on_test_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: |
| 93 | + """Called when the test epoch ends.""" |
| 94 | + self._log_event("on_test_epoch_end") |
| 95 | + |
| 96 | + @override |
| 97 | + def on_test_batch_start( |
| 98 | + self, |
| 99 | + trainer: "pl.Trainer", |
| 100 | + pl_module: "pl.LightningModule", |
| 101 | + batch: Any, |
| 102 | + batch_idx: int, |
| 103 | + dataloader_idx: int = 0, |
| 104 | + ) -> None: |
| 105 | + """Called when the test batch begins.""" |
| 106 | + self._log_event("on_test_batch_start") |
| 107 | + |
| 108 | + @override |
| 109 | + def on_test_batch_end( |
| 110 | + self, |
| 111 | + trainer: "pl.Trainer", |
| 112 | + pl_module: "pl.LightningModule", |
| 113 | + outputs: STEP_OUTPUT, |
| 114 | + batch: Any, |
| 115 | + batch_idx: int, |
| 116 | + dataloader_idx: int = 0, |
| 117 | + ) -> None: |
| 118 | + """Called when the test batch ends.""" |
| 119 | + self._log_event("on_test_batch_end") |
| 120 | + |
| 121 | + @override |
| 122 | + def load_state_dict(self, state_dict: Dict[str, Any]) -> None: |
| 123 | + """Called when loading a checkpoint, implement to reload callback state.""" |
| 124 | + self._log_event("load_state_dict") |
| 125 | + |
| 126 | + @override |
| 127 | + def on_save_checkpoint( |
| 128 | + self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", checkpoint: Dict[str, Any] |
| 129 | + ) -> None: |
| 130 | + """Called when saving a checkpoint.""" |
| 131 | + self._log_event("on_save_checkpoint") |
| 132 | + |
| 133 | + @override |
| 134 | + def on_load_checkpoint( |
| 135 | + self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", checkpoint: Dict[str, Any] |
| 136 | + ) -> None: |
| 137 | + """Called when loading a model checkpoint, use to reload state.""" |
| 138 | + self._log_event("on_load_checkpoint") |
| 139 | + |
| 140 | + @override |
| 141 | + def on_before_backward(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", loss: torch.Tensor) -> None: |
| 142 | + """Called before loss.backward().""" |
| 143 | + self._log_event("on_before_backward") |
| 144 | + |
| 145 | + @override |
| 146 | + def on_after_backward(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: |
| 147 | + """Called after loss.backward() and before optimizers are stepped.""" |
| 148 | + self._log_event("on_after_backward") |
| 149 | + |
| 150 | + @override |
| 151 | + def on_before_optimizer_step( |
| 152 | + self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", optimizer: torch.optim.Optimizer |
| 153 | + ) -> None: |
| 154 | + """Called before optimizer.step().""" |
| 155 | + self._log_event("on_before_optimizer_step") |
| 156 | + |
| 157 | + @override |
| 158 | + def on_before_zero_grad( |
| 159 | + self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", optimizer: torch.optim.Optimizer |
| 160 | + ) -> None: |
| 161 | + """Called before optimizer.zero_grad().""" |
| 162 | + self._log_event("on_before_zero_grad") |
0 commit comments