blob: 1f172eede441c8e5181a971cb8038c96ea51091c [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001/*
Fredrik Svedbergc222f8c2024-01-12 15:32:53 +01002 * SPDX-FileCopyrightText: Copyright 2020-2021, 2023-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Tim Hall79d07d22020-04-27 18:20:16 +01003 *
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 */
Tim Hall79d07d22020-04-27 18:20:16 +010018#define PY_SSIZE_T_CLEAN
19#include <Python.h>
Mauricio Briceno67e11f72021-05-05 12:47:28 +020020#include <numpy/ndarrayobject.h>
Tim Hall79d07d22020-04-27 18:20:16 +010021
22#include "mlw_decode.h"
23#include "mlw_encode.h"
24
Mauricio Briceno67e11f72021-05-05 12:47:28 +020025/* C extension wrapper for mlw_reorder_encode
26 *
27 * This method is exposed directly in python with the arguments with a
28 * prototype of the form:
29 *
30 * output = mlw_codec.reorder_encode(
31 * ifm_ublock_depth,
32 * ofm_ublock_depth,
33 * input,
34 * ofm_block_depth,
35 * is_depthwise,
36 * is_partkernel,
37 * ifm_bitdepth,
38 * decomp_h,
39 * decomp_w,
40 * verbose=0)
41 *
Fredrik Svedberg93d5c352021-05-11 13:51:47 +020042 * output: (bytearray, int)
Mauricio Briceno67e11f72021-05-05 12:47:28 +020043 */
44
45static PyObject *
46method_reorder_encode (PyObject *self, PyObject *args)
47{
48 /* Object to hold the input integer list. */
49 int ifm_ublock_depth;
50 int ofm_ublock_depth;
51 PyObject *input_object;
52 int ofm_block_depth;
53 int is_depthwise;
54 int is_partkernel;
55 int ifm_bitdepth;
56 int decomp_h;
57 int decomp_w;
58
59 /* Object to hold the input verbosity integer, the verbose argument
60 * is optional so defaulted to 0.
61 */
62 int verbose = 0;
63
64 /* Arguments to the method are delivered as a tuple, unpack the
65 * tuple to get the individual arguments, note the second is
66 * optional.
67 */
68 if (!PyArg_ParseTuple(args, "iiOiiiiii|i",
69 &ifm_ublock_depth,
70 &ofm_ublock_depth,
71 &input_object,
72 &ofm_block_depth,
73 &is_depthwise,
74 &is_partkernel,
75 &ifm_bitdepth,
76 &decomp_h,
77 &decomp_w,
78 &verbose))
79 return NULL;
80
Fredrik Svedberg93d5c352021-05-11 13:51:47 +020081 PyArrayObject* input_ndarray_object = (PyArrayObject*)PyArray_FROM_OTF(
Mauricio Briceno67e11f72021-05-05 12:47:28 +020082 input_object,
Mauricio Briceno3e4168d2021-06-09 09:49:05 +020083 NPY_INT16,
Mauricio Briceno67e11f72021-05-05 12:47:28 +020084 NPY_ARRAY_ALIGNED);
85 if (input_ndarray_object == NULL)
86 {
Fredrik Svedbergc222f8c2024-01-12 15:32:53 +010087 PyErr_SetString(PyExc_ValueError, "Invalid input array");
Mauricio Briceno67e11f72021-05-05 12:47:28 +020088 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 Briceno3e4168d2021-06-09 09:49:05 +0200102 int16_t* brick_weights = (int16_t*)PyArray_DATA(input_ndarray_object);
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200103 int brick_strides[4];
104 for (int i = 0; i < 4; i++)
105 {
Mauricio Briceno3e4168d2021-06-09 09:49:05 +0200106 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 Briceno67e11f72021-05-05 12:47:28 +0200113 }
Mauricio Briceno3e4168d2021-06-09 09:49:05 +0200114 if ((unsigned)PyArray_ITEMSIZE(input_ndarray_object) != sizeof(int16_t))
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200115 {
116 PyErr_SetString(PyExc_ValueError, "Invalid input type");
117 return NULL;
118 }
119 uint8_t* output_buffer = NULL;
Fredrik Svedberg93d5c352021-05-11 13:51:47 +0200120 int64_t padded_length;
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200121
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
Fredrik Svedbergc222f8c2024-01-12 15:32:53 +0100141 PyObject* ret = NULL;
142 if ( output_length < 0 ) {
143 ret = PyErr_NoMemory();
144 } else {
145 PyObject *output_byte_array = PyByteArray_FromStringAndSize((char*)output_buffer, output_length);
146 PyObject *padded_length_obj = Py_BuildValue("i", padded_length);
147 if ( output_byte_array && padded_length_obj ) {
148 ret = PyTuple_Pack(2, output_byte_array, padded_length_obj);
149 }
150 Py_XDECREF(output_byte_array);
151 Py_XDECREF(padded_length_obj);
152 }
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200153
154 /* Discard the output buffer */
155 mlw_free_outbuf(output_buffer);
156
Fredrik Svedberg93d5c352021-05-11 13:51:47 +0200157 Py_DECREF(input_ndarray_object);
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200158 return ret;
159}
160
Tim Hall79d07d22020-04-27 18:20:16 +0100161/* C extension wrapper for mlw_encode
162 *
163 * This method is exposed directly in python with the arguments with a
164 * prototype of the form:
165 *
166 * output = mlw_codec.encode(input, verbose=0)
167 *
168 * input: [int]
169 * verbose: int
170 * output: bytearray
171 */
172
173static PyObject *
174method_encode (PyObject *self, PyObject *args)
175{
176 /* Object to hold the input integer list. */
177 PyObject *input_list_object;
178
179 /* Object to hold the input verbosity integer, the verbose argument
180 * is optional so defaulted to 0.
181 */
182 int verbose = 0;
183
184 /* Arguments to the method are delivered as a tuple, unpack the
185 * tuple to get the individual arguments, note the second is
186 * optional.
187 */
188 if (!PyArg_ParseTuple(args, "O|i", &input_list_object, &verbose))
189 return NULL;
190
191 /* Unpack the length of the input integer list. */
Louis Verhaard60232142021-01-22 14:11:15 +0100192 Py_ssize_t input_length = PyObject_Length (input_list_object);
Fredrik Svedberg0e938a32021-05-20 11:13:00 +0200193 if (input_length < 0 || input_length > INT32_MAX) {
Louis Verhaard60232142021-01-22 14:11:15 +0100194 return NULL;
195 }
Tim Hall79d07d22020-04-27 18:20:16 +0100196
197 /* We need to marshall the integer list into an input buffer
198 * suitable for mlw_encode, use a temporary heap allocated buffer
199 * for that purpose.
200 */
201 int16_t *input_buffer = (int16_t *) malloc(sizeof(int16_t *) * input_length);
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200202 uint8_t *output_buffer = NULL;
Tim Hall79d07d22020-04-27 18:20:16 +0100203 if (input_buffer == NULL)
204 return PyErr_NoMemory();
205
206 /* Unpack the input integer list into the temporary buffer.
207 */
208 for (int i = 0; i < input_length; i++)
209 {
210 PyObject *item;
211 item = PyList_GetItem(input_list_object, i);
Louis Verhaard60232142021-01-22 14:11:15 +0100212 long value = PyLong_AsLong(item);
213 if (value < -255 || value > 255) {
214 PyErr_SetString(PyExc_ValueError, "Input value out of bounds");
215 return NULL;
216 }
Fredrik Svedberg0e938a32021-05-20 11:13:00 +0200217 input_buffer[i] = (int16_t)value;
Tim Hall79d07d22020-04-27 18:20:16 +0100218 }
Louis Verhaard60232142021-01-22 14:11:15 +0100219 if (PyErr_Occurred() != NULL) {
220 PyErr_SetString(PyExc_ValueError, "Invalid input");
221 return NULL;
222 }
Tim Hall79d07d22020-04-27 18:20:16 +0100223
Fredrik Svedberg0e938a32021-05-20 11:13:00 +0200224 int output_length = mlw_encode(input_buffer, (int)input_length, &output_buffer, verbose);
Tim Hall79d07d22020-04-27 18:20:16 +0100225
Fredrik Svedbergc222f8c2024-01-12 15:32:53 +0100226 PyObject *output_byte_array = output_length < 0 ? PyErr_NoMemory() :
227 PyByteArray_FromStringAndSize ((char *) output_buffer, output_length);
Tim Hall79d07d22020-04-27 18:20:16 +0100228
229 /* Discard the temporary input and output buffers. */
230 free (input_buffer);
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200231 mlw_free_outbuf(output_buffer);
Tim Hall79d07d22020-04-27 18:20:16 +0100232
233 return output_byte_array;
234}
235
236/* C extension wrapper for mlw_decode
237 *
238 * This method is exposed directly in python with the arguments with a
239 * prototype of the form:
240 *
241 * output = mlw_codec.decode(input, verbose=0)
242 *
243 * input: bytearray
244 * verbose: int
245 * output: [int]
246 */
247
248static PyObject *
249method_decode(PyObject *self, PyObject *args)
250{
251 /* Object to hold the input bytearray. */
252 PyObject *input_bytearray_object;
253
254 /* Object to hold the input verbosity integer, the verbose argument
255 * is optional so defaulted to 0.
256 */
257 int verbose = 0;
258
259 /* Arguments to the method are delivered as a tuple, unpack the
260 * tuple to get the individual arguments, note the second is
261 * optional.
262 */
263 if (!PyArg_ParseTuple(args, "Y|i", &input_bytearray_object, &verbose))
264 return NULL;
265
266 /* Unpack the input buffer and length from the bytearray object. */
267 uint8_t *input_buffer = (uint8_t *) PyByteArray_AsString(input_bytearray_object);
Louis Verhaard60232142021-01-22 14:11:15 +0100268 Py_ssize_t input_length = PyByteArray_Size(input_bytearray_object);
Fredrik Svedberg0e938a32021-05-20 11:13:00 +0200269 if (input_length < 0 || input_length > INT32_MAX) {
270 return NULL;
271 }
Tim Hall79d07d22020-04-27 18:20:16 +0100272
273 /* We don't know the output length required, we guess, but the guess
274 * will be too small, the mlw_decode call will do a resize (upwards)
275 * anyway.
276 */
Louis Verhaard60232142021-01-22 14:11:15 +0100277 int16_t *output_buffer = (int16_t *) malloc (input_length);
Tim Hall79d07d22020-04-27 18:20:16 +0100278 if (output_buffer == NULL)
279 return PyErr_NoMemory();
280
Fredrik Svedberg0e938a32021-05-20 11:13:00 +0200281 int output_length = mlw_decode (input_buffer, (int)input_length, &output_buffer, verbose);
Tim Hall79d07d22020-04-27 18:20:16 +0100282
283 /* Construct a new integer list and marshall the output buffer
284 * contents into the list. */
285 PyObject *output_list = PyList_New(output_length);
286 for (int i = 0; i <output_length; i++)
287 PyList_SetItem (output_list, i, PyLong_FromLong (output_buffer[i]));
288
289 free (output_buffer);
290
291 return output_list;
292}
293
294/* mlw_codec method descriptors.
295 */
296
297static PyMethodDef mlw_methods[] = {
298 {"decode", method_decode, METH_VARARGS, "Python interface for decode"},
299 {"encode", method_encode, METH_VARARGS, "Python interface for encode"},
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200300 {"reorder_encode", method_reorder_encode, METH_VARARGS, "Python interface for reorder and encode"},
Tim Hall79d07d22020-04-27 18:20:16 +0100301 {NULL, NULL, 0, NULL}
302};
303
304/* mlw_codec module descriptor.
305 */
306
307static struct PyModuleDef mlw_codecmodule = {
308 PyModuleDef_HEAD_INIT,
309 "mlw_codec",
310 "Python interface for the mlw encoder",
311 -1,
312 mlw_methods
313};
314
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200315PyMODINIT_FUNC PyInit_mlw_codec(void)
316{
Raul Farkas428a8d52023-01-16 16:52:18 +0000317 PyObject *ptype, *pvalue, *ptraceback;
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200318 PyObject* ret = PyModule_Create(&mlw_codecmodule);
Raul Farkas428a8d52023-01-16 16:52:18 +0000319 if (_import_array() < 0)
320 {
321 // Fetch currently set error
322 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
323 // Extract the error message
324 const char *pStrErrorMessage = PyUnicode_AsUTF8(pvalue);
325 // Re-format error message to start with "mlw_codec Error: " so it is
326 // clearer it comes from mlw_codec.
327 PyErr_Format(PyExc_RuntimeError, "mlw_codec error: %s", pStrErrorMessage);
328 return NULL;
329 }
330
Mauricio Briceno67e11f72021-05-05 12:47:28 +0200331 return ret;
Tim Hall79d07d22020-04-27 18:20:16 +0100332}