Skip to content
Draft
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
38 changes: 37 additions & 1 deletion 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,22 @@ 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.

Scalar expressions written in a Python loop are merged into vector ones
with :func:`~sparsediffpy.compact`: the objective (e.g. ``sum(terms)``)
and each run of consecutive scalar constraints. 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}")
objective = compact([objective])[0]
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
229 changes: 179 additions & 50 deletions sparsediffpy/vectorize.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Merge scalar terms written in a Python loop into vector expressions.
"""Merge scalar expressions written in a Python loop into vector expressions.

A loop such as ``[x[k] ** 2 + x[k + 1] - 1 for k in range(n - 1)]`` gives the
engine one small expression per term. :func:`compact` lowers each term to a
short list of instructions, in the style of LLVM IR::
engine one small expression per term. :func:`compact` lowers each expression
to a short list of instructions, in the style of LLVM IR::

%0 = load x ; arg: the index k
%1 = power 2.0 %0
Expand All @@ -12,9 +12,15 @@
%5 = neg %4
%6 = add %3 %5

Terms whose instructions are identical differ only in their args, so each such
Expressions whose instructions are identical differ only in their args, so each such
group is rebuilt once as a vector expression: a ``load`` becomes a gather of
``x`` at the args, a ``const`` becomes a constant vector.

An expression that merges with no other may itself be a long sum, such as
``t = t + x[k]`` accumulated in a loop. Like LLVM's horizontal reduction, its
chain of adds is flattened into operands, which are compacted and summed:
``sum(compact(operands))``. This is kept only if the graph gets smaller, and
it reassociates the sum (results can differ in the last bits).
"""

import numpy as np
Expand All @@ -29,64 +35,170 @@
"normal_cdf", "xexp"}


class _Unsupported(Exception):
pass
class UnsupportedExprError(Exception):
"""Raised when an expression uses an operation that cannot be vectorized."""


def compact(exprs):
"""Stack scalar expressions into a column vector, merging those of the same form.

``exprs`` is typically the list of scalar objective or constraint terms
built in a Python loop, such as
``[x[k] ** 2 + x[k + 1] for k in range(n - 1)]``. Expressions that apply the
same operations, and differ only in which entries of a Variable they index
or which constants they use, are rebuilt as one vector expression. Other
expressions (using matmul, sum, ...) are kept as they are, unless
they are a sum of mergeable operands.

def compact(terms):
"""Stack scalar expressions like :func:`vstack`, merging identical ones.
Parameters
----------
exprs : list of Expression or float
The scalar expressions to stack; each must have shape ``(1, 1)``.

Returns ``(expr, perm)``: ``expr`` has shape ``(len(terms), 1)`` and its
row ``j`` is ``terms[perm[j]]``, or ``perm`` is ``None`` if no row moved.
Terms using other operations (matmul, sum, ...) are kept as they are.
Returns
-------
expr : Expression
An expression of shape ``(len(exprs), 1)`` holding one expression per
row. Merged expressions are grouped together, so rows may be reordered.
perm : numpy.ndarray of int or None
Row ``j`` of ``expr`` is ``exprs[perm[j]]``. ``None`` if every
expression kept its position, i.e. ``expr`` is ``vstack(exprs)``.

Raises
------
ValueError
If ``exprs`` is empty or an expression does not have shape ``(1, 1)``.

Examples
--------
>>> x = sp.Scope().Variable(5, 1)
>>> expr, perm = sp.compact([x[k] ** 2 + x[k + 1] for k in range(4)])
>>> expr.shape, perm
((4, 1), None)
>>> expr, perm = sp.compact([x[0] ** 2, sp.exp(x[1]), x[2] ** 2])
>>> perm
array([0, 2, 1])
"""
if not terms:
raise ValueError("compact: need at least one term")
values, groups = {}, {} # values: id -> Variable/Parameter the terms use
for i, t in enumerate(terms):
if not exprs:
raise ValueError("compact: need at least one expression")
values, groups = {}, {} # values: id -> Variable/Parameter the exprs use
for i, t in enumerate(exprs):
t = cast_to_const(t)
if t.shape != (1, 1):
raise ValueError(f"compact: term {i} has shape {t.shape}, not (1, 1)")
raise ValueError(f"compact: expression {i} has shape {t.shape}, "
"not (1, 1)")
try:
insts, args = _lower(t, values)
except (_Unsupported, RecursionError): # deep terms are kept as is
insts, args = ("single", i), None
groups.setdefault(insts, []).append((i, t, args))
instructions, args = _lower(t, values)
except (UnsupportedExprError, RecursionError):
instructions, args = ("single", i), None
groups.setdefault(instructions, []).append((i, t, args))

parts, perm = [], []
for insts, members in groups.items():
for instructions, members in groups.items():
perm += [i for i, _, _ in members]
parts.append(members[0][1] if len(members) == 1 else
_vectorize(insts, [args for _, _, args in members], values))
parts.append(_reduce(members[0][1]) if len(members) == 1 else
_vectorize(instructions, [args for _, _, args in members],
values))
expr = parts[0] if len(parts) == 1 else atoms.vstack(parts)
perm = None if perm == list(range(len(perm))) else np.array(perm)
return expr, perm


def _lower(term, values):
"""Lower a scalar term to ``(insts, args)``.
def _reduce(expr):
"""Rewrite a sum of many operands as ``sum(compact(operands))``.

The chain of ``add`` nodes in ``expr`` is flattened into its operands,
which are compacted and summed. This reassociates the sum.

Parameters
----------
expr : Expression
A scalar expression that merged with no other.

Returns
-------
Expression
The rewritten sum if it has fewer nodes than ``expr``, else ``expr``.
"""
operands, stack = [], [expr]
while stack:
e = stack.pop()
if isinstance(e, Node) and e.opcode == "add":
stack.extend(reversed(e.args))
else:
operands.append(e)
if len(operands) < 2:
return expr
reduced = atoms.sum(compact(operands)[0])
return reduced if _count_nodes(reduced) < _count_nodes(expr) else expr


def _count_nodes(e):
"""Count the distinct nodes in the DAG rooted at ``e``.

Parameters
----------
e : Expression
The root of the DAG.

``insts`` lists the term's nodes in post-order as ``(opcode, *operands)``;
operands are value numbers (positions in the list), so a subexpression
shared inside the term is computed once. ``args`` holds the per-term data,
in instruction order: the index of each ``load`` and the value of each
``const``.
Returns
-------
int
The number of distinct nodes, counting shared subexpressions once.
"""
insts, args, number = [], [], {} # number: id(node) -> value number
seen, stack = set(), [e]
while stack:
e = stack.pop()
if id(e) not in seen:
seen.add(id(e))
stack.extend(e.args)
return len(seen)


def _lower(expr, values):
"""Lower a scalar expression to a list of instructions and its args.

Two expressions with equal ``instructions`` apply the same operations and differ
only in their ``args``, so they can be merged by :func:`_vectorize`.

Parameters
----------
expr : Expression
A scalar expression of shape ``(1, 1)``.
values : dict
Maps ``id`` to each Variable or Parameter the exprs use. Updated in
place with the ones ``expr`` uses.

Returns
-------
instructions : tuple of tuple
The expression's nodes in post-order, each as ``(opcode, *operands)``.
Operands are value numbers (positions in ``instructions``), so a
subexpression shared inside ``expr`` is computed once.
args : list
The per-expression data, in instruction order: the index of each ``load``
and the value of each ``const``.

Raises
------
UnsupportedExprError
If ``expr`` uses an operation that cannot be vectorized.
"""
instructions, args, number = [], [], {} # number: id(node) -> value number

def visit(e):
if id(e) in number:
return number[id(e)]
if e.shape != (1, 1):
raise _Unsupported
raise UnsupportedExprError
if isinstance(e, Constant):
inst = ("const",)
args.append(float(e.value[0, 0]))
elif isinstance(e, (Variable, Parameter)):
values[id(e)] = e
inst = ("global", id(e))
elif not isinstance(e, Node):
raise _Unsupported
raise UnsupportedExprError
elif e.opcode == "index" and isinstance(e.args[0], Variable):
values[id(e.args[0])] = e.args[0]
inst = ("load", id(e.args[0]))
Expand All @@ -98,32 +210,49 @@ def visit(e):
elif e.opcode == "power":
inst = ("power", e.attrs["p"], visit(e.args[0]))
else:
raise _Unsupported
number[id(e)] = len(insts)
insts.append(inst)
raise UnsupportedExprError
number[id(e)] = len(instructions)
instructions.append(inst)
return number[id(e)]

visit(term)
return tuple(insts), args
visit(expr)
return tuple(instructions), args


def _vectorize(instructions, args, values):
"""Build one vector expression from expressions with equal instructions.

The instructions are run once on column vectors with one row per
expression: each ``load`` becomes a gather of the Variable at every
expression's index, and each ``const`` a constant vector of every
expression's value.

def _vectorize(insts, args, values):
"""Run ``insts`` once on (lanes, 1) vectors, one lane per term.
Parameters
----------
instructions : tuple of tuple
The instructions shared by the expressions, as returned by :func:`_lower`.
args : list of list
``args[i]`` is the args of expression ``i``, as returned by :func:`_lower`.
values : dict
Maps ``id`` to each Variable or Parameter the instructions use.

``args[lane]`` is that term's args; transposed, each ``load``/``const``
gets one value per lane.
Returns
-------
Expression
An expression of shape ``(len(args), 1)``; row ``i`` is expression ``i``.
"""
lanes = len(args)
lane_args = iter(zip(*args))
n_rows = len(args)
arg_columns = iter(zip(*args)) # one load/const arg across all expressions
vecs = [] # vecs[n]: the vector for value number n
for inst in insts:
for inst in instructions:
opcode = inst[0]
if opcode == "load":
vecs.append(index_flat(values[inst[1]], next(lane_args), (lanes, 1)))
vecs.append(index_flat(values[inst[1]], next(arg_columns),
(n_rows, 1)))
elif opcode == "const":
c = np.array(next(lane_args))
c = np.array(next(arg_columns))
vecs.append(Constant(c[0] if np.all(c == c[0])
else c.reshape(lanes, 1)))
else c.reshape(n_rows, 1)))
elif opcode == "global":
vecs.append(values[inst[1]])
elif opcode == "add":
Expand All @@ -136,5 +265,5 @@ def _vectorize(insts, args, values):
vecs.append(vecs[inst[2]] ** inst[1])
else:
vecs.append(getattr(atoms, opcode)(vecs[inst[1]]))
# with no load/const (every term is e.g. the scalar y) the result is a scalar
return broadcast(vecs[-1], (lanes, 1))
# with no load/const (every expression is e.g. y) the result is a scalar
return broadcast(vecs[-1], (n_rows, 1))
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def test_unset_parameter_raises(scope):
# --- Caching ------------------------------------------------------------------

def count_calls(monkeypatch, name):
"""Count calls to the engine function `name` as seen from sparsediffpy.problem."""
"""Count calls to engine function `name` as seen from sparsediffpy.problem."""
calls, real = [], getattr(problem._C, name)

def counted(*args):
Expand Down
Loading