wlandau / instantiate

Pre-compiled CmdStan models in R packages
https://wlandau.github.io/instantiate/
Other
22 stars 2 forks source link

Unable to set `compile_model_methods=TRUE` in `install.libs.R` file #17

Closed ben18785 closed 9 months ago

ben18785 commented 9 months ago

Describe the bug

Cannot use precompiled model so that it allows log_p to be used within a package.

To Reproduce

  1. Create a new repo using instantiate::stan_package_create("mypackage")
  2. Create a new RStudio project in the "mypackage" directory
  3. Open the project and run instantiate::stan_package_configure()
  4. Edit the install.libs.R file so that the bottom becomes:
instantiate::stan_package_compile(
  models = instantiate::stan_package_model_files(path = bin_stan),
  compile_model_methods=TRUE
)
  1. Run devtools::install() which then fails to install with the following errors at the end:

Error in dyn.load("/private/var/folders/_f/0kqrxwdn69vgvgb02z2m3kqc0000gp/T/RtmpHO17o5/sourceCpp-x86_64-apple-darwin17.0-1.0.8/sourcecpp_a0bf6c45593e/sourceCpp_2.so") : unable to load shared object '/private/var/folders/_f/0kqrxwdn69vgvgb02z2m3kqc0000gp/T/RtmpHO17o5/sourceCpp-x86_64-apple-darwin17.0-1.0.8/sourcecpp_a0bf6c45593e/sourceCpp_2.so': dlopen(/private/var/folders/_f/0kqrxwdn69vgvgb02z2m3kqc0000gp/T/RtmpHO17o5/sourceCpp-x86_64-apple-darwin17.0-1.0.8/sourcecpp_a0bf6c45593e/sourceCpp_2.so, 0x0006): symbol not found in flat namespace '__ZN3tbb8internal26task_scheduler_observer_v37observeEb'

removing` ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/example’ Error in (function (command = NULL, args = character(), error_on_status = TRUE, : System command 'R' failed, exit status: 1, stdout & stderr were printed

Expected behavior

Package installs; it works fine when I don't edit the install.libs.R file to include compile_model_methods=TRUE

Session info

R version 4.1.2 (2021-11-01) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Monterey 12.2.1

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): [1] rstan_2.21.3 tidyselect_1.2.0 remotes_2.4.2
[4] purrr_1.0.1 colorspace_2.0-3 vctrs_0.6.2
[7] generics_0.1.2 testthat_3.1.2 usethis_2.1.5
[10] stats4_4.1.2 loo_2.4.1 utf8_1.2.2
[13] rlang_1.1.1 pkgbuild_1.3.1 pillar_1.9.0
[16] glue_1.6.2 withr_2.5.0 DBI_1.1.2
[19] sessioninfo_1.2.2 matrixStats_0.61.0 lifecycle_1.0.3
[22] instantiate_0.2.0 munsell_0.5.0 gtable_0.3.0
[25] devtools_2.4.3 codetools_0.2-18 memoise_2.0.1
[28] inline_0.3.19 callr_3.7.0 fastmap_1.1.0
[31] ps_1.6.0 parallel_4.1.2 fansi_1.0.2
[34] Rcpp_1.0.8 scales_1.2.1 cachem_1.0.6
[37] desc_1.4.1 pkgload_1.2.4 RcppParallel_5.1.5
[40] StanHeaders_2.21.0-7 fs_1.5.2 brio_1.1.3
[43] gridExtra_2.3 ggplot2_3.4.3 processx_3.5.2
[46] dplyr_1.1.2 rprojroot_2.0.2 grid_4.1.2
[49] cli_3.6.1 tools_4.1.2 magrittr_2.0.3
[52] tibble_3.2.1 crayon_1.5.0 pkgconfig_2.0.3
[55] ellipsis_0.3.2 prettyunits_1.1.1 rstudioapi_0.15.0
[58] R6_2.5.1 compiler_4.1.2

ben18785 commented 9 months ago

Update: the Stan model compiles if I just do:

stan_model <- cmdstanr::cmdstan_model(
    "src/stan/conditional_renewal.stan",
    compile_model_methods=TRUE,
    force_recompile=TRUE)

in the console. It also compiles if I do the following in the console:

instantiate::stan_package_compile(
  models = instantiate::stan_package_model_files(path = bin_stan),
  compile_model_methods=TRUE,
  force_recompile = TRUE
)

It just fails, when I do devtools::install()