Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #define PY_SSIZE_T_CLEAN |
| 20 | #include <Python.h> |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 21 | #include <numpy/ndarrayobject.h> |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 22 | |
| 23 | #include "mlw_decode.h" |
| 24 | #include "mlw_encode.h" |
| 25 | |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 26 | /* C extension wrapper for mlw_reorder_encode |
| 27 | * |
| 28 | * This method is exposed directly in python with the arguments with a |
| 29 | * prototype of the form: |
| 30 | * |
| 31 | * output = mlw_codec.reorder_encode( |
| 32 | * ifm_ublock_depth, |
| 33 | * ofm_ublock_depth, |
| 34 | * input, |
| 35 | * ofm_block_depth, |
| 36 | * is_depthwise, |
| 37 | * is_partkernel, |
| 38 | * ifm_bitdepth, |
| 39 | * decomp_h, |
| 40 | * decomp_w, |
| 41 | * verbose=0) |
| 42 | * |
Fredrik Svedberg | 93d5c35 | 2021-05-11 13:51:47 +0200 | [diff] [blame] | 43 | * output: (bytearray, int) |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | static PyObject * |
| 47 | method_reorder_encode (PyObject *self, PyObject *args) |
| 48 | { |
| 49 | /* Object to hold the input integer list. */ |
| 50 | int ifm_ublock_depth; |
| 51 | int ofm_ublock_depth; |
| 52 | PyObject *input_object; |
| 53 | int ofm_block_depth; |
| 54 | int is_depthwise; |
| 55 | int is_partkernel; |
| 56 | int ifm_bitdepth; |
| 57 | int decomp_h; |
| 58 | int decomp_w; |
| 59 | |
| 60 | /* Object to hold the input verbosity integer, the verbose argument |
| 61 | * is optional so defaulted to 0. |
| 62 | */ |
| 63 | int verbose = 0; |
| 64 | |
| 65 | /* Arguments to the method are delivered as a tuple, unpack the |
| 66 | * tuple to get the individual arguments, note the second is |
| 67 | * optional. |
| 68 | */ |
| 69 | if (!PyArg_ParseTuple(args, "iiOiiiiii|i", |
| 70 | &ifm_ublock_depth, |
| 71 | &ofm_ublock_depth, |
| 72 | &input_object, |
| 73 | &ofm_block_depth, |
| 74 | &is_depthwise, |
| 75 | &is_partkernel, |
| 76 | &ifm_bitdepth, |
| 77 | &decomp_h, |
| 78 | &decomp_w, |
| 79 | &verbose)) |
| 80 | return NULL; |
| 81 | |
Fredrik Svedberg | 93d5c35 | 2021-05-11 13:51:47 +0200 | [diff] [blame] | 82 | PyArrayObject* input_ndarray_object = (PyArrayObject*)PyArray_FROM_OTF( |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 83 | input_object, |
Mauricio Briceno | 3e4168d | 2021-06-09 09:49:05 +0200 | [diff] [blame] | 84 | NPY_INT16, |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 85 | NPY_ARRAY_ALIGNED); |
| 86 | if (input_ndarray_object == NULL) |
| 87 | { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | if ((int)PyArray_NDIM(input_ndarray_object) < 4) |
| 92 | { |
| 93 | PyErr_SetString(PyExc_ValueError, "Invalid input shape"); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | int ofm_depth = (int)PyArray_DIM(input_ndarray_object, 0); |
| 98 | int kernel_height = (int)PyArray_DIM(input_ndarray_object, 1); |
| 99 | int kernel_width = (int)PyArray_DIM(input_ndarray_object, 2); |
| 100 | int ifm_depth = (int)PyArray_DIM(input_ndarray_object, 3); |
| 101 | |
Mauricio Briceno | 3e4168d | 2021-06-09 09:49:05 +0200 | [diff] [blame] | 102 | int16_t* brick_weights = (int16_t*)PyArray_DATA(input_ndarray_object); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 103 | int brick_strides[4]; |
| 104 | for (int i = 0; i < 4; i++) |
| 105 | { |
Mauricio Briceno | 3e4168d | 2021-06-09 09:49:05 +0200 | [diff] [blame] | 106 | int stride = (int)PyArray_STRIDE(input_ndarray_object, i); |
| 107 | if (stride % sizeof(int16_t)) |
| 108 | { |
| 109 | PyErr_SetString(PyExc_ValueError, "Invalid stride"); |
| 110 | return NULL; |
| 111 | } |
| 112 | brick_strides[i] = stride / sizeof(int16_t); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 113 | } |
Mauricio Briceno | 3e4168d | 2021-06-09 09:49:05 +0200 | [diff] [blame] | 114 | if ((unsigned)PyArray_ITEMSIZE(input_ndarray_object) != sizeof(int16_t)) |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 115 | { |
| 116 | PyErr_SetString(PyExc_ValueError, "Invalid input type"); |
| 117 | return NULL; |
| 118 | } |
| 119 | uint8_t* output_buffer = NULL; |
Fredrik Svedberg | 93d5c35 | 2021-05-11 13:51:47 +0200 | [diff] [blame] | 120 | int64_t padded_length; |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 121 | |
| 122 | int output_length = mlw_reorder_encode( |
| 123 | ifm_ublock_depth, |
| 124 | ofm_ublock_depth, |
| 125 | ofm_depth, |
| 126 | kernel_height, |
| 127 | kernel_width, |
| 128 | ifm_depth, |
| 129 | brick_strides, |
| 130 | brick_weights, |
| 131 | ofm_block_depth, |
| 132 | is_depthwise, |
| 133 | is_partkernel, |
| 134 | ifm_bitdepth, |
| 135 | decomp_h, |
| 136 | decomp_w, |
| 137 | &output_buffer, |
| 138 | &padded_length, |
| 139 | verbose); |
| 140 | |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 141 | PyObject *output_byte_array = PyByteArray_FromStringAndSize((char*)output_buffer, output_length); |
| 142 | PyObject *padded_length_obj = Py_BuildValue("i", padded_length); |
| 143 | |
| 144 | /* Discard the output buffer */ |
| 145 | mlw_free_outbuf(output_buffer); |
| 146 | |
| 147 | PyObject* ret = PyTuple_Pack(2, output_byte_array, padded_length_obj); |
Fredrik Svedberg | 93d5c35 | 2021-05-11 13:51:47 +0200 | [diff] [blame] | 148 | |
| 149 | Py_DECREF(input_ndarray_object); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 150 | Py_DECREF(output_byte_array); |
| 151 | Py_DECREF(padded_length_obj); |
| 152 | return ret; |
| 153 | } |
| 154 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 155 | /* C extension wrapper for mlw_encode |
| 156 | * |
| 157 | * This method is exposed directly in python with the arguments with a |
| 158 | * prototype of the form: |
| 159 | * |
| 160 | * output = mlw_codec.encode(input, verbose=0) |
| 161 | * |
| 162 | * input: [int] |
| 163 | * verbose: int |
| 164 | * output: bytearray |
| 165 | */ |
| 166 | |
| 167 | static PyObject * |
| 168 | method_encode (PyObject *self, PyObject *args) |
| 169 | { |
| 170 | /* Object to hold the input integer list. */ |
| 171 | PyObject *input_list_object; |
| 172 | |
| 173 | /* Object to hold the input verbosity integer, the verbose argument |
| 174 | * is optional so defaulted to 0. |
| 175 | */ |
| 176 | int verbose = 0; |
| 177 | |
| 178 | /* Arguments to the method are delivered as a tuple, unpack the |
| 179 | * tuple to get the individual arguments, note the second is |
| 180 | * optional. |
| 181 | */ |
| 182 | if (!PyArg_ParseTuple(args, "O|i", &input_list_object, &verbose)) |
| 183 | return NULL; |
| 184 | |
| 185 | /* Unpack the length of the input integer list. */ |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 186 | Py_ssize_t input_length = PyObject_Length (input_list_object); |
Fredrik Svedberg | 0e938a3 | 2021-05-20 11:13:00 +0200 | [diff] [blame] | 187 | if (input_length < 0 || input_length > INT32_MAX) { |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 190 | |
| 191 | /* We need to marshall the integer list into an input buffer |
| 192 | * suitable for mlw_encode, use a temporary heap allocated buffer |
| 193 | * for that purpose. |
| 194 | */ |
| 195 | int16_t *input_buffer = (int16_t *) malloc(sizeof(int16_t *) * input_length); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 196 | uint8_t *output_buffer = NULL; |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 197 | if (input_buffer == NULL) |
| 198 | return PyErr_NoMemory(); |
| 199 | |
| 200 | /* Unpack the input integer list into the temporary buffer. |
| 201 | */ |
| 202 | for (int i = 0; i < input_length; i++) |
| 203 | { |
| 204 | PyObject *item; |
| 205 | item = PyList_GetItem(input_list_object, i); |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 206 | long value = PyLong_AsLong(item); |
| 207 | if (value < -255 || value > 255) { |
| 208 | PyErr_SetString(PyExc_ValueError, "Input value out of bounds"); |
| 209 | return NULL; |
| 210 | } |
Fredrik Svedberg | 0e938a3 | 2021-05-20 11:13:00 +0200 | [diff] [blame] | 211 | input_buffer[i] = (int16_t)value; |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 212 | } |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 213 | if (PyErr_Occurred() != NULL) { |
| 214 | PyErr_SetString(PyExc_ValueError, "Invalid input"); |
| 215 | return NULL; |
| 216 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 217 | |
Fredrik Svedberg | 0e938a3 | 2021-05-20 11:13:00 +0200 | [diff] [blame] | 218 | int output_length = mlw_encode(input_buffer, (int)input_length, &output_buffer, verbose); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 219 | |
| 220 | PyObject *output_byte_array = PyByteArray_FromStringAndSize ((char *) output_buffer, output_length); |
| 221 | |
| 222 | /* Discard the temporary input and output buffers. */ |
| 223 | free (input_buffer); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 224 | mlw_free_outbuf(output_buffer); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 225 | |
| 226 | return output_byte_array; |
| 227 | } |
| 228 | |
| 229 | /* C extension wrapper for mlw_decode |
| 230 | * |
| 231 | * This method is exposed directly in python with the arguments with a |
| 232 | * prototype of the form: |
| 233 | * |
| 234 | * output = mlw_codec.decode(input, verbose=0) |
| 235 | * |
| 236 | * input: bytearray |
| 237 | * verbose: int |
| 238 | * output: [int] |
| 239 | */ |
| 240 | |
| 241 | static PyObject * |
| 242 | method_decode(PyObject *self, PyObject *args) |
| 243 | { |
| 244 | /* Object to hold the input bytearray. */ |
| 245 | PyObject *input_bytearray_object; |
| 246 | |
| 247 | /* Object to hold the input verbosity integer, the verbose argument |
| 248 | * is optional so defaulted to 0. |
| 249 | */ |
| 250 | int verbose = 0; |
| 251 | |
| 252 | /* Arguments to the method are delivered as a tuple, unpack the |
| 253 | * tuple to get the individual arguments, note the second is |
| 254 | * optional. |
| 255 | */ |
| 256 | if (!PyArg_ParseTuple(args, "Y|i", &input_bytearray_object, &verbose)) |
| 257 | return NULL; |
| 258 | |
| 259 | /* Unpack the input buffer and length from the bytearray object. */ |
| 260 | uint8_t *input_buffer = (uint8_t *) PyByteArray_AsString(input_bytearray_object); |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 261 | Py_ssize_t input_length = PyByteArray_Size(input_bytearray_object); |
Fredrik Svedberg | 0e938a3 | 2021-05-20 11:13:00 +0200 | [diff] [blame] | 262 | if (input_length < 0 || input_length > INT32_MAX) { |
| 263 | return NULL; |
| 264 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 265 | |
| 266 | /* We don't know the output length required, we guess, but the guess |
| 267 | * will be too small, the mlw_decode call will do a resize (upwards) |
| 268 | * anyway. |
| 269 | */ |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 270 | int16_t *output_buffer = (int16_t *) malloc (input_length); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 271 | if (output_buffer == NULL) |
| 272 | return PyErr_NoMemory(); |
| 273 | |
Fredrik Svedberg | 0e938a3 | 2021-05-20 11:13:00 +0200 | [diff] [blame] | 274 | int output_length = mlw_decode (input_buffer, (int)input_length, &output_buffer, verbose); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 275 | |
| 276 | /* Construct a new integer list and marshall the output buffer |
| 277 | * contents into the list. */ |
| 278 | PyObject *output_list = PyList_New(output_length); |
| 279 | for (int i = 0; i <output_length; i++) |
| 280 | PyList_SetItem (output_list, i, PyLong_FromLong (output_buffer[i])); |
| 281 | |
| 282 | free (output_buffer); |
| 283 | |
| 284 | return output_list; |
| 285 | } |
| 286 | |
| 287 | /* mlw_codec method descriptors. |
| 288 | */ |
| 289 | |
| 290 | static PyMethodDef mlw_methods[] = { |
| 291 | {"decode", method_decode, METH_VARARGS, "Python interface for decode"}, |
| 292 | {"encode", method_encode, METH_VARARGS, "Python interface for encode"}, |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 293 | {"reorder_encode", method_reorder_encode, METH_VARARGS, "Python interface for reorder and encode"}, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 294 | {NULL, NULL, 0, NULL} |
| 295 | }; |
| 296 | |
| 297 | /* mlw_codec module descriptor. |
| 298 | */ |
| 299 | |
| 300 | static struct PyModuleDef mlw_codecmodule = { |
| 301 | PyModuleDef_HEAD_INIT, |
| 302 | "mlw_codec", |
| 303 | "Python interface for the mlw encoder", |
| 304 | -1, |
| 305 | mlw_methods |
| 306 | }; |
| 307 | |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame] | 308 | PyMODINIT_FUNC PyInit_mlw_codec(void) |
| 309 | { |
| 310 | PyObject* ret = PyModule_Create(&mlw_codecmodule); |
| 311 | import_array(); |
| 312 | return ret; |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 313 | } |