-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF/REGR: astype changing order of some 2d data #42475
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
Changes from all commits
0c9d549
bb497c9
2b1c561
2d0ef39
cd13406
996650e
79f2d50
c6a533b
c262158
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,7 +14,6 @@ | |||||||||||||||||
from typing import ( | ||||||||||||||||||
TYPE_CHECKING, | ||||||||||||||||||
Any, | ||||||||||||||||||
Literal, | ||||||||||||||||||
Sized, | ||||||||||||||||||
TypeVar, | ||||||||||||||||||
cast, | ||||||||||||||||||
|
@@ -1093,14 +1092,11 @@ def astype_nansafe( | |||||||||||||||||
The dtype was a datetime64/timedelta64 dtype, but it had no unit. | ||||||||||||||||||
""" | ||||||||||||||||||
if arr.ndim > 1: | ||||||||||||||||||
# Make sure we are doing non-copy ravel and reshape. | ||||||||||||||||||
flags = arr.flags | ||||||||||||||||||
flat = arr.ravel("K") | ||||||||||||||||||
flat = arr.ravel() | ||||||||||||||||||
result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna) | ||||||||||||||||||
order: Literal["C", "F"] = "F" if flags.f_contiguous else "C" | ||||||||||||||||||
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no | ||||||||||||||||||
# attribute "reshape" | ||||||||||||||||||
return result.reshape(arr.shape, order=order) # type: ignore[union-attr] | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC the point of the (im pretty sure i used the same flags-based pattern in the datetimelike arrays, so whatever fix is used here might need to be applied there too) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the behavior regression, what was happening was the following:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so what if we change L1100 to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
To be clear, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks for walking me through this |
||||||||||||||||||
return result.reshape(arr.shape) # type: ignore[union-attr] | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we revert all of #38507, do we remove the need for an ignore? i.e. is there another issue lurking here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverting would remove the need for the ignore, in the previous logic the reshape was inside an AFAICT there is no issue here though - the problematic case here would be if Lines 5731 to 5738 in 87d7855
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure. but let's leave that out of this PR which is being backported |
||||||||||||||||||
|
||||||||||||||||||
# We get here with 0-dim from sparse | ||||||||||||||||||
arr = np.atleast_1d(arr) | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wont this mean a perf hit in cases where arr is F-contiguous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep (some discussion about that in the pr body). Would be a regression from master (but not from 1.2.x). The other fix for this regression is to just use
arr.ravel(order)
whereorder
is the same as used forreshape
.That avoids the hit you're talking about, but doesn't fix the perf regression
(although that perf regression is a bit suspicious, was trying to profile it, but on larger data sizes the regression doesn't show up anymore)EDIT: Regression does show up even with larger data, the perf hit comes from the reduction step, eg
bottleneck.reduce.nanmean
on a call like.mean(axis=0)
or'reduce' of 'numpy.ufunc'
with.sum(axis=0)
. Only difference I can see on data going in is F-contiguity on master vs C-contiguity on this pr (and 1.2.x)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does
arr.ravel(order)
fix this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
arr.ravel(order)
fixes the regression in behavior, but not the performance regression (discussed a bit in the comments above). Seems like it comes down to computations likebottleneck.reduce.nanmean
being slower on F-contiguity data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes for sure that would be true.
ok so the question remains, this goes back to 1.2.x perf? are the f-contig routines hit? when do we hit these
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep exactly -> the benchmarks shown at the top of the pr are back to 1.2.x perf. With the perf regression on master, for those benchmarks we hit reduction routines with f-contig data. With this pr and 1.2.x, we hit those routines with C-contig data.