Add SwanLab training monitor#1256
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new monitoring module supporting SwanLab logging and integrates it across the base and specific trainers (DMD, DoPSD, Flow) to log training metrics. The feedback highlights two key improvements: first, wrapping the .item() call on metrics in a try-except block to prevent crashes when dealing with multi-element PyTorch tensors; second, making the SwanLab API key optional in the configuration to avoid security risks associated with hardcoded secrets and to support environment-based authentication.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for key, value in metrics.items(): | ||
| if hasattr(value, "item"): | ||
| value = value.item() | ||
| values[key] = value |
There was a problem hiding this comment.
Any PyTorch Tensor (regardless of its shape or number of elements) has the item() method. If a multi-element tensor is passed in metrics, calling value.item() will raise a ValueError: only one element tensors can be converted to Python scalars, which would crash the training loop.
To make the logging robust and prevent unexpected crashes, we should wrap the .item() call in a try-except block or check if the tensor has only a single element.
| for key, value in metrics.items(): | |
| if hasattr(value, "item"): | |
| value = value.item() | |
| values[key] = value | |
| for key, value in metrics.items(): | |
| if hasattr(value, "item"): | |
| try: | |
| value = value.item() | |
| except (ValueError, RuntimeError): | |
| pass | |
| values[key] = value |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
add swanlab key in yaml to activate monitor