<div dir="auto">It is pretty weird that these two statements don't necessarily produce the same result:<div dir="auto"><br></div><div dir="auto">someufunc(*inputs, out=out_arr)</div><div dir="auto">out_arr[...] = someufunc(*inputs)</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 27, 2019, 15:02 Sebastian Berg <<a href="mailto:sebastian@sipsolutions.net">sebastian@sipsolutions.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fri, 2019-09-27 at 11:50 -0700, Sebastian Berg wrote:<br>
> Hi all,<br>
> <br>
> Looking at the ufunc dispatching rules with an `out` argument, I was<br>
> a<br>
> bit surprised to realize this little gem is how things work:<br>
> <br>
> ```<br>
> arr = np.arange(10, dtype=np.uint16) + 2**15<br>
> print(arr)<br>
> # array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18], dtype=uint16)<br>
> <br>
<br>
Whoops, copied that print wrong of course.<br>
<br>
Just to be clear, I personally will consider this an accuracy/precision<br>
bug and assume that we can just switch the behaviour failry<br>
unceremoniously at some point (and if someone feels that should be a<br>
major release, I do not mind).<br>
It seems like one of those things that will definitely fix some bugs<br>
but could break the odd system/assumption somewhere. Similar to fixing<br>
the memory overlap issues.<br>
<br>
- Sebastian<br>
<br>
<br>
> out = np.zeros(10)<br>
> <br>
> np.add(arr, arr, out=out)<br>
> print(repr(out))<br>
> # array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18.])<br>
> ```<br>
> <br>
> This is strictly speaking correct/consistent. What the ufunc tries to<br>
> ensure is that whatever the loop produces fits into `out`.<br>
> However, I still find it unexpected that it does not pick the full<br>
> precision loop.<br>
> <br>
> There is currently only one way to achieve that, and this by using<br>
> `dtype=out.dtype` (or similar incarnations) which specify the exact<br>
> dtype [0].<br>
> <br>
> Of course this is also because I would like to simplify things for a<br>
> new dispatching system, but I would like to propose to disable the<br>
> above behaviour. This would mean:<br>
> <br>
> ```<br>
> # make the call:<br>
> np.add(arr, arr, out=out)<br>
> <br>
> # Equivalent to the current [1]:<br>
> np.add(arr, arr, out=out, dtype=(None, None, out.dtype))<br>
> <br>
> # Getting the old behaviour requires (assuming inputs have same<br>
> dtype):<br>
> np.add(arr, arr, out=out, dtypes=arr.dtype)<br>
> ```<br>
> <br>
> and thus force the high precision loop. In very rare cases, this<br>
> could<br>
> lead to no loop being found.<br>
> <br>
> The main incompatibility is if someone actually makes use of the<br>
> above<br>
> (integer over/underflow) behaviour, but wants to store it in a higher<br>
> precision array.<br>
> <br>
> I personally currently think we should change it, but am curious if<br>
> we<br>
> think that we may be able to get away with an accelerate process and<br>
> not a year long FutureWarning.<br>
> <br>
> Cheers,<br>
> <br>
> Sebastian<br>
> <br>
> <br>
> [0] You can also use `casting="no"` but in all relevant cases that<br>
> should find no loop, since the we typically only have homogeneous<br>
> loop<br>
> definitions, and<br>
> <br>
> [1] Which is normally the same as the shorter spelling<br>
> `dtype=out.dtype` of course.<br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@python.org" target="_blank" rel="noreferrer">NumPy-Discussion@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank" rel="noreferrer">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>