feat(adapter/nemo): add EventLoggingCallback for lifecycle monitoring#34
feat(adapter/nemo): add EventLoggingCallback for lifecycle monitoring#34
Conversation
Change-Id: I1497831c8c1fdca614c053df39957eb6116840d7
Summary of ChangesHello @kkkapu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new PyTorch Lightning callback designed to provide comprehensive logging of the model training lifecycle. This callback records timestamps for critical events, enabling developers to monitor and analyze the execution flow of their training processes, which can be invaluable for debugging, performance profiling, and understanding model behavior. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a StepTimerCallback to log PyTorch Lightning lifecycle events, which is useful for monitoring and debugging training jobs. The implementation is straightforward. My review includes a few suggestions to improve code consistency and cleanliness. Specifically, I've pointed out an opportunity to refactor the logging helper method to remove an unused parameter and then apply it consistently across all hooks, including load_state_dict which currently has a redundant logging format. I've also noted a commented-out import that can be removed.
| from lightning.pytorch import callbacks as pl_callbacks | ||
| from lightning.pytorch.utilities.types import STEP_OUTPUT | ||
|
|
||
| # from lightning.pytorch.utilities import types as pl_util_types |
| def _log_event(self, trainer: "pl.Trainer", hook_name: str) -> None: | ||
| _LOGGER.info(f"{hook_name: <30}") | ||
|
|
||
| @override | ||
| def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: | ||
| """Called when the train begins.""" | ||
| self._log_event(trainer, "on_train_start") |
There was a problem hiding this comment.
The _log_event method has an unused trainer parameter. It's good practice to remove it to simplify the method signature. This also enables _log_event to be used in hooks that don't receive a trainer object, like load_state_dict, ensuring consistent logging across the callback. All call sites will need to be updated as shown in the suggestion for on_train_start.
| def _log_event(self, trainer: "pl.Trainer", hook_name: str) -> None: | |
| _LOGGER.info(f"{hook_name: <30}") | |
| @override | |
| def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: | |
| """Called when the train begins.""" | |
| self._log_event(trainer, "on_train_start") | |
| def _log_event(self, hook_name: str) -> None: | |
| _LOGGER.info(f"{hook_name: <30}") | |
| @override | |
| def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: | |
| """Called when the train begins.""" | |
| self._log_event("on_train_start") |
| def load_state_dict(self, state_dict: Dict[str, Any]) -> None: | ||
| """Called when loading a checkpoint, implement to reload callback state.""" | ||
| timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") | ||
| _LOGGER.info(f"[{timestamp}] Hook: load_state_dict") |
There was a problem hiding this comment.
The logging implementation here is inconsistent with the other lifecycle hooks. It manually creates a timestamp, which is redundant because the logger is already configured to add one. This results in log entries with duplicate timestamps. To ensure consistent log formatting, this method should use the centralized _log_event helper, assuming it has been refactored to no longer require the trainer argument.
| def load_state_dict(self, state_dict: Dict[str, Any]) -> None: | |
| """Called when loading a checkpoint, implement to reload callback state.""" | |
| timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") | |
| _LOGGER.info(f"[{timestamp}] Hook: load_state_dict") | |
| @override | |
| def load_state_dict(self, state_dict: Dict[str, Any]) -> None: | |
| """Called when loading a checkpoint, implement to reload callback state.""" | |
| self._log_event("load_state_dict") |
Change-Id: If6fe4e90ef0b576568dc38a08aaf63d8fcfb7ba0
| @override | ||
| def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: | ||
| """Called when the train begins.""" | ||
| self._log_event("on_train_start") |
There was a problem hiding this comment.
can we generate some basic tests to validate some of the key events are logged, like on_train_*, on_train_batch_*
| """ | ||
|
|
||
| def _log_event(self, hook_name: str) -> None: | ||
| _LOGGER.info(f"{hook_name: <30}") |
There was a problem hiding this comment.
Is <30 needed?
nit: maybe add event= to make it more unique of a log statement
| _LOGGER.info(f"{hook_name: <30}") | |
| _LOGGER.info(f"event={hook_name}") |
Change-Id: Ifca5e8885cdf0a3ac0f10f925c5415e83bdda721
|
|
||
| def test_is_subtype_of_pytorch_lightning_callback(): | ||
| """Verify inheritance to ensure compatibility with PyTorch Lightning.""" | ||
| # assert issubclass(MLFlashpointCheckpointCallback, pl.callbacks.Callback) |
There was a problem hiding this comment.
can remove this commented out line
| """ | ||
| Tests that every lifecycle hook in StepTimerCallback logs the correct event. | ||
| """ | ||
|
|
There was a problem hiding this comment.
nit: can add "# Given # When # Then" comments to highlight different parts of the test. setup, execution, assertions. makes it easier to see what is being asserted and which parts are just setup vs execution
| @@ -0,0 +1,162 @@ | |||
| # Copyright 2025 Google LLC | |||
There was a problem hiding this comment.
this can prob stay as 2026? same for the other file
| _LOGGER = get_logger(__name__) | ||
|
|
||
|
|
||
| class StepTimerCallback(pl_callbacks.Callback): |
There was a problem hiding this comment.
nit: maybe renaming to EventLoggingCallback, since it isnt timing anything, and is just logging every event. should update test file/code accordingly
…omments Change-Id: Ibaa19a73eeccd19f5ef46a5d620c862c7cfd0fe8
Sample Log: https://gist.github.com/kkkapu/09eb0d8522dd6ee992c7833cb630714c