-
Notifications
You must be signed in to change notification settings - Fork 130
Speedup DimShuffle and Reshape in C and Numba backends #1226
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
416d210
Revert regression in Reshape C-impl speed
ricardoV94 ea4a86f
Revert regression in DimShuffle C-impl speed
ricardoV94 b69cdc8
Remove unused inplace option in DimShuffle
ricardoV94 50801a3
Simplify and speedup numba DimShuffle implementation
ricardoV94 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 |
---|---|---|
@@ -1,82 +1,81 @@ | ||
#section support_code_apply | ||
|
||
int APPLY_SPECIFIC(cpu_dimshuffle)(PyArrayObject *input, PyArrayObject **res, | ||
PARAMS_TYPE *params) { | ||
|
||
// This points to either the original input or a copy we create below. | ||
// Either way, this is what we should be working on/with. | ||
PyArrayObject *_input; | ||
|
||
if (*res) | ||
Py_XDECREF(*res); | ||
|
||
if (params->inplace) { | ||
_input = input; | ||
Py_INCREF((PyObject *)_input); | ||
} else { | ||
_input = (PyArrayObject *)PyArray_FromAny( | ||
(PyObject *)input, NULL, 0, 0, NPY_ARRAY_ALIGNED | NPY_ARRAY_ENSURECOPY, | ||
NULL); | ||
} | ||
|
||
PyArray_Dims permute; | ||
|
||
if (!PyArray_IntpConverter((PyObject *)params->transposition, &permute)) { | ||
return 1; | ||
} | ||
|
||
/* | ||
res = res.transpose(self.transposition) | ||
*/ | ||
PyArrayObject *transposed_input = | ||
(PyArrayObject *)PyArray_Transpose(_input, &permute); | ||
|
||
Py_DECREF(_input); | ||
|
||
PyDimMem_FREE(permute.ptr); | ||
|
||
npy_intp *res_shape = PyArray_DIMS(transposed_input); | ||
npy_intp N_shuffle = PyArray_SIZE(params->shuffle); | ||
npy_intp N_augment = PyArray_SIZE(params->augment); | ||
npy_intp N = N_augment + N_shuffle; | ||
npy_intp *_reshape_shape = PyDimMem_NEW(N); | ||
|
||
if (_reshape_shape == NULL) { | ||
PyErr_NoMemory(); | ||
return 1; | ||
} | ||
int APPLY_SPECIFIC(cpu_dimshuffle)(PyArrayObject *input, PyArrayObject **res, PARAMS_TYPE *params) { | ||
npy_int64* new_order; | ||
npy_intp nd_in; | ||
npy_intp nd_out; | ||
npy_intp* dimensions; | ||
npy_intp* strides; | ||
|
||
if (!PyArray_IS_C_CONTIGUOUS(params->_new_order)) { | ||
PyErr_SetString(PyExc_RuntimeError, "DimShuffle: param _new_order must be C-contiguous."); | ||
return 1; | ||
} | ||
new_order = (npy_int64*) PyArray_DATA(params->_new_order); | ||
nd_in = (npy_intp)(params->input_ndim); | ||
nd_out = PyArray_SIZE(params->_new_order); | ||
|
||
/* | ||
shape = list(res.shape[: len(self.shuffle)]) | ||
for augm in self.augment: | ||
shape.insert(augm, 1) | ||
*/ | ||
npy_intp aug_idx = 0; | ||
int res_idx = 0; | ||
for (npy_intp i = 0; i < N; i++) { | ||
if (aug_idx < N_augment && | ||
i == *((npy_intp *)PyArray_GetPtr(params->augment, &aug_idx))) { | ||
_reshape_shape[i] = 1; | ||
aug_idx++; | ||
} else { | ||
_reshape_shape[i] = res_shape[res_idx]; | ||
res_idx++; | ||
if (PyArray_NDIM(input) != nd_in) { | ||
PyErr_SetString(PyExc_ValueError, "DimShuffle: Input has less dimensions than expected."); | ||
return 1; | ||
} | ||
} | ||
|
||
PyArray_Dims reshape_shape = {.ptr = _reshape_shape, .len = (int)N}; | ||
// Compute new dimensions and strides | ||
dimensions = (npy_intp*) malloc(nd_out * sizeof(npy_intp)); | ||
strides = (npy_intp*) malloc(nd_out * sizeof(npy_intp)); | ||
if (dimensions == NULL || strides == NULL) { | ||
PyErr_NoMemory(); | ||
free(dimensions); | ||
free(strides); | ||
return 1; | ||
}; | ||
|
||
npy_intp original_size = PyArray_SIZE(input); | ||
npy_intp new_size = 1; | ||
for (npy_intp i = 0; i < nd_out; ++i) { | ||
if (new_order[i] != -1) { | ||
dimensions[i] = PyArray_DIMS(input)[new_order[i]]; | ||
strides[i] = PyArray_DIMS(input)[new_order[i]] == 1 ? 0 : PyArray_STRIDES(input)[new_order[i]]; | ||
} else { | ||
dimensions[i] = 1; | ||
strides[i] = 0; | ||
} | ||
new_size *= dimensions[i]; | ||
} | ||
|
||
/* res = res.reshape(shape) */ | ||
*res = (PyArrayObject *)PyArray_Newshape(transposed_input, &reshape_shape, | ||
NPY_CORDER); | ||
if (original_size != new_size) { | ||
PyErr_SetString(PyExc_ValueError, "DimShuffle: Attempting to squeeze axes with size not equal to one."); | ||
free(dimensions); | ||
free(strides); | ||
return 1; | ||
} | ||
|
||
Py_DECREF(transposed_input); | ||
if (*res) | ||
Py_XDECREF(*res); | ||
|
||
// Create the new array. | ||
*res = (PyArrayObject*)PyArray_New(&PyArray_Type, nd_out, dimensions, | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyArray_TYPE(input), strides, | ||
PyArray_DATA(input), PyArray_ITEMSIZE(input), | ||
// borrow only the writable flag from the base | ||
// the NPY_OWNDATA flag will default to 0. | ||
(NPY_ARRAY_WRITEABLE * PyArray_ISWRITEABLE(input)), | ||
NULL); | ||
|
||
if (*res == NULL) { | ||
free(dimensions); | ||
free(strides); | ||
return 1; | ||
} | ||
|
||
PyDimMem_FREE(reshape_shape.ptr); | ||
// Declare it a view of the original input | ||
Py_INCREF((PyObject*)input); | ||
PyArray_SetBaseObject(*res, (PyObject*)input); | ||
|
||
if (!*res) { | ||
return 1; | ||
} | ||
// recalculate flags: CONTIGUOUS, FORTRAN, ALIGNED | ||
PyArray_UpdateFlags(*res, NPY_ARRAY_UPDATE_ALL); | ||
|
||
return 0; | ||
} | ||
free(strides); | ||
free(dimensions); | ||
return 0; | ||
} |
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
Oops, something went wrong.
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.
Actually I don't know why we're using the externalCOp approach for this. It used to be a normal COp before, and then you didn't need this sort of alloc/params stuff...
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.
https://github.com/Theano/Theano/pull/6174/files#diff-5a53b33cd2901f5a5b47bb37ede6013047a77f63f34dc3eeecd79676e875d924