Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/feature/reduce-mem-…
Browse files Browse the repository at this point in the history
…pathfinder' into feature/reduce-mem-pathfinder
  • Loading branch information
SteveBronder committed Jan 10, 2025
2 parents d395878 + 2047ff4 commit f522a83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/stan/callbacks/multi_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class multi_writer {
* Checks if all underlying writers are nonnull.
*/
inline bool is_nonnull() const noexcept {
return stan::math::apply([](auto&&... output) { return (output.is_nonnull() && ...); },
output_);
return stan::math::apply(
[](auto&&... output) { return (output.is_nonnull() && ...); }, output_);
}

/**
Expand All @@ -70,10 +70,11 @@ struct is_multi_writer : std::false_type {};

template <typename... Types>
struct is_multi_writer<multi_writer<Types...>> : std::true_type {};
}
} // namespace internal

template <typename T>
inline constexpr bool is_multi_writer_v = internal::is_multi_writer<std::decay_t<T>>::value;
inline constexpr bool is_multi_writer_v
= internal::is_multi_writer<std::decay_t<T>>::value;

} // namespace callbacks
} // namespace stan
Expand Down
26 changes: 14 additions & 12 deletions src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ inline taylor_approx_t taylor_approximation(
*/
template <bool ReturnLpSamples, typename EigVec, typename ParamWriter>
inline auto ret_pathfinder(int return_code, EigVec&& elbo_est,
const std::atomic<size_t>& lp_calls, ParamWriter&& /* params */) noexcept {
const std::atomic<size_t>& lp_calls,
ParamWriter&& /* params */) noexcept {
if constexpr (ReturnLpSamples) {
return std::make_tuple(return_code, std::forward<EigVec>(elbo_est),
lp_calls.load());
Expand All @@ -473,7 +474,6 @@ inline auto ret_pathfinder(int return_code, EigVec&& elbo_est,
}
}


/**
* Estimate the approximate draws given the taylor approximation.
* @tparam RNG Type of random number generator
Expand Down Expand Up @@ -617,8 +617,8 @@ inline auto pathfinder_lbfgs_single(
logger, init_writer);
} catch (const std::exception& e) {
logger.error(path_num + e.what());
return internal::ret_pathfinder<ReturnLpSamples>(error_codes::SOFTWARE,
internal::elbo_est_t{}, 0, parameter_writer);
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, internal::elbo_est_t{}, 0, parameter_writer);
}

const auto num_parameters = cont_vector.size();
Expand Down Expand Up @@ -836,7 +836,8 @@ inline auto pathfinder_lbfgs_single(
+ " Optimization failed to start, pathfinder cannot be run.");
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, internal::elbo_est_t{},
std::atomic<size_t>{num_evals + lbfgs.grad_evals()}, parameter_writer);
std::atomic<size_t>{num_evals + lbfgs.grad_evals()},
parameter_writer);
} else {
logger.warn(prefix_err_msg +
" Stan will still attempt pathfinder but may fail or produce "
Expand All @@ -848,7 +849,8 @@ inline auto pathfinder_lbfgs_single(
"Failure: None of the LBFGS iterations completed "
"successfully");
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, internal::elbo_est_t{}, num_evals, parameter_writer);
error_codes::SOFTWARE, internal::elbo_est_t{}, num_evals,
parameter_writer);
} else {
if (refresh != 0) {
logger.info(path_num + "Best Iter: [" + std::to_string(best_iteration)
Expand All @@ -861,7 +863,8 @@ inline auto pathfinder_lbfgs_single(
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, parameter_writer);
error_codes::OK, std::move(est_draws), num_evals + est_draws.fn_calls,
parameter_writer);
} else {
std::vector<std::string> names;
names.push_back("lp_approx__");
Expand Down Expand Up @@ -959,22 +962,21 @@ inline auto pathfinder_lbfgs_single(
single_stream();
std::string pathfinder_time_str = "Elapsed Time: ";
pathfinder_time_str += std::to_string(pathfinder_delta_time)
+ std::string(" seconds (Pathfinder)");
+ std::string(" seconds (Pathfinder)");
single_stream(pathfinder_time_str);
single_stream();
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::OK, internal::elbo_est_t{}, num_evals, parameter_writer);
error_codes::OK, internal::elbo_est_t{}, num_evals, parameter_writer);
} else {
parameter_writer();
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, parameter_writer);
error_codes::OK, internal::elbo_est_t{}, num_evals, parameter_writer);
}

}
}

Expand Down

0 comments on commit f522a83

Please sign in to comment.