Fix TensorBoard logging compatibility with numpy >= 2.4.0#21544
Open
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
Open
Fix TensorBoard logging compatibility with numpy >= 2.4.0#21544paipeline wants to merge 2 commits intoLightning-AI:masterfrom
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
Conversation
- Handle numpy arrays properly in log_metrics() with fallback for TypeError - Add support for 0-dimensional and single-element 1D numpy arrays - Provide informative errors for multi-dimensional arrays - Add comprehensive tests for numpy array logging scenarios - Maintain full backward compatibility for existing tensor/scalar logging Fixes Lightning-AI#21503
for more information, see https://pre-commit.ci
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #21503
This PR resolves TensorBoard logging issues with numpy >= 2.4.0 by properly handling numpy arrays and the new TypeError behavior when converting 0-dimensional arrays to scalars.
Changes Made
Bug Fix - TensorBoard Logger (Fabric)
log_metrics()method insrc/lightning/fabric/loggers/tensorboard.pyto properly handle numpy arrays.item()on 0-dimensional arraysComprehensive Test Coverage
tests/tests_fabric/loggers/test_tensorboard_numpy_240.pyBackward Compatibility
Root Cause Analysis
Starting in numpy 2.4.0, converting 0-dimensional arrays to scalars raises a
TypeErrorinstead of silently succeeding. The TensorBoard logger'slog_metrics()method only handled PyTorch tensors with.item()but didn't account for numpy arrays being passed in practice.Solution Strategy
ndimanditemattributes (numpy-like arrays).item(), fall back tofloat()conversion.item(), fall back tofloat(v.flat[0])ValueErrorTypeErrorfrom numpy >= 2.4.0 with appropriate fallbacksTesting
The fix includes comprehensive tests that verify:
Impact
lightning.fabric.loggers.TensorBoardLoggerandlightning.pytorch.loggers.TensorBoardLogger(inheritance)This ensures PyTorch Lightning users can safely upgrade to numpy >= 2.4.0 without breaking their TensorBoard logging workflows.
📚 Documentation preview 📚: https://pytorch-lightning--21544.org.readthedocs.build/en/21544/