From baf3a011a6a407f9dd84e21f44d05ca393b7c90e Mon Sep 17 00:00:00 2001 From: martinmodrak Date: Wed, 12 Feb 2020 13:12:51 +0100 Subject: [PATCH 1/6] Using local_nested_autodiff for all instances of nested autodiff --- stan/math/mix/functor/grad_hessian.hpp | 38 ++-- .../mix/functor/grad_tr_mat_times_hessian.hpp | 57 +++-- stan/math/mix/functor/hessian.hpp | 36 ++- .../math/mix/functor/hessian_times_vector.hpp | 35 ++- stan/math/rev/core.hpp | 1 + stan/math/rev/core/local_nested_autodiff.hpp | 55 +++++ stan/math/rev/core/recover_memory_nested.hpp | 3 + .../rev/core/set_zero_all_adjoints_nested.hpp | 3 + stan/math/rev/core/start_nested.hpp | 3 + stan/math/rev/functor/coupled_ode_system.hpp | 211 ++++++++---------- stan/math/rev/functor/cvodes_ode_data.hpp | 5 +- stan/math/rev/functor/gradient.hpp | 26 +-- stan/math/rev/functor/idas_forward_system.hpp | 82 ++++--- stan/math/rev/functor/integrate_1d.hpp | 35 ++- stan/math/rev/functor/jacobian.hpp | 34 ++- stan/math/rev/functor/map_rect_reduce.hpp | 115 +++++----- test/unit/math/rev/core/gradable.hpp | 69 ++++++ .../rev/core/local_nested_autodiff_test.cpp | 98 ++++++++ test/unit/math/rev/core/var_test.cpp | 57 +---- .../rev/functor/coupled_ode_system_test.cpp | 18 +- 20 files changed, 552 insertions(+), 429 deletions(-) create mode 100644 stan/math/rev/core/local_nested_autodiff.hpp create mode 100644 test/unit/math/rev/core/gradable.hpp create mode 100644 test/unit/math/rev/core/local_nested_autodiff_test.cpp diff --git a/stan/math/mix/functor/grad_hessian.hpp b/stan/math/mix/functor/grad_hessian.hpp index 5f68eb7952a..8a1e8de1506 100644 --- a/stan/math/mix/functor/grad_hessian.hpp +++ b/stan/math/mix/functor/grad_hessian.hpp @@ -50,29 +50,25 @@ void grad_hessian( int d = x.size(); H.resize(d, d); grad_H.resize(d, Matrix(d, d)); - try { - for (int i = 0; i < d; ++i) { - for (int j = i; j < d; ++j) { - start_nested(); - Matrix >, Dynamic, 1> x_ffvar(d); - for (int k = 0; k < d; ++k) { - x_ffvar(k) - = fvar >(fvar(x(k), i == k), fvar(j == k, 0)); - } - fvar > fx_ffvar = f(x_ffvar); - H(i, j) = fx_ffvar.d_.d_.val(); - H(j, i) = H(i, j); - grad(fx_ffvar.d_.d_.vi_); - for (int k = 0; k < d; ++k) { - grad_H[i](j, k) = x_ffvar(k).val_.val_.adj(); - grad_H[j](i, k) = grad_H[i](j, k); - } - recover_memory_nested(); + for (int i = 0; i < d; ++i) { + for (int j = i; j < d; ++j) { + // Run nested autodiff in this scope + local_nested_autodiff nested; + + Matrix >, Dynamic, 1> x_ffvar(d); + for (int k = 0; k < d; ++k) { + x_ffvar(k) + = fvar >(fvar(x(k), i == k), fvar(j == k, 0)); + } + fvar > fx_ffvar = f(x_ffvar); + H(i, j) = fx_ffvar.d_.d_.val(); + H(j, i) = H(i, j); + grad(fx_ffvar.d_.d_.vi_); + for (int k = 0; k < d; ++k) { + grad_H[i](j, k) = x_ffvar(k).val_.val_.adj(); + grad_H[j](i, k) = grad_H[i](j, k); } } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; } } diff --git a/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp b/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp index 415260e7145..eb8ba285a0c 100644 --- a/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp +++ b/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp @@ -18,41 +18,38 @@ void grad_tr_mat_times_hessian( Eigen::Matrix& grad_tr_MH) { using Eigen::Dynamic; using Eigen::Matrix; - start_nested(); - try { - grad_tr_MH.resize(x.size()); - Matrix x_var(x.size()); - for (int i = 0; i < x.size(); ++i) { - x_var(i) = x(i); - } + // Run nested autodiff in this scope + local_nested_autodiff nested; - Matrix, Dynamic, 1> x_fvar(x.size()); - - var sum(0.0); - Matrix M_n(x.size()); - for (int n = 0; n < x.size(); ++n) { - for (int k = 0; k < x.size(); ++k) { - M_n(k) = M(n, k); - } - for (int k = 0; k < x.size(); ++k) { - x_fvar(k) = fvar(x_var(k), k == n); - } - fvar fx; - fvar grad_fx_dot_v; - gradient_dot_vector, double>(f, x_fvar, M_n, fx, grad_fx_dot_v); - sum += grad_fx_dot_v.d_; - } + grad_tr_MH.resize(x.size()); + + Matrix x_var(x.size()); + for (int i = 0; i < x.size(); ++i) { + x_var(i) = x(i); + } - grad(sum.vi_); - for (int i = 0; i < x.size(); ++i) { - grad_tr_MH(i) = x_var(i).adj(); + Matrix, Dynamic, 1> x_fvar(x.size()); + + var sum(0.0); + Matrix M_n(x.size()); + for (int n = 0; n < x.size(); ++n) { + for (int k = 0; k < x.size(); ++k) { + M_n(k) = M(n, k); + } + for (int k = 0; k < x.size(); ++k) { + x_fvar(k) = fvar(x_var(k), k == n); } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; + fvar fx; + fvar grad_fx_dot_v; + gradient_dot_vector, double>(f, x_fvar, M_n, fx, grad_fx_dot_v); + sum += grad_fx_dot_v.d_; + } + + grad(sum.vi_); + for (int i = 0; i < x.size(); ++i) { + grad_tr_MH(i) = x_var(i).adj(); } - recover_memory_nested(); } } // namespace math diff --git a/stan/math/mix/functor/hessian.hpp b/stan/math/mix/functor/hessian.hpp index 1b8c04a5af4..6e513137488 100644 --- a/stan/math/mix/functor/hessian.hpp +++ b/stan/math/mix/functor/hessian.hpp @@ -50,27 +50,23 @@ void hessian(const F& f, const Eigen::Matrix& x, fx = f(x); return; } - try { - for (int i = 0; i < x.size(); ++i) { - start_nested(); - Eigen::Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); - for (int j = 0; j < x.size(); ++j) { - x_fvar(j) = fvar(x(j), i == j); - } - fvar fx_fvar = f(x_fvar); - grad(i) = fx_fvar.d_.val(); - if (i == 0) { - fx = fx_fvar.val_.val(); - } - stan::math::grad(fx_fvar.d_.vi_); - for (int j = 0; j < x.size(); ++j) { - H(i, j) = x_fvar(j).val_.adj(); - } - recover_memory_nested(); + for (int i = 0; i < x.size(); ++i) { + // Run nested autodiff in this scope + local_nested_autodiff nested; + + Eigen::Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); + for (int j = 0; j < x.size(); ++j) { + x_fvar(j) = fvar(x(j), i == j); + } + fvar fx_fvar = f(x_fvar); + grad(i) = fx_fvar.d_.val(); + if (i == 0) { + fx = fx_fvar.val_.val(); + } + stan::math::grad(fx_fvar.d_.vi_); + for (int j = 0; j < x.size(); ++j) { + H(i, j) = x_fvar(j).val_.adj(); } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; } } diff --git a/stan/math/mix/functor/hessian_times_vector.hpp b/stan/math/mix/functor/hessian_times_vector.hpp index c3f44041f1f..3bc37cc58f5 100644 --- a/stan/math/mix/functor/hessian_times_vector.hpp +++ b/stan/math/mix/functor/hessian_times_vector.hpp @@ -17,26 +17,23 @@ void hessian_times_vector(const F& f, double& fx, Eigen::Matrix& Hv) { using Eigen::Matrix; - start_nested(); - try { - Matrix x_var(x.size()); - for (int i = 0; i < x_var.size(); ++i) { - x_var(i) = x(i); - } - var fx_var; - var grad_fx_var_dot_v; - gradient_dot_vector(f, x_var, v, fx_var, grad_fx_var_dot_v); - fx = fx_var.val(); - grad(grad_fx_var_dot_v.vi_); - Hv.resize(x.size()); - for (int i = 0; i < x.size(); ++i) { - Hv(i) = x_var(i).adj(); - } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; + + // Run nested autodiff in this scope + local_nested_autodiff nested; + + Matrix x_var(x.size()); + for (int i = 0; i < x_var.size(); ++i) { + x_var(i) = x(i); + } + var fx_var; + var grad_fx_var_dot_v; + gradient_dot_vector(f, x_var, v, fx_var, grad_fx_var_dot_v); + fx = fx_var.val(); + grad(grad_fx_var_dot_v.vi_); + Hv.resize(x.size()); + for (int i = 0; i < x.size(); ++i) { + Hv(i) = x_var(i).adj(); } - recover_memory_nested(); } template void hessian_times_vector(const F& f, diff --git a/stan/math/rev/core.hpp b/stan/math/rev/core.hpp index ea328f0f228..44a65bb4655 100644 --- a/stan/math/rev/core.hpp +++ b/stan/math/rev/core.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/rev/core/local_nested_autodiff.hpp b/stan/math/rev/core/local_nested_autodiff.hpp new file mode 100644 index 00000000000..6612b7c97f3 --- /dev/null +++ b/stan/math/rev/core/local_nested_autodiff.hpp @@ -0,0 +1,55 @@ +#ifndef STAN_MATH_REV_CORE_LOCAL_NESTED_AUTODIFF_HPP +#define STAN_MATH_REV_CORE_LOCAL_NESTED_AUTODIFF_HPP + +#include +#include +#include + +namespace stan { +namespace math { + +/** + * A class following the RAII idiom to start and recover nested autodiff scopes. + * This is the preferred way to use nested autodiff. Example: + * + * var a; // allocated normally + * { + * local_nested_autodiff nested; // Starts nested autodiff + * + * var nested_var; //allocated on the nested stack + * // Do stuff on the nested stack + * + * // Nested stack is automatically recovered at the end of scope where + * // nested was declared, including exceptions, returns, etc. + * } + * var b; + */ +class local_nested_autodiff { +public: + local_nested_autodiff() + { + start_nested(); + } + + ~local_nested_autodiff() + { + recover_memory_nested(); + } + + // Prevent undesirable operations + local_nested_autodiff(const local_nested_autodiff&) = delete; + local_nested_autodiff& operator=(const local_nested_autodiff&) = delete; + void* operator new(std::size_t) = delete; + + /** + * Reset all adjoint values in this nested stack + * to zero. + **/ + void set_zero_all_adjoints() { + set_zero_all_adjoints_nested(); + } +}; + +} // namespace math +} // namespace stan +#endif diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index c002f452e3f..5489ec03465 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -14,6 +14,9 @@ namespace math { * is nothing on the nested stack, then a * std::logic_error exception is thrown. * + * It is preferred to use the local_nested_autodiff class for + * nested autodiff as it handles recovery of memory automatically. + * * @throw std::logic_error if empty_nested() returns * true */ diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index d23bfe19fd7..e5ffa3e18ad 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -13,6 +13,9 @@ namespace math { /** * Reset all adjoint values in the top nested portion of the stack * to zero. + * + * It is preferred to use the local_nested_autodiff class for + * nested autodiff class as it handles recovery of memory automatically. */ static void set_zero_all_adjoints_nested() { if (empty_nested()) { diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index ff1aece8fb7..4c8fed065ee 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -9,6 +9,9 @@ namespace math { /** * Record the current position so that recover_memory_nested() * can find it. + * + * It is preferred to use the local_nested_autodiff class for + * nested autodiff as it handles recovery of memory automatically. */ static inline void start_nested() { ChainableStack::instance_->nested_var_stack_sizes_.push_back( diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 75b0670b4d0..5e62465bdac 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -116,50 +116,45 @@ struct coupled_ode_system { double t) const { using std::vector; - try { - start_nested(); - - vector y_vars; - y_vars.reserve(N_); - for (std::size_t i = 0; i < N_; ++i) - y_vars.emplace_back(new vari(z[i], false)); - - vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); - - check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), - "states", N_); - - for (size_t i = 0; i < N_; i++) { - dz_dt[i] = dy_dt_vars[i].val(); - dy_dt_vars[i].grad(); - - for (size_t j = 0; j < M_; j++) { - // orders derivatives by equation (i.e. if there are 2 eqns - // (y1, y2) and 2 parameters (a, b), dy_dt will be ordered as: - // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db - double temp_deriv = theta_nochain_[j].adj(); - const size_t offset = N_ + N_ * j; - for (size_t k = 0; k < N_; k++) { - temp_deriv += z[offset + k] * y_vars[k].adj(); - } - - dz_dt[offset + i] = temp_deriv; - } + // Run nested autodiff in this scope + local_nested_autodiff nested; + + vector y_vars; + y_vars.reserve(N_); + for (std::size_t i = 0; i < N_; ++i) + y_vars.emplace_back(new vari(z[i], false)); + + vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); - set_zero_all_adjoints_nested(); - // Parameters stored on the outer (non-nested) nochain stack are not - // reset to zero by the last call. This is done as a separate step here. - // See efficiency note above on template specalization for more details - // on this. - for (size_t j = 0; j < M_; ++j) { - theta_nochain_[j].vi_->set_zero_adjoint(); + check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), + "states", N_); + + for (size_t i = 0; i < N_; i++) { + dz_dt[i] = dy_dt_vars[i].val(); + dy_dt_vars[i].grad(); + + for (size_t j = 0; j < M_; j++) { + // orders derivatives by equation (i.e. if there are 2 eqns + // (y1, y2) and 2 parameters (a, b), dy_dt will be ordered as: + // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db + double temp_deriv = theta_nochain_[j].adj(); + const size_t offset = N_ + N_ * j; + for (size_t k = 0; k < N_; k++) { + temp_deriv += z[offset + k] * y_vars[k].adj(); } + + dz_dt[offset + i] = temp_deriv; + } + + nested.set_zero_all_adjoints(); + // Parameters stored on the outer (non-nested) nochain stack are not + // reset to zero by the last call. This is done as a separate step here. + // See efficiency note above on template specalization for more details + // on this. + for (size_t j = 0; j < M_; ++j) { + theta_nochain_[j].vi_->set_zero_adjoint(); } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; } - recover_memory_nested(); } /** @@ -276,44 +271,39 @@ struct coupled_ode_system { double t) const { using std::vector; - try { - start_nested(); - - vector y_vars; - y_vars.reserve(N_); - for (std::size_t i = 0; i < N_; ++i) - y_vars.emplace_back(new vari(z[i], false)); - - vector dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_); + // Run nested autodiff in this scope + local_nested_autodiff nested; - check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), - "states", N_); + vector y_vars; + y_vars.reserve(N_); + for (std::size_t i = 0; i < N_; ++i) + y_vars.emplace_back(new vari(z[i], false)); - for (size_t i = 0; i < N_; i++) { - dz_dt[i] = dy_dt_vars[i].val(); - dy_dt_vars[i].grad(); + vector dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_); - for (size_t j = 0; j < N_; j++) { - // orders derivatives by equation (i.e. if there are 2 eqns - // (y1, y2) and 2 initial conditions (y0_a, y0_b), dy_dt will be - // ordered as: dy1_dt, dy2_dt, dy1_d{y0_a}, dy2_d{y0_a}, dy1_d{y0_b}, - // dy2_d{y0_b} - double temp_deriv = 0; - const size_t offset = N_ + N_ * j; - for (size_t k = 0; k < N_; k++) { - temp_deriv += z[offset + k] * y_vars[k].adj(); - } + check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), + "states", N_); - dz_dt[offset + i] = temp_deriv; + for (size_t i = 0; i < N_; i++) { + dz_dt[i] = dy_dt_vars[i].val(); + dy_dt_vars[i].grad(); + + for (size_t j = 0; j < N_; j++) { + // orders derivatives by equation (i.e. if there are 2 eqns + // (y1, y2) and 2 initial conditions (y0_a, y0_b), dy_dt will be + // ordered as: dy1_dt, dy2_dt, dy1_d{y0_a}, dy2_d{y0_a}, dy1_d{y0_b}, + // dy2_d{y0_b} + double temp_deriv = 0; + const size_t offset = N_ + N_ * j; + for (size_t k = 0; k < N_; k++) { + temp_deriv += z[offset + k] * y_vars[k].adj(); } - set_zero_all_adjoints_nested(); + dz_dt[offset + i] = temp_deriv; } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; + + nested.set_zero_all_adjoints(); } - recover_memory_nested(); } /** @@ -457,60 +447,55 @@ struct coupled_ode_system { double t) const { using std::vector; - try { - start_nested(); - - vector y_vars; - y_vars.reserve(N_); - for (std::size_t i = 0; i < N_; ++i) - y_vars.emplace_back(new vari(z[i], false)); + // Run nested autodiff in this scope + local_nested_autodiff nested; - vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); + vector y_vars; + y_vars.reserve(N_); + for (std::size_t i = 0; i < N_; ++i) + y_vars.emplace_back(new vari(z[i], false)); - check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), - "states", N_); + vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); - for (size_t i = 0; i < N_; i++) { - dz_dt[i] = dy_dt_vars[i].val(); - dy_dt_vars[i].grad(); + check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), + "states", N_); - for (size_t j = 0; j < N_; j++) { - // orders derivatives by equation (i.e. if there are 2 eqns - // (y1, y2) and 2 parameters (a, b), dy_dt will be ordered as: - // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db - double temp_deriv = 0; - const size_t offset = N_ + N_ * j; - for (size_t k = 0; k < N_; k++) { - temp_deriv += z[offset + k] * y_vars[k].adj(); - } - - dz_dt[offset + i] = temp_deriv; + for (size_t i = 0; i < N_; i++) { + dz_dt[i] = dy_dt_vars[i].val(); + dy_dt_vars[i].grad(); + + for (size_t j = 0; j < N_; j++) { + // orders derivatives by equation (i.e. if there are 2 eqns + // (y1, y2) and 2 parameters (a, b), dy_dt will be ordered as: + // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db + double temp_deriv = 0; + const size_t offset = N_ + N_ * j; + for (size_t k = 0; k < N_; k++) { + temp_deriv += z[offset + k] * y_vars[k].adj(); } - for (size_t j = 0; j < M_; j++) { - double temp_deriv = theta_nochain_[j].adj(); - const size_t offset = N_ + N_ * N_ + N_ * j; - for (size_t k = 0; k < N_; k++) { - temp_deriv += z[offset + k] * y_vars[k].adj(); - } + dz_dt[offset + i] = temp_deriv; + } - dz_dt[offset + i] = temp_deriv; + for (size_t j = 0; j < M_; j++) { + double temp_deriv = theta_nochain_[j].adj(); + const size_t offset = N_ + N_ * N_ + N_ * j; + for (size_t k = 0; k < N_; k++) { + temp_deriv += z[offset + k] * y_vars[k].adj(); } - set_zero_all_adjoints_nested(); - // Parameters stored on the outer (non-nested) nochain stack are not - // reset to zero by the last call. This is done as a separate step here. - // See efficiency note above on template specalization for more details - // on this. - for (size_t j = 0; j < M_; ++j) { - theta_nochain_[j].vi_->set_zero_adjoint(); - } + dz_dt[offset + i] = temp_deriv; + } + + nested.set_zero_all_adjoints(); + // Parameters stored on the outer (non-nested) nochain stack are not + // reset to zero by the last call. This is done as a separate step here. + // See efficiency note above on template specalization for more details + // on this. + for (size_t j = 0; j < M_; ++j) { + theta_nochain_[j].vi_->set_zero_adjoint(); } - } catch (const std::exception& e) { - recover_memory_nested(); - throw; } - recover_memory_nested(); } /** diff --git a/stan/math/rev/functor/cvodes_ode_data.hpp b/stan/math/rev/functor/cvodes_ode_data.hpp index 2036c5f5d4e..69eed446063 100644 --- a/stan/math/rev/functor/cvodes_ode_data.hpp +++ b/stan/math/rev/functor/cvodes_ode_data.hpp @@ -163,14 +163,15 @@ class cvodes_ode_data { * y to be the initial of the coupled ode system. */ inline int jacobian_states(double t, const double y[], SUNMatrix J) const { - start_nested(); + // Run nested autodiff in this scope + local_nested_autodiff nested; + const std::vector y_vec_var(y, y + N_); coupled_ode_system ode_jacobian(f_, y_vec_var, theta_dbl_, x_, x_int_, msgs_); std::vector&& jacobian_y = std::vector(ode_jacobian.size()); ode_jacobian(ode_jacobian.initial_state(), jacobian_y, t); std::move(jacobian_y.begin() + N_, jacobian_y.end(), SM_DATA_D(J)); - recover_memory_nested(); return 0; } diff --git a/stan/math/rev/functor/gradient.hpp b/stan/math/rev/functor/gradient.hpp index 103cc677178..2c27e054b4c 100644 --- a/stan/math/rev/functor/gradient.hpp +++ b/stan/math/rev/functor/gradient.hpp @@ -41,21 +41,17 @@ namespace math { template void gradient(const F& f, const Eigen::Matrix& x, double& fx, Eigen::Matrix& grad_fx) { - start_nested(); - try { - Eigen::Matrix x_var(x.size()); - for (int i = 0; i < x.size(); ++i) - x_var(i) = var(new vari(x(i), false)); - var fx_var = f(x_var); - fx = fx_var.val(); - grad_fx.resize(x.size()); - grad(fx_var.vi_); - grad_fx = x_var.adj(); - } catch (const std::exception& /*e*/) { - recover_memory_nested(); - throw; - } - recover_memory_nested(); + // Run nested autodiff in this scope + local_nested_autodiff nested; + + Eigen::Matrix x_var(x.size()); + for (int i = 0; i < x.size(); ++i) + x_var(i) = var(new vari(x(i), false)); + var fx_var = f(x_var); + fx = fx_var.val(); + grad_fx.resize(x.size()); + grad(fx_var.vi_); + grad_fx = x_var.adj(); } } // namespace math diff --git a/stan/math/rev/functor/idas_forward_system.hpp b/stan/math/rev/functor/idas_forward_system.hpp index 7d41d131a42..cb534ca0e4c 100644 --- a/stan/math/rev/functor/idas_forward_system.hpp +++ b/stan/math/rev/functor/idas_forward_system.hpp @@ -121,56 +121,50 @@ class idas_forward_system : public idas_system { auto yys_mat = matrix_d_from_NVarray(yys, ns); auto yps_mat = matrix_d_from_NVarray(yps, ns); - try { - stan::math::start_nested(); - - MatrixXd J, r; - VectorXd f_val; - - auto fyy - = [&t, &vyp, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { - std::vector yy(x.data(), x.data() + N); - auto eval - = dae->f_(t, yy, vyp, vtheta, dae->x_r_, dae->x_i_, dae->msgs_); - Eigen::Map res(eval.data(), N); - return res; - }; - stan::math::jacobian(fyy, vec_yy, f_val, J); - r = J * yys_mat; - - auto fyp - = [&t, &vyy, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { - std::vector yp(x.data(), x.data() + N); + // Run nested autodiff in this scope + stan::math::local_nested_autodiff nested; + + MatrixXd J, r; + VectorXd f_val; + + auto fyy + = [&t, &vyp, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { + std::vector yy(x.data(), x.data() + N); + auto eval + = dae->f_(t, yy, vyp, vtheta, dae->x_r_, dae->x_i_, dae->msgs_); + Eigen::Map res(eval.data(), N); + return res; + }; + stan::math::jacobian(fyy, vec_yy, f_val, J); + r = J * yys_mat; + + auto fyp + = [&t, &vyy, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { + std::vector yp(x.data(), x.data() + N); + auto eval + = dae->f_(t, vyy, yp, vtheta, dae->x_r_, dae->x_i_, dae->msgs_); + Eigen::Map res(eval.data(), N); + return res; + }; + stan::math::jacobian(fyp, vec_yp, f_val, J); + r += J * yps_mat; + + if (dae->is_var_par) { + auto fpar + = [&t, &vyy, &vyp, &N, &M, &dae](const matrix_v& x) -> vector_v { + std::vector par(x.data(), x.data() + M); auto eval - = dae->f_(t, vyy, yp, vtheta, dae->x_r_, dae->x_i_, dae->msgs_); + = dae->f_(t, vyy, vyp, par, dae->x_r_, dae->x_i_, dae->msgs_); Eigen::Map res(eval.data(), N); return res; }; - stan::math::jacobian(fyp, vec_yp, f_val, J); - r += J * yps_mat; - - if (dae->is_var_par) { - auto fpar - = [&t, &vyy, &vyp, &N, &M, &dae](const matrix_v& x) -> vector_v { - std::vector par(x.data(), x.data() + M); - auto eval - = dae->f_(t, vyy, vyp, par, dae->x_r_, dae->x_i_, dae->msgs_); - Eigen::Map res(eval.data(), N); - return res; - }; - stan::math::jacobian(fpar, vec_par, f_val, J); - r.block(0, (dae->is_var_yy0 ? N : 0) + (dae->is_var_yp0 ? N : 0), N, - M) - += J; // only for theta - } - - matrix_d_to_NVarray(r, ress, ns); - } catch (const std::exception& e) { - stan::math::recover_memory_nested(); - throw; + stan::math::jacobian(fpar, vec_par, f_val, J); + r.block(0, (dae->is_var_yy0 ? N : 0) + (dae->is_var_yp0 ? N : 0), N, + M) + += J; // only for theta } - stan::math::recover_memory_nested(); + matrix_d_to_NVarray(r, ress, ns); return 0; }; diff --git a/stan/math/rev/functor/integrate_1d.hpp b/stan/math/rev/functor/integrate_1d.hpp index e61a2f1fbdc..e38a6ddf3b5 100644 --- a/stan/math/rev/functor/integrate_1d.hpp +++ b/stan/math/rev/functor/integrate_1d.hpp @@ -36,28 +36,25 @@ inline double gradient_of_f(const F &f, const double &x, const double &xc, const std::vector &x_i, size_t n, std::ostream *msgs) { double gradient = 0.0; - start_nested(); + + // Run nested autodiff in this scope + local_nested_autodiff nested; + std::vector theta_var(theta_vals.size()); - try { - for (size_t i = 0; i < theta_vals.size(); i++) { - theta_var[i] = theta_vals[i]; - } - var fx = f(x, xc, theta_var, x_r, x_i, msgs); - fx.grad(); - gradient = theta_var[n].adj(); - if (is_nan(gradient)) { - if (fx.val() == 0) { - gradient = 0; - } else { - throw_domain_error("gradient_of_f", "The gradient of f", n, - "is nan for parameter ", ""); - } + for (size_t i = 0; i < theta_vals.size(); i++) { + theta_var[i] = theta_vals[i]; + } + var fx = f(x, xc, theta_var, x_r, x_i, msgs); + fx.grad(); + gradient = theta_var[n].adj(); + if (is_nan(gradient)) { + if (fx.val() == 0) { + gradient = 0; + } else { + throw_domain_error("gradient_of_f", "The gradient of f", n, + "is nan for parameter ", ""); } - } catch (const std::exception &e) { - recover_memory_nested(); - throw; } - recover_memory_nested(); return gradient; } diff --git a/stan/math/rev/functor/jacobian.hpp b/stan/math/rev/functor/jacobian.hpp index 966a71c570d..e2f29ac3d6c 100644 --- a/stan/math/rev/functor/jacobian.hpp +++ b/stan/math/rev/functor/jacobian.hpp @@ -16,26 +16,22 @@ void jacobian(const F& f, const Eigen::Matrix& x, Eigen::Matrix& J) { using Eigen::Dynamic; using Eigen::Matrix; - start_nested(); - try { - Matrix x_var(x); - Matrix fx_var = f(x_var); - fx.resize(fx_var.size()); - J.resize(x.size(), fx_var.size()); - fx = fx_var.val(); - grad(fx_var(0).vi_); - J.col(0) = x_var.adj(); - for (int i = 1; i < fx_var.size(); ++i) { - set_zero_all_adjoints_nested(); - grad(fx_var(i).vi_); - J.col(i) = x_var.adj(); - } - J.transposeInPlace(); - } catch (const std::exception& e) { - recover_memory_nested(); - throw; + // Run nested autodiff in this scope + local_nested_autodiff nested; + + Matrix x_var(x); + Matrix fx_var = f(x_var); + fx.resize(fx_var.size()); + J.resize(x.size(), fx_var.size()); + fx = fx_var.val(); + grad(fx_var(0).vi_); + J.col(0) = x_var.adj(); + for (int i = 1; i < fx_var.size(); ++i) { + nested.set_zero_all_adjoints(); + grad(fx_var(i).vi_); + J.col(i) = x_var.adj(); } - recover_memory_nested(); + J.transposeInPlace(); } } // namespace math diff --git a/stan/math/rev/functor/map_rect_reduce.hpp b/stan/math/rev/functor/map_rect_reduce.hpp index 38ce7d643d5..25f994a4d35 100644 --- a/stan/math/rev/functor/map_rect_reduce.hpp +++ b/stan/math/rev/functor/map_rect_reduce.hpp @@ -24,34 +24,30 @@ struct map_rect_reduce { const size_type num_job_specific_params = job_specific_params.rows(); matrix_d out(1 + num_shared_params + num_job_specific_params, 0); - try { - start_nested(); - vector_v shared_params_v = to_var(shared_params); - vector_v job_specific_params_v = to_var(job_specific_params); - - vector_v fx_v - = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs); - - const size_type size_f = fx_v.rows(); - - out.resize(Eigen::NoChange, size_f); - - for (size_type i = 0; i < size_f; ++i) { - out(0, i) = fx_v(i).val(); - set_zero_all_adjoints_nested(); - fx_v(i).grad(); - for (size_type j = 0; j < num_shared_params; ++j) { - out(1 + j, i) = shared_params_v(j).vi_->adj_; - } - for (size_type j = 0; j < num_job_specific_params; ++j) { - out(1 + num_shared_params + j, i) - = job_specific_params_v(j).vi_->adj_; - } + // Run nested autodiff in this scope + local_nested_autodiff nested; + + vector_v shared_params_v = to_var(shared_params); + vector_v job_specific_params_v = to_var(job_specific_params); + + vector_v fx_v + = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs); + + const size_type size_f = fx_v.rows(); + + out.resize(Eigen::NoChange, size_f); + + for (size_type i = 0; i < size_f; ++i) { + out(0, i) = fx_v(i).val(); + nested.set_zero_all_adjoints(); + fx_v(i).grad(); + for (size_type j = 0; j < num_shared_params; ++j) { + out(1 + j, i) = shared_params_v(j).vi_->adj_; + } + for (size_type j = 0; j < num_job_specific_params; ++j) { + out(1 + num_shared_params + j, i) + = job_specific_params_v(j).vi_->adj_; } - recover_memory_nested(); - } catch (const std::exception& e) { - recover_memory_nested(); - throw; } return out; } @@ -67,28 +63,24 @@ struct map_rect_reduce { const size_type num_job_specific_params = job_specific_params.rows(); matrix_d out(1 + num_job_specific_params, 0); - try { - start_nested(); - vector_v job_specific_params_v = to_var(job_specific_params); + // Run nested autodiff in this scope + local_nested_autodiff nested; + + vector_v job_specific_params_v = to_var(job_specific_params); - vector_v fx_v = F()(shared_params, job_specific_params_v, x_r, x_i, msgs); + vector_v fx_v = F()(shared_params, job_specific_params_v, x_r, x_i, msgs); - const size_type size_f = fx_v.rows(); + const size_type size_f = fx_v.rows(); - out.resize(Eigen::NoChange, size_f); + out.resize(Eigen::NoChange, size_f); - for (size_type i = 0; i < size_f; ++i) { - out(0, i) = fx_v(i).val(); - set_zero_all_adjoints_nested(); - fx_v(i).grad(); - for (size_type j = 0; j < num_job_specific_params; ++j) { - out(1 + j, i) = job_specific_params_v(j).vi_->adj_; - } + for (size_type i = 0; i < size_f; ++i) { + out(0, i) = fx_v(i).val(); + nested.set_zero_all_adjoints(); + fx_v(i).grad(); + for (size_type j = 0; j < num_job_specific_params; ++j) { + out(1 + j, i) = job_specific_params_v(j).vi_->adj_; } - recover_memory_nested(); - } catch (const std::exception& e) { - recover_memory_nested(); - throw; } return out; } @@ -104,31 +96,28 @@ struct map_rect_reduce { const size_type num_shared_params = shared_params.rows(); matrix_d out(1 + num_shared_params, 0); - try { - start_nested(); - vector_v shared_params_v = to_var(shared_params); + // Run nested autodiff in this scope + local_nested_autodiff nested; - vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs); + vector_v shared_params_v = to_var(shared_params); - const size_type size_f = fx_v.rows(); + vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs); - out.resize(Eigen::NoChange, size_f); + const size_type size_f = fx_v.rows(); - for (size_type i = 0; i < size_f; ++i) { - out(0, i) = fx_v(i).val(); - set_zero_all_adjoints_nested(); - fx_v(i).grad(); - for (size_type j = 0; j < num_shared_params; ++j) { - out(1 + j, i) = shared_params_v(j).vi_->adj_; - } - } - recover_memory_nested(); - } catch (const std::exception& e) { - recover_memory_nested(); - throw; + out.resize(Eigen::NoChange, size_f); + + for (size_type i = 0; i < size_f; ++i) { + out(0, i) = fx_v(i).val(); + nested.set_zero_all_adjoints(); + fx_v(i).grad(); + for (size_type j = 0; j < num_shared_params; ++j) { + out(1 + j, i) = shared_params_v(j).vi_->adj_; } - return out; } + + return out; +} }; } // namespace internal diff --git a/test/unit/math/rev/core/gradable.hpp b/test/unit/math/rev/core/gradable.hpp new file mode 100644 index 00000000000..8a1be3f231d --- /dev/null +++ b/test/unit/math/rev/core/gradable.hpp @@ -0,0 +1,69 @@ +#ifndef TEST_UNIT_MATH_REV_CORE_GRADABLE_HPP +#define TEST_UNIT_MATH_REV_CORE_GRADABLE_HPP + +#include +#include +#include +#include +#include + +struct gradable { + AVEC x_; + AVAR f_; + Eigen::Matrix g_expected_; + gradable(const AVEC& x, const AVAR& f, + const Eigen::Matrix& g_expected) + : x_(x), f_(f), g_expected_(g_expected) {} + void test() { + std::vector g; + f_.grad(x_, g); + EXPECT_EQ(g_expected_.size(), static_cast(g.size())); + for (int i = 0; i < g_expected_.size(); ++i) + EXPECT_FLOAT_EQ(g_expected_(i), g[i]); + } + + double adj() { + return f_.adj(); + } +}; + +gradable setup_quad_form() { + using Eigen::Dynamic; + using Eigen::Matrix; + using stan::math::quad_form; + using stan::math::var; + using std::vector; + + Matrix u(3); + u << 2, 3, 5; + + Matrix S(3, 3); + S << 7, 11, 13, 17, 19, 23, 29, 31, 37; + + vector x; + for (int i = 0; i < u.size(); ++i) + x.push_back(u(i)); + for (int i = 0; i < S.size(); ++i) + x.push_back(S(i)); + + var f = quad_form(S, u); + + Matrix g_expected(12); + g_expected << 322, 440, 616, 4, 6, 10, 6, 9, 15, 10, 15, 25; + + return gradable(x, f, g_expected); +} + +gradable setup_simple() { + AVAR a = 3; + AVAR b = 7; + AVEC x; + x.push_back(a); + x.push_back(b); + AVAR f = 2 * a * b; + Eigen::Matrix g_expected(2); + g_expected << 14, 6; + return gradable(x, f, g_expected); +} + +#endif \ No newline at end of file diff --git a/test/unit/math/rev/core/local_nested_autodiff_test.cpp b/test/unit/math/rev/core/local_nested_autodiff_test.cpp new file mode 100644 index 00000000000..eb2ed3483aa --- /dev/null +++ b/test/unit/math/rev/core/local_nested_autodiff_test.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include + +struct AgradLocalNested : public testing::Test { + void SetUp() { + // make sure memory's clean before starting each test + stan::math::recover_memory(); + } +}; + + +TEST_F(AgradLocalNested, local_nested_autodiff_base) { + { + stan::math::local_nested_autodiff nested; + EXPECT_THROW(stan::math::recover_memory(), std::logic_error); + } + stan::math::recover_memory(); // Should not throw + + gradable g_out = setup_quad_form(); + for (int i = 0; i < 100; ++i) { + stan::math::local_nested_autodiff nested; + gradable g = setup_quad_form(); + g.test(); + nested.set_zero_all_adjoints(); + EXPECT_EQ(g.adj(), 0); + } + g_out.test(); +} + +TEST_F(AgradLocalNested, local_nested_autodiff_Gradient1) { + using stan::math::local_nested_autodiff; + + gradable g0 = setup_simple(); + + { + local_nested_autodiff nested; + gradable g1 = setup_quad_form(); + g1.test(); + } + + { + local_nested_autodiff nested; + gradable g2 = setup_simple(); + g2.test(); + } + + g0.test(); + stan::math::recover_memory(); +} + +TEST_F(AgradLocalNested,local_nested_autodiff_Gradient2) { + using stan::math::local_nested_autodiff; + + gradable g0 = setup_quad_form(); + + { + local_nested_autodiff nested; + gradable g1 = setup_simple(); + g1.test(); + } + + { + local_nested_autodiff nested; + gradable g2 = setup_quad_form(); + g2.test(); + } + + g0.test(); + stan::math::recover_memory(); +} + +TEST_F(AgradLocalNested, local_nested_autodiff_Gradient3) { + using stan::math::local_nested_autodiff; + + { + local_nested_autodiff nested; + gradable g1 = setup_simple(); + { + local_nested_autodiff nested2; + gradable g2 = setup_quad_form(); + { + local_nested_autodiff nested3; + gradable g3 = setup_quad_form(); + { + local_nested_autodiff nested4; + gradable g4 = setup_simple(); + g4.test(); + } + g3.test(); + } + g2.test(); + } + g1.test(); + } + stan::math::recover_memory(); +} \ No newline at end of file diff --git a/test/unit/math/rev/core/var_test.cpp b/test/unit/math/rev/core/var_test.cpp index 9ffc46f8499..72b455af121 100644 --- a/test/unit/math/rev/core/var_test.cpp +++ b/test/unit/math/rev/core/var_test.cpp @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include #include #include @@ -190,61 +190,6 @@ TEST_F(AgradRev, print) { // return y * // } -struct gradable { - AVEC x_; - AVAR f_; - Eigen::Matrix g_expected_; - gradable(const AVEC& x, const AVAR& f, - const Eigen::Matrix& g_expected) - : x_(x), f_(f), g_expected_(g_expected) {} - void test() { - std::vector g; - f_.grad(x_, g); - EXPECT_EQ(g_expected_.size(), static_cast(g.size())); - for (int i = 0; i < g_expected_.size(); ++i) - EXPECT_FLOAT_EQ(g_expected_(i), g[i]); - } -}; - -gradable setup_quad_form() { - using Eigen::Dynamic; - using Eigen::Matrix; - using stan::math::quad_form; - using stan::math::var; - using std::vector; - - Matrix u(3); - u << 2, 3, 5; - - Matrix S(3, 3); - S << 7, 11, 13, 17, 19, 23, 29, 31, 37; - - vector x; - for (int i = 0; i < u.size(); ++i) - x.push_back(u(i)); - for (int i = 0; i < S.size(); ++i) - x.push_back(S(i)); - - var f = quad_form(S, u); - - Matrix g_expected(12); - g_expected << 322, 440, 616, 4, 6, 10, 6, 9, 15, 10, 15, 25; - - return gradable(x, f, g_expected); -} - -gradable setup_simple() { - AVAR a = 3; - AVAR b = 7; - AVEC x; - x.push_back(a); - x.push_back(b); - AVAR f = 2 * a * b; - Eigen::Matrix g_expected(2); - g_expected << 14, 6; - return gradable(x, f, g_expected); -} - TEST_F(AgradRev, basicGradient1) { using stan::math::recover_memory; diff --git a/test/unit/math/rev/functor/coupled_ode_system_test.cpp b/test/unit/math/rev/functor/coupled_ode_system_test.cpp index 3de230967d1..e947a427d4b 100644 --- a/test/unit/math/rev/functor/coupled_ode_system_test.cpp +++ b/test/unit/math/rev/functor/coupled_ode_system_test.cpp @@ -18,7 +18,8 @@ struct StanAgradRevOde : public ::testing::Test { TEST_F(StanAgradRevOde, coupled_ode_system_dv) { using stan::math::coupled_ode_system; - stan::math::start_nested(); + // Run nested autodiff in this scope + stan::math::local_nested_autodiff nested; harm_osc_ode_fun harm_osc; @@ -49,14 +50,15 @@ TEST_F(StanAgradRevOde, coupled_ode_system_dv) { EXPECT_EQ(stack_size, stan::math::nested_size()) << "expecting no new things on the stack"; + std::cout << "A" << std::endl; system(z0, dz_dt, t0); + std::cout << "B" << std::endl; EXPECT_FLOAT_EQ(0.5, dz_dt[0]); EXPECT_FLOAT_EQ(-1.075, dz_dt[1]); EXPECT_FLOAT_EQ(2, dz_dt[2]); EXPECT_FLOAT_EQ(-1.8, dz_dt[3]); - stan::math::recover_memory_nested(); } TEST_F(StanAgradRevOde, initial_state_dv) { using stan::math::coupled_ode_system; @@ -164,7 +166,8 @@ TEST_F(StanAgradRevOde, memory_recovery_exception_dv) { TEST_F(StanAgradRevOde, coupled_ode_system_vd) { using stan::math::coupled_ode_system; - stan::math::start_nested(); + // Run nested autodiff in this scope + stan::math::local_nested_autodiff nested; harm_osc_ode_fun harm_osc; @@ -208,9 +211,8 @@ TEST_F(StanAgradRevOde, coupled_ode_system_vd) { EXPECT_FLOAT_EQ(-1.0 * 1.0 - 0.15 * 0.0, dz_dt[3]); EXPECT_FLOAT_EQ(0.0 * 0.0 + 1.0 * 1.0, dz_dt[4]); EXPECT_FLOAT_EQ(-1.0 * 0.0 - 0.15 * 1.0, dz_dt[5]); - - stan::math::recover_memory_nested(); } + TEST_F(StanAgradRevOde, initial_state_vd) { using stan::math::coupled_ode_system; using stan::math::var; @@ -317,7 +319,9 @@ TEST_F(StanAgradRevOde, memory_recovery_exception_vd) { TEST_F(StanAgradRevOde, coupled_ode_system_vv) { using stan::math::coupled_ode_system; - stan::math::start_nested(); + // Run nested autodiff in this scope + stan::math::local_nested_autodiff nested; + const size_t N = 2; const size_t M = 1; const size_t z_size = N + N * N + N * M; @@ -369,8 +373,6 @@ TEST_F(StanAgradRevOde, coupled_ode_system_vv) { EXPECT_FLOAT_EQ(-0.15, dz_dt[5]); EXPECT_FLOAT_EQ(0, dz_dt[6]); EXPECT_FLOAT_EQ(-0.5, dz_dt[7]); - - stan::math::recover_memory_nested(); } TEST_F(StanAgradRevOde, initial_state_vv) { using stan::math::coupled_ode_system; From 9af29af2d3eafe2461c72c7e94e4527b2909eb14 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 12 Feb 2020 12:22:54 +0000 Subject: [PATCH 2/6] [Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (tags/RELEASE_500/final) --- stan/math/rev/core/local_nested_autodiff.hpp | 24 +++++-------- stan/math/rev/core/recover_memory_nested.hpp | 4 +-- .../rev/core/set_zero_all_adjoints_nested.hpp | 4 +-- stan/math/rev/core/start_nested.hpp | 4 +-- stan/math/rev/functor/coupled_ode_system.hpp | 12 +++---- stan/math/rev/functor/idas_forward_system.hpp | 6 ++-- stan/math/rev/functor/integrate_1d.hpp | 2 +- stan/math/rev/functor/map_rect_reduce.hpp | 36 +++++++++---------- test/unit/math/rev/core/gradable.hpp | 4 +-- .../rev/core/local_nested_autodiff_test.cpp | 9 +++-- .../rev/functor/coupled_ode_system_test.cpp | 3 +- 11 files changed, 46 insertions(+), 62 deletions(-) diff --git a/stan/math/rev/core/local_nested_autodiff.hpp b/stan/math/rev/core/local_nested_autodiff.hpp index 6612b7c97f3..a4904ee8803 100644 --- a/stan/math/rev/core/local_nested_autodiff.hpp +++ b/stan/math/rev/core/local_nested_autodiff.hpp @@ -11,30 +11,24 @@ namespace math { /** * A class following the RAII idiom to start and recover nested autodiff scopes. * This is the preferred way to use nested autodiff. Example: - * + * * var a; // allocated normally * { * local_nested_autodiff nested; // Starts nested autodiff - * + * * var nested_var; //allocated on the nested stack * // Do stuff on the nested stack - * + * * // Nested stack is automatically recovered at the end of scope where - * // nested was declared, including exceptions, returns, etc. + * // nested was declared, including exceptions, returns, etc. * } * var b; */ class local_nested_autodiff { -public: - local_nested_autodiff() - { - start_nested(); - } + public: + local_nested_autodiff() { start_nested(); } - ~local_nested_autodiff() - { - recover_memory_nested(); - } + ~local_nested_autodiff() { recover_memory_nested(); } // Prevent undesirable operations local_nested_autodiff(const local_nested_autodiff&) = delete; @@ -45,9 +39,7 @@ class local_nested_autodiff { * Reset all adjoint values in this nested stack * to zero. **/ - void set_zero_all_adjoints() { - set_zero_all_adjoints_nested(); - } + void set_zero_all_adjoints() { set_zero_all_adjoints_nested(); } }; } // namespace math diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 5489ec03465..517af0c7f8e 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -14,9 +14,9 @@ namespace math { * is nothing on the nested stack, then a * std::logic_error exception is thrown. * - * It is preferred to use the local_nested_autodiff class for + * It is preferred to use the local_nested_autodiff class for * nested autodiff as it handles recovery of memory automatically. - * + * * @throw std::logic_error if empty_nested() returns * true */ diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index e5ffa3e18ad..94f670f0d0c 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -13,8 +13,8 @@ namespace math { /** * Reset all adjoint values in the top nested portion of the stack * to zero. - * - * It is preferred to use the local_nested_autodiff class for + * + * It is preferred to use the local_nested_autodiff class for * nested autodiff class as it handles recovery of memory automatically. */ static void set_zero_all_adjoints_nested() { diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 4c8fed065ee..b5cb13c6aab 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -9,8 +9,8 @@ namespace math { /** * Record the current position so that recover_memory_nested() * can find it. - * - * It is preferred to use the local_nested_autodiff class for + * + * It is preferred to use the local_nested_autodiff class for * nested autodiff as it handles recovery of memory automatically. */ static inline void start_nested() { diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index 5e62465bdac..b76d4c28694 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -126,8 +126,8 @@ struct coupled_ode_system { vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); - check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), - "states", N_); + check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), "states", + N_); for (size_t i = 0; i < N_; i++) { dz_dt[i] = dy_dt_vars[i].val(); @@ -281,8 +281,8 @@ struct coupled_ode_system { vector dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_); - check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), - "states", N_); + check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), "states", + N_); for (size_t i = 0; i < N_; i++) { dz_dt[i] = dy_dt_vars[i].val(); @@ -457,8 +457,8 @@ struct coupled_ode_system { vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); - check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), - "states", N_); + check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(), "states", + N_); for (size_t i = 0; i < N_; i++) { dz_dt[i] = dy_dt_vars[i].val(); diff --git a/stan/math/rev/functor/idas_forward_system.hpp b/stan/math/rev/functor/idas_forward_system.hpp index cb534ca0e4c..4bd313bd8b4 100644 --- a/stan/math/rev/functor/idas_forward_system.hpp +++ b/stan/math/rev/functor/idas_forward_system.hpp @@ -127,8 +127,7 @@ class idas_forward_system : public idas_system { MatrixXd J, r; VectorXd f_val; - auto fyy - = [&t, &vyp, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { + auto fyy = [&t, &vyp, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { std::vector yy(x.data(), x.data() + N); auto eval = dae->f_(t, yy, vyp, vtheta, dae->x_r_, dae->x_i_, dae->msgs_); @@ -138,8 +137,7 @@ class idas_forward_system : public idas_system { stan::math::jacobian(fyy, vec_yy, f_val, J); r = J * yys_mat; - auto fyp - = [&t, &vyy, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { + auto fyp = [&t, &vyy, &vtheta, &N, &dae](const matrix_v& x) -> vector_v { std::vector yp(x.data(), x.data() + N); auto eval = dae->f_(t, vyy, yp, vtheta, dae->x_r_, dae->x_i_, dae->msgs_); diff --git a/stan/math/rev/functor/integrate_1d.hpp b/stan/math/rev/functor/integrate_1d.hpp index e38a6ddf3b5..5b3778b4072 100644 --- a/stan/math/rev/functor/integrate_1d.hpp +++ b/stan/math/rev/functor/integrate_1d.hpp @@ -52,7 +52,7 @@ inline double gradient_of_f(const F &f, const double &x, const double &xc, gradient = 0; } else { throw_domain_error("gradient_of_f", "The gradient of f", n, - "is nan for parameter ", ""); + "is nan for parameter ", ""); } } diff --git a/stan/math/rev/functor/map_rect_reduce.hpp b/stan/math/rev/functor/map_rect_reduce.hpp index 25f994a4d35..d316cd00624 100644 --- a/stan/math/rev/functor/map_rect_reduce.hpp +++ b/stan/math/rev/functor/map_rect_reduce.hpp @@ -30,8 +30,7 @@ struct map_rect_reduce { vector_v shared_params_v = to_var(shared_params); vector_v job_specific_params_v = to_var(job_specific_params); - vector_v fx_v - = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs); + vector_v fx_v = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs); const size_type size_f = fx_v.rows(); @@ -45,8 +44,7 @@ struct map_rect_reduce { out(1 + j, i) = shared_params_v(j).vi_->adj_; } for (size_type j = 0; j < num_job_specific_params; ++j) { - out(1 + num_shared_params + j, i) - = job_specific_params_v(j).vi_->adj_; + out(1 + num_shared_params + j, i) = job_specific_params_v(j).vi_->adj_; } } return out; @@ -96,28 +94,28 @@ struct map_rect_reduce { const size_type num_shared_params = shared_params.rows(); matrix_d out(1 + num_shared_params, 0); - // Run nested autodiff in this scope - local_nested_autodiff nested; + // Run nested autodiff in this scope + local_nested_autodiff nested; - vector_v shared_params_v = to_var(shared_params); + vector_v shared_params_v = to_var(shared_params); - vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs); + vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs); - const size_type size_f = fx_v.rows(); + const size_type size_f = fx_v.rows(); - out.resize(Eigen::NoChange, size_f); + out.resize(Eigen::NoChange, size_f); - for (size_type i = 0; i < size_f; ++i) { - out(0, i) = fx_v(i).val(); - nested.set_zero_all_adjoints(); - fx_v(i).grad(); - for (size_type j = 0; j < num_shared_params; ++j) { - out(1 + j, i) = shared_params_v(j).vi_->adj_; + for (size_type i = 0; i < size_f; ++i) { + out(0, i) = fx_v(i).val(); + nested.set_zero_all_adjoints(); + fx_v(i).grad(); + for (size_type j = 0; j < num_shared_params; ++j) { + out(1 + j, i) = shared_params_v(j).vi_->adj_; + } } + + return out; } - - return out; -} }; } // namespace internal diff --git a/test/unit/math/rev/core/gradable.hpp b/test/unit/math/rev/core/gradable.hpp index 8a1be3f231d..5480d13e97a 100644 --- a/test/unit/math/rev/core/gradable.hpp +++ b/test/unit/math/rev/core/gradable.hpp @@ -22,9 +22,7 @@ struct gradable { EXPECT_FLOAT_EQ(g_expected_(i), g[i]); } - double adj() { - return f_.adj(); - } + double adj() { return f_.adj(); } }; gradable setup_quad_form() { diff --git a/test/unit/math/rev/core/local_nested_autodiff_test.cpp b/test/unit/math/rev/core/local_nested_autodiff_test.cpp index eb2ed3483aa..da310be7786 100644 --- a/test/unit/math/rev/core/local_nested_autodiff_test.cpp +++ b/test/unit/math/rev/core/local_nested_autodiff_test.cpp @@ -10,13 +10,12 @@ struct AgradLocalNested : public testing::Test { } }; - TEST_F(AgradLocalNested, local_nested_autodiff_base) { { stan::math::local_nested_autodiff nested; EXPECT_THROW(stan::math::recover_memory(), std::logic_error); } - stan::math::recover_memory(); // Should not throw + stan::math::recover_memory(); // Should not throw gradable g_out = setup_quad_form(); for (int i = 0; i < 100; ++i) { @@ -25,7 +24,7 @@ TEST_F(AgradLocalNested, local_nested_autodiff_base) { g.test(); nested.set_zero_all_adjoints(); EXPECT_EQ(g.adj(), 0); - } + } g_out.test(); } @@ -50,7 +49,7 @@ TEST_F(AgradLocalNested, local_nested_autodiff_Gradient1) { stan::math::recover_memory(); } -TEST_F(AgradLocalNested,local_nested_autodiff_Gradient2) { +TEST_F(AgradLocalNested, local_nested_autodiff_Gradient2) { using stan::math::local_nested_autodiff; gradable g0 = setup_quad_form(); @@ -85,7 +84,7 @@ TEST_F(AgradLocalNested, local_nested_autodiff_Gradient3) { gradable g3 = setup_quad_form(); { local_nested_autodiff nested4; - gradable g4 = setup_simple(); + gradable g4 = setup_simple(); g4.test(); } g3.test(); diff --git a/test/unit/math/rev/functor/coupled_ode_system_test.cpp b/test/unit/math/rev/functor/coupled_ode_system_test.cpp index e947a427d4b..d03656ad080 100644 --- a/test/unit/math/rev/functor/coupled_ode_system_test.cpp +++ b/test/unit/math/rev/functor/coupled_ode_system_test.cpp @@ -58,7 +58,6 @@ TEST_F(StanAgradRevOde, coupled_ode_system_dv) { EXPECT_FLOAT_EQ(-1.075, dz_dt[1]); EXPECT_FLOAT_EQ(2, dz_dt[2]); EXPECT_FLOAT_EQ(-1.8, dz_dt[3]); - } TEST_F(StanAgradRevOde, initial_state_dv) { using stan::math::coupled_ode_system; @@ -321,7 +320,7 @@ TEST_F(StanAgradRevOde, coupled_ode_system_vv) { // Run nested autodiff in this scope stan::math::local_nested_autodiff nested; - + const size_t N = 2; const size_t M = 1; const size_t z_size = N + N * N + N * M; From 64d319314b4fa66a4fef96979d4c3c5a99845640 Mon Sep 17 00:00:00 2001 From: martinmodrak Date: Wed, 12 Feb 2020 13:28:27 +0100 Subject: [PATCH 3/6] Fixed lint --- test/unit/math/rev/core/gradable.hpp | 2 +- test/unit/math/rev/core/local_nested_autodiff_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/math/rev/core/gradable.hpp b/test/unit/math/rev/core/gradable.hpp index 5480d13e97a..1f90a655336 100644 --- a/test/unit/math/rev/core/gradable.hpp +++ b/test/unit/math/rev/core/gradable.hpp @@ -64,4 +64,4 @@ gradable setup_simple() { return gradable(x, f, g_expected); } -#endif \ No newline at end of file +#endif diff --git a/test/unit/math/rev/core/local_nested_autodiff_test.cpp b/test/unit/math/rev/core/local_nested_autodiff_test.cpp index da310be7786..a177594f765 100644 --- a/test/unit/math/rev/core/local_nested_autodiff_test.cpp +++ b/test/unit/math/rev/core/local_nested_autodiff_test.cpp @@ -94,4 +94,4 @@ TEST_F(AgradLocalNested, local_nested_autodiff_Gradient3) { g1.test(); } stan::math::recover_memory(); -} \ No newline at end of file +} From 75639482ccc77eaa068daab8766673d5d7a24ba6 Mon Sep 17 00:00:00 2001 From: martinmodrak Date: Wed, 12 Feb 2020 13:45:57 +0100 Subject: [PATCH 4/6] Removed forgotten debug output --- test/unit/math/rev/functor/coupled_ode_system_test.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/unit/math/rev/functor/coupled_ode_system_test.cpp b/test/unit/math/rev/functor/coupled_ode_system_test.cpp index d03656ad080..67f00885b3a 100644 --- a/test/unit/math/rev/functor/coupled_ode_system_test.cpp +++ b/test/unit/math/rev/functor/coupled_ode_system_test.cpp @@ -50,9 +50,7 @@ TEST_F(StanAgradRevOde, coupled_ode_system_dv) { EXPECT_EQ(stack_size, stan::math::nested_size()) << "expecting no new things on the stack"; - std::cout << "A" << std::endl; system(z0, dz_dt, t0); - std::cout << "B" << std::endl; EXPECT_FLOAT_EQ(0.5, dz_dt[0]); EXPECT_FLOAT_EQ(-1.075, dz_dt[1]); From c91407747ced822004ea7ccdb48e62443224add8 Mon Sep 17 00:00:00 2001 From: martinmodrak Date: Fri, 14 Feb 2020 17:31:22 +0100 Subject: [PATCH 5/6] Changed name to nested_rev_autodiff --- stan/math/mix/functor/grad_hessian.hpp | 2 +- .../mix/functor/grad_tr_mat_times_hessian.hpp | 2 +- stan/math/mix/functor/hessian.hpp | 2 +- .../math/mix/functor/hessian_times_vector.hpp | 2 +- stan/math/rev/core.hpp | 2 +- ...d_autodiff.hpp => nested_rev_autodiff.hpp} | 16 ++++----- stan/math/rev/core/recover_memory_nested.hpp | 2 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 2 +- stan/math/rev/core/start_nested.hpp | 2 +- stan/math/rev/functor/coupled_ode_system.hpp | 6 ++-- stan/math/rev/functor/cvodes_ode_data.hpp | 2 +- stan/math/rev/functor/gradient.hpp | 2 +- stan/math/rev/functor/idas_forward_system.hpp | 2 +- stan/math/rev/functor/integrate_1d.hpp | 2 +- stan/math/rev/functor/jacobian.hpp | 2 +- stan/math/rev/functor/map_rect_reduce.hpp | 6 ++-- ..._test.cpp => nested_rev_autodiff_test.cpp} | 34 +++++++++---------- .../rev/functor/coupled_ode_system_test.cpp | 6 ++-- 18 files changed, 47 insertions(+), 47 deletions(-) rename stan/math/rev/core/{local_nested_autodiff.hpp => nested_rev_autodiff.hpp} (68%) rename test/unit/math/rev/core/{local_nested_autodiff_test.cpp => nested_rev_autodiff_test.cpp} (64%) diff --git a/stan/math/mix/functor/grad_hessian.hpp b/stan/math/mix/functor/grad_hessian.hpp index 8a1e8de1506..9fcf47ba1f6 100644 --- a/stan/math/mix/functor/grad_hessian.hpp +++ b/stan/math/mix/functor/grad_hessian.hpp @@ -53,7 +53,7 @@ void grad_hessian( for (int i = 0; i < d; ++i) { for (int j = i; j < d; ++j) { // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; Matrix >, Dynamic, 1> x_ffvar(d); for (int k = 0; k < d; ++k) { diff --git a/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp b/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp index eb8ba285a0c..1463a21e150 100644 --- a/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp +++ b/stan/math/mix/functor/grad_tr_mat_times_hessian.hpp @@ -20,7 +20,7 @@ void grad_tr_mat_times_hessian( using Eigen::Matrix; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; grad_tr_MH.resize(x.size()); diff --git a/stan/math/mix/functor/hessian.hpp b/stan/math/mix/functor/hessian.hpp index 6e513137488..d885d49e224 100644 --- a/stan/math/mix/functor/hessian.hpp +++ b/stan/math/mix/functor/hessian.hpp @@ -52,7 +52,7 @@ void hessian(const F& f, const Eigen::Matrix& x, } for (int i = 0; i < x.size(); ++i) { // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; Eigen::Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); for (int j = 0; j < x.size(); ++j) { diff --git a/stan/math/mix/functor/hessian_times_vector.hpp b/stan/math/mix/functor/hessian_times_vector.hpp index 3bc37cc58f5..ebcc0f3933f 100644 --- a/stan/math/mix/functor/hessian_times_vector.hpp +++ b/stan/math/mix/functor/hessian_times_vector.hpp @@ -19,7 +19,7 @@ void hessian_times_vector(const F& f, using Eigen::Matrix; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; Matrix x_var(x.size()); for (int i = 0; i < x_var.size(); ++i) { diff --git a/stan/math/rev/core.hpp b/stan/math/rev/core.hpp index 44a65bb4655..bfa25a9145c 100644 --- a/stan/math/rev/core.hpp +++ b/stan/math/rev/core.hpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/stan/math/rev/core/local_nested_autodiff.hpp b/stan/math/rev/core/nested_rev_autodiff.hpp similarity index 68% rename from stan/math/rev/core/local_nested_autodiff.hpp rename to stan/math/rev/core/nested_rev_autodiff.hpp index a4904ee8803..a8f71d21c4a 100644 --- a/stan/math/rev/core/local_nested_autodiff.hpp +++ b/stan/math/rev/core/nested_rev_autodiff.hpp @@ -1,5 +1,5 @@ -#ifndef STAN_MATH_REV_CORE_LOCAL_NESTED_AUTODIFF_HPP -#define STAN_MATH_REV_CORE_LOCAL_NESTED_AUTODIFF_HPP +#ifndef STAN_MATH_REV_CORE_nested_rev_autodiff_HPP +#define STAN_MATH_REV_CORE_nested_rev_autodiff_HPP #include #include @@ -14,7 +14,7 @@ namespace math { * * var a; // allocated normally * { - * local_nested_autodiff nested; // Starts nested autodiff + * nested_rev_autodiff nested; // Starts nested autodiff * * var nested_var; //allocated on the nested stack * // Do stuff on the nested stack @@ -24,15 +24,15 @@ namespace math { * } * var b; */ -class local_nested_autodiff { +class nested_rev_autodiff { public: - local_nested_autodiff() { start_nested(); } + nested_rev_autodiff() { start_nested(); } - ~local_nested_autodiff() { recover_memory_nested(); } + ~nested_rev_autodiff() { recover_memory_nested(); } // Prevent undesirable operations - local_nested_autodiff(const local_nested_autodiff&) = delete; - local_nested_autodiff& operator=(const local_nested_autodiff&) = delete; + nested_rev_autodiff(const nested_rev_autodiff&) = delete; + nested_rev_autodiff& operator=(const nested_rev_autodiff&) = delete; void* operator new(std::size_t) = delete; /** diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 517af0c7f8e..1cd493a1a11 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -14,7 +14,7 @@ namespace math { * is nothing on the nested stack, then a * std::logic_error exception is thrown. * - * It is preferred to use the local_nested_autodiff class for + * It is preferred to use the nested_rev_autodiff class for * nested autodiff as it handles recovery of memory automatically. * * @throw std::logic_error if empty_nested() returns diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 94f670f0d0c..34dc367556c 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -14,7 +14,7 @@ namespace math { * Reset all adjoint values in the top nested portion of the stack * to zero. * - * It is preferred to use the local_nested_autodiff class for + * It is preferred to use the nested_rev_autodiff class for * nested autodiff class as it handles recovery of memory automatically. */ static void set_zero_all_adjoints_nested() { diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index b5cb13c6aab..d17ff7e153b 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -10,7 +10,7 @@ namespace math { * Record the current position so that recover_memory_nested() * can find it. * - * It is preferred to use the local_nested_autodiff class for + * It is preferred to use the nested_rev_autodiff class for * nested autodiff as it handles recovery of memory automatically. */ static inline void start_nested() { diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index b76d4c28694..a8cf3b71627 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -117,7 +117,7 @@ struct coupled_ode_system { using std::vector; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; vector y_vars; y_vars.reserve(N_); @@ -272,7 +272,7 @@ struct coupled_ode_system { using std::vector; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; vector y_vars; y_vars.reserve(N_); @@ -448,7 +448,7 @@ struct coupled_ode_system { using std::vector; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; vector y_vars; y_vars.reserve(N_); diff --git a/stan/math/rev/functor/cvodes_ode_data.hpp b/stan/math/rev/functor/cvodes_ode_data.hpp index 69eed446063..0906d13fadb 100644 --- a/stan/math/rev/functor/cvodes_ode_data.hpp +++ b/stan/math/rev/functor/cvodes_ode_data.hpp @@ -164,7 +164,7 @@ class cvodes_ode_data { */ inline int jacobian_states(double t, const double y[], SUNMatrix J) const { // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; const std::vector y_vec_var(y, y + N_); coupled_ode_system ode_jacobian(f_, y_vec_var, theta_dbl_, diff --git a/stan/math/rev/functor/gradient.hpp b/stan/math/rev/functor/gradient.hpp index 2c27e054b4c..43bc7321209 100644 --- a/stan/math/rev/functor/gradient.hpp +++ b/stan/math/rev/functor/gradient.hpp @@ -42,7 +42,7 @@ template void gradient(const F& f, const Eigen::Matrix& x, double& fx, Eigen::Matrix& grad_fx) { // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; Eigen::Matrix x_var(x.size()); for (int i = 0; i < x.size(); ++i) diff --git a/stan/math/rev/functor/idas_forward_system.hpp b/stan/math/rev/functor/idas_forward_system.hpp index 4bd313bd8b4..605cb4ab9dd 100644 --- a/stan/math/rev/functor/idas_forward_system.hpp +++ b/stan/math/rev/functor/idas_forward_system.hpp @@ -122,7 +122,7 @@ class idas_forward_system : public idas_system { auto yps_mat = matrix_d_from_NVarray(yps, ns); // Run nested autodiff in this scope - stan::math::local_nested_autodiff nested; + stan::math::nested_rev_autodiff nested; MatrixXd J, r; VectorXd f_val; diff --git a/stan/math/rev/functor/integrate_1d.hpp b/stan/math/rev/functor/integrate_1d.hpp index 5b3778b4072..418aeafc43f 100644 --- a/stan/math/rev/functor/integrate_1d.hpp +++ b/stan/math/rev/functor/integrate_1d.hpp @@ -38,7 +38,7 @@ inline double gradient_of_f(const F &f, const double &x, const double &xc, double gradient = 0.0; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; std::vector theta_var(theta_vals.size()); for (size_t i = 0; i < theta_vals.size(); i++) { diff --git a/stan/math/rev/functor/jacobian.hpp b/stan/math/rev/functor/jacobian.hpp index e2f29ac3d6c..75692b06911 100644 --- a/stan/math/rev/functor/jacobian.hpp +++ b/stan/math/rev/functor/jacobian.hpp @@ -17,7 +17,7 @@ void jacobian(const F& f, const Eigen::Matrix& x, using Eigen::Dynamic; using Eigen::Matrix; // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; Matrix x_var(x); Matrix fx_var = f(x_var); diff --git a/stan/math/rev/functor/map_rect_reduce.hpp b/stan/math/rev/functor/map_rect_reduce.hpp index d316cd00624..0148acb4b6c 100644 --- a/stan/math/rev/functor/map_rect_reduce.hpp +++ b/stan/math/rev/functor/map_rect_reduce.hpp @@ -25,7 +25,7 @@ struct map_rect_reduce { matrix_d out(1 + num_shared_params + num_job_specific_params, 0); // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; vector_v shared_params_v = to_var(shared_params); vector_v job_specific_params_v = to_var(job_specific_params); @@ -62,7 +62,7 @@ struct map_rect_reduce { matrix_d out(1 + num_job_specific_params, 0); // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; vector_v job_specific_params_v = to_var(job_specific_params); @@ -95,7 +95,7 @@ struct map_rect_reduce { matrix_d out(1 + num_shared_params, 0); // Run nested autodiff in this scope - local_nested_autodiff nested; + nested_rev_autodiff nested; vector_v shared_params_v = to_var(shared_params); diff --git a/test/unit/math/rev/core/local_nested_autodiff_test.cpp b/test/unit/math/rev/core/nested_rev_autodiff_test.cpp similarity index 64% rename from test/unit/math/rev/core/local_nested_autodiff_test.cpp rename to test/unit/math/rev/core/nested_rev_autodiff_test.cpp index a177594f765..2c2d667f5b5 100644 --- a/test/unit/math/rev/core/local_nested_autodiff_test.cpp +++ b/test/unit/math/rev/core/nested_rev_autodiff_test.cpp @@ -10,16 +10,16 @@ struct AgradLocalNested : public testing::Test { } }; -TEST_F(AgradLocalNested, local_nested_autodiff_base) { +TEST_F(AgradLocalNested, nested_rev_autodiff_base) { { - stan::math::local_nested_autodiff nested; + stan::math::nested_rev_autodiff nested; EXPECT_THROW(stan::math::recover_memory(), std::logic_error); } stan::math::recover_memory(); // Should not throw gradable g_out = setup_quad_form(); for (int i = 0; i < 100; ++i) { - stan::math::local_nested_autodiff nested; + stan::math::nested_rev_autodiff nested; gradable g = setup_quad_form(); g.test(); nested.set_zero_all_adjoints(); @@ -28,19 +28,19 @@ TEST_F(AgradLocalNested, local_nested_autodiff_base) { g_out.test(); } -TEST_F(AgradLocalNested, local_nested_autodiff_Gradient1) { - using stan::math::local_nested_autodiff; +TEST_F(AgradLocalNested, nested_rev_autodiff_Gradient1) { + using stan::math::nested_rev_autodiff; gradable g0 = setup_simple(); { - local_nested_autodiff nested; + nested_rev_autodiff nested; gradable g1 = setup_quad_form(); g1.test(); } { - local_nested_autodiff nested; + nested_rev_autodiff nested; gradable g2 = setup_simple(); g2.test(); } @@ -49,19 +49,19 @@ TEST_F(AgradLocalNested, local_nested_autodiff_Gradient1) { stan::math::recover_memory(); } -TEST_F(AgradLocalNested, local_nested_autodiff_Gradient2) { - using stan::math::local_nested_autodiff; +TEST_F(AgradLocalNested, nested_rev_autodiff_Gradient2) { + using stan::math::nested_rev_autodiff; gradable g0 = setup_quad_form(); { - local_nested_autodiff nested; + nested_rev_autodiff nested; gradable g1 = setup_simple(); g1.test(); } { - local_nested_autodiff nested; + nested_rev_autodiff nested; gradable g2 = setup_quad_form(); g2.test(); } @@ -70,20 +70,20 @@ TEST_F(AgradLocalNested, local_nested_autodiff_Gradient2) { stan::math::recover_memory(); } -TEST_F(AgradLocalNested, local_nested_autodiff_Gradient3) { - using stan::math::local_nested_autodiff; +TEST_F(AgradLocalNested, nested_rev_autodiff_Gradient3) { + using stan::math::nested_rev_autodiff; { - local_nested_autodiff nested; + nested_rev_autodiff nested; gradable g1 = setup_simple(); { - local_nested_autodiff nested2; + nested_rev_autodiff nested2; gradable g2 = setup_quad_form(); { - local_nested_autodiff nested3; + nested_rev_autodiff nested3; gradable g3 = setup_quad_form(); { - local_nested_autodiff nested4; + nested_rev_autodiff nested4; gradable g4 = setup_simple(); g4.test(); } diff --git a/test/unit/math/rev/functor/coupled_ode_system_test.cpp b/test/unit/math/rev/functor/coupled_ode_system_test.cpp index 67f00885b3a..a097aee047e 100644 --- a/test/unit/math/rev/functor/coupled_ode_system_test.cpp +++ b/test/unit/math/rev/functor/coupled_ode_system_test.cpp @@ -19,7 +19,7 @@ TEST_F(StanAgradRevOde, coupled_ode_system_dv) { using stan::math::coupled_ode_system; // Run nested autodiff in this scope - stan::math::local_nested_autodiff nested; + stan::math::nested_rev_autodiff nested; harm_osc_ode_fun harm_osc; @@ -164,7 +164,7 @@ TEST_F(StanAgradRevOde, coupled_ode_system_vd) { using stan::math::coupled_ode_system; // Run nested autodiff in this scope - stan::math::local_nested_autodiff nested; + stan::math::nested_rev_autodiff nested; harm_osc_ode_fun harm_osc; @@ -317,7 +317,7 @@ TEST_F(StanAgradRevOde, coupled_ode_system_vv) { using stan::math::coupled_ode_system; // Run nested autodiff in this scope - stan::math::local_nested_autodiff nested; + stan::math::nested_rev_autodiff nested; const size_t N = 2; const size_t M = 1; From f3f01a3877d60aad8f97d9463623bf1d7f40fcca Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 19 Feb 2020 11:24:41 +0000 Subject: [PATCH 6/6] [Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (tags/RELEASE_500/final) --- stan/math/rev/functor/coupled_ode_system.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stan/math/rev/functor/coupled_ode_system.hpp b/stan/math/rev/functor/coupled_ode_system.hpp index fdb66f173fa..c627398aa8e 100644 --- a/stan/math/rev/functor/coupled_ode_system.hpp +++ b/stan/math/rev/functor/coupled_ode_system.hpp @@ -119,7 +119,7 @@ struct coupled_ode_system { // Run nested autodiff in this scope nested_rev_autodiff nested; - vector y_vars(z.begin(), z.begin() + N_); + vector y_vars(z.begin(), z.begin() + N_); vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_); @@ -271,7 +271,7 @@ struct coupled_ode_system { // Run nested autodiff in this scope nested_rev_autodiff nested; - vector y_vars(z.begin(), z.begin() + N_); + vector y_vars(z.begin(), z.begin() + N_); vector dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_); @@ -444,7 +444,7 @@ struct coupled_ode_system { // Run nested autodiff in this scope nested_rev_autodiff nested; - vector y_vars(z.begin(), z.begin() + N_); + vector y_vars(z.begin(), z.begin() + N_); vector dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_);