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 18, 2024
1 parent e484fa1 commit e88ca19
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 54 deletions.
10 changes: 2 additions & 8 deletions src/stan/callbacks/multi_stream_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,22 @@ class multi_stream_writer {
*/
template <typename T>
void operator()(T&& x) {
stan::math::for_each([&](auto&& output) {
output(x);
}, output_);
stan::math::for_each([&](auto&& output) { output(x); }, output_);
}
void operator()() {
stan::math::for_each([](auto&& output) {
output();
}, output_);
stan::math::for_each([](auto&& output) { output(); }, output_);
}

/**
* Get the underlying stream
*/
inline auto& get_stream() noexcept { return output_; }


private:
/**
* Output stream
*/
std::tuple<std::reference_wrapper<Streams>...> output_;

};

} // namespace callbacks
Expand Down
3 changes: 2 additions & 1 deletion src/stan/callbacks/unique_stream_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class unique_stream_writer final : public writer {
return;
*output_ << comment_prefix_ << message << std::endl;
}
using Stream = std::decay_t<Stream>;
using Stream = std::decay_t<Stream>;

private:
/**
* Comma formatter for writing Eigen matrices
Expand Down
48 changes: 27 additions & 21 deletions src/stan/services/pathfinder/multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ namespace internal {
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, Eigen::Index base_idx,
ElboEst&& elbo_est, const Eigen::Index path_num,
Eigen::Index base_idx,
const Eigen::Index path_sample_idx, RNG&& rng,
ParamWriter&& param_writer) {
// FIX THESE
Expand Down Expand Up @@ -81,8 +82,8 @@ template <typename Writer>
struct concurrent_writer {
std::reference_wrapper<Writer> writer;
std::reference_wrapper<std::mutex> mut_;
explicit concurrent_writer(std::mutex& mut, Writer& writer) :
writer(writer), mut_(mut) {}
explicit concurrent_writer(std::mutex& mut, Writer& writer)
: writer(writer), mut_(mut) {}
template <typename T>
void operator()(T&& t) {
std::lock_guard<std::mutex> lock(mut_.get());
Expand Down Expand Up @@ -203,37 +204,41 @@ inline int pathfinder_lbfgs_multi(
auto pathfinder_ret
= stan::services::pathfinder::pathfinder_lbfgs_single<true>(
model, *(init[iter]), random_seed, stride_id + iter,
init_radius, history_size, init_alpha, tol_obj, tol_rel_obj,
tol_grad, tol_rel_grad, tol_param, num_iterations,
num_elbo_draws, num_draws, save_iterations, refresh,
interrupt, logger, init_writers[iter],
single_path_parameter_writer[iter],
single_path_diagnostic_writer[iter], calculate_lp, psis_resample);
init_radius, history_size, init_alpha, tol_obj,
tol_rel_obj, tol_grad, tol_rel_grad, tol_param,
num_iterations, num_elbo_draws, num_draws,
save_iterations, refresh, interrupt, logger,
init_writers[iter], single_path_parameter_writer[iter],
single_path_diagnostic_writer[iter], calculate_lp,
psis_resample);
if (unlikely(std::get<0>(pathfinder_ret) != error_codes::OK)) {
logger.error(std::string("Pathfinder iteration: ")
+ std::to_string(iter) + " failed.");
+ std::to_string(iter) + " failed.");
return;
}
lp_calls += std::get<2>(pathfinder_ret);
elbo_estimates.push_back(std::move(std::get<1>(pathfinder_ret)));
} else {
// For no psis, have single write to both single and multi writers
using multi_writer = stan::callbacks::multi_stream_writer<SingleParamWriter, internal::concurrent_writer<ParamWriter>>;
internal::concurrent_writer safe_write{write_mutex, parameter_writer};
using multi_writer = stan::callbacks::multi_stream_writer<
SingleParamWriter, internal::concurrent_writer<ParamWriter>>;
internal::concurrent_writer safe_write{write_mutex,
parameter_writer};
multi_writer multi_param_writer(
single_path_parameter_writer[iter], safe_write);
auto pathfinder_ret
= stan::services::pathfinder::pathfinder_lbfgs_single<true>(
model, *(init[iter]), random_seed, stride_id + iter,
init_radius, history_size, init_alpha, tol_obj, tol_rel_obj,
tol_grad, tol_rel_grad, tol_param, num_iterations,
num_elbo_draws, num_draws, save_iterations, refresh,
interrupt, logger, init_writers[iter],
multi_param_writer,
single_path_diagnostic_writer[iter], calculate_lp, psis_resample);
init_radius, history_size, init_alpha, tol_obj,
tol_rel_obj, tol_grad, tol_rel_grad, tol_param,
num_iterations, num_elbo_draws, num_draws,
save_iterations, refresh, interrupt, logger,
init_writers[iter], multi_param_writer,
single_path_diagnostic_writer[iter], calculate_lp,
psis_resample);
if (unlikely(std::get<0>(pathfinder_ret) != error_codes::OK)) {
logger.error(std::string("Pathfinder iteration: ")
+ std::to_string(iter) + " failed.");
+ std::to_string(iter) + " failed.");
return;
}
lp_calls += std::get<2>(pathfinder_ret);
Expand Down Expand Up @@ -277,8 +282,9 @@ 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_num,
path_sample_idx, rng, parameter_writer);
internal::gen_pathfinder_draw(constrain_fun, model, elbo_est, path_num,
path_num, path_sample_idx, rng,
parameter_writer);
}
const auto end_psis_time = std::chrono::steady_clock::now();
psis_delta_time
Expand Down
3 changes: 2 additions & 1 deletion src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,8 @@ inline auto pathfinder_lbfgs_single(
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::OK, std::move(est_draws), num_evals + est_draws.fn_calls);
} else {
Eigen::Matrix<double, 1, Eigen::Dynamic> constrained_draws_vec(names.size());
Eigen::Matrix<double, 1, Eigen::Dynamic> constrained_draws_vec(
names.size());
constrained_draws_vec(2) = stride_id - (ReturnLpSamples ? 1 : 0);
Eigen::Array<double, Eigen::Dynamic, 1> lp_ratio;
auto&& elbo_draws = elbo_best.repeat_draws;
Expand Down
7 changes: 4 additions & 3 deletions src/test/unit/services/pathfinder/eight_schools_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class ServicesPathfinderEightSchools : public testing::Test {
stan_model model;
};

constexpr std::array param_indices{0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20};
constexpr std::array param_indices{0, 1, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

auto init_init_context() { return stan::io::empty_var_context(); }

Expand Down Expand Up @@ -237,7 +237,8 @@ TEST_F(ServicesPathfinderEightSchools, single) {
r_constrainted_draws_mat.col(i) = constrained_draws2;
}
}
Eigen::RowVectorXd mean_r_vals = r_constrainted_draws_mat.rowwise().mean().transpose();
Eigen::RowVectorXd mean_r_vals
= r_constrainted_draws_mat.rowwise().mean().transpose();
Eigen::RowVectorXd sd_r_vals
= (((r_constrainted_draws_mat.colwise() - mean_r_vals.transpose())
.array()
Expand Down
22 changes: 12 additions & 10 deletions src/test/unit/services/pathfinder/normal_glm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ TEST_F(ServicesPathfinderGLM, single) {
std::vector<std::tuple<Eigen::VectorXd, Eigen::VectorXd>> input_iters;

int rc = stan::services::pathfinder::pathfinder_lbfgs_single(
model, init_context, seed, stride_id, init_radius, history_size, init_alpha,
tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param, num_iterations,
num_elbo_draws, num_draws, save_iterations, refresh, callback, logger,
init, parameter, diagnostics);
model, init_context, seed, stride_id, init_radius, history_size,
init_alpha, tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param,
num_iterations, num_elbo_draws, num_draws, save_iterations, refresh,
callback, logger, init, parameter, diagnostics);
ASSERT_EQ(rc, 0);

Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, 0, ", ", ", ", "\n", "",
Expand Down Expand Up @@ -156,10 +156,10 @@ TEST_F(ServicesPathfinderGLM, single_noreturnlp) {
std::vector<std::tuple<Eigen::VectorXd, Eigen::VectorXd>> input_iters;

int rc = stan::services::pathfinder::pathfinder_lbfgs_single(
model, init_context, seed, stride_id, init_radius, history_size, init_alpha,
tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param, num_iterations,
num_elbo_draws, num_draws, save_iterations, refresh, callback, logger,
init, parameter, diagnostics, calculate_lp);
model, init_context, seed, stride_id, init_radius, history_size,
init_alpha, tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param,
num_iterations, num_elbo_draws, num_draws, save_iterations, refresh,
callback, logger, init, parameter, diagnostics, calculate_lp);
ASSERT_EQ(rc, 0);
Eigen::MatrixXd param_vals = parameter.get_eigen_state_values();
EXPECT_EQ(11, param_vals.cols());
Expand Down Expand Up @@ -354,7 +354,8 @@ TEST_F(ServicesPathfinderGLM, multi_noresample_noreturnlp) {
Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, 0, ", ", ", ", "\n", "",
"", "");
EXPECT_EQ(param_vals.cols(), 11);
EXPECT_EQ(param_vals.rows(), 8000); // They can be in any order and any number
EXPECT_EQ(param_vals.rows(),
8000); // They can be in any order and any number
for (Eigen::Index i = 0; i < num_multi_draws; i++) {
EXPECT_GE(param_vals.col(2)(i), 0);
EXPECT_LE(param_vals.col(2)(i), num_paths - 1);
Expand Down Expand Up @@ -479,7 +480,8 @@ TEST_F(ServicesPathfinderGLM, multi_noresample_returnlp) {

Eigen::MatrixXd param_vals = parameter.get_eigen_state_values();
EXPECT_EQ(param_vals.cols(), 11);
EXPECT_EQ(param_vals.rows(), 8000); // They can be in any order and any number
EXPECT_EQ(param_vals.rows(),
8000); // They can be in any order and any number
for (Eigen::Index i = 0; i < num_paths * num_draws; i++) {
EXPECT_GE(param_vals.col(2)(i), 0);
EXPECT_LE(param_vals.col(2)(i), num_paths - 1);
Expand Down
24 changes: 14 additions & 10 deletions src/test/unit/services/pathfinder/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,12 @@ class in_memory_writer : public stan::callbacks::stream_writer {
return param_vals;
}
inline auto get_values() const {
Eigen::MatrixXd param_vals(values_[0].cols(), values_.size() * values_[0].rows());
Eigen::MatrixXd param_vals(values_[0].cols(),
values_.size() * values_[0].rows());
for (size_t i = 0; i < values_.size(); ++i) {
param_vals.block(0, i * values_[i].rows(), values_[i].cols(), values_[i].rows()) = values_[i].transpose();
param_vals.block(0, i * values_[i].rows(), values_[i].cols(),
values_[i].rows())
= values_[i].transpose();
}
return param_vals;
}
Expand Down Expand Up @@ -524,14 +527,15 @@ template <typename T1>
inline auto get_mean_sd(T1&& param_vals) {
Eigen::RowVectorXd mean_vals = param_vals.colwise().mean().eval();
Eigen::RowVectorXd sd_vals = (((param_vals.rowwise() - mean_vals)
.array()
.square()
.matrix()
.colwise()
.sum()
.array()
/ (param_vals.rows() - 1))
.sqrt()).eval();
.array()
.square()
.matrix()
.colwise()
.sum()
.array()
/ (param_vals.rows() - 1))
.sqrt())
.eval();
return std::make_pair(mean_vals, sd_vals);
}
} // namespace test
Expand Down

0 comments on commit e88ca19

Please sign in to comment.