From 1e776fa530e4f94a80ca8a2451d9e752c9f85869 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 5 Jan 2024 14:21:48 -0500 Subject: [PATCH] Remove array from writer overloads and have pathfinder only write with matrices --- src/stan/callbacks/unique_stream_writer.hpp | 51 +-------------------- src/stan/callbacks/writer.hpp | 33 +------------ src/stan/services/pathfinder/multi.hpp | 4 +- src/stan/services/pathfinder/single.hpp | 30 ++++++------ 4 files changed, 19 insertions(+), 99 deletions(-) diff --git a/src/stan/callbacks/unique_stream_writer.hpp b/src/stan/callbacks/unique_stream_writer.hpp index eacc0ddda3..3dc5294100 100644 --- a/src/stan/callbacks/unique_stream_writer.hpp +++ b/src/stan/callbacks/unique_stream_writer.hpp @@ -77,6 +77,7 @@ class unique_stream_writer final : public writer { return; write_vector(values); } + /** * Writes a set of values in csv format followed by a newline. * @@ -109,38 +110,6 @@ class unique_stream_writer final : public writer { *output_ << values.format(CommaInitFmt); } - /** - * Writes a set of values in csv format followed by a newline. - * - * Note: the precision of the output is determined by the settings - * of the stream on construction. - * - * @param[in] v Values in an Eigen column array - */ - void operator()(const Eigen::Array& values) { - if (output_ == nullptr) - return; - Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, Eigen::DontAlignCols, - ", ", "", "", "\n", "", ""); - *output_ << values.transpose().format(CommaInitFmt); - } - - /** - * Writes a set of values in csv format followed by a newline. - * - * Note: the precision of the output is determined by the settings - * of the stream on construction. - * - * @param[in] v Values in an Eigen row array - */ - void operator()(const Eigen::Array& values) { - if (output_ == nullptr) - return; - Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, Eigen::DontAlignCols, - ", ", "", "", "\n", "", ""); - *output_ << values.format(CommaInitFmt); - } - /** * Writes multiple rows and columns of values in csv format. * @@ -159,24 +128,6 @@ class unique_stream_writer final : public writer { *output_ << values.transpose().format(CommaInitFmt); } - /** - * Writes multiple rows and columns of values in csv format. - * - * Note: the precision of the output is determined by the settings - * of the stream on construction. - * - * @param[in] values An array of values. The input is expected to have - * parameters in the rows and samples in the columns. The array is then - * transposed for the output. - */ - void operator()(const Eigen::Array& values) { - if (output_ == nullptr) - return; - Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, Eigen::DontAlignCols, - ", ", "", "", "\n", "", ""); - *output_ << values.transpose().format(CommaInitFmt); - } - /** * Writes the comment_prefix to the stream followed by a newline. */ diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index fa61d46069..36a1bf4cdf 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -56,19 +56,7 @@ class writer { * parameters in the rows and samples in the columns. The matrix is then * transposed for the output. */ - virtual void operator()(const Eigen::MatrixXd& values) {} - - /** - * Writes multiple rows and columns of values in csv format. - * - * Note: the precision of the output is determined by the settings - * of the stream on construction. - * - * @param[in] values An array of values. The input is expected to have - * parameters in the rows and samples in the columns. The array is then - * transposed for the output. - */ - virtual void operator()(const Eigen::Array& vals) {} + virtual void operator()(const Eigen::Matrix& values) {} /** * Writes a set of values in csv format followed by a newline. @@ -90,25 +78,6 @@ class writer { */ virtual void operator()(const Eigen::Matrix& values) {} - /** - * Writes a set of values in csv format followed by a newline. - * - * Note: the precision of the output is determined by the settings - * of the stream on construction. - * - * @param[in] v Values in an Eigen column array - */ - virtual void operator()(const Eigen::Array& values) {} - - /** - * Writes a set of values in csv format followed by a newline. - * - * Note: the precision of the output is determined by the settings - * of the stream on construction. - * - * @param[in] v Values in an Eigen row array - */ - virtual void operator()(const Eigen::Array& values) {} }; } // namespace callbacks diff --git a/src/stan/services/pathfinder/multi.hpp b/src/stan/services/pathfinder/multi.hpp index b59a3e03f8..f7836b7341 100644 --- a/src/stan/services/pathfinder/multi.hpp +++ b/src/stan/services/pathfinder/multi.hpp @@ -170,13 +170,13 @@ inline int pathfinder_lbfgs_multi( num_returned_samples += ilpr.size(); } // Rows are individual parameters and columns are samples per iteration - Eigen::Array samples( + Eigen::Matrix samples( individual_samples[0].rows(), num_returned_samples); Eigen::Index filling_start_row = 0; for (size_t i = 0; i < successful_pathfinders; ++i) { const Eigen::Index individ_num_samples = individual_samples[i].cols(); samples.middleCols(filling_start_row, individ_num_samples) - = individual_samples[i]; + = individual_samples[i].matrix(); filling_start_row += individ_num_samples; } double psis_delta_time = 0; diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index d533633c81..e3a5a7b45a 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -619,7 +619,7 @@ inline auto pathfinder_lbfgs_single( logger.error(e.what()); return internal::ret_pathfinder( error_codes::SOFTWARE, Eigen::Array(0), - Eigen::Array(0, 0), 0); + Eigen::Matrix(0, 0), 0); } const auto num_parameters = cont_vector.size(); @@ -836,7 +836,7 @@ inline auto pathfinder_lbfgs_single( + " Optimization failed to start, pathfinder cannot be run."); return internal::ret_pathfinder( error_codes::SOFTWARE, Eigen::Array(0), - Eigen::Array(0, 0), + Eigen::Matrix(0, 0), std::atomic{num_evals + lbfgs.grad_evals()}); } else { logger.warn(prefix_err_msg + @@ -850,7 +850,7 @@ inline auto pathfinder_lbfgs_single( "successfully"); return internal::ret_pathfinder( error_codes::SOFTWARE, Eigen::Array(0), - Eigen::Array(0, 0), num_evals); + Eigen::Matrix(0, 0), num_evals); } else { if (refresh != 0) { logger.info(path_num + "Best Iter: [" + std::to_string(best_iteration) @@ -858,7 +858,7 @@ inline auto pathfinder_lbfgs_single( + " evaluations: (" + std::to_string(num_evals) + ")"); } } - Eigen::Array constrained_draws_mat; + Eigen::Matrix constrained_draws_mat; Eigen::Array lp_ratio; auto&& elbo_draws = elbo_best.repeat_draws; auto&& elbo_lp_ratio = elbo_best.lp_ratio; @@ -880,24 +880,24 @@ inline auto pathfinder_lbfgs_single( 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::Array(names.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) { - constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i); + constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i).matrix(); unconstrained_col = elbo_draws.col(i); constrained_draws_mat.col(i).tail(num_unconstrained_params) = constrain_fun(rng, unconstrained_col, - approx_samples_constrained_col); + approx_samples_constrained_col).matrix(); } for (Eigen::Index i = elbo_draws.cols(), j = 0; i < total_size; ++i, ++j) { - constrained_draws_mat.col(i).head(2) = lp_draws.row(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) = constrain_fun(rng, unconstrained_col, - approx_samples_constrained_col); + approx_samples_constrained_col).matrix(); } } catch (const std::exception& e) { std::string err_msg = e.what(); @@ -908,31 +908,31 @@ inline auto pathfinder_lbfgs_single( + "Returning the approximate samples used for ELBO calculation: " + err_msg); constrained_draws_mat - = Eigen::Array( + = Eigen::Matrix( names.size(), elbo_draws.cols()); Eigen::VectorXd approx_samples_constrained_col; Eigen::VectorXd unconstrained_col; for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) { - constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i); + constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i).matrix(); unconstrained_col = elbo_draws.col(i); constrained_draws_mat.col(i).tail(num_unconstrained_params) = constrain_fun(rng, unconstrained_col, - approx_samples_constrained_col); + approx_samples_constrained_col).matrix(); } lp_ratio = std::move(elbo_best.lp_ratio); } } else { constrained_draws_mat - = Eigen::Array( + = Eigen::Matrix( names.size(), elbo_draws.cols()); Eigen::VectorXd approx_samples_constrained_col; Eigen::VectorXd unconstrained_col; for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) { - constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i); + constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i).matrix(); unconstrained_col = elbo_draws.col(i); constrained_draws_mat.col(i).tail(num_unconstrained_params) = constrain_fun(rng, unconstrained_col, - approx_samples_constrained_col); + approx_samples_constrained_col).matrix(); } lp_ratio = std::move(elbo_best.lp_ratio); }