Skip to content
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

Add optional methods for log_prob, grad_log_prob, hessian, un/constrain pars #701

Merged
merged 22 commits into from
Oct 9, 2022
Merged

Add optional methods for log_prob, grad_log_prob, hessian, un/constrain pars #701

merged 22 commits into from
Oct 9, 2022

Conversation

andrjohns
Copy link
Collaborator

Submission Checklist

  • Run unit tests
  • Declare copyright holder and agree to license (see below)

Summary

This PR adds the (optional) ability to compile and expose several methods from the Stan model so that they can be called by cmdstanr. After fitting the model, the user just needs to call the $init_model_methods() method, and R will compile and expose the following methods to the fit object:

  • log_prob
  • grad_log_prob
  • constrain_pars
  • unconstrain_pars

Additionally, if the user calls $init_model_methods() with the hessian = TRUE flag, a Hessian member function will also be compiled and exposed. I've defaulted this to FALSE since it requires all functions in the Stan model to be fvar<var> compatible, and I've already run into some issues with bernoulli_logit_glm in the basic testing (Math issue/PR to come).

These methods and the dependencies (Rcpp and RcppEigen) are left as suggests/optional, since I don't think we want cmdstanr to require compilation/linking at installation.

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):
Andrew Johnson

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

@rok-cesnovar rok-cesnovar self-requested a review October 6, 2022 07:13
@codecov-commenter
Copy link

codecov-commenter commented Oct 6, 2022

Codecov Report

Merging #701 (99edeba) into master (93d6ea7) will increase coverage by 0.37%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #701      +/-   ##
==========================================
+ Coverage   89.98%   90.35%   +0.37%     
==========================================
  Files          12       12              
  Lines        3653     3794     +141     
==========================================
+ Hits         3287     3428     +141     
  Misses        366      366              
Impacted Files Coverage Δ
R/args.R 98.65% <100.00%> (+<0.01%) ⬆️
R/fit.R 98.14% <100.00%> (+0.37%) ⬆️
R/model.R 91.57% <100.00%> (+0.14%) ⬆️
R/run.R 95.27% <100.00%> (+<0.01%) ⬆️
R/utils.R 84.00% <100.00%> (+4.25%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@rok-cesnovar
Copy link
Member

Awesome @andrjohns! Will review this later today.

@andrjohns
Copy link
Collaborator Author

@rok-cesnovar The WSL backend seems to be a bit brittle still. I'll be working through the WSL fixes PR soon (after the Math library system libs PR), should we disable the WSL-backend for now until I've got the next PR stable and in?

@rok-cesnovar
Copy link
Member

I am also fine leaving it as is. Will just ignore the failure on that test for the time being.

@andrjohns
Copy link
Collaborator Author

And before I forget, we can also cut down on the compilation time needed here in the future. The only function that needs to be compiled for each model specifically is the model_ptr function to initialise the stan model. The other functions can instead be compiled to take a generic stan::model::model_base type, which allows them to work with an arbitrary model pointer (the only exception being the hessian function, but that's about to be resolved with this PR).

The idea would be to compile the log_prob/grad_log_prob/etc functions once and store the compiled .dll/.so in the .cmdstanr folder, then when the user calls init_model_methods() they only compile the model_ptr function for the model and the rest are just loaded from the disk

@rok-cesnovar
Copy link
Member

The changes look great to me, and we can merge as is. Thanks again, this is awesome!

I have a few ideas/thoughts to discuss:

  • I am guessing this works only from a certain version of CmdStan onward, right? I am okay saying this works for 2.30+, no need to explore the exact version. I would just like to add a guard so that it doesn't fail for those versions where it does not work.
  • how do you feel about adding an argument to cmdstan_model() that would compile this automatically without having to call the $init_model_methods()?

Caching this would be great, but we can handle this once this is in.

@andrjohns
Copy link
Collaborator Author

andrjohns commented Oct 7, 2022

Great!

I am guessing this works only from a certain version of CmdStan onward, right? I am okay saying this works for 2.30+, no need to explore the exact version. I would just like to add a guard so that it doesn't fail for those versions where it does not work.

Nope, it's not relying on anything introduced recently (as far as I know), just existing headers in the Stan library (with the #ifdef to handle the move in the json parser from cmdstan)

how do you feel about adding an argument to cmdstan_model() that would compile this automatically without having to call the $init_model_methods()?

Good idea! I can add that tomorrow

Copy link
Member

@rok-cesnovar rok-cesnovar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants