Skip to content

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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.35%. Comparing base (93d6ea7) to head (99edeba).
⚠️ Report is 828 commits behind head on master.

Additional details and impacted files
@@            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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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