Save posterior samples to backend rather than holding in RAM?

I have a large model that has potential to consume my RAM by virtue of holding all the posterior samples in memory. I would like to instead write the samples to file as they are obtained, assuming this will then liberate some RAM.

Looking through the docs for the pm.sample() method it seems that ‘trace’ could perhaps achieve this? But I’m stuck on what ‘backend’ instance to provide, there doesn’t seem to be an example script to demonstrate this. Any tips would be greatly appreciated, thanks!

CC @michaelosthege

Hi @cowirihy,

yes you can use McBackend with a ClickHouse database backend as described here.

If your model still consumes too much RAM even without the MCMC draws & stats, you could also offload the RAM-intensive parts of the model to another machine using pytensor-federated.


This is great, I will try that. Thanks so much