Skip to content
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

Match numpy sign semantics #8330

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

prutschman-iv
Copy link

@prutschman-iv prutschman-iv commented May 20, 2024

Here is my first pass at modifying the sign function to match numpy's NaN semantics. It includes a test case using check_unary_inf_nan. This is to fix #8327

I've confirmed that the tests pass on v12, and have rebased to main, but am currently having build difficulties on main, so this is a draft for now.

The non-complex logic is based directly on numpy's logic. The static_casts are there to avoid a compiler error about ambiguous int casting for the ternary operator when the template is used with integer types.

@prutschman-iv
Copy link
Author

I've been looking more at the PTX code generated. I think the original method of (in0.imag() > 0) - (in0.imag() < 0) may be superior for the complex case, and it would preserve the semantics by virtue of only executing after the ifnan test. (This is of course only instruction counting, not a proper benchmark.)

@kmaehashi kmaehashi self-assigned this May 21, 2024
@kmaehashi kmaehashi added cat:bug Bugs to-be-backported Pull-requests to be backported to stable branch prio:medium labels May 21, 2024
@ev-br
Copy link
Contributor

ev-br commented May 21, 2024

Note that numpy 2.0 redefines the complex sign to return z/|z|.

cdf5ca8

@prutschman-iv prutschman-iv force-pushed the match-numpy-sign-semantics branch from 1d27ab7 to e1628c0 Compare May 23, 2024 18:01
@prutschman-iv
Copy link
Author

I have verified the changes with the main branch. I believe this is ready for merge.

@prutschman-iv prutschman-iv marked this pull request as ready for review May 23, 2024 18:01
Once guarded by the isnan test, the (x > 0) - (x < 0) has the correct
semantics, and produces tighter PTX code.
@prutschman-iv prutschman-iv force-pushed the match-numpy-sign-semantics branch from e1628c0 to 0c48c6f Compare June 7, 2024 18:03
Copy link
Contributor

mergify bot commented Dec 17, 2024

This pull request is now in conflicts. Could you fix it @prutschman-iv? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:bug Bugs prio:medium to-be-backported Pull-requests to be backported to stable branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cupy.sign(NaN) == 0, unlike numpy
3 participants