Seed the ADVI trainer's functions before compiling, as pymc does - #781
Open
jessegrabowski wants to merge 4 commits into
Open
jessegrabowski wants to merge 4 commits into
jessegrabowski wants to merge 4 commits into
Conversation
…does The trainer reseeded a compiled function by overwriting its RNG input storage with jax_typify hardcoded, which only worked on JAX. pymc's rule replaces it: a linker that copies RNGs at compile time gets a fresh compile per seed, and every other linker reseeds the cached function's shared variables through the graph.
…ery compiled step
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #781 +/- ##
===========================================
+ Coverage 51.60% 93.24% +41.64%
===========================================
Files 73 108 +35
Lines 8003 10278 +2275
===========================================
+ Hits 4130 9584 +5454
+ Misses 3873 694 -3179
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Trainernow seeds its compiled step and sampling functions before compiling, followingModel.compile_fn. It used to overwrite a compiled function's RNG storage afterward withjax_typifyhardcoded, which worked on JAX and did nothing on MLX. A linker that copies RNGs at compile time now gets a fresh compile per seed. Every other linker gets one cached compile per model, reseeded in place withreseed_rngs.The compiled functions are no longer attributes on the
Trainer. It subclasses pymc'sWithMemoization, and the unseeded compiles arelocally_cachedmethodentries keyed on the model, so a fit against a new model compiles again and a fit against the same one does not.The guide parameters and optimizer state now live in shared variables the
Trainercreates once and hands to every compile. Each compiled step used to allocate its own Adam buffers, so a seeded recompile on JAX or MLX reset the moments mid-training.GradientTransformationgained apytensor_inithook for those buffers.The MLX case of
test_reseeding_a_continued_fitis skipped. Compiling twice against one float64 shared variable stores a float32mx.arrayinto it, which is pymc-devs/pytensor#2378.