cmdstanr icon indicating copy to clipboard operation
cmdstanr copied to clipboard

Compile model methods once and reuse for all models

Open andrjohns opened this issue 2 years ago • 14 comments

Submission Checklist

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

Summary

This PR adds the ability for the model methods to be pre-compiled and then simply linked to the object file produced by cmdstan. This will significantly speed up users' workflows, as they only need to perform the model method compilation once, instead of every time $init_model_methods() is called

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:

  • Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
  • Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

andrjohns avatar Jan 10 '24 21:01 andrjohns

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (3c7a1a9) 88.28% compared to head (eae8189) 88.32%.

:exclamation: Current head eae8189 differs from pull request most recent head 0ac09f9. Consider uploading reports for the commit 0ac09f9 to get more accurate results

Files Patch % Lines
R/utils.R 93.05% 5 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #894      +/-   ##
==========================================
+ Coverage   88.28%   88.32%   +0.03%     
==========================================
  Files          12       12              
  Lines        4534     4592      +58     
==========================================
+ Hits         4003     4056      +53     
- Misses        531      536       +5     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Jan 11 '24 18:01 codecov-commenter

I tested as

library(cmdstanr)
# Using a non-merged PR #894
devtools::load_all('~/proj/cmdstanr2')
model1 <- cmdstan_model(stan_file = root("Birthdays", "gpbf1.stan"),
                        include_paths = root("Birthdays"),
                        compile_model_methods=TRUE, force_recompile=TRUE)

and got

Compiling Stan program...
Compiling and caching additional model methods...
Linking precompiled model methods to model object file...
Error in dyn.load(methods_dll, local = TRUE, now = TRUE) : 
  unable to load shared object '/tmp/RtmpNbmTDV/file2c1322e092b0.so':
  /tmp/RtmpNbmTDV/file2c1322e092b0.so: cannot open shared object file: No such file or directory

Can you provide more instructions on how to test this?

avehtari avatar Jan 21 '24 15:01 avehtari

@avehtari Thanks for catching that issue! It looks like Linux needs the Stan model to be compiled with an additional flag for R to be able to link to it afterwards.

I've pushed a fix and the CI is passing, can you pull the changes and try again when you get a minute?

andrjohns avatar Jan 22 '24 05:01 andrjohns

I get the same error

avehtari avatar Jan 22 '24 08:01 avehtari

This will significantly speed up users' workflows, as they only need to perform the model method compilation once, instead of every time $init_model_methods() is called

Awesome! Thanks for working on this.

jgabry avatar Jan 23 '24 18:01 jgabry

Sorted! This is ready for review now

andrjohns avatar Jan 24 '24 13:01 andrjohns

Compilation and re-use of model methods in other model worked.

But it seems I still need to recompile every model in a session, and I just save the time not needing to recompile the model methods part several times? As the models usually don't need to be recompiled, can you explain why the model methods can't be used with pre-compiled models?

avehtari avatar Jan 24 '24 13:01 avehtari

Compiling brms generated model with compile_model_methd=TRUE crashes. Also with another brms model, but not with my own handwritten model code.

library(cmdstanr)
data("VerbAgg", package = "lme4")
VerbAgg$r3 <- as.numeric(VerbAgg$resp)
sc <- brms::make_stancode(r3 ~ btype + mode + situ + (btype + mode + situ | id), 
                    data = VerbAgg, family = brms::cumulative())
sf <- write_stan_file(sc)
m1 <- cmdstan_model(sf, compile_model_methods=TRUE, force_recompile=TRUE)
Linking precompiled model methods to model object file...

 *** caught segfault ***
address 0x7f0b7bb8c008, cause 'invalid permissions'

Traceback:
 1: dyn.load(methods_dll, local = TRUE, now = TRUE)
 2: force(code)
 3: force(code)
 4: with_envvar(c(R_MAKEVARS_USER = makevars_file), {    set_makevars(new, path, makevars_file, assignment = assignment)    force(code)})
 5: withr::with_makevars(new_makevars, expr)
 6: force(code)
 7: withr::with_path(c(paste0(cmdstan_path(), lib_paths), toolchain_PATH_env_var()),     withr::with_makevars(new_makevars, expr))
 8: with_cmdstan_flags(dyn.load(methods_dll, local = TRUE, now = TRUE))
 9: expose_model_methods(private$model_methods_env_, verbose = !quiet)
10: self$compile(...)
11: initialize(...)
12: CmdStanModel$new(stan_file = stan_file, exe_file = exe_file,     compile = compile, ...)
13: cmdstan_model(sf, compile_model_methods = TRUE, force_recompile = TRUE)

avehtari avatar Jan 25 '24 09:01 avehtari

Looks related to this: https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771/4

Can you try compiling with STAN_THREADS enabled?

andrjohns avatar Jan 25 '24 10:01 andrjohns

Another error

> + > > Compiling Stan program...
 
 *** caught segfault ***
address 0x7f6676099008, cause 'invalid permissions'

Traceback:
 1: dyn.load("/tmp/RtmpmAozHV/sourceCpp-x86_64-pc-linux-gnu-1.0.12/sourcecpp_272bf5990ba0/sourceCpp_2.so")
 2: eval(ei, envir)
 3: eval(ei, envir)
 4: withVisible(eval(ei, envir))
 5: source(scriptPath, local = env)
 6: Rcpp::sourceCpp(code = code, env = env, verbose = verbose)
 7: force(code)
 8: force(code)
 9: with_envvar(c(R_MAKEVARS_USER = makevars_file), {    set_makevars(new, path, makevars_file, assignment = assignment)    force(code)})
10: withr::with_makevars(c(USE_CXX14 = 1, PKG_CPPFLAGS = ifelse(cmdstan_version() <=     "2.30.1", "-DCMDSTAN_JSON", ""), PKG_CXXFLAGS = paste0(cxxflags,     cmdstanr_includes, collapse = " "), PKG_LIBS = libs), Rcpp::sourceCpp(code = code,     env = env, verbose = verbose))
11: force(code)
12: withr::with_path(paste0(cmdstan_path(), lib_paths), withr::with_makevars(c(USE_CXX14 = 1,     PKG_CPPFLAGS = ifelse(cmdstan_version() <= "2.30.1", "-DCMDSTAN_JSON",         ""), PKG_CXXFLAGS = paste0(cxxflags, cmdstanr_includes,         collapse = " "), PKG_LIBS = libs), Rcpp::sourceCpp(code = code,     env = env, verbose = verbose)))
13: rcpp_source_stan(code, env, verbose)
14: expose_model_methods(env = private$model_methods_env_, verbose = !quiet,     hessian = compile_hessian_method)
15: self$compile(...)
16: initialize(...)
17: CmdStanModel$new(stan_file = stan_file, exe_file = exe_file,     compile = compile, ...)
18: cmdstan_model(sf, compile_model_methods = TRUE, force_recompile = TRUE)

avehtari avatar Jan 25 '24 10:01 avehtari

That call stack indicates that you're not on this branch, since it's using the "old" pathway for exposing model methods

andrjohns avatar Jan 25 '24 11:01 andrjohns

Either way I'll have a proper look into this tomorrow and add a fix

andrjohns avatar Jan 25 '24 11:01 andrjohns

After rebuilding with threads I get

> + + > > Compiling Stan program...
Linking precompiled model methods to model object file...
Error in dyn.load(methods_dll, local = TRUE, now = TRUE) : 
  unable to load shared object '/tmp/RtmpS8Bk6H/file282fa157edb31.so':
  /tmp/RtmpS8Bk6H/file282fa157edb31.so: cannot open shared object file: No such file or directory

avehtari avatar Jan 25 '24 11:01 avehtari

FYI that I'm going to leave this PR for the v1.0/CRAN branch. A lot of the complexity/issues here are caused by the windows cmdstan using mingw32-make/gcc from pacman, while R and Rcpp use the RTools utilities, causing a bunch of headaches when linking objects between the two.

Once we move to RTools-only on windows, this PR/implementation will be much simpler and easier

andrjohns avatar Apr 23 '24 05:04 andrjohns