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 | * |
| 43 | * output: bytearray |
| 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 | |
| 82 | PyArrayObject* input_ndarray_object = PyArray_FROM_OTF( |
| 83 | input_object, |
| 84 | NPY_INT64, |
| 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 | |
| 102 | int64_t* brick_weights = (int64_t*)PyArray_DATA(input_ndarray_object); |
| 103 | int brick_strides[4]; |
| 104 | for (int i = 0; i < 4; i++) |
| 105 | { |
| 106 | brick_strides[i] = (int)PyArray_STRIDE(input_ndarray_object, i); |
| 107 | } |
| 108 | if ((unsigned)PyArray_ITEMSIZE(input_ndarray_object) != sizeof(int64_t)) |
| 109 | { |
| 110 | PyErr_SetString(PyExc_ValueError, "Invalid input type"); |
| 111 | return NULL; |
| 112 | } |
| 113 | uint8_t* output_buffer = NULL; |
| 114 | int padded_length; |
| 115 | |
| 116 | int output_length = mlw_reorder_encode( |
| 117 | ifm_ublock_depth, |
| 118 | ofm_ublock_depth, |
| 119 | ofm_depth, |
| 120 | kernel_height, |
| 121 | kernel_width, |
| 122 | ifm_depth, |
| 123 | brick_strides, |
| 124 | brick_weights, |
| 125 | ofm_block_depth, |
| 126 | is_depthwise, |
| 127 | is_partkernel, |
| 128 | ifm_bitdepth, |
| 129 | decomp_h, |
| 130 | decomp_w, |
| 131 | &output_buffer, |
| 132 | &padded_length, |
| 133 | verbose); |
| 134 | |
| 135 | if (output_buffer == NULL) |
| 136 | { |
| 137 | return PyErr_NoMemory(); |
| 138 | } |
| 139 | |
| 140 | PyObject *output_byte_array = PyByteArray_FromStringAndSize((char*)output_buffer, output_length); |
| 141 | PyObject *padded_length_obj = Py_BuildValue("i", padded_length); |
| 142 | |
| 143 | /* Discard the output buffer */ |
| 144 | mlw_free_outbuf(output_buffer); |
| 145 | |
| 146 | PyObject* ret = PyTuple_Pack(2, output_byte_array, padded_length_obj); |
| 147 | Py_DECREF(output_byte_array); |
| 148 | Py_DECREF(padded_length_obj); |
| 149 | return ret; |
| 150 | } |
| 151 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 152 | /* C extension wrapper for mlw_encode |
| 153 | * |
| 154 | * This method is exposed directly in python with the arguments with a |
| 155 | * prototype of the form: |
| 156 | * |
| 157 | * output = mlw_codec.encode(input, verbose=0) |
| 158 | * |
| 159 | * input: [int] |
| 160 | * verbose: int |
| 161 | * output: bytearray |
| 162 | */ |
| 163 | |
| 164 | static PyObject * |
| 165 | method_encode (PyObject *self, PyObject *args) |
| 166 | { |
| 167 | /* Object to hold the input integer list. */ |
| 168 | PyObject *input_list_object; |
| 169 | |
| 170 | /* Object to hold the input verbosity integer, the verbose argument |
| 171 | * is optional so defaulted to 0. |
| 172 | */ |
| 173 | int verbose = 0; |
| 174 | |
| 175 | /* Arguments to the method are delivered as a tuple, unpack the |
| 176 | * tuple to get the individual arguments, note the second is |
| 177 | * optional. |
| 178 | */ |
| 179 | if (!PyArg_ParseTuple(args, "O|i", &input_list_object, &verbose)) |
| 180 | return NULL; |
| 181 | |
| 182 | /* Unpack the length of the input integer list. */ |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 183 | Py_ssize_t input_length = PyObject_Length (input_list_object); |
| 184 | if (input_length < 0) { |
| 185 | return NULL; |
| 186 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 187 | |
| 188 | /* We need to marshall the integer list into an input buffer |
| 189 | * suitable for mlw_encode, use a temporary heap allocated buffer |
| 190 | * for that purpose. |
| 191 | */ |
| 192 | int16_t *input_buffer = (int16_t *) malloc(sizeof(int16_t *) * input_length); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame^] | 193 | uint8_t *output_buffer = NULL; |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 194 | if (input_buffer == NULL) |
| 195 | return PyErr_NoMemory(); |
| 196 | |
| 197 | /* Unpack the input integer list into the temporary buffer. |
| 198 | */ |
| 199 | for (int i = 0; i < input_length; i++) |
| 200 | { |
| 201 | PyObject *item; |
| 202 | item = PyList_GetItem(input_list_object, i); |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 203 | long value = PyLong_AsLong(item); |
| 204 | if (value < -255 || value > 255) { |
| 205 | PyErr_SetString(PyExc_ValueError, "Input value out of bounds"); |
| 206 | return NULL; |
| 207 | } |
| 208 | input_buffer[i] = value; |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 209 | } |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 210 | if (PyErr_Occurred() != NULL) { |
| 211 | PyErr_SetString(PyExc_ValueError, "Invalid input"); |
| 212 | return NULL; |
| 213 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 214 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 215 | int output_length = mlw_encode(input_buffer, input_length, &output_buffer, verbose); |
| 216 | |
| 217 | PyObject *output_byte_array = PyByteArray_FromStringAndSize ((char *) output_buffer, output_length); |
| 218 | |
| 219 | /* Discard the temporary input and output buffers. */ |
| 220 | free (input_buffer); |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame^] | 221 | mlw_free_outbuf(output_buffer); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 222 | |
| 223 | return output_byte_array; |
| 224 | } |
| 225 | |
| 226 | /* C extension wrapper for mlw_decode |
| 227 | * |
| 228 | * This method is exposed directly in python with the arguments with a |
| 229 | * prototype of the form: |
| 230 | * |
| 231 | * output = mlw_codec.decode(input, verbose=0) |
| 232 | * |
| 233 | * input: bytearray |
| 234 | * verbose: int |
| 235 | * output: [int] |
| 236 | */ |
| 237 | |
| 238 | static PyObject * |
| 239 | method_decode(PyObject *self, PyObject *args) |
| 240 | { |
| 241 | /* Object to hold the input bytearray. */ |
| 242 | PyObject *input_bytearray_object; |
| 243 | |
| 244 | /* Object to hold the input verbosity integer, the verbose argument |
| 245 | * is optional so defaulted to 0. |
| 246 | */ |
| 247 | int verbose = 0; |
| 248 | |
| 249 | /* Arguments to the method are delivered as a tuple, unpack the |
| 250 | * tuple to get the individual arguments, note the second is |
| 251 | * optional. |
| 252 | */ |
| 253 | if (!PyArg_ParseTuple(args, "Y|i", &input_bytearray_object, &verbose)) |
| 254 | return NULL; |
| 255 | |
| 256 | /* Unpack the input buffer and length from the bytearray object. */ |
| 257 | uint8_t *input_buffer = (uint8_t *) PyByteArray_AsString(input_bytearray_object); |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 258 | Py_ssize_t input_length = PyByteArray_Size(input_bytearray_object); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 259 | |
| 260 | /* We don't know the output length required, we guess, but the guess |
| 261 | * will be too small, the mlw_decode call will do a resize (upwards) |
| 262 | * anyway. |
| 263 | */ |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 264 | int16_t *output_buffer = (int16_t *) malloc (input_length); |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 265 | if (output_buffer == NULL) |
| 266 | return PyErr_NoMemory(); |
| 267 | |
| 268 | int output_length = mlw_decode (input_buffer, input_length, &output_buffer, verbose); |
| 269 | |
| 270 | /* Construct a new integer list and marshall the output buffer |
| 271 | * contents into the list. */ |
| 272 | PyObject *output_list = PyList_New(output_length); |
| 273 | for (int i = 0; i <output_length; i++) |
| 274 | PyList_SetItem (output_list, i, PyLong_FromLong (output_buffer[i])); |
| 275 | |
| 276 | free (output_buffer); |
| 277 | |
| 278 | return output_list; |
| 279 | } |
| 280 | |
| 281 | /* mlw_codec method descriptors. |
| 282 | */ |
| 283 | |
| 284 | static PyMethodDef mlw_methods[] = { |
| 285 | {"decode", method_decode, METH_VARARGS, "Python interface for decode"}, |
| 286 | {"encode", method_encode, METH_VARARGS, "Python interface for encode"}, |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame^] | 287 | {"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] | 288 | {NULL, NULL, 0, NULL} |
| 289 | }; |
| 290 | |
| 291 | /* mlw_codec module descriptor. |
| 292 | */ |
| 293 | |
| 294 | static struct PyModuleDef mlw_codecmodule = { |
| 295 | PyModuleDef_HEAD_INIT, |
| 296 | "mlw_codec", |
| 297 | "Python interface for the mlw encoder", |
| 298 | -1, |
| 299 | mlw_methods |
| 300 | }; |
| 301 | |
Mauricio Briceno | 67e11f7 | 2021-05-05 12:47:28 +0200 | [diff] [blame^] | 302 | PyMODINIT_FUNC PyInit_mlw_codec(void) |
| 303 | { |
| 304 | PyObject* ret = PyModule_Create(&mlw_codecmodule); |
| 305 | import_array(); |
| 306 | return ret; |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 307 | } |