Skip to content

Commit

Permalink
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-buildbot committed Dec 13, 2024
1 parent 48bbb6e commit 0b46b4a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
99 changes: 51 additions & 48 deletions src/stan/services/pathfinder/multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bool DoAll = false, typename ConstrainFun, typename Model, typename ElboEst, typename RNG, typename ParamWriter>
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<double, 1, Eigen::Dynamic> 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 <bool DoAll = false, typename ConstrainFun, typename Model,
typename ElboEst, typename RNG, typename ParamWriter>
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<double, 1, Eigen::Dynamic> 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.
Expand Down Expand Up @@ -197,8 +200,8 @@ inline int pathfinder_lbfgs_multi(
stan::rng_t rng = util::create_rng(random_seed, stride_id + iter);
std::lock_guard<std::mutex> lock(write_mutex);
internal::gen_pathfinder_draw<true>(constrain_fun, model,
std::get<1>(pathfinder_ret), 0, 0,
rng, parameter_writer);
std::get<1>(pathfinder_ret),
0, 0, rng, parameter_writer);
}
}
});
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ template <bool ReturnLpSamples, typename EigVec,
std::enable_if_t<ReturnLpSamples>* = nullptr>
inline auto ret_pathfinder(int return_code, EigVec&& elbo_est,
const std::atomic<size_t>& lp_calls) {
return std::make_tuple(return_code, std::forward<EigVec>(elbo_est), lp_calls.load());
return std::make_tuple(return_code, std::forward<EigVec>(elbo_est),
lp_calls.load());
}

template <bool ReturnLpSamples, typename EigVec,
Expand Down Expand Up @@ -615,8 +616,8 @@ inline auto pathfinder_lbfgs_single(
logger, init_writer);
} catch (const std::exception& e) {
logger.error(e.what());
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, internal::elbo_est_t{}, 0);
return internal::ret_pathfinder<ReturnLpSamples>(error_codes::SOFTWARE,
internal::elbo_est_t{}, 0);
}

const auto num_parameters = cont_vector.size();
Expand Down Expand Up @@ -863,10 +864,10 @@ inline auto pathfinder_lbfgs_single(
}
if constexpr (ReturnLpSamples) {
internal::elbo_est_t est_draws = internal::est_approx_draws<false>(
lp_fun, constrain_fun, rng, taylor_approx_best, num_draws,
taylor_approx_best.alpha, path_num, logger, calculate_lp);
return internal::ret_pathfinder<ReturnLpSamples>(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<ReturnLpSamples>(
error_codes::OK, std::move(est_draws), num_evals + est_draws.fn_calls);
} else {
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> constrained_draws_mat;
Eigen::Array<double, Eigen::Dynamic, 1> lp_ratio;
Expand All @@ -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<double, Eigen::Dynamic, 1>(elbo_lp_ratio.size()
+ new_lp_ratio.size());
lp_ratio = Eigen::Array<double, Eigen::Dynamic, 1>(
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<double, Eigen::Dynamic, Eigen::Dynamic>(names.size(),
total_size);
= Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(
names.size(), total_size);
Eigen::VectorXd unconstrained_col;
Eigen::VectorXd approx_samples_constrained_col;
for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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<ReturnLpSamples>(
error_codes::OK, internal::elbo_est_t{}, num_evals);

}
}

Expand Down
11 changes: 4 additions & 7 deletions src/test/unit/services/pathfinder/normal_glm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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];
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
}

}
5 changes: 3 additions & 2 deletions src/test/unit/services/pathfinder/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class in_memory_writer : public stan::callbacks::stream_writer {
std::vector<std::string> times_;
tbb::concurrent_vector<std::vector<double>> states_;
tbb::concurrent_vector<Eigen::VectorXd> eigen_states_;
tbb::concurrent_vector<std::tuple<Eigen::VectorXd, Eigen::VectorXd>> optim_path_;
tbb::concurrent_vector<std::tuple<Eigen::VectorXd, Eigen::VectorXd>>
optim_path_;
Eigen::MatrixXd values_;
in_memory_writer(std::ostream& stream)
: stan::callbacks::stream_writer(stream) {}
Expand Down Expand Up @@ -172,7 +173,7 @@ class in_memory_writer : public stan::callbacks::stream_writer {
}
}
void operator()(const std::tuple<Eigen::VectorXd, Eigen::VectorXd>& xx) {
optim_path_.push_back(xx);
optim_path_.push_back(xx);
}
template <typename EigVec, stan::require_eigen_vector_t<EigVec>* = nullptr>
void operator()(const EigVec& vals) {
Expand Down

0 comments on commit 0b46b4a

Please sign in to comment.