|
1 | 1 | #section support_code_apply |
2 | 2 |
|
3 | | -int APPLY_SPECIFIC(cpu_dimshuffle)(PyArrayObject* input, PyArrayObject** res, PARAMS_TYPE* params) { |
4 | | - npy_bool* input_broadcastable; |
5 | | - npy_int64* new_order; |
6 | | - npy_intp nd_in; |
7 | | - npy_intp nd_out; |
8 | | - PyArrayObject* basename; |
9 | | - npy_intp* dimensions; |
10 | | - npy_intp* strides; |
11 | | - |
12 | | - if (!PyArray_IS_C_CONTIGUOUS(params->input_broadcastable)) { |
13 | | - PyErr_SetString(PyExc_RuntimeError, "DimShuffle: param input_broadcastable must be C-contiguous."); |
14 | | - return 1; |
15 | | - } |
16 | | - if (!PyArray_IS_C_CONTIGUOUS(params->_new_order)) { |
17 | | - PyErr_SetString(PyExc_RuntimeError, "DimShuffle: param _new_order must be C-contiguous."); |
18 | | - return 1; |
19 | | - } |
20 | | - input_broadcastable = (npy_bool*) PyArray_DATA(params->input_broadcastable); |
21 | | - new_order = (npy_int64*) PyArray_DATA(params->_new_order); |
22 | | - nd_in = PyArray_SIZE(params->input_broadcastable); |
23 | | - nd_out = PyArray_SIZE(params->_new_order); |
24 | | - |
25 | | - /* check_input_nd */ |
26 | | - if (PyArray_NDIM(input) != nd_in) { |
27 | | - PyErr_SetString(PyExc_NotImplementedError, "input nd"); |
28 | | - return 1; |
29 | | - } |
| 3 | +int APPLY_SPECIFIC(cpu_dimshuffle)(PyArrayObject *input, PyArrayObject **res, |
| 4 | + PARAMS_TYPE *params) { |
30 | 5 |
|
31 | | - /* clear_output */ |
32 | | - if (*res) |
33 | | - Py_XDECREF(*res); |
| 6 | + // This points to either the original input or a copy we create below. |
| 7 | + // Either way, this is what we should be working on/with. |
| 8 | + PyArrayObject *_input; |
34 | 9 |
|
35 | | - /* get_base */ |
36 | | - if (params->inplace) { |
37 | | - basename = input; |
38 | | - Py_INCREF((PyObject*)basename); |
39 | | - } else { |
40 | | - basename = |
41 | | - (PyArrayObject*)PyArray_FromAny((PyObject*)input, |
42 | | - NULL, 0, 0, NPY_ARRAY_ALIGNED|NPY_ARRAY_ENSURECOPY, NULL); |
43 | | - } |
| 10 | + if (*res) |
| 11 | + Py_XDECREF(*res); |
44 | 12 |
|
45 | | - /* shape_statements and strides_statements */ |
46 | | - dimensions = (npy_intp*) malloc(nd_out * sizeof(npy_intp)); |
47 | | - strides = (npy_intp*) malloc(nd_out * sizeof(npy_intp)); |
48 | | - if (dimensions == NULL || strides == NULL) { |
49 | | - PyErr_NoMemory(); |
50 | | - free(dimensions); |
51 | | - free(strides); |
52 | | - return 1; |
53 | | - }; |
54 | | - |
55 | | - for (npy_intp i = 0; i < nd_out; ++i) { |
56 | | - if (new_order[i] != -1) { |
57 | | - dimensions[i] = PyArray_DIMS(basename)[new_order[i]]; |
58 | | - strides[i] = PyArray_DIMS(basename)[new_order[i]] == 1 ? |
59 | | - 0 : PyArray_STRIDES(basename)[new_order[i]]; |
60 | | - } else { |
61 | | - dimensions[i] = 1; |
62 | | - strides[i] = 0; |
63 | | - } |
64 | | - } |
| 13 | + if (params->inplace) { |
| 14 | + _input = input; |
| 15 | + Py_INCREF((PyObject *)_input); |
| 16 | + } else { |
| 17 | + _input = (PyArrayObject *)PyArray_FromAny( |
| 18 | + (PyObject *)input, NULL, 0, 0, NPY_ARRAY_ALIGNED | NPY_ARRAY_ENSURECOPY, |
| 19 | + NULL); |
| 20 | + } |
65 | 21 |
|
66 | | - /* set the strides of the broadcasted dimensions. |
67 | | - * This algorithm is from numpy: PyArray_Newshape() in |
68 | | - * cvs/numpy/numpy/core/src/multiarraymodule.c */ |
69 | | - if (nd_out > 0) { |
70 | | - if (strides[nd_out - 1] == 0) |
71 | | - strides[nd_out - 1] = PyArray_DESCR(basename)->elsize; |
72 | | - for (npy_intp i = nd_out - 2; i > -1; --i) { |
73 | | - if (strides[i] == 0) |
74 | | - strides[i] = strides[i + 1] * dimensions[i + 1]; |
75 | | - } |
76 | | - } |
| 22 | + PyArray_Dims permute; |
| 23 | + |
| 24 | + if (!PyArray_IntpConverter((PyObject *)params->transposition, &permute)) { |
| 25 | + return 1; |
| 26 | + } |
77 | 27 |
|
78 | | - /* close_bracket */ |
79 | | - // create a new array. |
80 | | - *res = (PyArrayObject*)PyArray_New(&PyArray_Type, nd_out, dimensions, |
81 | | - PyArray_TYPE(basename), strides, |
82 | | - PyArray_DATA(basename), PyArray_ITEMSIZE(basename), |
83 | | - // borrow only the writable flag from the base |
84 | | - // the NPY_OWNDATA flag will default to 0. |
85 | | - (NPY_ARRAY_WRITEABLE * PyArray_ISWRITEABLE(basename)), |
86 | | - NULL); |
87 | | - |
88 | | - if (*res == NULL) { |
89 | | - free(dimensions); |
90 | | - free(strides); |
91 | | - return 1; |
| 28 | + /* |
| 29 | + res = res.transpose(self.transposition) |
| 30 | + */ |
| 31 | + PyArrayObject *transposed_input = |
| 32 | + (PyArrayObject *)PyArray_Transpose(_input, &permute); |
| 33 | + |
| 34 | + PyDimMem_FREE(permute.ptr); |
| 35 | + |
| 36 | + npy_intp *res_shape = PyArray_DIMS(transposed_input); |
| 37 | + npy_intp N_shuffle = PyArray_SIZE(params->shuffle); |
| 38 | + npy_intp N_augment = PyArray_SIZE(params->augment); |
| 39 | + npy_intp N = N_augment + N_shuffle; |
| 40 | + npy_intp *_reshape_shape = (npy_intp *)malloc(N * sizeof(npy_intp)); |
| 41 | + |
| 42 | + if (_reshape_shape == NULL) { |
| 43 | + PyErr_NoMemory(); |
| 44 | + free(_reshape_shape); |
| 45 | + return 1; |
| 46 | + } |
| 47 | + |
| 48 | + /* |
| 49 | + shape = list(res.shape[: len(self.shuffle)]) |
| 50 | + for augm in self.augment: |
| 51 | + shape.insert(augm, 1) |
| 52 | + */ |
| 53 | + npy_intp aug_idx = 0; |
| 54 | + int res_idx = 0; |
| 55 | + for (npy_intp i = 0; i < N; i++) { |
| 56 | + if (aug_idx < N_augment && |
| 57 | + i == *((npy_intp *)PyArray_GetPtr(params->augment, &aug_idx))) { |
| 58 | + _reshape_shape[i] = 1; |
| 59 | + aug_idx++; |
| 60 | + } else { |
| 61 | + _reshape_shape[i] = res_shape[res_idx]; |
| 62 | + res_idx++; |
92 | 63 | } |
| 64 | + } |
| 65 | + |
| 66 | + PyArray_Dims reshape_shape = {.ptr = _reshape_shape, .len = (int)N}; |
| 67 | + |
| 68 | + /* res = res.reshape(shape) */ |
| 69 | + *res = (PyArrayObject *)PyArray_Newshape(transposed_input, &reshape_shape, |
| 70 | + NPY_CORDER); |
93 | 71 |
|
94 | | - // recalculate flags: CONTIGUOUS, FORTRAN, ALIGNED |
95 | | - PyArray_UpdateFlags(*res, NPY_ARRAY_UPDATE_ALL); |
| 72 | + /* Py_XDECREF(transposed_input); */ |
96 | 73 |
|
97 | | - // we are making a view in both inplace and non-inplace cases |
98 | | - PyArray_SetBaseObject(*res, (PyObject*)basename); |
| 74 | + PyDimMem_FREE(reshape_shape.ptr); |
99 | 75 |
|
100 | | - free(strides); |
101 | | - free(dimensions); |
| 76 | + if (!*res) { |
| 77 | + return 1; |
| 78 | + } |
102 | 79 |
|
103 | | - return 0; |
| 80 | + return 0; |
104 | 81 | } |
0 commit comments