-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
BUG: Add promoter to ldexp
for python integers to prevent overflow
#29597
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: main
Are you sure you want to change the base?
Conversation
What will happen with complex input? |
@mattip this only promotes integer inputs (Python scalar integer, actually), so the rest would stay the same? |
Thanks a for looking at this, this is very cool to see this use! I am curious if we can push this further and actually use the promoter always. That is, we:
|
Thanks, that extension makes a lot of sense! I tried implementing this, but it causes a segmentation fault. It seems to be due to the fact that Edit: that does seem to be it; reverting the operation for the first dtype and setting the others in this way doesn't crash (though of course the promotion doesn't work properly) |
Oh, hmmm, the pyfloat one should work ok probably, although the abstract version woukd be nicer. |
|
Making the abstract and PyFloat do the same thing probably makes sense, but the fact that it doesn't work here seems like a bug somewhere that I (or we) need track down to make this work (and a couple of other cases, because we should use this for other float only functions). |
To be clear it doesn't crash, it just doesn't promote further up than In line 223-226 of abstractdtypes.c we do in else if (other == &PyArray_PyLongDType) {
Py_INCREF(cls);
return cls;
} This is equivalent to doing no promotion as we already know it's a float, but looking at NEP 50, this actually seems correct behavior - Python ints can promote to the lowest float (given we need a float) right? So for |
Yes, we promote to numpy/numpy/_core/src/multiarray/abstractdtypes.c Lines 217 to 226 in 583993f
|
Ah, that makes sense (if I am getting the right picture). We are getting the abstract version as a result. In other paths that is OK because we only need a concrete dtype instance (via default_descr), but here we need to convert the abstract one to the concrete Float64 one. |
Ah, I see, thanks! Yes, I think that's right, we're getting the |
Yeah, I suppose it probably isn't a concern in practice, so the choice we have may be the pragmatic one, even if it is slightly weird/reversed if we use it here. Reverse or not, until we have a case where this fails, it may be a reasonable to path to "promote to a concrete DType". |
Makes sense! If it's a missing step, I suppose we should add it to (I realize this is an unrelated change to this PR, so completely happy to revert and open another PR if you prefer.) |
Yeah, I am not sure I want that function to ensure no abstract result. We could add a mini internal helper for now that ensures we have a concrete dtype, also means we can refactor it in a single place if we ever have to. (Also could check if you need the PyFloat rather than the abstract float version, since that didn't actually help initially.) |
Makes sense! I actually meant that Using the |
Hmmmmmmm, we had some reason for changing that. FWIW, it's probably fine then, we should be using the abstract version. |
We can just pass the Edit: unless there is a reason to not define the slots (I guess not, since the only other two are |
Basically except that may return one of the |
Hmm, there doesn't seem to be a reference to the I've added two slots to the abstract float dtype (I can add if you want them on the other abstract dtypes as well). The internal helper, should it be only for promoters? Edit: sorry, I suppose if it's a helper it can just be used directly in ldexp? Pushed this. |
The new test seems to reliably fail on armhf (will investigate). |
Fixes #29182.
Adds a promoter for first argument
PyArray_PyLongDType
toldexp
. It casts straight todouble
, not sure if that is more arbitrary than needed.Thanks for reviewing!