Fix TensorBoard logging compatibility with numpy 2.4.0+ scalar handling#21549
Open
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
Open
Fix TensorBoard logging compatibility with numpy 2.4.0+ scalar handling#21549paipeline wants to merge 2 commits intoLightning-AI:masterfrom
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
Conversation
Fixes Lightning-AI#21503: TensorBoard logging breaks with certain scalar values with numpy >= 2.4.0 Changes: - Enhanced log_metrics() in TensorBoardLogger to handle numpy arrays with .item() method - Added try/catch around .item() calls to catch numpy 2.4.0 TypeError - Implemented robust fallback using arr.dtype.type(arr) for 0-d arrays - Added float(arr) as secondary fallback for edge cases - Maintains full backward compatibility with PyTorch tensors and native Python types - Added comprehensive test coverage for numpy dtypes and edge cases The fix ensures that numpy 0-dimensional arrays (scalars) are properly converted to native Python scalars even when numpy 2.4.0+ raises TypeError on .item() calls. This resolves TensorBoard logging failures without breaking existing functionality.
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.
Summary
This PR fixes issue #21503 where TensorBoard logging breaks with certain scalar values when using numpy >= 2.4.0.
Problem
NumPy 2.4.0 introduced breaking changes where calling
.item()on 0-dimensional arrays can raiseTypeErrorinstead of returning the scalar value. This affects TensorBoard logging when users pass numpy arrays as metric values, causing Lightning to crash during logging.Root Cause
The
log_metrics()method inTensorBoardLoggeronly handled PyTorchTensorobjects by calling.item(), but didn't properly handle numpy arrays. When numpy arrays were passed through, they could cause issues in downstream TensorBoard code.Solution
Enhanced the
log_metrics()method to:.item()method usinghasattr(v, "item").item()calls in try/catch to handle numpy 2.4.0 TypeErrorv.dtype.type(v)for 0-dimensional arrays andfloat(v)as secondary fallbackChanges
Core Fix
src/lightning/fabric/loggers/tensorboard.pyto add numpy array handling inlog_metrics()Tests
tests/tests_fabric/loggers/test_tensorboard.py:test_tensorboard_numpy_24_scalar_compatibility()- Tests various numpy scalar types and simulates numpy 2.4.0 behaviortest_tensorboard_numpy_dtype_coverage()- Ensures all common numpy dtypes work correctlyDocumentation/Demos
demonstrate_fix_21503.py- Demonstrates the fix and fallback mechanismsreproduce_issue_21503.py- Shows the original issue contextTesting
The fix has been tested with:
✅ All common numpy dtypes: float16/32/64, int8/16/32/64, uint8/16/32/64, bool
✅ PyTorch tensors: Maintains existing tensor handling
✅ Native Python types: No impact on int/float values
✅ Numpy 2.4.0 simulation: Handles simulated TypeError from .item() calls
✅ Zero breaking changes: All existing functionality preserved
Impact
🔧 Fixes critical compatibility issue preventing TensorBoard logging with numpy 2.4.0+
🔒 Zero breaking changes - maintains full backward compatibility
⚡ Automatic improvement - no user configuration required
🎯 Comprehensive coverage - handles all numpy dtypes and edge cases
This fix ensures Lightning works properly with modern numpy versions while maintaining compatibility with existing code.
Fixes #21503
Type of change
cc @pganssle-google @ethanwharris @lantiga
📚 Documentation preview 📚: https://pytorch-lightning--21549.org.readthedocs.build/en/21549/