Skip to content

Conversation

@pganssle-google
Copy link

@pganssle-google pganssle-google commented Jan 23, 2026

What does this PR do?

As of Numpy 2.4.0, converting an 0-dimensional array to a scalar is now a TypeError, following the expiration of a lengthy deprecation period.

This change detects the situation and adjusts the types accordingly before passing them to tensorboard.

Fixes #21503

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs) No discussion yet.
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--21504.org.readthedocs.build/en/21504/

As of [Numpy
2.4.0](https://numpy.org/doc/stable/release/2.4.0-notes.html#raise-typeerror-on-attempt-to-convert-array-with-ndim-0-to-scalar),
converting an 0-dimensional array to a scalar is now a TypeError,
following the expiration of a lengthy deprecation period.

This change detects the situation and adjusts the types accordingly
before passing them to tensorboard.
@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Jan 24, 2026
@codecov
Copy link

codecov bot commented Jan 24, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 87%. Comparing base (0a0f061) to head (a93d2e4).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21504   +/-   ##
=======================================
- Coverage      87%      87%   -0%     
=======================================
  Files         270      270           
  Lines       24067    24075    +8     
=======================================
+ Hits        20863    20869    +6     
- Misses       3204     3206    +2     

deependujha
deependujha previously approved these changes Jan 27, 2026
Comment on lines +214 to +217
if isinstance(v, (int, float, np.number)):
v = np.array(v)
elif isinstance(v, np.ndarray) and v.ndim > 0 and v.size == 1:
v = np.array(v.item())
Copy link
Collaborator

@deependujha deependujha Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(v, (int, float, np.number)):
v = np.array(v)
elif isinstance(v, np.ndarray) and v.ndim > 0 and v.size == 1:
v = np.array(v.item())
if isinstance(v, (np.number)) or isinstance(v, np.ndarray) and v.ndim > 0 and v.size == 1:
v = float(v.item())

Why are we converting them into np.array? Is there any additional benefit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I skipped that part the tests failed, at least for me.

TBH, this whole thing is a bug in tensorboardX. tensorboardX does some inappropriate conversion with scalars which hits the deprecated-now-removed code path. IIRC, if you don't coerce to a numpy array with the right shape before passing it to tensorboardX, it does it anyway, it just does it incorrectly.

So I guess it's up to you how much you want to have code here to work around a bug upstream. In practical terms it means this part of the library is not compatible with numpy 2.4 until tensorboardX is fixed, which may never happen, and even if it does, it will require a more recent version of tensorboardX to work properly. But from a purity perspective, this isn't actually your bug 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fabric lightning.fabric.Fabric has conflicts pl Generic label for PyTorch Lightning package

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

2 participants