bridgestan: Missing support for precompiled header
If I compile models using cmdstan, it seems to consistently finish about twice as fast as if I compile a model with bridgestan:
// justatest.stan
parameters {
real x;
}
model {
x ~ normal(0, 1);
}
Then
%%time
compiled = bridgestan.compile_model("justatest.stan")
# Wall time: 15.1 s
%%time
model = cmdstanpy.CmdStanModel("foo", "justatest.stan")
# Wall time: 7.18 s
I would have expected pretty much no difference, or even a bit faster comiles for bridgestan, because it doesn’t have to also compile the sampler and the command line tool?
Update This seems to be because cmdstan precompiles the header
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 25 (10 by maintainers)
If I understand this correctly, the reason that the precompiled header helps so much is because without it the compiler has to parse ~300_000 lines of code in ~3000 different files each time a model is compiled. This would also explain why reducing the optimization level helps so little with compile times. The optimization step in general is just shorter than the parsing step?
Then maybe a cleaner solution would be to reduce the number of includes that aren’t actually needed in a model. The special functions code from boost for instance is already 26_000 lines of code, but I’d guess only a very small subset of models actually uses more than one or two of those.
I guess that is easier said than done though, as that would require stanc to pick and choose includes depending on which functions are used in the model, instead of pulling everything in each time?
The precompiled headers are huge because they include the entire math library plus all the header-only dependencies (boost, eigen, …). The precomp. headers are much smaller with clang (see https://github.com/stan-dev/cmdstan/issues/872), at least they were a few years back.
clang is particularly annoying with precompiled headers because it breaks for any minor or patch version bump. And its seems that macs just update clang automatically.
CmdStan handles this by having separate precompiled headers for threads. So when you compile the first model with threads it builds separate precompiled headers with _threads suffix.
Similar numbers for me. It has a huge effect. If it didn’t I think we would have never made it the default in CmdStan.
I believe @rok-cesnovar knows more about pre-compiled headers/helped set them up for CmdStan. The doc for them always has lots of scary warnings, like
(emphasis mine)
This seems to me like it would be a huge problem with
STAN_THREADS, which affects quite a bit of stuff in the Math library. However, it seems like it is not a problem in practice for CmdStan - for now, anywayI can take a closer look tomorrow, with that ruled out my questions would be: Are there any additional commands used in our compilation compared to CmdStan (I think not, but good to check) Which commands invoked by make specifically are slower, assuming it isn’t just “all of them”