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
7 changes: 1 addition & 6 deletions src/atoms/affine/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ static void jacobian_init_impl(expr *node)
jacobian_init(node->left);
jacobian_init(node->right);

/* we never have to store more than the sum of children's nnz */
int nnz_max = node->left->jacobian->nnz + node->right->jacobian->nnz;
node->jacobian = new_sparse_matrix_alloc(node->size, node->n_vars, nnz_max);

/* fill sparsity pattern */
sum_matrices_alloc(node->left->jacobian, node->right->jacobian, node->jacobian);
node->jacobian = sum_matrices_alloc(node->left->jacobian, node->right->jacobian);
}

static void eval_jacobian_impl(expr *node)
Expand Down
5 changes: 1 addition & 4 deletions src/atoms/bivariate_full_dom/matmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,7 @@ static void jacobian_init_chain_rule(expr *node)
mnode->X = new_permuted_dense_full(m, k, NULL);
mnode->term1 = BA_dense_kron_I_matrices_alloc(mnode->YT, m, f->jacobian);
mnode->term2 = BA_kron_matrices_alloc(mnode->X, n, g->jacobian);
int max_nnz = mnode->term1->nnz + mnode->term2->nnz;
max_nnz = MIN(max_nnz, sat_mul_int(node->size, node->n_vars));
node->jacobian = new_sparse_matrix_alloc(node->size, node->n_vars, max_nnz);
sum_matrices_alloc(mnode->term1, mnode->term2, node->jacobian);
node->jacobian = sum_matrices_alloc(mnode->term1, mnode->term2);
}

static void eval_jacobian_chain_rule(expr *node)
Expand Down
6 changes: 1 addition & 5 deletions src/atoms/bivariate_full_dom/multiply.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ static void jacobian_init_impl(expr *node)
{
jacobian_init(node->left);
jacobian_init(node->right);
int nnz_max = node->left->jacobian->nnz + node->right->jacobian->nnz;
node->jacobian = new_sparse_matrix_alloc(node->size, node->n_vars, nnz_max);

/* fill sparsity pattern */
sum_matrices_alloc(node->left->jacobian, node->right->jacobian, node->jacobian);
node->jacobian = sum_matrices_alloc(node->left->jacobian, node->right->jacobian);
}

static void eval_jacobian_impl(expr *node)
Expand Down
4 changes: 1 addition & 3 deletions src/atoms/bivariate_restricted_dom/quad_over_lin.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ static void jacobian_init_impl(expr *node)
ey->i[0] = y->var_id;
qnode->ypart = new_sparse_matrix(ey);

node->jacobian =
new_sparse_matrix_alloc(1, node->n_vars, qnode->xpart->nnz + 1);
sum_matrices_alloc(qnode->xpart, qnode->ypart, node->jacobian);
node->jacobian = sum_matrices_alloc(qnode->xpart, qnode->ypart);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/atoms/elementwise_full_dom/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ void wsum_hess_init_elementwise(expr *node)
node->work->hess_term2 = copy_sparsity_matrices_alloc(child->wsum_hess);

/* wsum_hess = term1 + term2 */
int max_nnz = node->work->hess_term1->nnz + node->work->hess_term2->nnz;
node->wsum_hess =
new_sparse_matrix_alloc(node->n_vars, node->n_vars, max_nnz);
sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2,
node->wsum_hess);
sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/atoms/other/quad_form.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,8 @@ static void wsum_hess_init_impl(expr *node)
node->work->hess_term2 = copy_sparsity_matrices_alloc(x->wsum_hess);

/* hess = term1 + term2 */
int max_nnz = node->work->hess_term1->nnz + node->work->hess_term2->nnz;
node->wsum_hess = new_sparse_matrix_alloc(node->n_vars, node->n_vars, max_nnz);
sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2,
node->wsum_hess);
node->wsum_hess =
sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2);
}

static void eval_wsum_hess_impl(expr *node, const double *w)
Expand Down
5 changes: 5 additions & 0 deletions tests/all_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include "wsum_hess/bivariate_restricted_dom/test_rel_entr.h"
#include "wsum_hess/bivariate_restricted_dom/test_rel_entr_scalar_vector.h"
#include "wsum_hess/bivariate_restricted_dom/test_rel_entr_vector_scalar.h"
#include "wsum_hess/composite/test_binary_sums.h"
#include "wsum_hess/composite/test_chain_rule_wsum_hess.h"
#include "wsum_hess/composite/test_shared_dag.h"
#include "wsum_hess/elementwise_full_dom/test_exp.h"
Expand Down Expand Up @@ -394,6 +395,10 @@ int main(void)
mu_run_test(test_wsum_hess_add_shared_subexpr, tests_run);
mu_run_test(test_wsum_hess_multiply_shared_subexpr, tests_run);
mu_run_test(test_wsum_hess_matmul_shared_subexpr, tests_run);
mu_run_test(test_binary_sum_add_dense, tests_run);
mu_run_test(test_binary_sum_multiply_dense, tests_run);
mu_run_test(test_binary_sum_multiply_sparse, tests_run);
mu_run_test(test_binary_sum_matmul_dense, tests_run);
mu_run_test(test_shared_dag_chain, tests_run);
mu_run_test(test_shared_dag_scaling_atoms, tests_run);
mu_run_test(test_shared_dag_quad_form, tests_run);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ const char *test_quad_over_lin4(void)
eval_jacobian(node);

double expected_vals[4] = {-76.25, 71.0, 94.0, 117.0};
mu_assert("composed quad_over_lin keeps full row dense",
node->jacobian->kind == MATRIX_PERMUTED_DENSE);
int expected_Ap[2] = {0, 4};
int expected_Ai[4] = {2, 5, 6, 7};

Expand Down
136 changes: 136 additions & 0 deletions tests/wsum_hess/composite/test_binary_sums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#ifndef TEST_BINARY_SUMS_H
#define TEST_BINARY_SUMS_H

#include "atoms/affine.h"
#include "atoms/bivariate_full_dom.h"
#include "atoms/elementwise_full_dom.h"
#include "minunit.h"
#include "numerical_diff.h"

/* A binary sum of two matrices that together fill a dense block must stay
dense instead of falling back to a sparse matrix. */

/* sum(sqrt(1 + (A x + A y)^2)): the Jacobian of the add and the Hessian of
the outer power are both binary sums with a dense result. */
const char *test_binary_sum_add_dense(void)
{
double A[12] = {1, 2, -1, 3, 2, -1, 3, 1, -2, 1, 1, 2};
double u[6] = {0.2, 0.3, -0.1, 0.4, 0.1, -0.2};
double w = 1.0;
double one = 1.0;

expr *x = new_variable(3, 1, 0, 6);
expr *y = new_variable(3, 1, 3, 6);
expr *Ax = new_left_matmul_dense(NULL, x, 4, 3, A);
expr *Ay = new_left_matmul_dense(NULL, y, 4, 3, A);
expr *z = new_add(Ax, Ay);
expr *shifted = new_add(new_power(z, 2.0),
new_promote(new_parameter(1, 1, -1, 6, &one), 4, 1));
expr *root = new_sum(new_power(shifted, 0.5), -1);

mu_assert("add dense jacobian check failed",
check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("add dense wsum_hess check failed",
check_wsum_hess(root, u, &w, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("add jacobian should be dense",
z->jacobian->kind == MATRIX_PERMUTED_DENSE && z->jacobian->nnz == 24);
mu_assert("elementwise wsum_hess should be dense",
root->wsum_hess->kind == MATRIX_PERMUTED_DENSE &&
root->wsum_hess->nnz == 36);

free_expr(root);
return 0;
}

/* (A x) * (B x) elementwise: the Jacobian is a binary sum of two dense
matrices. */
const char *test_binary_sum_multiply_dense(void)
{
double A[12] = {1, 2, -1, 3, 2, -1, 3, 1, -2, 1, 1, 2};
double B[12] = {2, -1, 3, 1, 1, 2, -1, 2, 1, 3, -2, 1};
double u[3] = {0.2, 0.3, -0.1};
double w[4] = {1.0, -0.5, 2.0, 0.3};

expr *x = new_variable(3, 1, 0, 3);
expr *Ax = new_left_matmul_dense(NULL, x, 4, 3, A);
expr *Bx = new_left_matmul_dense(NULL, x, 4, 3, B);
expr *root = new_elementwise_mult(Ax, Bx);

mu_assert("multiply dense jacobian check failed",
check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("multiply dense wsum_hess check failed",
check_wsum_hess(root, u, w, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("multiply jacobian should be dense",
root->jacobian->kind == MATRIX_PERMUTED_DENSE &&
root->jacobian->nnz == 12);

free_expr(root);
return 0;
}

/* (S x) * (T x) elementwise with one entry per row in S and T: the Jacobian
is a binary sum that does not fill a dense block and must stay sparse. */
const char *test_binary_sum_multiply_sparse(void)
{
double u[3] = {0.2, 0.3, -0.1};
double w[4] = {1.0, -0.5, 2.0, 0.3};

CSR_matrix *S = new_CSR_matrix(4, 3, 4);
CSR_matrix *T = new_CSR_matrix(4, 3, 4);
for (int i = 0; i < 4; i++)
{
S->p[i] = i;
S->i[i] = i % 3;
S->x[i] = 0.5 + i;
T->p[i] = i;
T->i[i] = (i + 1) % 3;
T->x[i] = 2.0 - i;
}
S->p[4] = 4;
T->p[4] = 4;

expr *x = new_variable(3, 1, 0, 3);
expr *Sx = new_left_matmul(NULL, x, S);
expr *Tx = new_left_matmul(NULL, x, T);
expr *root = new_elementwise_mult(Sx, Tx);
free_CSR_matrix(S);
free_CSR_matrix(T);

mu_assert("multiply sparse jacobian check failed",
check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("multiply sparse wsum_hess check failed",
check_wsum_hess(root, u, w, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("multiply jacobian should stay sparse",
root->jacobian->kind == MATRIX_SPARSE && root->jacobian->nnz == 8);

free_expr(root);
return 0;
}

/* (A x)^T (B x): the chain-rule Jacobian of matmul is a binary sum with a
dense result. */
const char *test_binary_sum_matmul_dense(void)
{
double A[4] = {1, 2, -1, 3};
double B[4] = {2, -1, 3, 1};
double u[2] = {0.2, 0.3};
double w = 1.0;

expr *x = new_variable(2, 1, 0, 2);
expr *Ax = new_left_matmul_dense(NULL, x, 2, 2, A);
expr *Bx = new_left_matmul_dense(NULL, x, 2, 2, B);
expr *root = new_matmul(new_transpose(Ax), Bx);

mu_assert("matmul dense jacobian check failed",
check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("matmul dense wsum_hess check failed",
check_wsum_hess(root, u, &w, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("matmul jacobian should be dense",
root->jacobian->kind == MATRIX_PERMUTED_DENSE &&
root->jacobian->nnz == 2);

free_expr(root);
return 0;
}

#endif
2 changes: 2 additions & 0 deletions tests/wsum_hess/other/test_quad_form_dense.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const char *test_wsum_hess_quad_form_dense_exp(void)

mu_assert("dense quad_form nonlinear composition wsum_hess failed",
check_wsum_hess(node, u_vals, &w, NUMERICAL_DIFF_DEFAULT_H));
mu_assert("quad_form binary Hessian sum remains dense",
node->wsum_hess->kind == MATRIX_PERMUTED_DENSE);

free_expr(node);
return 0;
Expand Down
Loading