cmdstan icon indicating copy to clipboard operation
cmdstan copied to clipboard

Optional profiling

Open adrian-lison opened this issue 1 year ago • 5 comments

Profiling is a great tool for debugging and performance optimization, but it also adds a bit of overhead to the execution of the Stan program. So it would be nice to skip the profiling statements whenever you don't need them and want the model to run as fast as possible - without the need to manually remove them from the stan program or to always keep two version of the same program (one with and one without profiling statements).

Ideal solution Ideally, there would be an additonal profile = FALSE/TRUE argument to the sample function of a compiled stan model that would be passed on to CmdStan.

Workaround As a workaround, one can automatically create a temporary copy of the stan program with all profiling statements removed, compile this temporary copy, and sample from this model without profiling. This requires no changes to CmdStan. The disadvantage is of course that this requires a second compiling of the stan model.

I have implemented this workaround as a wrapper of cmdstan_model in this repo. We have been using it during the development work for epinowcast and it has been quite robust. It also supports included files. It would also be possible to use this workaround for a profile = FALSE/TRUE argument in the sample function.

Context Regarding the ideal solution of integrating this functionality with CmdStan, I lack the expertise to judge how difficult that would be and probably could not do it myself. If people think a workaround like above is acceptable, I'm happy to help implementing it for cmdstanr, but would need some guidance (and a few details still need to be sorted out).

adrian-lison avatar Jan 16 '23 21:01 adrian-lison