stan-dev / stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
2.55k stars 365 forks source link

Only swallow domain_errors in various algorithms #3259

Closed WardBrian closed 4 months ago

WardBrian commented 5 months ago

Submission Checklist

Summary

Closes #3258. This changes any place which caught std::exception from the model to only catch std::domain_error, matching what is done during initialization.

This prevents odd behavior like an indexing exception that only occurs randomly being swallowed if initialization happens to pass, and allows things like an exit() function in the language.

Intended Effect

Cause certain exceptions to terminate the inference algorithms.

How to Verify

This model will usually initialize but quickly fail now:

parameters {
  real mu;
}
model {
  mu ~ normal(0, 1);

  if (mu > 1) {
    array[1] int sigma;
    print(sigma[2]);
  }
}

Side Effects

I've installed handlers in the service functions, but code that calls the internals of these algorithms (e.g run_adaptive_sampler) not through the services will need to be ready to catch exceptions

Additionally, if any Math functions raise an exception which is intended to be recoverable but is not a domain_error, they will need updating. Because this is the current behavior in initialization, I suspect we have probably already smoked any of these out.

Documentation

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

Simons Foundation

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

WardBrian commented 5 months ago

This is failing downstream due to https://github.com/stan-dev/math/issues/1039: Some CVODES errors are still thrown as generic runtime_errors in https://github.com/stan-dev/math/blob/develop/stan/math/prim/err/check_flag_sundials.hpp

@syclik - the discussion from back then suggested these becoming domain_errors, but they didn't. Was there some reason for this, or should I go ahead and open a PR to math which does that?

WardBrian commented 5 months ago

@SteveBronder mind giving this a look?

WardBrian commented 4 months ago

std::out_of_range would be considered fatal by this and lead to the algorithms terminating. Is there something else you're looking for clarification on about them?

SteveBronder commented 4 months ago

out of range can make sense, but what about std::invalid_argument? Like quad_form_sym would throw that for symmetric matrices. Would we just fail on that error and end the program?

WardBrian commented 4 months ago

Those have been considered unrecoverable (at least during initialization) for a while. invalid_argument is thrown for things like check_square, which won't change if you just 'try again'.

I think if there are usages in Math we want to be recoverable, we should change those to match the others

SteveBronder commented 4 months ago

Okay looking at this again I think this is fine to merge 👍