Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions sparsediffpy/_bindings/problem/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <numpy/arrayobject.h>

#include "problem.h"
#include "subexpr.h"

/* Also need expr types for capsule handling */
#include "../atoms/common.h"
Expand All @@ -21,4 +22,29 @@ static void problem_capsule_destructor(PyObject *capsule)
}
}

/* Convert obj to a contiguous double array of exactly n entries (at least n if
* at_least is set). Returns a new reference, or NULL with an exception set. The
* engine reads these buffers without knowing their length, so a short array
* would make it read past the end. */
static PyArrayObject *double_array_of_size(PyObject *obj, npy_intp n, int at_least,
const char *name)
{
PyArrayObject *arr =
(PyArrayObject *) PyArray_FROM_OTF(obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!arr)
{
return NULL;
}
npy_intp size = PyArray_SIZE(arr);
if (at_least ? size < n : size != n)
{
PyErr_Format(PyExc_ValueError, "%s must have %s%zd entries, got %zd", name,
at_least ? "at least " : "", (Py_ssize_t) n,
(Py_ssize_t) size);
Py_DECREF(arr);
return NULL;
}
return arr;
}

#endif /* PROBLEM_COMMON_H */
3 changes: 1 addition & 2 deletions sparsediffpy/_bindings/problem/constraint_forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ static PyObject *py_problem_constraint_forward(PyObject *self, PyObject *args)
return NULL;
}

PyArrayObject *u_array =
(PyArrayObject *) PyArray_FROM_OTF(u_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
PyArrayObject *u_array = double_array_of_size(u_obj, prob->n_vars, 0, "u");
if (!u_array)
{
return NULL;
Expand Down
6 changes: 6 additions & 0 deletions sparsediffpy/_bindings/problem/gradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ static PyObject *py_problem_gradient(PyObject *self, PyObject *args)
return NULL;
}

if (!prob->jacobian)
{
PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first");
return NULL;
}

problem_gradient(prob);

npy_intp size = prob->n_vars;
Expand Down
21 changes: 17 additions & 4 deletions sparsediffpy/_bindings/problem/hessian.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ static PyObject *py_problem_hessian(PyObject *self, PyObject *args)
return NULL;
}

if (!prob->lagrange_hessian)
{
PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first");
return NULL;
}

/* Convert lagrange to contiguous C array */
PyArrayObject *lagrange_arr = (PyArrayObject *) PyArray_FROM_OTF(
lagrange_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
PyArrayObject *lagrange_arr = double_array_of_size(
lagrange_obj, prob->total_constraint_size, 0, "lagrange");
if (!lagrange_arr)
{
return NULL;
Expand Down Expand Up @@ -182,9 +188,16 @@ static PyObject *py_problem_eval_hessian_vals_coo(PyObject *self, PyObject *args
return NULL;
}

if (!prob->lagrange_hessian_coo)
{
PyErr_SetString(PyExc_RuntimeError,
"call problem_init_hessian_coo_lower_triangular first");
return NULL;
}

/* Convert lagrange to contiguous C array */
PyArrayObject *lagrange_arr = (PyArrayObject *) PyArray_FROM_OTF(
lagrange_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
PyArrayObject *lagrange_arr = double_array_of_size(
lagrange_obj, prob->total_constraint_size, 0, "lagrange");
if (!lagrange_arr)
{
return NULL;
Expand Down
12 changes: 12 additions & 0 deletions sparsediffpy/_bindings/problem/jacobian.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ static PyObject *py_problem_jacobian(PyObject *self, PyObject *args)
return NULL;
}

if (!prob->jacobian)
{
PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first");
return NULL;
}

if (prob->n_constraints == 0)
{
/* Return empty CSR components */
Expand Down Expand Up @@ -161,6 +167,12 @@ static PyObject *py_problem_eval_jacobian_vals(PyObject *self, PyObject *args)
return NULL;
}

if (!prob->jacobian)
{
PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first");
return NULL;
}

problem_jacobian(prob);

CSR_matrix *jac = prob->jacobian;
Expand Down
3 changes: 1 addition & 2 deletions sparsediffpy/_bindings/problem/objective_forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ static PyObject *py_problem_objective_forward(PyObject *self, PyObject *args)
return NULL;
}

PyArrayObject *u_array =
(PyArrayObject *) PyArray_FROM_OTF(u_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
PyArrayObject *u_array = double_array_of_size(u_obj, prob->n_vars, 0, "u");
if (!u_array)
{
return NULL;
Expand Down
8 changes: 8 additions & 0 deletions sparsediffpy/_bindings/problem/register_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ static PyObject *py_problem_register_params(PyObject *self, PyObject *args)
"invalid parameter capsule in list");
return NULL;
}
/* the engine calls exit(1) on a constant node */
if (((parameter_expr *) node)->param_id == PARAM_FIXED)
{
free(param_nodes);
PyErr_SetString(PyExc_ValueError,
"constant node passed as a parameter");
return NULL;
}
param_nodes[i] = node;
}

Expand Down
20 changes: 18 additions & 2 deletions sparsediffpy/_bindings/problem/update_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@ static PyObject *py_problem_update_params(PyObject *self, PyObject *args)
return NULL;
}

PyArrayObject *theta_array = (PyArrayObject *) PyArray_FROM_OTF(
theta_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
/* the engine calls exit(1) when no parameters are registered */
if (prob->n_param_nodes == 0)
{
PyErr_SetString(PyExc_ValueError, "no parameters registered");
return NULL;
}

/* each node copies theta[param_id : param_id + size] */
npy_intp theta_size = 0;
for (int i = 0; i < prob->n_param_nodes; i++)
{
expr *node = prob->param_nodes[i];
npy_intp end = ((parameter_expr *) node)->param_id + node->size;
theta_size = end > theta_size ? end : theta_size;
}

PyArrayObject *theta_array =
double_array_of_size(theta_obj, theta_size, 1, "theta");
if (!theta_array)
{
return NULL;
Expand Down
90 changes: 90 additions & 0 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Misuse of the raw problem bindings raises instead of crashing the process."""

import numpy as np
import pytest

from sparsediffpy import _sparsediffengine as C

N = 3


def make_problem(param=None):
"""minimize sum(exp(x)) subject to exp(x), optionally with x scaled by param."""
x = C.make_variable(N, 1, 0, N)
if param is not None:
x = C.make_param_scalar_mult(param, x)
con = C.make_exp(C.make_variable(N, 1, 0, N))
return C.make_problem(C.make_sum(C.make_exp(x), -1), [con], False)


def forwarded(prob):
u = np.ones(N)
C.problem_objective_forward(prob, u)
C.problem_constraint_forward(prob, u)
return prob


@pytest.mark.parametrize("call", [
lambda p: C.problem_gradient(p),
lambda p: C.problem_jacobian(p),
lambda p: C.problem_eval_jacobian_vals(p),
lambda p: C.problem_hessian(p, 1.0, np.ones(N)),
], ids=["gradient", "jacobian", "eval_jacobian_vals", "hessian"])
def test_derivatives_before_init(call):
with pytest.raises(RuntimeError, match="problem_init_derivatives"):
call(forwarded(make_problem()))


def test_hessian_coo_values_before_coo_init():
prob = forwarded(make_problem())
C.problem_init_derivatives(prob)
with pytest.raises(RuntimeError, match="coo"):
C.problem_eval_hessian_vals_coo(prob, 1.0, np.ones(N))


@pytest.mark.parametrize("call", [
lambda p: C.problem_objective_forward(p, np.ones(N - 1)),
lambda p: C.problem_constraint_forward(p, np.ones(N + 1)),
lambda p: C.problem_hessian(p, 1.0, np.ones(1)),
], ids=["objective_u", "constraint_u", "lagrange"])
def test_wrong_lengths(call):
prob = forwarded(make_problem())
C.problem_init_derivatives(prob)
C.problem_gradient(prob)
C.problem_jacobian(prob)
with pytest.raises(ValueError, match="must have"):
call(prob)


def test_update_params_without_params():
with pytest.raises(ValueError, match="no parameters"):
C.problem_update_params(make_problem(), np.ones(1))


def test_short_theta():
param = C.make_parameter(1, 1, 2, N, np.ones(1)) # reads theta[2]
prob = make_problem(param)
C.problem_register_params(prob, [param])
with pytest.raises(ValueError, match="at least 3"):
C.problem_update_params(prob, np.ones(2))
C.problem_update_params(prob, np.full(3, 2.0))
assert C.problem_objective_forward(prob, np.ones(N)) == pytest.approx(
N * np.exp(2.0))


def test_register_constant_as_parameter():
const = C.make_parameter(1, 1, -1, N, np.ones(1))
with pytest.raises(ValueError, match="constant"):
C.problem_register_params(make_problem(), [const])


def test_correct_sequence_still_works():
prob = make_problem()
C.problem_init_derivatives(prob)
forwarded(prob)
np.testing.assert_allclose(C.problem_gradient(prob), np.e * np.ones(N))
data, _, _, shape = C.problem_jacobian(prob)
assert shape == (N, N)
np.testing.assert_allclose(data, np.e * np.ones(N))
data, _, _, _ = C.problem_hessian(prob, 1.0, np.ones(N))
np.testing.assert_allclose(data, 2 * np.e * np.ones(N))
Loading