Skip to content

Fix TensorBoard logging compatibility with numpy >= 2.4.0#21544

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

Fix TensorBoard logging compatibility with numpy >= 2.4.0#21544
paipeline wants to merge 2 commits intoLightning-AI:masterfrom
paipeline:fix/tensorboard-numpy-240-compatibility-21503

Conversation

@paipeline
Copy link
Copy Markdown

@paipeline paipeline commented Feb 22, 2026

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)

  • Enhanced log_metrics() method in src/lightning/fabric/loggers/tensorboard.py to properly handle numpy arrays
  • Added fallback mechanism for numpy >= 2.4.0 TypeError when calling .item() on 0-dimensional arrays
  • Supports both 0-dimensional and single-element 1D numpy arrays with appropriate scalar conversion
  • Provides clear error messages for unsupported multi-dimensional arrays

Comprehensive Test Coverage

  • New test file: tests/tests_fabric/loggers/test_tensorboard_numpy_240.py
  • Tests cover: 0-dimensional arrays, 1D single-element arrays, multiple numpy dtypes, TypeError fallback simulation, invalid multi-dimensional arrays, mixed type logging, and backward compatibility

Backward Compatibility

  • Zero breaking changes - all existing functionality preserved
  • Torch tensors continue to work exactly as before
  • Python scalars and dictionaries handled identically

Root Cause Analysis

Starting in numpy 2.4.0, converting 0-dimensional arrays to scalars raises a TypeError instead of silently succeeding. The TensorBoard logger's log_metrics() method only handled PyTorch tensors with .item() but didn't account for numpy arrays being passed in practice.

Solution Strategy

  1. Array Detection: Check for objects with ndim and item attributes (numpy-like arrays)
  2. Dimension Handling:
    • 0-dimensional arrays: Try .item(), fall back to float() conversion
    • 1-dimensional single-element: Try .item(), fall back to float(v.flat[0])
    • Multi-dimensional: Raise informative ValueError
  3. Graceful Degradation: Handle TypeError from numpy >= 2.4.0 with appropriate fallbacks

Testing

The fix includes comprehensive tests that verify:

  • 0-dimensional numpy arrays log correctly
  • 1-dimensional single-element arrays work
  • TypeError fallback for numpy >= 2.4.0 behavior
  • Multi-dimensional arrays raise appropriate errors
  • All existing tensor/scalar logging unchanged
  • Mixed type logging in single call
  • Multiple numpy dtypes supported

Impact

  • Fixes: Critical logging failures for users with numpy >= 2.4.0
  • Enables: Seamless numpy array logging in TensorBoard
  • Maintains: Full backward compatibility
  • Affects: Both lightning.fabric.loggers.TensorBoardLogger and lightning.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/

- 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
@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Feb 22, 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