Fix NumPy 2.4.0 compatibility in TensorBoard logger#21543
Open
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
Open
Fix NumPy 2.4.0 compatibility in TensorBoard logger#21543paipeline wants to merge 2 commits intoLightning-AI:masterfrom
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
Conversation
- Add try-catch around tensor.item() to handle TypeError in NumPy 2.4.0+ - NumPy 2.4.0 raises TypeError when converting 0-dimensional arrays to scalars - Fallback to float(tensor.detach().cpu().numpy()) when .item() fails - Add comprehensive test for 0-dimensional tensors and numpy-backed tensors - Fixes issue Lightning-AI#21503 The fix maintains backward compatibility while supporting NumPy >= 2.4.0. Tests cover zero-dimensional tensors and tensors created from numpy arrays, which are most likely to trigger the NumPy 2.4.0 TypeError.
for more information, see https://pre-commit.ci
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
Fixes #21503 - TensorBoard logger compatibility issue with NumPy 2.4.0+
Problem
As of NumPy 2.4.0, converting a 0-dimensional array to a scalar using
.item()raises aTypeError, following the expiration of a lengthy deprecation period. This breaks TensorBoard logging when tensors are backed by NumPy arrays.Solution
Added a try-catch around
tensor.item()inlog_metrics()with a fallback tofloat(tensor.detach().cpu().numpy())when the TypeError occurs. This maintains backward compatibility with older NumPy versions while supporting NumPy >= 2.4.0.Changes
Testing
The fix includes a new test that specifically tests:
Backward Compatibility
✅ Fully backward compatible - no changes to existing behavior with NumPy < 2.4.0
✅ Fixes the issue with NumPy >= 2.4.0
✅ Preserves all existing functionality and API
📚 Documentation preview 📚: https://pytorch-lightning--21543.org.readthedocs.build/en/21543/