Using the 'cores' argument in a Stan based R package with precompiled models

Hi all,

I am currently working on an R package making use of precompiled Stan code as set up by rstantools::rstan_package_skeleton(). This generally works well, but it fails as soon as I try
to use the ‘cores’ argument of rstan::sampling. Below is the code of the function I use

fit_TIRT_stan <- function(data, blocks = NULL, init = 0, ...) {
  data <- make_TIRT_data(data, blocks)
  stan_data <- make_stan_data(data)
  stan_pars = c("gamma", "lambda", "psi", "Cor_trait", "eta")
  # TODO: fix problems when cores > 1
    data = stan_data, pars = stan_pars, 
    init = init, ...

When running fit_TIRT_stan(, cores = 2), I get the error

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: Objekt 'model_thurstonian_irt_model' not found

(thurstonian_irt_model is the name of the precompiled Stan file). Everything works well when running the model only on one core. Do you have an idea what is going wrong based on this information?

If necessary, I will try to build a minimal example using a dummy R package created with rstantools::rstan_package_skeleton().

Many thanks for your help!

This usually has to do with a different version of the package being picked up by the nodes than that which is loaded in the current R session. But if you can wait a bit, we are going to roll out a different build process for packages with precompiled models. I don’t know whether it will fix this problem or what new problems it will create, but it fixes some longstanding ones.

Thanks Ben! I get the same error when installing the package from source or binary versions. Do you have any specific workaround in mind? It is not a major issue for me just something I would like to fix at some point.

Can you verify that you don’t have multiple versions of the package with the same name installed in different places via installed.packages()?

sum(installed.packages()[, 1] == "<package name>")

evaluates to 1. I have completely uninstalled the package and installed it again, still the same issue.

It probably has something to do with this line in rstan

or the ones slightly above it that pertain to the paths. Somehow, it is not getting the right path to your package.

You also might try it on winbuilder to see if it is just a problem with the paths on your machine.

Thanks Ben, I will try out winbuilder.

@paul.buerkner I’m getting a similar error and would like to resolve it. Any insight you can share on how you fixed your problem?

I have used the latest rstantools version to prepare my package via rstan_package_skeleton. cores will work when I built the package via build source or build binary package but not when running build & reload. Not sure why.

1 Like