Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR caches the expensive model methods (logp extraction, and function compilation).
It introduces a decorator for methods that can invalidate the cache of a model (such as registering a new variable or changing the initval strategy). When those methods are called the old cache is reset.
Closes #7815
One important aspect to be able to cache compiled functions is to reproduce the
random_seed
behavior frompytensorf.compile
. In hindsight this was a poor mixing of concerns, compiling the function and setting the RNG variables should not be mixed. This behavior is now disabled by settingrandom_seed=False
and use the newseed_compiled_function
helper instead. This allows us to cache the compiled functions but still respect the behavior ofrandom_seed
that may be passed to these functions.I decided to put the cache at the Model level so it's easier for the user to control. That means some parts of the codebase will contort themselves to use
model.compile_fn
in order to benefit from the cache.It's also important that they avoid creating new expressions (such as model.logp().sum())` as this will be a new variable and invalidate the cache.
📚 Documentation preview 📚: https://pymc--7816.org.readthedocs.build/en/7816/