aboutsummaryrefslogtreecommitdiff
path: root/ethosu/mlw_codec/mlw_codecmodule.c
blob: 1f172eede441c8e5181a971cb8038c96ea51091c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * SPDX-FileCopyrightText: Copyright 2020-2021, 2023-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <numpy/ndarrayobject.h>

#include "mlw_decode.h"
#include "mlw_encode.h"

/* C extension wrapper for mlw_reorder_encode
 *
 * This method is exposed directly in python with the arguments with a
 * prototype of the form:
 *
 * output = mlw_codec.reorder_encode(
 *  ifm_ublock_depth,
 *  ofm_ublock_depth,
 *  input,
 *  ofm_block_depth,
 *  is_depthwise,
 *  is_partkernel,
 *  ifm_bitdepth,
 *  decomp_h,
 *  decomp_w,
 *  verbose=0)
 *
 * output: (bytearray, int)
 */

static PyObject *
method_reorder_encode (PyObject *self, PyObject *args)
{
    /* Object to hold the input integer list. */
    int ifm_ublock_depth;
    int ofm_ublock_depth;
    PyObject *input_object;
    int ofm_block_depth;
    int is_depthwise;
    int is_partkernel;
    int ifm_bitdepth;
    int decomp_h;
    int decomp_w;

    /* Object to hold the input verbosity integer, the verbose argument
     * is optional so defaulted to 0.
     */
    int verbose = 0;

    /* Arguments to the method are delivered as a tuple, unpack the
     * tuple to get the individual arguments, note the second is
     * optional.
     */
    if (!PyArg_ParseTuple(args, "iiOiiiiii|i",
        &ifm_ublock_depth,
        &ofm_ublock_depth,
        &input_object,
        &ofm_block_depth,
        &is_depthwise,
        &is_partkernel,
        &ifm_bitdepth,
        &decomp_h,
        &decomp_w,
        &verbose))
        return NULL;

    PyArrayObject* input_ndarray_object = (PyArrayObject*)PyArray_FROM_OTF(
        input_object,
        NPY_INT16,
        NPY_ARRAY_ALIGNED);
    if (input_ndarray_object == NULL)
    {
        PyErr_SetString(PyExc_ValueError, "Invalid input array");
        return NULL;
    }

    if ((int)PyArray_NDIM(input_ndarray_object) < 4)
    {
        PyErr_SetString(PyExc_ValueError, "Invalid input shape");
        return NULL;
    }

    int ofm_depth = (int)PyArray_DIM(input_ndarray_object, 0);
    int kernel_height = (int)PyArray_DIM(input_ndarray_object, 1);
    int kernel_width = (int)PyArray_DIM(input_ndarray_object, 2);
    int ifm_depth = (int)PyArray_DIM(input_ndarray_object, 3);

    int16_t* brick_weights = (int16_t*)PyArray_DATA(input_ndarray_object);
    int brick_strides[4];
    for (int i = 0; i < 4; i++)
    {
        int stride = (int)PyArray_STRIDE(input_ndarray_object, i);
        if (stride % sizeof(int16_t))
        {
            PyErr_SetString(PyExc_ValueError, "Invalid stride");
            return NULL;
        }
        brick_strides[i] = stride / sizeof(int16_t);
    }
    if ((unsigned)PyArray_ITEMSIZE(input_ndarray_object) != sizeof(int16_t))
    {
        PyErr_SetString(PyExc_ValueError, "Invalid input type");
        return NULL;
    }
    uint8_t* output_buffer = NULL;
    int64_t padded_length;

    int output_length = mlw_reorder_encode(
        ifm_ublock_depth,
        ofm_ublock_depth,
        ofm_depth,
        kernel_height,
        kernel_width,
        ifm_depth,
        brick_strides,
        brick_weights,
        ofm_block_depth,
        is_depthwise,
        is_partkernel,
        ifm_bitdepth,
        decomp_h,
        decomp_w,
        &output_buffer,
        &padded_length,
        verbose);

    PyObject* ret = NULL;
    if ( output_length < 0 ) {
        ret = PyErr_NoMemory();
    } else {
        PyObject *output_byte_array = PyByteArray_FromStringAndSize((char*)output_buffer, output_length);
        PyObject *padded_length_obj = Py_BuildValue("i", padded_length);
        if ( output_byte_array && padded_length_obj ) {
            ret = PyTuple_Pack(2, output_byte_array, padded_length_obj);
        }
        Py_XDECREF(output_byte_array);
        Py_XDECREF(padded_length_obj);
    }

    /* Discard the output buffer */
    mlw_free_outbuf(output_buffer);

    Py_DECREF(input_ndarray_object);
    return ret;
}

/* C extension wrapper for mlw_encode
 *
 * This method is exposed directly in python with the arguments with a
 * prototype of the form:
 *
 * output = mlw_codec.encode(input, verbose=0)
 *
 * input: [int]
 * verbose: int
 * output: bytearray
 */

static PyObject *
method_encode (PyObject *self, PyObject *args)
{
  /* Object to hold the input integer list. */
  PyObject *input_list_object;

  /* Object to hold the input verbosity integer, the verbose argument
   * is optional so defaulted to 0.
   */
  int verbose = 0;

  /* Arguments to the method are delivered as a tuple, unpack the
   * tuple to get the individual arguments, note the second is
   * optional.
   */
  if (!PyArg_ParseTuple(args, "O|i", &input_list_object, &verbose))
    return NULL;

  /* Unpack the length of the input integer list.  */
  Py_ssize_t input_length = PyObject_Length (input_list_object);
  if (input_length < 0 || input_length > INT32_MAX) {
    return NULL;
  }

  /* We need to marshall the integer list into an input buffer
   * suitable for mlw_encode, use a temporary heap allocated buffer
   * for that purpose.
   */
  int16_t *input_buffer = (int16_t *) malloc(sizeof(int16_t *) * input_length);
  uint8_t *output_buffer = NULL;
  if (input_buffer == NULL)
    return PyErr_NoMemory();

  /* Unpack the input integer list into the temporary buffer.
   */
  for (int i = 0; i < input_length; i++)
    {
      PyObject *item;
      item = PyList_GetItem(input_list_object, i);
      long value = PyLong_AsLong(item);
      if (value < -255 || value > 255) {
        PyErr_SetString(PyExc_ValueError, "Input value out of bounds");
        return NULL;
      }
      input_buffer[i] = (int16_t)value;
    }
  if (PyErr_Occurred() != NULL) {
    PyErr_SetString(PyExc_ValueError, "Invalid input");
    return NULL;
  }

  int output_length = mlw_encode(input_buffer, (int)input_length, &output_buffer, verbose);

  PyObject *output_byte_array = output_length < 0 ? PyErr_NoMemory() :
    PyByteArray_FromStringAndSize ((char *) output_buffer, output_length);

  /* Discard the temporary input and output buffers.  */
  free (input_buffer);
  mlw_free_outbuf(output_buffer);

  return output_byte_array;
}

/* C extension wrapper for mlw_decode
 *
 * This method is exposed directly in python with the arguments with a
 * prototype of the form:
 *
 * output = mlw_codec.decode(input, verbose=0)
 *
 * input: bytearray
 * verbose: int
 * output: [int]
 */

static PyObject *
method_decode(PyObject *self, PyObject *args)
{
  /* Object to hold the input bytearray. */
  PyObject *input_bytearray_object;

  /* Object to hold the input verbosity integer, the verbose argument
   * is optional so defaulted to 0.
   */
  int verbose = 0;

  /* Arguments to the method are delivered as a tuple, unpack the
   * tuple to get the individual arguments, note the second is
   * optional.
   */
  if (!PyArg_ParseTuple(args, "Y|i", &input_bytearray_object, &verbose))
    return NULL;

  /* Unpack the input buffer and length from the bytearray object.  */
  uint8_t *input_buffer = (uint8_t *) PyByteArray_AsString(input_bytearray_object);
  Py_ssize_t input_length = PyByteArray_Size(input_bytearray_object);
  if (input_length < 0 || input_length > INT32_MAX) {
    return NULL;
  }

  /* We don't know the output length required, we guess, but the guess
   * will be too small, the mlw_decode call will do a resize (upwards)
   * anyway.
   */
  int16_t *output_buffer = (int16_t *) malloc (input_length);
  if (output_buffer == NULL)
    return PyErr_NoMemory();

  int output_length = mlw_decode (input_buffer, (int)input_length, &output_buffer, verbose);

  /* Construct a new integer list and marshall the output buffer
   * contents into the list.  */
  PyObject *output_list = PyList_New(output_length);
  for (int i = 0; i <output_length; i++)
    PyList_SetItem (output_list, i, PyLong_FromLong (output_buffer[i]));

  free (output_buffer);

  return output_list;
}

/* mlw_codec method descriptors.
 */

static PyMethodDef mlw_methods[] = {
    {"decode", method_decode, METH_VARARGS, "Python interface for decode"},
    {"encode", method_encode, METH_VARARGS, "Python interface for encode"},
    {"reorder_encode", method_reorder_encode, METH_VARARGS, "Python interface for reorder and encode"},
    {NULL, NULL, 0, NULL}
};

/* mlw_codec module descriptor.
 */

static struct PyModuleDef mlw_codecmodule = {
    PyModuleDef_HEAD_INIT,
    "mlw_codec",
    "Python interface for the mlw encoder",
    -1,
    mlw_methods
};

PyMODINIT_FUNC PyInit_mlw_codec(void)
{
    PyObject *ptype, *pvalue, *ptraceback;
    PyObject* ret = PyModule_Create(&mlw_codecmodule);
    if (_import_array() < 0)
    {
      // Fetch currently set error
      PyErr_Fetch(&ptype, &pvalue, &ptraceback);
      // Extract the error message
      const char *pStrErrorMessage = PyUnicode_AsUTF8(pvalue);
      // Re-format error message to start with "mlw_codec Error: " so it is
      // clearer it comes from mlw_codec.
      PyErr_Format(PyExc_RuntimeError, "mlw_codec error: %s", pStrErrorMessage);
      return NULL;
    }

    return ret;
}