|
| 1 | +#define PY_SSIZE_T_CLEAN |
| 2 | +#include <Python.h> |
| 3 | +#include <numpy/arrayobject.h> |
| 4 | +#include <stdbool.h> |
| 5 | + |
| 6 | +#include "affine.h" |
| 7 | +#include "elementwise_univariate.h" |
| 8 | +#include "expr.h" |
| 9 | + |
| 10 | +// Capsule name for expr* pointers |
| 11 | +#define EXPR_CAPSULE_NAME "DNLP_EXPR" |
| 12 | + |
| 13 | +static int ensure_numpy(void) |
| 14 | +{ |
| 15 | + import_array(); |
| 16 | + return 0; |
| 17 | +} |
| 18 | + |
| 19 | +static void expr_capsule_destructor(PyObject *capsule) |
| 20 | +{ |
| 21 | + expr *node = (expr *) PyCapsule_GetPointer(capsule, EXPR_CAPSULE_NAME); |
| 22 | + if (node) |
| 23 | + { |
| 24 | + free_expr(node); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +static PyObject *py_make_variable(PyObject *self, PyObject *args) |
| 29 | +{ |
| 30 | + int d1, d2, var_id, n_vars; |
| 31 | + if (!PyArg_ParseTuple(args, "iiii", &d1, &d2, &var_id, &n_vars)) |
| 32 | + { |
| 33 | + return NULL; |
| 34 | + } |
| 35 | + |
| 36 | + expr *node = new_variable(d1, d2, var_id, n_vars); |
| 37 | + if (!node) |
| 38 | + { |
| 39 | + PyErr_SetString(PyExc_RuntimeError, "failed to create variable node"); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor); |
| 43 | +} |
| 44 | + |
| 45 | +static PyObject *py_make_log(PyObject *self, PyObject *args) |
| 46 | +{ |
| 47 | + PyObject *child_capsule; |
| 48 | + if (!PyArg_ParseTuple(args, "O", &child_capsule)) |
| 49 | + { |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME); |
| 53 | + if (!child) |
| 54 | + { |
| 55 | + PyErr_SetString(PyExc_ValueError, "invalid child capsule"); |
| 56 | + return NULL; |
| 57 | + } |
| 58 | + |
| 59 | + expr *node = new_log(child); |
| 60 | + if (!node) |
| 61 | + { |
| 62 | + PyErr_SetString(PyExc_RuntimeError, "failed to create log node"); |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor); |
| 66 | +} |
| 67 | + |
| 68 | +static PyObject *py_forward(PyObject *self, PyObject *args) |
| 69 | +{ |
| 70 | + PyObject *node_capsule; |
| 71 | + PyObject *u_obj; |
| 72 | + if (!PyArg_ParseTuple(args, "OO", &node_capsule, &u_obj)) |
| 73 | + { |
| 74 | + return NULL; |
| 75 | + } |
| 76 | + |
| 77 | + expr *node = (expr *) PyCapsule_GetPointer(node_capsule, EXPR_CAPSULE_NAME); |
| 78 | + if (!node) |
| 79 | + { |
| 80 | + PyErr_SetString(PyExc_ValueError, "invalid node capsule"); |
| 81 | + return NULL; |
| 82 | + } |
| 83 | + |
| 84 | + PyArrayObject *u_array = |
| 85 | + (PyArrayObject *) PyArray_FROM_OTF(u_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); |
| 86 | + if (!u_array) |
| 87 | + { |
| 88 | + return NULL; |
| 89 | + } |
| 90 | + |
| 91 | + node->forward(node, (const double *) PyArray_DATA(u_array)); |
| 92 | + |
| 93 | + npy_intp size = node->size; |
| 94 | + PyObject *out = PyArray_SimpleNew(1, &size, NPY_DOUBLE); |
| 95 | + if (!out) |
| 96 | + { |
| 97 | + Py_DECREF(u_array); |
| 98 | + return NULL; |
| 99 | + } |
| 100 | + memcpy(PyArray_DATA((PyArrayObject *) out), node->value, size * sizeof(double)); |
| 101 | + |
| 102 | + Py_DECREF(u_array); |
| 103 | + return out; |
| 104 | +} |
| 105 | + |
| 106 | +static PyMethodDef DNLPMethods[] = { |
| 107 | + {"make_variable", py_make_variable, METH_VARARGS, "Create variable node"}, |
| 108 | + {"make_log", py_make_log, METH_VARARGS, "Create log node"}, |
| 109 | + {"forward", py_forward, METH_VARARGS, "Run forward pass and return values"}, |
| 110 | + {NULL, NULL, 0, NULL}}; |
| 111 | + |
| 112 | +static struct PyModuleDef dnlp_module = {PyModuleDef_HEAD_INIT, "DNLP_diff_engine", |
| 113 | + NULL, -1, DNLPMethods}; |
| 114 | + |
| 115 | +PyMODINIT_FUNC PyInit_DNLP_diff_engine(void) |
| 116 | +{ |
| 117 | + if (ensure_numpy() < 0) return NULL; |
| 118 | + return PyModule_Create(&dnlp_module); |
| 119 | +} |
0 commit comments