Skip to content

Comments

Fix NumPy 2.4.0 compatibility in TensorBoard logger#21543

Open
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
paipeline:fix/numpy-24-tensorboard-compatibility-21503
Open

Fix NumPy 2.4.0 compatibility in TensorBoard logger#21543
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
paipeline:fix/numpy-24-tensorboard-compatibility-21503

Conversation

@paipeline
Copy link

@paipeline paipeline commented Feb 20, 2026

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 a TypeError, 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() in log_metrics() with a fallback to float(tensor.detach().cpu().numpy()) when the TypeError occurs. This maintains backward compatibility with older NumPy versions while supporting NumPy >= 2.4.0.

Changes

  • src/lightning/fabric/loggers/tensorboard.py: Added exception handling for NumPy 2.4.0 TypeError
  • tests/tests_fabric/loggers/test_tensorboard.py: Added comprehensive test for 0-dimensional tensors and numpy-backed tensors

Testing

The fix includes a new test that specifically tests:

  • Zero-dimensional float and int tensors
  • Tensors created from NumPy arrays (most likely to trigger the issue)

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/

- 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.
@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fabric lightning.fabric.Fabric

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tensorboard logging breaks with certain scalar values with numpy >= 2.4.0

1 participant