bridgestan: Improved error handling in the C API
There was some discussion about error handling in #88, but I thought a separate issue would be a better place to discuss this.
Currently, the error messages of the C++ exception are printed to stderr, which works fine, but has the disadvantage that many users might not know to look there, and depending on the setup it might not even be easy to find it (for example in a jupyter notebook that is started from jupyterhub or from a system service). Also, if several models are failing at the same time, a user would have to figure out which error belongs to which model manually.
The two (I think) most important kinds of failures would be
- Errors during model creation (ie
bs_construct). Users will I think provide invalid data fairly often, and the error message should contain information about what part of the data was invalid. - Errors during
bs_log_density_gradient(or for different sampling / VI algorithms the other log_density functions): Error messages often contain information about why there was a divergence during sampling, and sampling algorithms might want to collect this information to help model debugging.
I can see a couple of different options of how this could be achieved, my favorites would probably be those:
- Store the error information in the
bs_model_rngstruct as an extra field, and provide an accessor function for it. This unfortunately makes thebs_log_density_gradientfunction family no longer thread safe. We can work around that issue however by creating newbs_model_rngstructs for each thread. With the current API this would require loading the data several times however. This again can be avoided by splittingbs_model_rnginto two separate types: One for the dataset and the potential error message from model creating, and a separate context for rng and error messages during sampling. This would then look like this:
bs_model *model = bs_model_construct(data)
if (model == 0) {
// A failure here indicates that we couldn't even allocate the error msg,
// which would usually not happen...
return -1;
}
// Callers have to check that there wasn't an error explicitly again!
const char *msg = bs_model_error_msg(model);
if (msg != 0) {
// Data was invalid...
// Do something with message
bs_model_free(model);
}
// In each thread do the following:
{
bs_ctx *ctx = bs_ctx_construct(model, seed, seed_idx);
if (ctx == 0)
stop();
}
// Call log_density and other functions in the sampler or...
for (int i = 0; i < 10; i++) {
int rc = bs_log_density(ctx, ...);
if (rc != 0) {
char *msg = bs_ctx_error_msg(ctx);
// do something with message
}
}
bs_ctx_free(ctx); // Frees the msg field if necessary
}
// And finally a single call to free the memory in the
// original thread
bs_model_free(model); # Frees the msg field if necessary
The additional function declarations:
// Create a new model. A non-null return value still has to
// be checked for errors with `bs_model_error_msg`.
bs_model *bs_model_construct(const char*);
// Check for an error in model creation.
const char *bs_model_error_msg(const bs_model*);
// Create a new ctx object with rng and space for an error message.
// the model has to outlive the ctx.
bs_ctx *ctx bs_ctx_construct(const bs_model*, int, int);
// Most other functions now take a bs_ctx argument.
// The ctx arguments are not const, because we might change the error.
int bs_log_density_gradient(bs_ctx*, ...);
const char *bs_ctx_error_msg(const bs_ctx*);
In this scenario bs_model would be Sync (functions that take a const pointer to bs_model can be called concurrently from different threads), but not Send (ownership of bs_model can not be transferred to a different thread, ie it has to be deallocated from the same thread where it was created. Is that actually a requirement?). bs_ctx doesn’t need to be Sync or Send, as every thread can just create a separate context (which should be super cheap).
- Add
char **error_msgarguments to the fallible functions, and add abs_free_error_msg(char *)function. This would then look something like this:
char *msg;
bs_model_rng *ctx = bs_construct(data, *msg);
if (ctx == 0) {
do_something_with_msg(msg);
bs_free_error_msg(msg);
}
int rc = bs_log_density(ctx, *msg, ...);
if (rc != 0) {
do_something_with_msg(msg);
bs_free_error_msg(msg);
}
I hope this is at least somewhat useful to you, happy to discuss more if there is interest in a change like this.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (3 by maintainers)
I haven’t done anything, seeing as I have no clue who all of you are.
On Friday, March 24, 2023, Brian Ward @.***> wrote: