Skip to content

Replace nan_to_num with where for CuPy#595

Merged
Saransh-cpp merged 20 commits intomainfrom
use-where-for-cupy
Jul 15, 2025
Merged

Replace nan_to_num with where for CuPy#595
Saransh-cpp merged 20 commits intomainfrom
use-where-for-cupy

Conversation

@Saransh-cpp
Copy link
Member

@Saransh-cpp Saransh-cpp commented May 30, 2025

Description

Fixes #593
XRef #615

Using where is okay for other backends, but it is an issue for the SymPy backend. The shim SympyLib.where function is not a generic replacement for np.where, instead, it will only work for this particular case (return the second argument (first value) as that has the normal values). Hence, we will have to manually check that no other compute function uses where as actual np.where.

@Saransh-cpp
Copy link
Member Author

The failing disassemble and SymPy (unrelated) tests were expected. I wasn't expecting the Numba tests to fail.

@Saransh-cpp Saransh-cpp added the bug The problem described is something that must be fixed label May 31, 2025
@Saransh-cpp Saransh-cpp force-pushed the use-where-for-cupy branch from 38518ae to 42b138f Compare May 31, 2025 13:54
@Saransh-cpp Saransh-cpp force-pushed the use-where-for-cupy branch from 8ab72f1 to 9d7a648 Compare June 1, 2025 17:07
@Saransh-cpp Saransh-cpp force-pushed the use-where-for-cupy branch from 7c1626a to fc7f41f Compare June 1, 2025 20:57
Copy link
Member Author

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Even though the SymPy solution is not ideal, @ikrommyd's comment - #593 (comment) - suggests that CuPy releasing a fix in the future versions will not work well with older CUDA versions. We can easily revert back to the nan_to_num implementation once CuPy fixes this issue.

I'd leave it to the other maintainers to decide if this should go in.

posinf=inf,
neginf=-inf,
)
return lib.where(z != 0, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) * 1
Copy link
Member Author

Choose a reason for hiding this comment

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

There is absolutely no issue in switching to this definition for the numerical backends, so this is not a hack.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the only caveat this has is that for the JAX backend gradients could be wrongly propagated as NaNs. This is typically solved by using a double-where statement, see: https://github.com/tensorflow/probability/blob/main/discussion/where-nan.pdf

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, @pfackeldey! TIL.

I have updated the PR with nested wheres.

(We do really need GPU/Dask/JAX integration tests for Scikit-HEP...)

# this function is to handle exceptional values — we know that the "normal" values
# are in the second argument and the "exceptional" ones are in the third argument.
# TODO: remove once https://github.com/cupy/cupy/issues/9143 is fixed.
def where(self, val1: sympy.Expr, val2: sympy.Expr, val3: sympy.Expr) -> sympy.Expr:
Copy link
Member Author

Choose a reason for hiding this comment

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

This might be considered a hack/quick fix for the SymPy backend (as explained in the comment above). The only issue with this PR is that we will have to manually check that where is not used as a regular np.where in compute functions.

@Saransh-cpp Saransh-cpp marked this pull request as ready for review June 3, 2025 13:40
@ikrommyd
Copy link

ikrommyd commented Jun 3, 2025

Even though the SymPy solution is not ideal, @ikrommyd's comment - #593 (comment) - suggests that CuPy releasing a fix in the future versions will not work well with older CUDA versions. We can easily revert back to the nan_to_num implementation once CuPy fixes this issue.

I'd leave it to the other maintainers to decide if this should go in.

I’m basically suggesting that when cupy puts out a fix, it’s gonna naturally be only in the latest release and therefore you can’t expect people to always be on the latest release because for whatever reason they may not have a cuda version compatible with that release. This is a “safe” assumption I made. I don’t know the cupy/cuda support matrix by heart. My point was that we may not want to put all our faith on cupy in order for vector to work with the cuda backend if awkward array.

pre-commit-ci bot and others added 7 commits June 30, 2025 16:00
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.11.12 → v0.11.13](astral-sh/ruff-pre-commit@v0.11.12...v0.11.13)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* fix: get codecov back up again

* upload cov files

* upload cov files

* some debugging

* style: pre-commit fixes

* specify path

* try XML report

* use extend

* all fixed

* revert more debug statements

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
#608)

Bumps the actions group with 1 update: [actions/attest-build-provenance](https://github.com/actions/attest-build-provenance).


Updates `actions/attest-build-provenance` from 2.3.0 to 2.4.0
- [Release notes](https://github.com/actions/attest-build-provenance/releases)
- [Changelog](https://github.com/actions/attest-build-provenance/blob/main/RELEASE.md)
- [Commits](actions/attest-build-provenance@db473fd...e8998f9)

---
updated-dependencies:
- dependency-name: actions/attest-build-provenance
  dependency-version: 2.4.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* Update talks.md

* add citation info in README

* add citing information in docs

* style: pre-commit fixes

* uniformity

* style: pre-commit fixes

* plural

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Jun 30, 2025

Codecov Report

❌ Patch coverage is 33.33333% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.58%. Comparing base (a929d34) to head (879cc5d).
⚠️ Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
src/vector/backends/_numba_object.py 0.00% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #595      +/-   ##
==========================================
+ Coverage   87.49%   87.58%   +0.09%     
==========================================
  Files          95       95              
  Lines       12066    12057       -9     
==========================================
+ Hits        10557    10560       +3     
+ Misses       1509     1497      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ikrommyd
Copy link

I'm working for the actual fix on cupy in cupy/cupy#9240 but I think it's still good to merge this because it will be a while since this comes in a cupy release (+ you'd still need people to be on the latest cupy).

@Saransh-cpp
Copy link
Member Author

pinging @pfackeldey and @ianna here

Copy link
Member

@ianna ianna left a comment

Choose a reason for hiding this comment

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

@Saransh-cpp - thanks for fixing it! As it is a temporary fix, I think it would be better if there is an issue linked to the cupy one. Thanks!

Saransh-cpp

This comment was marked as duplicate.

@Saransh-cpp Saransh-cpp requested a review from ianna July 15, 2025 13:15
@Saransh-cpp
Copy link
Member Author

Thanks for the review, @ianna! I've opened a new issue. This should be ready to review again.

Copy link
Member

@ianna ianna left a comment

Choose a reason for hiding this comment

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

@Saransh-cpp - thank you for fixing it! Looks good to me!

@Saransh-cpp Saransh-cpp merged commit 096cf76 into main Jul 15, 2025
19 of 20 checks passed
@Saransh-cpp Saransh-cpp deleted the use-where-for-cupy branch July 15, 2025 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug The problem described is something that must be fixed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError with cuda backend

4 participants