From 0b46b4a853956fd4569c8a7a026532868797bc4d Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 13 Dec 2024 15:39:21 -0500 Subject: [PATCH] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/services/pathfinder/multi.hpp | 99 ++++++++++--------- src/stan/services/pathfinder/single.hpp | 31 +++--- .../services/pathfinder/normal_glm_test.cpp | 11 +-- src/test/unit/services/pathfinder/util.hpp | 5 +- 4 files changed, 74 insertions(+), 72 deletions(-) diff --git a/src/stan/services/pathfinder/multi.hpp b/src/stan/services/pathfinder/multi.hpp index 1e6654fcaa..17e71dc7f5 100644 --- a/src/stan/services/pathfinder/multi.hpp +++ b/src/stan/services/pathfinder/multi.hpp @@ -25,53 +25,56 @@ namespace pathfinder { namespace internal { /** - * Generate a single draw from the pathfinder algorithm. - * @tparam DoAll If true, generate all draws. If false, generate only the - * draw at `path_sample_idx`. - * @tparam ConstrainFun A functor with a valid `operator(EigVector, EigVector, Model, RNG)` - * @tparam Model A model implementation - * @tparam ElboEst A struct with the ELBO estimate - * @tparam RNG A random number generator - * @tparam ParamWriter A writer callback for parameter values - * @param[in] constrain_fun A functor to transform parameters to the constrained - * space - * @param[in] model A model implementation - * @param[in] elbo_est A struct with the ELBO estimate - * @param[in] path_num The path number - * @param[in] path_sample_idx The index of the sample to generate - * @param[in] rng A random number generator - * @param[in] param_writer A writer callback for parameter values - */ - template - inline void gen_pathfinder_draw(ConstrainFun&& constrain_fun, Model&& model, - ElboEst&& elbo_est, const Eigen::Index path_num, - const Eigen::Index path_sample_idx, - RNG&& rng, ParamWriter&& param_writer) { - // FIX THESE - auto&& lp_draws = elbo_est.lp_mat; - auto&& new_draws = elbo_est.repeat_draws; - const Eigen::Index param_size = new_draws.rows(); - const auto num_samples = new_draws.cols(); - Eigen::VectorXd unconstrained_col; - Eigen::VectorXd approx_samples_constrained_col; - unconstrained_col = new_draws.col(path_sample_idx); - constrain_fun(approx_samples_constrained_col, unconstrained_col, model, rng); - const auto uc_param_size = approx_samples_constrained_col.size(); - Eigen::Matrix sample_row(uc_param_size + 2); + * Generate a single draw from the pathfinder algorithm. + * @tparam DoAll If true, generate all draws. If false, generate only the + * draw at `path_sample_idx`. + * @tparam ConstrainFun A functor with a valid `operator(EigVector, EigVector, + * Model, RNG)` + * @tparam Model A model implementation + * @tparam ElboEst A struct with the ELBO estimate + * @tparam RNG A random number generator + * @tparam ParamWriter A writer callback for parameter values + * @param[in] constrain_fun A functor to transform parameters to the constrained + * space + * @param[in] model A model implementation + * @param[in] elbo_est A struct with the ELBO estimate + * @param[in] path_num The path number + * @param[in] path_sample_idx The index of the sample to generate + * @param[in] rng A random number generator + * @param[in] param_writer A writer callback for parameter values + */ +template +inline void gen_pathfinder_draw(ConstrainFun&& constrain_fun, Model&& model, + ElboEst&& elbo_est, const Eigen::Index path_num, + const Eigen::Index path_sample_idx, RNG&& rng, + ParamWriter&& param_writer) { + // FIX THESE + auto&& lp_draws = elbo_est.lp_mat; + auto&& new_draws = elbo_est.repeat_draws; + const Eigen::Index param_size = new_draws.rows(); + const auto num_samples = new_draws.cols(); + Eigen::VectorXd unconstrained_col; + Eigen::VectorXd approx_samples_constrained_col; + unconstrained_col = new_draws.col(path_sample_idx); + constrain_fun(approx_samples_constrained_col, unconstrained_col, model, rng); + const auto uc_param_size = approx_samples_constrained_col.size(); + Eigen::Matrix sample_row(uc_param_size + 2); + sample_row.head(2) = lp_draws.row(path_sample_idx).matrix(); + sample_row.tail(uc_param_size) = approx_samples_constrained_col; + param_writer(sample_row); + if constexpr (DoAll) { + for (int i = 1; i < num_samples; ++i) { + unconstrained_col = new_draws.col(i); + constrain_fun(approx_samples_constrained_col, unconstrained_col, model, + rng); sample_row.head(2) = lp_draws.row(path_sample_idx).matrix(); sample_row.tail(uc_param_size) = approx_samples_constrained_col; param_writer(sample_row); - if constexpr (DoAll) { - for (int i = 1; i < num_samples; ++i) { - unconstrained_col = new_draws.col(i); - constrain_fun(approx_samples_constrained_col, unconstrained_col, model, rng); - sample_row.head(2) = lp_draws.row(path_sample_idx).matrix(); - sample_row.tail(uc_param_size) = approx_samples_constrained_col; - param_writer(sample_row); - } - } + } } } +} // namespace internal /** * Runs multiple pathfinders with final approximate samples drawn using PSIS. @@ -197,8 +200,8 @@ inline int pathfinder_lbfgs_multi( stan::rng_t rng = util::create_rng(random_seed, stride_id + iter); std::lock_guard lock(write_mutex); internal::gen_pathfinder_draw(constrain_fun, model, - std::get<1>(pathfinder_ret), 0, 0, - rng, parameter_writer); + std::get<1>(pathfinder_ret), + 0, 0, rng, parameter_writer); } } }); @@ -208,7 +211,8 @@ inline int pathfinder_lbfgs_multi( return error_codes::SOFTWARE; } const auto end_pathfinders_time = std::chrono::steady_clock::now(); - auto pathfinders_delta_time = stan::services::util::duration_diff(start_pathfinders_time, end_pathfinders_time); + auto pathfinders_delta_time = stan::services::util::duration_diff( + start_pathfinders_time, end_pathfinders_time); double psis_delta_time = 0; stan::rng_t rng = util::create_rng(random_seed, stride_id); if (psis_resample && calculate_lp) { @@ -239,9 +243,8 @@ inline int pathfinder_lbfgs_multi( Eigen::Index path_num = std::floor(draw_idx / num_draws); auto path_sample_idx = draw_idx % num_draws; auto&& elbo_est = elbo_estimates[path_num]; - internal::gen_pathfinder_draw(constrain_fun, model, elbo_est, - path_num, path_sample_idx, rng, parameter_writer); - + internal::gen_pathfinder_draw(constrain_fun, model, elbo_est, path_num, + path_sample_idx, rng, parameter_writer); } const auto end_psis_time = std::chrono::steady_clock::now(); psis_delta_time diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 16fea83f72..71664dbb05 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -463,7 +463,8 @@ template * = nullptr> inline auto ret_pathfinder(int return_code, EigVec&& elbo_est, const std::atomic& lp_calls) { - return std::make_tuple(return_code, std::forward(elbo_est), lp_calls.load()); + return std::make_tuple(return_code, std::forward(elbo_est), + lp_calls.load()); } template ( - error_codes::SOFTWARE, internal::elbo_est_t{}, 0); + return internal::ret_pathfinder(error_codes::SOFTWARE, + internal::elbo_est_t{}, 0); } const auto num_parameters = cont_vector.size(); @@ -863,10 +864,10 @@ inline auto pathfinder_lbfgs_single( } if constexpr (ReturnLpSamples) { internal::elbo_est_t est_draws = internal::est_approx_draws( - lp_fun, constrain_fun, rng, taylor_approx_best, num_draws, - taylor_approx_best.alpha, path_num, logger, calculate_lp); - return internal::ret_pathfinder(error_codes::OK, - std::move(est_draws), num_evals + est_draws.fn_calls); + lp_fun, constrain_fun, rng, taylor_approx_best, num_draws, + taylor_approx_best.alpha, path_num, logger, calculate_lp); + return internal::ret_pathfinder( + error_codes::OK, std::move(est_draws), num_evals + est_draws.fn_calls); } else { Eigen::Matrix constrained_draws_mat; Eigen::Array lp_ratio; @@ -884,14 +885,14 @@ inline auto pathfinder_lbfgs_single( auto&& new_lp_ratio = est_draws.lp_ratio; auto&& lp_draws = est_draws.lp_mat; auto&& new_draws = est_draws.repeat_draws; - lp_ratio = Eigen::Array(elbo_lp_ratio.size() - + new_lp_ratio.size()); + lp_ratio = Eigen::Array( + elbo_lp_ratio.size() + new_lp_ratio.size()); lp_ratio.head(elbo_lp_ratio.size()) = elbo_lp_ratio.array(); lp_ratio.tail(new_lp_ratio.size()) = new_lp_ratio.array(); const auto total_size = elbo_draws.cols() + new_draws.cols(); constrained_draws_mat - = Eigen::Matrix(names.size(), - total_size); + = Eigen::Matrix( + names.size(), total_size); Eigen::VectorXd unconstrained_col; Eigen::VectorXd approx_samples_constrained_col; for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) { @@ -903,7 +904,7 @@ inline auto pathfinder_lbfgs_single( .matrix(); } for (Eigen::Index i = elbo_draws.cols(), j = 0; i < total_size; - ++i, ++j) { + ++i, ++j) { constrained_draws_mat.col(i).head(2) = lp_draws.row(j).matrix(); unconstrained_col = new_draws.col(j); constrained_draws_mat.col(i).tail(num_unconstrained_params) @@ -913,7 +914,8 @@ inline auto pathfinder_lbfgs_single( } } catch (const std::domain_error& e) { std::string err_msg = e.what(); - logger.warn(path_num + "Final sampling approximation failed with error: " + logger.warn(path_num + + "Final sampling approximation failed with error: " + err_msg); logger.info( path_num @@ -958,12 +960,11 @@ inline auto pathfinder_lbfgs_single( start_pathfinder_time, end_pathfinder_time); std::string pathfinder_time_str = "Elapsed Time: "; pathfinder_time_str += std::to_string(pathfinder_delta_time) - + std::string(" seconds (Pathfinder)"); + + std::string(" seconds (Pathfinder)"); parameter_writer(pathfinder_time_str); parameter_writer(); return internal::ret_pathfinder( error_codes::OK, internal::elbo_est_t{}, num_evals); - } } diff --git a/src/test/unit/services/pathfinder/normal_glm_test.cpp b/src/test/unit/services/pathfinder/normal_glm_test.cpp index 4fd691b326..4a5676efdb 100644 --- a/src/test/unit/services/pathfinder/normal_glm_test.cpp +++ b/src/test/unit/services/pathfinder/normal_glm_test.cpp @@ -355,7 +355,7 @@ TEST_F(ServicesPathfinderGLM, multi_noresample_noreturnlp) { ASSERT_EQ(rc, 0); Eigen::MatrixXd param_vals(parameter.eigen_states_.size(), - parameter.eigen_states_[0].size()); + parameter.eigen_states_[0].size()); for (Eigen::Index i = 0; i < parameter.eigen_states_.size(); ++i) { param_vals.row(i) = parameter.eigen_states_[i]; } @@ -368,7 +368,6 @@ TEST_F(ServicesPathfinderGLM, multi_noresample_noreturnlp) { for (Eigen::Index i = 0; i < num_draws * num_paths; ++i) { EXPECT_TRUE(std::isnan(param_vals.coeff(i, 1))); } - } TEST_F(ServicesPathfinderGLM, multi_resample_noreturnlp) { @@ -414,7 +413,7 @@ TEST_F(ServicesPathfinderGLM, multi_resample_noreturnlp) { diagnostics, calculate_lp, resample); ASSERT_EQ(rc, 0); Eigen::MatrixXd param_vals(parameter.eigen_states_.size(), - parameter.eigen_states_[0].size()); + parameter.eigen_states_[0].size()); for (Eigen::Index i = 0; i < parameter.eigen_states_.size(); ++i) { param_vals.row(i) = parameter.eigen_states_[i]; } @@ -424,9 +423,8 @@ TEST_F(ServicesPathfinderGLM, multi_resample_noreturnlp) { EXPECT_EQ(param_vals.cols(), 10); EXPECT_EQ(param_vals.rows(), 8000); for (Eigen::Index i = 0; i < num_draws * num_paths; ++i) { - EXPECT_TRUE(std::isnan(param_vals.coeff(i, 1))) << "row: " << i; + EXPECT_TRUE(std::isnan(param_vals.coeff(i, 1))) << "row: " << i; } - } TEST_F(ServicesPathfinderGLM, multi_noresample_returnlp) { @@ -473,7 +471,7 @@ TEST_F(ServicesPathfinderGLM, multi_noresample_returnlp) { ASSERT_EQ(rc, 0); Eigen::MatrixXd param_vals(parameter.eigen_states_.size(), - parameter.eigen_states_[0].size()); + parameter.eigen_states_[0].size()); for (Eigen::Index i = 0; i < parameter.eigen_states_.size(); ++i) { param_vals.row(i) = parameter.eigen_states_[i]; } @@ -486,5 +484,4 @@ TEST_F(ServicesPathfinderGLM, multi_noresample_returnlp) { for (Eigen::Index i = 0; i < num_draws * num_paths; ++i) { EXPECT_FALSE(std::isnan(param_vals.coeff(i, 1))) << "row: " << i; } - } diff --git a/src/test/unit/services/pathfinder/util.hpp b/src/test/unit/services/pathfinder/util.hpp index 980b048a4b..37ae194f88 100644 --- a/src/test/unit/services/pathfinder/util.hpp +++ b/src/test/unit/services/pathfinder/util.hpp @@ -143,7 +143,8 @@ class in_memory_writer : public stan::callbacks::stream_writer { std::vector times_; tbb::concurrent_vector> states_; tbb::concurrent_vector eigen_states_; - tbb::concurrent_vector> optim_path_; + tbb::concurrent_vector> + optim_path_; Eigen::MatrixXd values_; in_memory_writer(std::ostream& stream) : stan::callbacks::stream_writer(stream) {} @@ -172,7 +173,7 @@ class in_memory_writer : public stan::callbacks::stream_writer { } } void operator()(const std::tuple& xx) { - optim_path_.push_back(xx); + optim_path_.push_back(xx); } template * = nullptr> void operator()(const EigVec& vals) {