Skip to content
Merged
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
52 changes: 40 additions & 12 deletions include/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,32 @@ typedef bool (*is_affine_fn)(const struct expr *node);
typedef void (*free_type_data_fn)(struct expr *node);
typedef bool (*set_needs_refresh_children_fn)(struct expr *node);

/* AFFINITY_UNKNOWN must be 0 (the workspace is calloc'ed) */
typedef enum
{
AFFINITY_UNKNOWN = 0,
AFFINE,
NON_AFFINE
} affinity;

/* Workspace for derivative computation */
typedef struct
{
double *dwork;
int *iwork;

/* node->is_affine(node), computed once by jacobian_init (affinity is
structural, and the recursive is_affine is too costly per eval). */
bool is_affine_cached;
affinity affine_state; /* memoized node->is_affine(node) */

/* True once eval_jacobian has run this parameter epoch; cleared by
expr_set_needs_refresh. Only consulted for affine nodes, where it
lets the eval_jacobian wrapper skip the values_version bump (same
role the old jacobian_csc_filled latch played). */
/* The Jacobian values are valid. Cleared by expr_set_needs_refresh and,
for a non-affine node, by a new forward pass. */
bool jacobian_evaluated;

/* forward pass that last evaluated this node (see expr_forward) */
uint64_t fwd_epoch;

int hess_n_in; /* number of wsum_hess_init calls (in-edges) */
int hess_visits; /* eval_wsum_hess calls so far in this pass */
double *hess_w; /* weights summed over them (iff hess_n_in > 1) */
double *local_jac_diag; /* cached f'(g(x)) diagonal */
matrix *hess_term1; /* Jg^T D Jg workspace */
matrix *hess_term2; /* child wsum_hess workspace */
Expand All @@ -78,7 +89,8 @@ typedef struct expr
double *value;
matrix *jacobian;
matrix *wsum_hess;
forward_fn forward;
forward_fn forward; /* starts a forward pass */
forward_fn forward_impl; /* the atom's rule; recurses with expr_forward */
jacobian_init_fn jacobian_init_impl;
wsum_hess_init_fn wsum_hess_init_impl;
eval_jacobian_fn eval_jacobian_impl;
Expand All @@ -87,7 +99,8 @@ typedef struct expr
// ------------------------------------------------------------------------
// other things
// ------------------------------------------------------------------------
is_affine_fn is_affine;
is_affine_fn is_affine; /* memoizing wrapper */
is_affine_fn is_affine_impl; /* the atom's (recursive) rule */
local_jacobian_fn local_jacobian; /* used by elementwise univariate atoms*/
local_wsum_hess_fn local_wsum_hess; /* used by elementwise univariate atoms*/
free_type_data_fn free_type_data; /* Cleanup for type-specific fields */
Expand Down Expand Up @@ -129,14 +142,29 @@ void init_expr(expr *node, int d1, int d2, int n_vars, forward_fn forward,

void free_expr(expr *node);

/* Guarded init: skips if already initialized (safe for DAGs
* where a node may be visited through multiple parents). */
/* Forward pass on a DAG. node->forward(node, u) starts a pass and evaluates
* the node. Atoms recurse into their children with expr_forward, which
* evaluates a node at most once per pass. A new value invalidates a non-affine
* node's Jacobian. */
void expr_forward(expr *node, const double *u);

/* Guarded init: the impl runs once per node, however many parents it has.
*
* Every wsum_hess_init(node) call counts as one in-edge of the node and must
* be matched by exactly one eval_wsum_hess(node, w) call per Hessian pass
* (an atom does this for each child; problem does it for its roots). */
void jacobian_init(expr *node);
void wsum_hess_init(expr *node);

/* Eval wrappers: run the atom's eval_*_impl and bump the output matrix's
* values_version so version-guarded caches (CSC mirrors, spd CSR views)
* refresh. Always call these instead of the impl slots. */
* refresh. Always call these instead of the impl slots.
*
* eval_jacobian skips a node whose Jacobian is still valid.
*
* eval_wsum_hess evaluates a node with several in-edges once per pass, at the
* sum of their weights. An atom must therefore only add a child's Hessian to
* its own, never rescale it (a scaling goes into the child's weights). */
void eval_jacobian(expr *node);
void eval_wsum_hess(expr *node, const double *w);

Expand Down
3 changes: 2 additions & 1 deletion include/subexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ typedef struct quad_form_expr
/* Q f(x) as an n x 1 permuted_dense: the transposed operand that forms the
composition Jacobian 2 (Q f(x))^T J_f through the matrix dispatchers. */
matrix *Qx;
int n; /* quadratic dimension = left->size */
int n; /* quadratic dimension = left->size */
double *hess_w; /* weights 2w Q f(x) passed to the child's hessian */

/* parametric dense path: param_source feeds Q each solve (NULL otherwise) */
expr *param_source;
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/affine/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
static void forward(expr *node, const double *u)
{
/* children's forward passes */
node->left->forward(node->left, u);
node->right->forward(node->right, u);
expr_forward(node->left, u);
expr_forward(node->right, u);

/* add left and right values */
for (int i = 0; i < node->size; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void forward(expr *node, const double *u)
expr *x = node->left;
broadcast_expr *bcast = (broadcast_expr *) node;

x->forward(x, u);
expr_forward(x, u);

if (bcast->type == BROADCAST_ROW)
{
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/affine/convolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void forward(expr *node, const double *u)

if (cnode->base.needs_parameter_refresh)
{
cnode->param_source->forward(cnode->param_source, NULL);
expr_forward(cnode->param_source, NULL);
/* refresh the convolution matrix values if it exists (necessary to check
for null in case someone calls forward before initializing the jacobian,
which might happen when we expose Python bindings to SparseDiffEngine) */
Expand All @@ -84,7 +84,7 @@ static void forward(expr *node, const double *u)
cnode->base.needs_parameter_refresh = false;
}

child->forward(child, u);
expr_forward(child, u);

const double *a = cnode->param_source->value;
const double *x = child->value;
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/diag_vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void forward(expr *node, const double *u)
int n = x->size;

/* child's forward pass */
x->forward(x, u);
expr_forward(x, u);

/* zero-initialize output, TODO: do we need to do this? */
memset(node->value, 0, node->size * sizeof(double));
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/hstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void forward(expr *node, const double *u)
/* children's forward passes */
for (int i = 0; i < hnode->n_args; i++)
{
hnode->args[i]->forward(hnode->args[i], u);
expr_forward(hnode->args[i], u);
}

/* concatenate values horizontally */
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static void forward(expr *node, const double *u)
index_expr *idx = (index_expr *) node;

/* child's forward pass */
x->forward(x, u);
expr_forward(x, u);

/* gather selected elements */
for (int i = 0; i < idx->n_idxs; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/affine/kron.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static void refresh_param_values(kron_expr *knode)
return;
}

knode->param_source->forward(knode->param_source, NULL);
expr_forward(knode->param_source, NULL);
knode->base.needs_parameter_refresh = false;
}

Expand All @@ -54,7 +54,7 @@ static void forward(expr *node, const double *u)
kron_expr *knode = (kron_expr *) node;

refresh_param_values(knode);
child->forward(child, u);
expr_forward(child, u);

const double *a = knode->param_source->value;
const double *x = child->value;
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/affine/left_matmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ static void forward(expr *node, const double *u)
/* call forward on param_source if it exists and needs refresh */
if (lnode->param_source != NULL && lnode->base.needs_parameter_refresh)
{
lnode->param_source->forward(lnode->param_source, NULL);
expr_forward(lnode->param_source, NULL);
}

refresh_param_values(lnode);

expr *x = node->left;

/* child's forward pass */
node->left->forward(node->left, u);
expr_forward(node->left, u);

/* y = A_kron @ vec(f(x)) */
matrix *A = lnode->A;
Expand Down
17 changes: 10 additions & 7 deletions src/atoms/affine/neg.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
static void forward(expr *node, const double *u)
{
/* child's forward pass */
node->left->forward(node->left, u);
expr_forward(node->left, u);

/* negate values */
for (int i = 0; i < node->size; i++)
Expand Down Expand Up @@ -62,20 +62,23 @@ static void wsum_hess_init_impl(expr *node)
/* initialize child's wsum_hess */
wsum_hess_init(x);

/* weights passed to the child */
node->work->dwork = (double *) sp_malloc(node->size * sizeof(double));

/* same sparsity pattern as child (polymorphic copy) */
node->wsum_hess = copy_sparsity_matrices_alloc(x->wsum_hess);
}

static void eval_wsum_hess_impl(expr *node, const double *w)
{
/* For f(x) = -g(x): d²f/dx² = -d²g/dx² */
eval_wsum_hess(node->left, w);

/* negate values (sparsity pattern set in wsum_hess_init_impl) */
for (int k = 0; k < node->left->wsum_hess->nnz; k++)
/* For f(x) = -g(x): sum_i w_i Hf_i = sum_i (-w_i) Hg_i */
for (int i = 0; i < node->size; i++)
{
node->wsum_hess->x[k] = -node->left->wsum_hess->x[k];
node->work->dwork[i] = -w[i];
}
eval_wsum_hess(node->left, node->work->dwork);
memcpy(node->wsum_hess->x, node->left->wsum_hess->x,
node->wsum_hess->nnz * sizeof(double));
}

static bool is_affine(const expr *node)
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/promote.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

static void forward(expr *node, const double *u)
{
node->left->forward(node->left, u);
expr_forward(node->left, u);

/* broadcast scalar value to all output elements */
for (int i = 0; i < node->size; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/reshape.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

static void forward(expr *node, const double *u)
{
node->left->forward(node->left, u);
expr_forward(node->left, u);
memcpy(node->value, node->left->value, node->size * sizeof(double));
}

Expand Down
20 changes: 12 additions & 8 deletions src/atoms/affine/scalar_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/
#include "atoms/affine.h"
#include "sparsediff_linalg/cblas_wrapper.h"
#include "sparsediff_linalg/tracked_alloc.h"
#include "subexpr.h"
#include <assert.h>
Expand All @@ -35,14 +36,14 @@ static void forward(expr *node, const double *u)
its values) */
if (snode->base.needs_parameter_refresh)
{
snode->param_source->forward(snode->param_source, NULL);
expr_forward(snode->param_source, NULL);
snode->base.needs_parameter_refresh = false;
}

double a = snode->param_source->value[0];

/* child's forward pass */
child->forward(child, u);
expr_forward(child, u);

/* local forward pass: multiply each element by scalar a */
for (int i = 0; i < node->size; i++)
Expand Down Expand Up @@ -84,20 +85,23 @@ static void wsum_hess_init_impl(expr *node)
/* initialize child's weighted Hessian */
wsum_hess_init(x);

/* weights passed to the child */
node->work->dwork = (double *) sp_malloc(node->size * sizeof(double));

/* same sparsity as child */
node->wsum_hess = copy_sparsity_matrices_alloc(x->wsum_hess);
}

static void eval_wsum_hess_impl(expr *node, const double *w)
{
expr *x = node->left;
eval_wsum_hess(x, w);

double a = ((scalar_mult_expr *) node)->param_source->value[0];
for (int j = 0; j < x->wsum_hess->nnz; j++)
{
node->wsum_hess->x[j] = a * x->wsum_hess->x[j];
}

memcpy(node->work->dwork, w, node->size * sizeof(double));
cblas_dscal(node->size, a, node->work->dwork, 1);
eval_wsum_hess(x, node->work->dwork);
memcpy(node->wsum_hess->x, x->wsum_hess->x,
node->wsum_hess->nnz * sizeof(double));
}

static bool is_affine(const expr *node)
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void forward(expr *node, const double *u)
int axis = snode->axis;

/* child's forward pass */
x->forward(x, u);
expr_forward(x, u);

if (axis == -1)
{
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void forward(expr *node, const double *u)
expr *x = node->left;

/* child's forward pass */
x->forward(x, u);
expr_forward(x, u);

/* local forward pass */
double sum = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/affine/transpose.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
static void forward(expr *node, const double *u)
{
/* forward pass for child */
node->left->forward(node->left, u);
expr_forward(node->left, u);

/* local forward pass */
int d1 = node->d1;
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/affine/vector_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ static void forward(expr *node, const double *u)
its values) */
if (vnode->base.needs_parameter_refresh)
{
vnode->param_source->forward(vnode->param_source, NULL);
expr_forward(vnode->param_source, NULL);
vnode->base.needs_parameter_refresh = false;
}

const double *a = vnode->param_source->value;

/* child's forward pass */
child->forward(child, u);
expr_forward(child, u);

/* local forward pass: elementwise multiply by a */
for (int i = 0; i < node->size; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/bivariate_full_dom/matmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ static void forward(expr *node, const double *u)
expr *y = node->right;

/* children's forward passes */
x->forward(x, u);
y->forward(y, u);
expr_forward(x, u);
expr_forward(y, u);

/* local forward pass */
int m = x->d1, k = x->d2, n = y->d2;
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/bivariate_full_dom/multiply.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void forward(expr *node, const double *u)
expr *y = node->right;

/* children's forward passes */
x->forward(x, u);
y->forward(y, u);
expr_forward(x, u);
expr_forward(y, u);

/* local forward pass */
for (int i = 0; i < node->size; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/atoms/bivariate_restricted_dom/atan2.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ static void forward(expr *node, const double *u)
expr *x = node->right;

/* children's forward passes */
y->forward(y, u);
x->forward(x, u);
expr_forward(y, u);
expr_forward(x, u);

/* local forward pass */
for (int i = 0; i < node->size; i++)
Expand Down
Loading
Loading