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 Feb 5, 2025
1 parent ed3029f commit e973755
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 114 deletions.
4 changes: 3 additions & 1 deletion src/stan/callbacks/unique_stream_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ class unique_stream_writer final : public writer {
/**
* Checks if stream is valid.
*/
bool is_valid() const noexcept { return output_ != nullptr && (*output_).good(); }
bool is_valid() const noexcept {
return output_ != nullptr && (*output_).good();
}

private:
/**
Expand Down
4 changes: 3 additions & 1 deletion src/stan/io/stan_csv_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ class stan_csv_reader {
for (int col = 0; col < cols; col++) {
std::getline(ls, line, ',');
boost::trim(line);
auto [ptr, ec] = std::from_chars(line.data(), line.data() + line.size(), samples.coeffRef(row, col));
auto [ptr, ec]
= std::from_chars(line.data(), line.data() + line.size(),
samples.coeffRef(row, col));
if (ec != std::errc()) {
samples(row, col) = std::numeric_limits<double>::quiet_NaN();
}
Expand Down
11 changes: 7 additions & 4 deletions src/stan/services/pathfinder/multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ inline int pathfinder_lbfgs_multi(
std::vector<SingleDiagnosticWriter>& single_path_diagnostic_writer,
ParamWriter& parameter_writer, DiagnosticWriter& diagnostic_writer,
bool calculate_lp = true, bool psis_resample = true) {
using stan::services::pathfinder::internal::write_times;
using stan::services::pathfinder::internal::write_times;
const auto start_pathfinders_time = std::chrono::steady_clock::now();
std::vector<std::string> param_names;
param_names.push_back("lp_approx__");
Expand All @@ -130,7 +130,8 @@ inline int pathfinder_lbfgs_multi(
multi_writer_t multi_param_writer(
single_path_parameter_writer[iter], safe_write);
auto pathfinder_ret
= stan::services::pathfinder::pathfinder_lbfgs_single<false, true>(
= stan::services::pathfinder::pathfinder_lbfgs_single<false,
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,
Expand Down Expand Up @@ -172,7 +173,8 @@ inline int pathfinder_lbfgs_multi(
auto non_writer = [](auto&&... /* x */) {};
for (int iter = r.begin(); iter < r.end(); ++iter) {
auto pathfinder_ret
= stan::services::pathfinder::pathfinder_lbfgs_single<true, true>(
= stan::services::pathfinder::pathfinder_lbfgs_single<true,
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,
Expand Down Expand Up @@ -265,7 +267,8 @@ inline int pathfinder_lbfgs_multi(
}
double psis_delta_time = stan::services::util::duration_diff(
start_psis_time, std::chrono::steady_clock::now());
write_times<true>(parameter_writer, pathfinders_delta_time, psis_delta_time);
write_times<true>(parameter_writer, pathfinders_delta_time,
psis_delta_time);
return error_codes::OK;
}
stan::callbacks::concurrent_writer safe_write{parameter_writer};
Expand Down
20 changes: 10 additions & 10 deletions src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,20 +539,20 @@ auto pathfinder_impl(RNG&& rng, LPFun&& lp_fun, ConstrainFun&& constrain_fun,
* @param psis_delta_time Time taken for PSIS
*/
template <bool MultiPathfinder, typename ParamWriter>
inline void write_times(ParamWriter&& parameter_writer, double pathfinders_delta_time,
double psis_delta_time) {
inline void write_times(ParamWriter&& parameter_writer,
double pathfinders_delta_time, double psis_delta_time) {
parameter_writer();
const auto time_header = std::string("Elapsed Time: ");
std::string optim_time_str = time_header
+ std::to_string(pathfinders_delta_time)
+ std::string(" seconds") +
(MultiPathfinder ? " (Pathfinders)" : " (Pathfinder)");
std::string optim_time_str
= time_header + std::to_string(pathfinders_delta_time)
+ std::string(" seconds")
+ (MultiPathfinder ? " (Pathfinders)" : " (Pathfinder)");
parameter_writer(optim_time_str);
if (psis_delta_time != 0) {
std::string psis_time_str = std::string(time_header.size(), ' ')
+ std::to_string(psis_delta_time)
+ " seconds (PSIS)";
parameter_writer(psis_time_str);
std::string psis_time_str = std::string(time_header.size(), ' ')
+ std::to_string(psis_delta_time)
+ " seconds (PSIS)";
parameter_writer(psis_time_str);
}
std::string total_time_str
= std::string(time_header.size(), ' ')
Expand Down
200 changes: 102 additions & 98 deletions src/test/unit/services/pathfinder/eight_schools_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class ServicesPathfinderEightSchools : public testing::Test {
static constexpr int num_iterations = 2000;
static constexpr int refresh = 1;
static constexpr bool save_iterations = false;

};

constexpr std::array param_indices{0, 1, 3, 4, 5, 6, 7, 8, 9, 10,
Expand Down Expand Up @@ -153,7 +152,8 @@ TEST_F(ServicesPathfinderEightSchools, multi_psis_only_output) {
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
using stream_writer = stan::callbacks::unique_stream_writer<std::ofstream>;
using string_writer = stan::callbacks::unique_stream_writer<std::stringstream>;
using string_writer
= stan::callbacks::unique_stream_writer<std::stringstream>;
std::vector<stream_writer> single_path_parameter_writer(num_paths);
string_writer parameter_writer{std::make_unique<std::stringstream>(), "# "};
std::vector<stan::callbacks::json_writer<std::stringstream>>
Expand All @@ -170,8 +170,8 @@ TEST_F(ServicesPathfinderEightSchools, multi_psis_only_output) {
num_iterations, num_elbo_draws, num_draws, num_multi_draws, num_paths,
save_iterations, refresh, callback, logger,
std::vector<stan::callbacks::stream_writer>(num_paths, init),
single_path_parameter_writer, single_path_diagnostic_writer, parameter_writer,
diagnostics, calculate_lp, resample);
single_path_parameter_writer, single_path_diagnostic_writer,
parameter_writer, diagnostics, calculate_lp, resample);

auto str = parameter_writer.get_stream().str();
{
Expand All @@ -187,7 +187,6 @@ TEST_F(ServicesPathfinderEightSchools, multi_psis_only_output) {
}
}


TEST_F(ServicesPathfinderEightSchools, multi_and_single_psis_output) {
constexpr bool calculate_lp = true;
constexpr bool resample = true;
Expand Down Expand Up @@ -253,7 +252,8 @@ TEST_F(ServicesPathfinderEightSchools, multi_nopsis_only_output) {
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
using stream_writer = stan::callbacks::unique_stream_writer<std::ofstream>;
using string_writer = stan::callbacks::unique_stream_writer<std::stringstream>;
using string_writer
= stan::callbacks::unique_stream_writer<std::stringstream>;
std::vector<stream_writer> single_path_parameter_writer(num_paths);
string_writer parameter_writer{std::make_unique<std::stringstream>(), "# "};
std::vector<stan::callbacks::json_writer<std::stringstream>>
Expand All @@ -270,8 +270,8 @@ TEST_F(ServicesPathfinderEightSchools, multi_nopsis_only_output) {
num_iterations, num_elbo_draws, num_draws, num_multi_draws, num_paths,
save_iterations, refresh, callback, logger,
std::vector<stan::callbacks::stream_writer>(num_paths, init),
single_path_parameter_writer, single_path_diagnostic_writer, parameter_writer,
diagnostics, calculate_lp, resample);
single_path_parameter_writer, single_path_diagnostic_writer,
parameter_writer, diagnostics, calculate_lp, resample);
auto str = parameter_writer.get_stream().str();
{
auto&& streamer = parameter_writer.get_stream();
Expand All @@ -286,17 +286,19 @@ TEST_F(ServicesPathfinderEightSchools, multi_nopsis_only_output) {
}
}


TEST_F(ServicesPathfinderEightSchools, multi_and_single_nopsis_output) {
constexpr bool calculate_lp = false;
constexpr bool resample = false;
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
using unique_string_writer = stan::callbacks::unique_stream_writer<std::stringstream>;
using unique_string_writer
= stan::callbacks::unique_stream_writer<std::stringstream>;
std::vector<unique_string_writer> single_path_parameter_writer;
unique_string_writer parameter_writer{std::make_unique<std::stringstream>(), "# "};
unique_string_writer parameter_writer{std::make_unique<std::stringstream>(),
"# "};
for (int i = 0; i < num_paths; ++i) {
single_path_parameter_writer.emplace_back(std::make_unique<std::stringstream>(), "# ");
single_path_parameter_writer.emplace_back(
std::make_unique<std::stringstream>(), "# ");
}
std::vector<stan::callbacks::json_writer<std::stringstream>>
single_path_diagnostic_writer(num_paths);
Expand All @@ -312,8 +314,8 @@ TEST_F(ServicesPathfinderEightSchools, multi_and_single_nopsis_output) {
num_iterations, num_elbo_draws, num_draws, num_multi_draws, num_paths,
save_iterations, refresh, callback, logger,
std::vector<stan::callbacks::stream_writer>(num_paths, init),
single_path_parameter_writer, single_path_diagnostic_writer, parameter_writer,
diagnostics, calculate_lp, resample);
single_path_parameter_writer, single_path_diagnostic_writer,
parameter_writer, diagnostics, calculate_lp, resample);

{
auto str = parameter_writer.get_stream().str();
Expand All @@ -333,7 +335,8 @@ TEST_F(ServicesPathfinderEightSchools, multi_and_single_nopsis_output) {
for (auto&& single_param : single_path_parameter_writer) {
auto&& streamer = single_param.get_stream();
auto&& str = streamer.str();
std::ofstream file("../../single_path_" + std::to_string(sentinal) + ".csv");
std::ofstream file("../../single_path_" + std::to_string(sentinal)
+ ".csv");
file << str;
std::stringstream tmp_stream;
auto stan_data = stan::io::stan_csv_reader::parse(streamer, &tmp_stream);
Expand All @@ -349,16 +352,18 @@ TEST_F(ServicesPathfinderEightSchools, multi_and_single_nopsis_output) {
}

TEST_F(ServicesPathfinderEightSchools, single_output) {
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
stan::test::mock_callback callback;
using unique_string_writer = stan::callbacks::unique_stream_writer<std::stringstream>;
unique_string_writer parameter_writer{std::make_unique<std::stringstream>(), "# "};
int return_code = stan::services::pathfinder::pathfinder_lbfgs_single(
model, 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_writer, diagnostics);
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
stan::test::mock_callback callback;
using unique_string_writer
= stan::callbacks::unique_stream_writer<std::stringstream>;
unique_string_writer parameter_writer{std::make_unique<std::stringstream>(),
"# "};
int return_code = stan::services::pathfinder::pathfinder_lbfgs_single(
model, 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_writer, diagnostics);
auto str = parameter_writer.get_stream().str();
{
auto&& streamer = parameter_writer.get_stream();
Expand All @@ -373,85 +378,84 @@ int return_code = stan::services::pathfinder::pathfinder_lbfgs_single(
}
}


TEST_F(ServicesPathfinderEightSchools, single) {
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
stan::test::mock_callback callback;
int return_code = stan::services::pathfinder::pathfinder_lbfgs_single(
model, 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);
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
stan::test::mock_callback callback;
int return_code = stan::services::pathfinder::pathfinder_lbfgs_single(
model, 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);

Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, 0, ", ", ", ", "\n", "",
"", "");
Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, 0, ", ", ", ", "\n", "",
"", "");

Eigen::MatrixXd param_vals = parameter.get_eigen_state_values();
for (auto&& x_i : param_vals.col(2)) {
EXPECT_EQ(x_i, stride_id - 1);
}
auto param_tmp = param_vals(Eigen::all, param_indices);
auto mean_sd_pair = stan::test::get_mean_sd(param_tmp);
auto&& mean_vals = mean_sd_pair.first;
auto&& sd_vals = mean_sd_pair.second;
Eigen::MatrixXd param_vals = parameter.get_eigen_state_values();
for (auto&& x_i : param_vals.col(2)) {
EXPECT_EQ(x_i, stride_id - 1);
}
auto param_tmp = param_vals(Eigen::all, param_indices);
auto mean_sd_pair = stan::test::get_mean_sd(param_tmp);
auto&& mean_vals = mean_sd_pair.first;
auto&& sd_vals = mean_sd_pair.second;

Eigen::MatrixXd r_answer = stan::test::eight_schools_r_answer();
Eigen::MatrixXd r_answer = stan::test::eight_schools_r_answer();

Eigen::MatrixXd r_constrainted_draws_mat(20, 100);
{
stan::rng_t rng = stan::services::util::create_rng(0123, 0);
auto fn = [&model = ServicesPathfinderEightSchools::model](auto&& u) {
return -model.log_prob_propto_jacobian(u, 0);
};
Eigen::VectorXd unconstrained_draws;
Eigen::VectorXd constrained_draws1;
Eigen::VectorXd constrained_draws2(20);
Eigen::VectorXd lp_approx_vec(100);
// Results are from Lu's R code
lp_approx_vec << -12.0415891980758, -14.6692843779338, -13.4109656242788,
-12.227160804752, -10.8994669454787, -13.9464452858378,
-17.7039786093493, -11.3031695577237, -12.1849838459723,
-14.2633656680052, -13.7685697251323, -11.0849801402767,
-10.8285877691116, -12.3078922043268, -18.4862079401751,
-14.878979392217, -13.9884320991932, -15.7658450000531,
-13.5906482194447, -12.9120430284407, -18.2651279783073,
-13.0161106634425, -14.6633050842275, -15.708171891455,
-13.8002820377402, -13.4484536964903, -12.9558192824891,
-18.030159468489, -12.436042490926, -12.7938205793498,
-15.4295215357008, -11.7361108739125, -14.1692223330973,
-12.4698540687768, -16.2225112479695, -14.6021099557893,
-15.4163482862364, -11.9367428966647, -15.6987363918049,
-13.2541127046878, -13.395247477582, -13.7297660475934,
-15.5881489265056, -13.5906575138153, -19.5817805593569,
-15.3874299612537, -14.7803838914721, -13.5453155677371,
-18.5256438441971, -21.7907055918946, -13.9876362902857,
-14.3584339685507, -12.3086782261963, -13.4520009680182,
-13.2565205387879, -14.8449352555917, -11.7995060730947,
-16.1673766763038, -13.8230070576965, -14.4323461406136,
-14.5139646362747, -15.7152727007162, -16.0978882701874,
-12.8437110780737, -16.1267323384854, -17.5695117515445,
-15.7244669033694, -14.318592510172, -13.6331931944301,
-15.3973326320899, -16.6577158373945, -17.0600363400148,
-13.3516348546988, -12.2942663317071, -19.1148011460955,
-17.6392635944591, -13.3379766819778, -13.8803098238232,
-12.5059777414601, -15.8823434809178, -14.5040005356544,
-17.9707192175747, -14.3296312988667, -15.9246135209721,
-20.6431707513941, -14.2483182078639, -12.9012691966467,
-11.8312105455114, -14.2360469104402, -14.1732053430172,
-12.7669225560584, -14.3443242235104, -14.4185150275073,
-16.9557240942739, -14.2902638224899, -13.2814736915503,
-20.7083049704887, -17.6192198763631, -10.705036567492,
-12.1087056948567;
for (Eigen::Index i = 0; i < r_answer.cols(); ++i) {
unconstrained_draws = r_answer.col(i);
model.write_array(rng, unconstrained_draws, constrained_draws1);
constrained_draws2.tail(18) = constrained_draws1;
constrained_draws2(0) = lp_approx_vec(i);
constrained_draws2(1) = -fn(unconstrained_draws);
r_constrainted_draws_mat.col(i) = constrained_draws2;
Eigen::MatrixXd r_constrainted_draws_mat(20, 100);
{
stan::rng_t rng = stan::services::util::create_rng(0123, 0);
auto fn = [&model = ServicesPathfinderEightSchools::model](auto&& u) {
return -model.log_prob_propto_jacobian(u, 0);
};
Eigen::VectorXd unconstrained_draws;
Eigen::VectorXd constrained_draws1;
Eigen::VectorXd constrained_draws2(20);
Eigen::VectorXd lp_approx_vec(100);
// Results are from Lu's R code
lp_approx_vec << -12.0415891980758, -14.6692843779338, -13.4109656242788,
-12.227160804752, -10.8994669454787, -13.9464452858378,
-17.7039786093493, -11.3031695577237, -12.1849838459723,
-14.2633656680052, -13.7685697251323, -11.0849801402767,
-10.8285877691116, -12.3078922043268, -18.4862079401751,
-14.878979392217, -13.9884320991932, -15.7658450000531,
-13.5906482194447, -12.9120430284407, -18.2651279783073,
-13.0161106634425, -14.6633050842275, -15.708171891455,
-13.8002820377402, -13.4484536964903, -12.9558192824891,
-18.030159468489, -12.436042490926, -12.7938205793498,
-15.4295215357008, -11.7361108739125, -14.1692223330973,
-12.4698540687768, -16.2225112479695, -14.6021099557893,
-15.4163482862364, -11.9367428966647, -15.6987363918049,
-13.2541127046878, -13.395247477582, -13.7297660475934,
-15.5881489265056, -13.5906575138153, -19.5817805593569,
-15.3874299612537, -14.7803838914721, -13.5453155677371,
-18.5256438441971, -21.7907055918946, -13.9876362902857,
-14.3584339685507, -12.3086782261963, -13.4520009680182,
-13.2565205387879, -14.8449352555917, -11.7995060730947,
-16.1673766763038, -13.8230070576965, -14.4323461406136,
-14.5139646362747, -15.7152727007162, -16.0978882701874,
-12.8437110780737, -16.1267323384854, -17.5695117515445,
-15.7244669033694, -14.318592510172, -13.6331931944301,
-15.3973326320899, -16.6577158373945, -17.0600363400148,
-13.3516348546988, -12.2942663317071, -19.1148011460955,
-17.6392635944591, -13.3379766819778, -13.8803098238232,
-12.5059777414601, -15.8823434809178, -14.5040005356544,
-17.9707192175747, -14.3296312988667, -15.9246135209721,
-20.6431707513941, -14.2483182078639, -12.9012691966467,
-11.8312105455114, -14.2360469104402, -14.1732053430172,
-12.7669225560584, -14.3443242235104, -14.4185150275073,
-16.9557240942739, -14.2902638224899, -13.2814736915503,
-20.7083049704887, -17.6192198763631, -10.705036567492,
-12.1087056948567;
for (Eigen::Index i = 0; i < r_answer.cols(); ++i) {
unconstrained_draws = r_answer.col(i);
model.write_array(rng, unconstrained_draws, constrained_draws1);
constrained_draws2.tail(18) = constrained_draws1;
constrained_draws2(0) = lp_approx_vec(i);
constrained_draws2(1) = -fn(unconstrained_draws);
r_constrainted_draws_mat.col(i) = constrained_draws2;
}
}
}
Eigen::RowVectorXd mean_r_vals
= r_constrainted_draws_mat.rowwise().mean().transpose();
Eigen::RowVectorXd sd_r_vals
Expand Down

0 comments on commit e973755

Please sign in to comment.