Why is it so slow for stan to compile model?

I tried to run a few Bayesian models with rstan package, but it seems that it takes stan a long time to compile models. Is there any way to speed up compiling?


Not unless you are willing to slow down the model running (by reducing the level of optimization).

Can you share your current compiler flags?

I’ve found that the debug symbols “-g” add a significant amount to the compilation time, and I’ve never had a stan model crash where the debugging symbols are of any use, so I omit it now.

1 Like

currently, the generated c++ code for a Stan model requires too much compilation of code in the Stan services and math library. we’re working on it!

To do as Ben is suggesting and lower the optimization level (which seems helpful for quick iteration on a model), you can change the -O flag in ‘make/local’ (and there should be something similar for RStan and PyStan I believe) by adding the following text: ‘O=0’

Speeding up compilation at the expense of longer run times seems attractive for the phase where I develop a model. I use RStudio, knitr, where the Stan model is inside a code chuck that starts with the three backticks ``` followed by {stan output.var=‘jwmod’}
(if I just copy the code, formatting of this message is garbled up).

I am wondering, how do I change the compiler options from there?

It is a bit inconvenient to wait 1 minute for Stan to compile a simple model. As @bgoodri said, it is possible to speed up compilation by reducing optimization level in ~/.R/Makevars (change CXX14FLAGS=-O3 to CXX14FLAGS=-O0 for example), but then it will result in slower sampling.

Why does it take so long for the model to compile in the first place? I have not looked at Stan code, but is it possible to precompile most of Stan code into a dynamic library? This way, we only need to compile code that is just related to the model, and not everything. Probably, this is what Stan is doing already, I’m not sure… Thanks! :)

1 Like

Stan compilation speedup was introduced in Stan 2.20, but Rstan is only up to Stan 2.19.2.

right now we have lightweight wrappers CmdStanPy (still in beta) and CmdStanR (almost to beta release) which use the latest CmdStan (now at 2.21, 2.22 coming soon) - or any version of CmdStan that you’d like.