-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Coerce tensorboard metrics into numpy arrays #21504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Coerce tensorboard metrics into numpy arrays #21504
Conversation
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.
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is 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 |
| 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()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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?
There was a problem hiding this comment.
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 😅
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
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
📚 Documentation preview 📚: https://pytorch-lightning--21504.org.readthedocs.build/en/21504/