-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
use fused types for reshape #22454
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
Merged
Merged
use fused types for reshape #22454
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,95 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
cimport cython | ||
from cython cimport Py_ssize_t | ||
import cython | ||
from cython import Py_ssize_t | ||
|
||
import numpy as np | ||
from numpy cimport (ndarray, | ||
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, | ||
from numpy cimport (int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, | ||
uint32_t, uint64_t, float32_t, float64_t) | ||
|
||
|
||
cdef double NaN = <double> np.NaN | ||
cdef double nan = NaN | ||
ctypedef fused reshape_t: | ||
uint8_t | ||
uint16_t | ||
uint32_t | ||
uint64_t | ||
int8_t | ||
int16_t | ||
int32_t | ||
int64_t | ||
float32_t | ||
float64_t | ||
object | ||
|
||
include "reshape_helper.pxi" | ||
|
||
@cython.wraparound(False) | ||
@cython.boundscheck(False) | ||
def unstack(reshape_t[:, :] values, uint8_t[:] mask, | ||
Py_ssize_t stride, Py_ssize_t length, Py_ssize_t width, | ||
reshape_t[:, :] new_values, uint8_t[:, :] 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 reshape_t is not object: | ||
# evaluated at compile-time | ||
with nogil: | ||
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 | ||
|
||
else: | ||
# object-dtype, identical to above but we cannot use nogil | ||
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 | ||
|
||
|
||
unstack_uint8 = unstack["uint8_t"] | ||
unstack_uint16 = unstack["uint16_t"] | ||
unstack_uint32 = unstack["uint32_t"] | ||
unstack_uint64 = unstack["uint64_t"] | ||
unstack_int8 = unstack["int8_t"] | ||
unstack_int16 = unstack["int16_t"] | ||
unstack_int32 = unstack["int32_t"] | ||
unstack_int64 = unstack["int64_t"] | ||
unstack_float32 = unstack["float32_t"] | ||
unstack_float64 = unstack["float64_t"] | ||
unstack_object = unstack["object"] |
This file was deleted.
Oops, something went wrong.
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.
what if we make the inner a function instead?
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.
No dice; we'd need a "nogil" version and a not-nogil version.
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.
I think like long-term cython may implement a feature discussed in #22452 that may let us clear this up further. For now this is much more readable than the existing tempita implementation.