-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: faster unstacking #15510
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
Closed
Closed
PERF: faster unstacking #15510
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# cython: profile=False | ||
|
||
from numpy cimport * | ||
cimport numpy as np | ||
import numpy as np | ||
|
||
cimport cython | ||
|
||
import_array() | ||
|
||
cimport util | ||
|
||
from numpy cimport NPY_INT8 as NPY_int8 | ||
from numpy cimport NPY_INT16 as NPY_int16 | ||
from numpy cimport NPY_INT32 as NPY_int32 | ||
from numpy cimport NPY_INT64 as NPY_int64 | ||
from numpy cimport NPY_FLOAT16 as NPY_float16 | ||
from numpy cimport NPY_FLOAT32 as NPY_float32 | ||
from numpy cimport NPY_FLOAT64 as NPY_float64 | ||
|
||
from numpy cimport (int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, | ||
uint32_t, uint64_t, float16_t, float32_t, float64_t) | ||
|
||
int8 = np.dtype(np.int8) | ||
int16 = np.dtype(np.int16) | ||
int32 = np.dtype(np.int32) | ||
int64 = np.dtype(np.int64) | ||
float16 = np.dtype(np.float16) | ||
float32 = np.dtype(np.float32) | ||
float64 = np.dtype(np.float64) | ||
|
||
cdef double NaN = <double> np.NaN | ||
cdef double nan = NaN | ||
|
||
include "reshape_helper.pxi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Template for each `dtype` helper function for take | ||
|
||
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in | ||
""" | ||
|
||
# ---------------------------------------------------------------------- | ||
# reshape | ||
# ---------------------------------------------------------------------- | ||
|
||
{{py: | ||
|
||
# name, c_type | ||
dtypes = [('uint8', 'uint8_t'), | ||
('uint16', 'uint16_t'), | ||
('uint32', 'uint32_t'), | ||
('uint64', 'uint64_t'), | ||
('int8', 'int8_t'), | ||
('int16', 'int16_t'), | ||
('int32', 'int32_t'), | ||
('int64', 'int64_t'), | ||
('float32', 'float32_t'), | ||
('float64', 'float64_t'), | ||
('object', 'object')] | ||
}} | ||
|
||
{{for dtype, c_type in dtypes}} | ||
|
||
|
||
@cython.wraparound(False) | ||
@cython.boundscheck(False) | ||
def unstack_{{dtype}}(ndarray[{{c_type}}, ndim=2] values, | ||
ndarray[uint8_t, ndim=1] mask, | ||
Py_ssize_t stride, | ||
Py_ssize_t length, | ||
Py_ssize_t width, | ||
ndarray[{{c_type}}, ndim=2] new_values, | ||
ndarray[uint8_t, ndim=2] new_mask): | ||
""" | ||
transform long sorted_values to wide new_values | ||
|
||
Parameters | ||
---------- | ||
values : typed ndarray | ||
mask : boolean ndarray | ||
stride : int | ||
length : int | ||
width : int | ||
new_values : typed ndarray | ||
result array | ||
new_mask : boolean ndarray | ||
result mask | ||
|
||
""" | ||
|
||
cdef: | ||
Py_ssize_t i, j, w, nulls, s, offset | ||
|
||
{{if dtype == 'object'}} | ||
if True: | ||
{{else}} | ||
with nogil: | ||
{{endif}} | ||
|
||
for i in range(stride): | ||
|
||
nulls = 0 | ||
for j in range(length): | ||
|
||
for w in range(width): | ||
|
||
offset = j * width + w | ||
|
||
if mask[offset]: | ||
s = i * width + w | ||
new_values[j, s] = values[offset - nulls, i] | ||
new_mask[j, s] = 1 | ||
else: | ||
nulls += 1 | ||
|
||
{{endfor}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@jreback couldn use some help grokking how we get here. In Block._unstack adding an assertion
assert mask.all()
doesn't break any tests. is that something we can rely on? (if so we can simplify code a good bit) If not, how can we construct a counter-example?