-
-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only swallow domain_errors in various algorithms #3259
Changes from 4 commits
643fd8b
5681a81
2709df7
759a569
24ef95f
c95036b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,7 +123,12 @@ int lbfgs(Model& model, const stan::io::var_context& init, | |
" # evals" | ||
" Notes "); | ||
|
||
ret = lbfgs.step(); | ||
try { | ||
ret = lbfgs.step(); | ||
} catch (const std::exception& e) { | ||
logger.error(e.what()); | ||
return error_codes::SOFTWARE; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Generally for any while loops I'd like to just have the try on the outside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved both |
||
lp = lbfgs.logp(); | ||
lbfgs.params_r(cont_vector); | ||
|
||
|
@@ -154,8 +159,16 @@ int lbfgs(Model& model, const stan::io::var_context& init, | |
if (save_iterations) { | ||
std::vector<double> values; | ||
std::stringstream msg; | ||
model.write_array(rng, cont_vector, disc_vector, values, true, true, | ||
&msg); | ||
try { | ||
model.write_array(rng, cont_vector, disc_vector, values, true, true, | ||
&msg); | ||
} catch (const std::exception& e) { | ||
if (msg.str().length() > 0) { | ||
logger.info(msg); | ||
} | ||
logger.error(e.what()); | ||
return error_codes::SOFTWARE; | ||
} | ||
if (msg.str().length() > 0) | ||
logger.info(msg); | ||
|
||
|
@@ -167,7 +180,16 @@ int lbfgs(Model& model, const stan::io::var_context& init, | |
if (!save_iterations) { | ||
std::vector<double> values; | ||
std::stringstream msg; | ||
model.write_array(rng, cont_vector, disc_vector, values, true, true, &msg); | ||
try { | ||
model.write_array(rng, cont_vector, disc_vector, values, true, true, | ||
&msg); | ||
} catch (const std::exception& e) { | ||
if (msg.str().length() > 0) { | ||
logger.info(msg); | ||
} | ||
logger.error(e.what()); | ||
return error_codes::SOFTWARE; | ||
} | ||
if (msg.str().length() > 0) | ||
logger.info(msg); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,7 +250,7 @@ inline elbo_est_t est_approx_draws(LPF&& lp_fun, ConstrainF&& constrain_fun, | |
approx_samples_col = approx_samples.col(i); | ||
++lp_fun_calls; | ||
lp_mat.coeffRef(i, 1) = lp_fun(approx_samples_col, pathfinder_ss); | ||
} catch (const std::exception& e) { | ||
} catch (const std::domain_error& e) { | ||
lp_mat.coeffRef(i, 1) = -std::numeric_limits<double>::infinity(); | ||
} | ||
log_stream(logger, pathfinder_ss, iter_msg); | ||
|
@@ -530,7 +530,7 @@ auto pathfinder_impl(RNG&& rng, LPFun&& lp_fun, ConstrainFun&& constrain_fun, | |
lp_fun, constrain_fun, rng, taylor_appx, | ||
num_elbo_draws, alpha, iter_msg, logger), | ||
taylor_appx); | ||
} catch (const std::exception& e) { | ||
} catch (const std::domain_error& e) { | ||
logger.warn(iter_msg + "ELBO estimation failed " | ||
+ " with error: " + e.what()); | ||
return std::make_pair(internal::elbo_est_t{}, internal::taylor_approx_t{}); | ||
|
@@ -820,7 +820,16 @@ inline auto pathfinder_lbfgs_single( | |
logger.info(lbfgs_ss); | ||
lbfgs_ss.str(""); | ||
} | ||
throw e; | ||
if (ReturnLpSamples) { | ||
// we want to terminate multi-path pathfinder during these unrecoverable | ||
// exceptions | ||
throw; | ||
Comment on lines
+824
to
+826
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? One pathfinder can fail while the others succeed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If one path does something unrecoverable like indexes into a vector out of bounds, I think the right thing to do is stop and say "something is clearly wrong with your model". Besides that, a primary motivation behind this PR is also to implement an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Are only unrecoverable errors able to be thrown here? Like if the call to log_prob throws it could be from something recoverable like one of the ode solvers failing for a given set of parameters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else { | ||
logger.error(e.what()); | ||
SteveBronder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return internal::ret_pathfinder<ReturnLpSamples>( | ||
error_codes::SOFTWARE, Eigen::Array<double, Eigen::Dynamic, 1>(0), | ||
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0), 0); | ||
} | ||
} | ||
} | ||
if (unlikely(save_iterations)) { | ||
|
@@ -900,7 +909,7 @@ inline auto pathfinder_lbfgs_single( | |
approx_samples_constrained_col) | ||
.matrix(); | ||
} | ||
} catch (const std::exception& e) { | ||
} catch (const std::domain_error& e) { | ||
std::string err_msg = e.what(); | ||
logger.warn(path_num + "Final sampling approximation failed with error: " | ||
+ err_msg); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this to the outer part of the while loop?