Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
7 changes: 4 additions & 3 deletions sparsediffpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"""

from sparsediffpy import _sparsediffengine # noqa: F401
from sparsediffpy._expression import ( # noqa: F401
from sparsediffpy.expression import ( # noqa: F401
DimensionError, Expression, Parameter, Scope, Variable, broadcast, index_flat,
)
from sparsediffpy._atoms import ( # noqa: F401
from sparsediffpy.atoms import ( # noqa: F401
asinh, atan, atan2, atanh, cos, diag_vec, entr, exp, hstack, log, logistic,
normal_cdf, power, prod, quad_form, quad_over_lin, rel_entr, reshape, sin,
sinh, sum, tan, tanh, trace, vstack, xexp,
)
from sparsediffpy._compile import CompiledExpression, Problem, compile # noqa: F401
from sparsediffpy.problem import CompiledExpression, Problem, compile # noqa: F401
from sparsediffpy.vectorize import compact # noqa: F401
30 changes: 15 additions & 15 deletions sparsediffpy/_atoms.py → sparsediffpy/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import scipy.sparse as sps

from sparsediffpy import _sparsediffengine as _C
from sparsediffpy._expression import (
from sparsediffpy.expression import (
Constant, Node, Parameter, Variable, _check_shape, _check_symmetric,
cast_to_const,
)
Expand All @@ -24,7 +24,7 @@ def atom(x):
x = cast_to_const(x)
if restricted:
_require_variables(name, x)
return Node(make, [x], x.shape)
return Node(make, [x], x.shape, name)
atom.__name__ = atom.__qualname__ = name
atom.__doc__ = f"Elementwise {name}."
return atom
Expand Down Expand Up @@ -56,7 +56,7 @@ def atan2(y, x):
_require_variables("atan2", y, x)
if y.shape != x.shape:
raise ValueError(f"atan2: shape mismatch {y.shape} vs {x.shape}")
return Node(_C.make_atan2, [y, x], y.shape)
return Node(_C.make_atan2, [y, x], y.shape, "atan2")


def _reduction_shape(x, axis):
Expand All @@ -73,7 +73,7 @@ def sum(x, axis=None):
x = cast_to_const(x)
shape = _reduction_shape(x, axis)
c_axis = -1 if axis is None else axis
return Node(lambda c: _C.make_sum(c, c_axis), [x], shape)
return Node(lambda c: _C.make_sum(c, c_axis), [x], shape, "sum")


def prod(x, axis=None):
Expand All @@ -84,30 +84,30 @@ def prod(x, axis=None):
shape = _reduction_shape(x, axis)
make = {None: _C.make_prod, 0: _C.make_prod_axis_zero,
1: _C.make_prod_axis_one}[axis]
return Node(make, [x], shape)
return Node(make, [x], shape, "prod")


def reshape(x, d1, d2):
"""Column-major reshape."""
x, shape = cast_to_const(x), _check_shape(d1, d2)
if x.size != shape[0] * shape[1]:
raise ValueError(f"Cannot reshape {x.shape} to {shape}")
return Node(lambda c: _C.make_reshape(c, *shape), [x], shape)
return Node(lambda c: _C.make_reshape(c, *shape), [x], shape, "reshape")


def trace(x):
x = cast_to_const(x)
if x.shape[0] != x.shape[1]:
raise ValueError(f"trace requires a square matrix, got {x.shape}")
return Node(_C.make_trace, [x], (1, 1))
return Node(_C.make_trace, [x], (1, 1), "trace")


def diag_vec(x):
"""(n, 1) column vector -> (n, n) diagonal matrix."""
x = cast_to_const(x)
if x.shape[1] != 1:
raise ValueError(f"diag_vec requires a column vector, got {x.shape}")
return Node(_C.make_diag_vec, [x], (x.shape[0], x.shape[0]))
return Node(_C.make_diag_vec, [x], (x.shape[0], x.shape[0]), "diag_vec")


def hstack(exprs):
Expand All @@ -117,7 +117,7 @@ def hstack(exprs):
raise ValueError(f"hstack: need >=1 args with equal rows, got "
f"{[e.shape for e in exprs]}")
shape = (exprs[0].shape[0], np.sum([e.shape[1] for e in exprs]).item())
return Node(lambda *caps: _C.make_hstack(list(caps)), exprs, shape)
return Node(lambda *caps: _C.make_hstack(list(caps)), exprs, shape, "hstack")


def vstack(exprs):
Expand All @@ -127,7 +127,7 @@ def vstack(exprs):
raise ValueError(f"vstack: need >=1 args with equal columns, got "
f"{[e.shape for e in exprs]}")
shape = (np.sum([e.shape[0] for e in exprs]).item(), exprs[0].shape[1])
return Node(lambda *caps: _C.make_vstack(list(caps)), exprs, shape)
return Node(lambda *caps: _C.make_vstack(list(caps)), exprs, shape, "vstack")


def quad_form(x, Q):
Expand All @@ -147,26 +147,26 @@ def quad_form(x, Q):
if Q._flat is not None:
_check_symmetric(Q._flat, Q.shape)
return Node(lambda p, c: _C.make_quad_form(p, c, "dense", None, n),
[Q, x], (1, 1))
[Q, x], (1, 1), "quad_form")
if not isinstance(Q, Constant):
raise TypeError("quad_form: Q must be a constant or a Parameter")
V = (Q.value + Q.value.T) / 2 # the engine assumes symmetric Q
if not sps.issparse(V):
data = np.ascontiguousarray(V).ravel()
return Node(lambda c: _C.make_quad_form(None, c, "dense", data, n),
[x], (1, 1))
[x], (1, 1), "quad_form")
V = sps.csr_matrix(V)
return Node(lambda c: _C.make_quad_form(
None, c, "sparse", V.data, V.indices.astype(np.int32),
V.indptr.astype(np.int32), n, n), [x], (1, 1))
V.indptr.astype(np.int32), n, n), [x], (1, 1), "quad_form")


def quad_over_lin(x, z):
"""sum(x**2) / z for distinct Variables x and scalar z."""
_require_variables("quad_over_lin", x, z)
if z.shape != (1, 1):
raise ValueError(f"quad_over_lin: z must be scalar, got {z.shape}")
return Node(_C.make_quad_over_lin, [x, z], (1, 1))
return Node(_C.make_quad_over_lin, [x, z], (1, 1), "quad_over_lin")


def rel_entr(x, y):
Expand All @@ -181,4 +181,4 @@ def rel_entr(x, y):
shape = y.shape
else:
raise ValueError(f"rel_entr: incompatible shapes {x.shape} and {y.shape}")
return Node(_C.make_rel_entr, [x, y], shape)
return Node(_C.make_rel_entr, [x, y], shape, "rel_entr")
45 changes: 29 additions & 16 deletions sparsediffpy/_expression.py → sparsediffpy/expression.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Expression graph: Scope, leaves (Variable/Parameter/Constant), and operators.

Every non-leaf is a generic ``Node(make, args, shape)``: ``make`` receives the
C capsules of ``args`` and returns the node's own capsule. Shapes are always
Every non-leaf is a generic ``Node(make, args, shape, opcode)``: ``make``
receives the C capsules of ``args`` and returns the node's own capsule, and
``opcode`` names the operation. Shapes are always
2-D ``(d1, d2)`` and values are flattened column-major, like the C engine.
"""

Expand Down Expand Up @@ -38,7 +39,7 @@ def size(self):

@property
def T(self):
return Node(_C.make_transpose, [self], self.shape[::-1])
return Node(_C.make_transpose, [self], self.shape[::-1], "transpose")

def __add__(self, other):
return _add(self, cast_to_const(other))
Expand All @@ -53,7 +54,7 @@ def __rsub__(self, other):
return _add(cast_to_const(other), -self)

def __neg__(self):
return Node(_C.make_neg, [self], self.shape)
return Node(_C.make_neg, [self], self.shape, "neg")

def __mul__(self, other):
return _mul(self, cast_to_const(other))
Expand All @@ -70,7 +71,8 @@ def __rmatmul__(self, other):
def __pow__(self, p):
if not isinstance(p, numbers.Real):
raise TypeError("Exponent must be a constant number")
return Node(lambda c: _C.make_power(c, float(p)), [self], self.shape)
return Node(lambda c: _C.make_power(c, float(p)), [self], self.shape,
"power", {"p": float(p)})

def __getitem__(self, key):
return _index(self, key)
Expand All @@ -82,12 +84,19 @@ def __iter__(self):


class Node(Expression):
"""An operation: ``make(*child_capsules)`` builds its C capsule."""
"""An operation: ``make(*child_capsules)`` builds its C capsule.

def __init__(self, make, args, shape):
``opcode`` names the operation and ``attrs`` holds the parameters that
rewrites such as :func:`compact` read (``idx`` for index, ``p`` for power).
Compilation only uses ``make``.
"""

def __init__(self, make, args, shape, opcode, attrs=None):
self.make = make
self.args = tuple(args)
self.shape = tuple(shape)
self.opcode = opcode
self.attrs = attrs


class Constant(Expression):
Expand Down Expand Up @@ -254,23 +263,26 @@ def broadcast(x, shape):
raise ValueError(f"Cannot broadcast {x.shape} to {shape}")
if isinstance(x, Constant):
return Constant(np.broadcast_to(x.dense(), shape))
return Node(lambda c: _C.make_broadcast(c, *shape), [x], shape)
return Node(lambda c: _C.make_broadcast(c, *shape), [x], shape,
"broadcast")


def _add(a, b):
shape = _broadcast_shape(a.shape, b.shape)
return Node(_C.make_add, [broadcast(a, shape), broadcast(b, shape)], shape)
return Node(_C.make_add, [broadcast(a, shape), broadcast(b, shape)], shape,
"add")


def _mul(a, b):
if _is_data(b) and not _is_data(a):
a, b = b, a # put the constant/parameter operand (if any) first
if _is_data(a) and a.shape == (1, 1):
return Node(_C.make_param_scalar_mult, [a, b], b.shape)
return Node(_C.make_param_scalar_mult, [a, b], b.shape, "scalar_mult")
shape = _broadcast_shape(a.shape, b.shape)
a, b = broadcast(a, shape), broadcast(b, shape)
make = _C.make_param_vector_mult if _is_data(a) else _C.make_multiply
return Node(make, [a, b], shape)
if _is_data(a):
return Node(_C.make_param_vector_mult, [a, b], shape, "vector_mult")
return Node(_C.make_multiply, [a, b], shape, "multiply")


def _matmul(a, b):
Expand All @@ -279,11 +291,11 @@ def _matmul(a, b):
shape = (a.shape[0], b.shape[1])
if _is_data(a):
return Node(_matmul_with_data(_C.make_left_matmul, a), _data_args(a, b),
shape)
shape, "left_matmul")
if _is_data(b):
return Node(_matmul_with_data(_C.make_right_matmul, b), _data_args(b, a),
shape)
return Node(_C.make_matmul, [a, b], shape)
shape, "right_matmul")
return Node(_C.make_matmul, [a, b], shape, "matmul")


def _data_args(data, x):
Expand Down Expand Up @@ -312,7 +324,8 @@ def index_flat(x, flat_indices, shape):
raise ValueError(f"{idx.size} indices do not fill shape {shape}")
if idx.size and (idx.min() < 0 or idx.max() >= x.size):
raise IndexError(f"flat index out of range for size {x.size}")
return Node(lambda c: _C.make_index(c, shape[0], shape[1], idx), [x], shape)
return Node(lambda c: _C.make_index(c, shape[0], shape[1], idx), [x], shape,
"index", {"idx": idx})


def _axis_indices(key, n):
Expand Down
37 changes: 36 additions & 1 deletion sparsediffpy/_compile.py → sparsediffpy/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,34 @@
import scipy.sparse as sps

from sparsediffpy import _sparsediffengine as _C
from sparsediffpy._expression import Constant, Parameter, Variable, _flatten_shaped, _flatten_vector, cast_to_const
from sparsediffpy.expression import (
Constant, Parameter, Variable, _flatten_shaped, _flatten_vector, cast_to_const,
index_flat,
)
from sparsediffpy.vectorize import compact


def _compact_in_order(exprs):
"""``compact(exprs)`` with its rows gathered back into their given order."""
expr, perm = compact(exprs)
if perm is None:
return expr
return index_flat(expr, np.argsort(perm), expr.shape)


def _compact_constraints(constraints):
"""Compact each run of consecutive scalar constraints, keeping row order."""
out, run = [], []
for c in constraints + [None]:
if c is not None and c.shape == (1, 1):
run.append(c)
continue
if run:
out.append(_compact_in_order(run))
run = []
if c is not None:
out.append(c)
return out


def _find_scope(exprs):
Expand Down Expand Up @@ -99,13 +126,21 @@ class Problem:
gradient/jacobian are skipped when the matching forward has not moved.
Skipped calls return the cached object, which the problem owns: do not
modify it in place.

Each run of consecutive scalar constraints, such as one per iteration of
a Python loop, is merged into vector expressions with
:func:`~sparsediffpy.compact`. Constraint rows keep the order they were
given in. ``objective`` and ``constraints`` hold the expressions as built
in the engine.
"""

def __init__(self, objective, constraints=(), verbose=False):
objective = cast_to_const(objective)
constraints = [cast_to_const(c) for c in constraints]
if objective.shape != (1, 1):
raise ValueError(f"Objective must be scalar, got {objective.shape}")
constraints = _compact_constraints(constraints)
self.objective, self.constraints = objective, constraints
self._scope = _find_scope([objective, *constraints])
build = _Builder(self._scope)
obj_cap = build(objective)
Expand Down
Loading
Loading