You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from float32 to float64.
The question I have is whether this is considered counter-intuitive behavior for an in-place operation. The slower runtime would seem to imply that a temporary ndarray was created, which defeats the purpose of an in-place operation. (i.e. to avoid memory allocation for temporaries)
The difference is because scalars are no longer treated as different from arrays, so you now get the speed that previously you'd have gotten for something like data -= np.array([0.]) (I verified this gives the slow result also on older numpy). Only for a python float will the float32 loop be chosen. You can still force the choice with np.subtract(data, np.array([0.]), out=data, dtype=data.dtype).
A separate question is whether in general for an in-place operation one should effectively do dtype=in.dtype (if a suitable loop exists). It might well break other things, though...
p.s. Note that even now the operation is done buffered, not via a full temporary copy, so memory should not be a big worry.
A separate question is whether in general for an in-place operation one should effectively do dtype=in.dtype (if a suitable loop exists). It might well break other things, though...
That's probably a good idea - xref gh-25621 for this.
@pijyoi - since there is already a larger issue open, and I gave what hopefully are useful work-arounds, I'll close this one. Please do feel free to reopen if there is more to add!
p.s. Thanks for reporting! Even though the change to no longer treating scalars any differently from arrays is, I think, very much for the better, it is good to realize also more unexpected consequences.
Activity
mhvk commentedon Mar 31, 2024
The difference is because scalars are no longer treated as different from arrays, so you now get the speed that previously you'd have gotten for something like
data -= np.array([0.])(I verified this gives the slow result also on older numpy). Only for a python float will thefloat32loop be chosen. You can still force the choice withnp.subtract(data, np.array([0.]), out=data, dtype=data.dtype).A separate question is whether in general for an in-place operation one should effectively do
dtype=in.dtype(if a suitable loop exists). It might well break other things, though...p.s. Note that even now the operation is done buffered, not via a full temporary copy, so memory should not be a big worry.
rgommers commentedon Apr 1, 2024
That's probably a good idea - xref gh-25621 for this.
mhvk commentedon Apr 1, 2024
Ah, yes, and I see I even read that issue at the time -- but obviously forgot about it. Your idea of an experiment seems good.
mhvk commentedon Apr 1, 2024
@pijyoi - since there is already a larger issue open, and I gave what hopefully are useful work-arounds, I'll close this one. Please do feel free to reopen if there is more to add!
p.s. Thanks for reporting! Even though the change to no longer treating scalars any differently from arrays is, I think, very much for the better, it is good to realize also more unexpected consequences.