Skip to content

Commit 7639a7d

Browse files
committed
update with runtime throws instead of static_asserts for laplace
1 parent f203d7e commit 7639a7d

3 files changed

Lines changed: 79 additions & 22 deletions

File tree

stan/math/mix/functor/laplace_marginal_density.hpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,15 @@ inline void set_zero_adjoint(Output&& output) {
304304
if constexpr (is_all_arithmetic_scalar_v<output_i_t>) {
305305
return;
306306
} else if constexpr (is_std_vector<output_i_t>::value) {
307-
Eigen::Map<const Eigen::Matrix<var, -1, -1>> map_x(output_i.data(),
308-
output_i.size());
309-
map_x.adj().setZero();
307+
for (Eigen::Index i = 0; i < output_i.size(); ++i) {
308+
output_i[i].adj() = 0;
309+
}
310310
} else if constexpr (is_eigen_v<output_i_t>) {
311311
output_i.adj().setZero();
312312
} else if constexpr (is_stan_scalar_v<output_i_t>) {
313313
output_i.adj() = 0;
314314
} else {
315-
static_assert(
316-
sizeof(std::decay_t<Output>*) == 0,
317-
"INTERNAL ERROR:(laplace_marginal_lpdf) set_zero_adjoints was "
315+
throw std::domain_error("INTERNAL ERROR:(laplace_marginal_lpdf) set_zero_adjoints was "
318316
"not able to deduce the actions needed for the given type.");
319317
}
320318
},
@@ -357,10 +355,8 @@ inline void collect_adjoints(Output& output, Input&& input) {
357355
input_i.adj() = 0;
358356
}
359357
} else {
360-
static_assert(
361-
sizeof(std::decay_t<Output>*) == 0,
362-
"INTERNAL ERROR:(laplace_marginal_lpdf) collect_adjoints was not "
363-
"able to deduce the actiopns needed for the given type.");
358+
throw std::domain_error("INTERNAL ERROR:(laplace_marginal_lpdf) set_zero_adjoints was "
359+
"not able to deduce the actions needed for the given type.");
364360
}
365361
},
366362
std::forward<Output>(output), std::forward<Input>(input));
@@ -811,10 +807,9 @@ inline void collect_adjoints(Output&& output, Input&& input) {
811807
} else if constexpr (is_stan_scalar_v<output_i_t>) {
812808
output_i += input_i;
813809
} else {
814-
static_assert(
815-
sizeof(std::decay_t<Output>*) == 0,
816-
"INTERNAL ERROR:(laplace_marginal_lpdf) collect_adjoints was not "
817-
"able to deduce the actiopns needed for the given type.");
810+
throw std::domain_error("INTERNAL ERROR:(laplace_marginal_lpdf) set_zero_adjoints was "
811+
"not able to deduce the actions needed for the given type.");
812+
818813
}
819814
},
820815
std::forward<Output>(output), std::forward<Input>(input));
@@ -861,10 +856,8 @@ inline void copy_compute_s2(Output&& output, Input&& input) {
861856
input_i.adj() = 0;
862857
}
863858
} else {
864-
static_assert(
865-
sizeof(std::decay_t<Output>*) == 0,
866-
"INTERNAL ERROR:(laplace_marginal_lpdf) copy_compute_s2 was not "
867-
"able to deduce the actiopns needed for the given type.");
859+
throw std::domain_error("INTERNAL ERROR:(laplace_marginal_lpdf) set_zero_adjoints was "
860+
"not able to deduce the actions needed for the given type.");
868861
}
869862
},
870863
std::forward<Output>(output), std::forward<Input>(input));
@@ -920,8 +913,7 @@ inline constexpr auto make_zeroed_arena(Input&& input) {
920913
template <typename Output, typename Input>
921914
inline void collect_adjoints(Output&& output, const vari* ret, Input&& input) {
922915
if constexpr (is_tuple_v<Output>) {
923-
static_assert(!is_tuple_v<Output>,
924-
"INTERNAL ERROR:(laplace_marginal_lpdf)"
916+
throw std::domain_error("INTERNAL ERROR:(laplace_marginal_lpdf)"
925917
"Accumulate Adjoints called on a tuple, but tuples cannot be "
926918
"on the reverse mode stack!"
927919
"This is an internal error, please report it to the stan "

stan/math/prim/fun/promote_scalar.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ inline constexpr auto promote_scalar(UnPromotedTypes&& x) {
4545
} else if constexpr (is_stan_scalar_v<UnPromotedTypes>) {
4646
return PromotionScalars(std::forward<UnPromotedTypes>(x));
4747
} else {
48-
static_assert(sizeof(std::decay_t<UnPromotedTypes>*) == 0,
49-
"Missed type in promote_scalar!");
48+
throw std::domain_error("promote_scalar: "
49+
"Unrecognized type for promotion");
5050
}
5151
}
5252

test/unit/math/laplace/laplace_marginal_lpdf_test.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,71 @@ struct poisson_log_likelihood_tuple {
3939
}
4040
};
4141

42+
struct poisson_log_likelihood_tuple_expanded {
43+
template <typename Theta, typename Eta, typename Eta1, typename Eta2>
44+
auto operator()(const Theta& theta, const std::vector<int>& delta_int,
45+
Eta&& eta, Eta1&& eta1, Eta2&& eta2, std::ostream* pstream) const {
46+
return stan::math::poisson_log_lpmf(delta_int, theta) + std::get<0>(eta)
47+
+ std::get<1>(eta) + stan::math::sum(eta1) + stan::math::sum(eta2);
48+
}
49+
};
50+
51+
TEST(laplace, poisson_log_phi_dim_2_tuple_extended) {
52+
using stan::math::laplace_marginal;
53+
using stan::math::laplace_marginal_tol;
54+
using stan::math::to_vector;
55+
using stan::math::value_of;
56+
using stan::math::var;
57+
// logger->current_test_name_ = "poisson_log_phi_dim_2";
58+
int dim_phi = 2;
59+
Eigen::Matrix<double, Eigen::Dynamic, 1> phi_dbl(dim_phi);
60+
phi_dbl << 1.6, 0.45;
61+
62+
int dim_theta = 2;
63+
Eigen::VectorXd theta_0(dim_theta);
64+
theta_0 << 0, 0;
65+
66+
int dim_x = 2;
67+
std::vector<Eigen::VectorXd> x(dim_theta);
68+
Eigen::VectorXd x_0{{0.05100797, 0.16086164}};
69+
Eigen::VectorXd x_1{{-0.59823393, 0.98701425}};
70+
x[0] = x_0;
71+
x[1] = x_1;
72+
73+
Eigen::VectorXd y_dummy;
74+
75+
std::vector<int> n_samples = {1, 1};
76+
std::vector<int> sums = {1, 0};
77+
78+
constexpr double tolerance = 1e-12;
79+
constexpr int max_num_steps = 100;
80+
using stan::is_var_v;
81+
using stan::scalar_type_t;
82+
using stan::math::test::laplace_issue;
83+
constexpr std::array known_issues{laplace_issue{0, 0, 0}};
84+
stan::test::ad_tolerances tols;
85+
tols.gradient_grad_ = 1e-1;
86+
stan::math::test::run_solver_grid(
87+
[&](int solver_num, int hessian_block_size, int max_steps_line_search,
88+
auto&& theta_0) {
89+
auto f_ll = [&](auto&& eta1, auto&& eta2, auto&& eta3) {
90+
auto eta1_tuple = std::make_tuple(eta1(0), eta1(1));
91+
return laplace_marginal_tol<false>(
92+
poisson_log_likelihood_tuple_expanded{},
93+
std::forward_as_tuple(sums, eta1_tuple, eta2, eta3), theta_0,
94+
stan::math::test::squared_kernel_functor{},
95+
std::forward_as_tuple(
96+
x, std::make_tuple(phi_dbl(0), phi_dbl(1))),
97+
tolerance, max_num_steps, hessian_block_size, solver_num,
98+
max_steps_line_search, nullptr);
99+
};
100+
Eigen::VectorXd test1(phi_dbl);
101+
std::vector<double> test2 = {1.0, 1.0};
102+
stan::test::expect_ad<true>(tols, f_ll, phi_dbl, test1, test2);
103+
},
104+
theta_0);
105+
}
106+
42107
TEST(laplace, poisson_log_phi_dim_2_tuple) {
43108
using stan::math::laplace_marginal;
44109
using stan::math::laplace_marginal_tol;

0 commit comments

Comments
 (0)