Hi all. I just started working with RStan last week, working through some example models. However, compiling a model takes very long for me. I can’t imagine that the models I run are complex enough to warrant this, and my laptop is brand new too.
For example, I have this simple example model from the Bayesian Cognitive Modeling book (see stan-dev/example-models)
// Inferring a Rate
data {
int<lower=1> n;
int<lower=0> k;
}
parameters {
real<lower=0, upper=1> theta;
}
model {
// Prior Distribution for Rate Theta
theta ~ beta(1, 1);
// Observed Counts
k ~ binomial(n, theta);
}
I compiled this with RStan using the following R code:
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
m <- stan_model("Rate_1.stan")
Almost immediately, I got
recompiling to avoid crashing R session
After that, the model did compile successfully, but it took about forty seconds. I then used the sampling()
function using m
, and that worked fine within a second.
What could be causing the long compilation time?
Operating System: Ubuntu 24.04.2 LTS
Interface Version: RStan 2.32.7
40 seconds seems long. On my computer (2023 MacBook Pro with the M2 chip) stan_model()
takes 18 seconds to compile that model, and I did not get the recompiling to avoid crashing R session
message. Sadly, IDK what’s going on.
1 Like
Hi @gilles an welcome to the Stan forums.
Great news. Nevertheless, I would strongly recommend moving to cmdstanr
if you want to stay in R. It’s easier to install and it’s up to date with Stan and it’s also more stable.
Because of CRAN’s lack of dependency management and low ceiling on package size, we have a hard time keeping RStan up to date on CRAN. RStan is at Stan version 2.32 (May 2023), whereas cmdstanr is at Stan version 2.36 (Dec 2024). We’re currently putting together version 2.37 for release.
Hope it’s a Mac ARM chip—they’re much faster at running multiple chains of Stan because of the memory architecture. In any case, the first compilation you run is often bringing things in from all over the system. If you try to edit the model and recompile, it should be faster the second time.
The bottom line is that Stan takes a long time to compile in the best of cases (10 seconds minimum), because it’s doing a ton of C++ optimization on the fly. It’s possible to compile models with level 0 C++ optimization rather than level 3, which can cut compile time in half while making run time about 10 times slower. I’m not sure this is possible through RStan, but it should be possible through cmdstanr.
2 Likes
Thank you for the reply! It’s good to know that compilation time above 10 seconds is to be expected; I guess part of my issue was that I did not know what to expect of the compilation or run times at all. Is expected compilation/run time something that is mentioned anywhere in the documentation?
I started using cmdstanr
and it indeed compiles much faster (the same model took seven seconds now). It may take some more time for me to get comfortable with handling an R6 object, but it seems like the best way to go.
Thanks again!
Who knows? We have so much doc it would be hard to answer definitively. Certainly the CmdStanPy doc doesn’t mention it and that’s the interface I use these days.
You’ll be happy (or horrified) to learn that this was sped up with a lot of effort from our original compile times of nearly 45s. We originally thought Stan would only be used for very hard problems where 45s of compile time was dwarfed by fitting time, but that didn’t take into account the fact that people like to try different models and have to debug them all.
1 Like