Repository navigation
Recipe for automatically plumbing PRNG keys #41167
Unanswered
prasanthcakewalk
asked this question in
Ideas
Replies: 1 comment 1 reply
|
Very interesting, thanks for sharing! I do worry a bit about pushing traced values into a global context – that may lead to issues. Relatedly, have you seen the experimental |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi all. I wanted to share a recipe for automatically plumbing PRNG keys, while still maintaining support for JAX transforms and protecting against inadvertent reuse of keys.
The basic idea is to manage a stateful key vendor using a context variable in the global namespace. Context variables can be used to place local (traced) objects into the global namespace and access them directly from further up the call stack without plumbing those objects in and out of functions:
The recipe builds on this idea, adding a bell and a whistle to protect against unsafe jit. The full recipe is described here as part of a short tutorial: https://prasanth.codeberg.page/treepie/features/jit-aware-contextvars.html#auto-plumbing-random-keys.
Some context: I've been working on a library that
provideswill provide a few simple abstract features to simplify JAX programming, reduce boiler plate, etc. It's inspired by ideas from all around the jax ecosystem. Looking through the documentation forRefobjects and JEP 28845 (on stateful randomness), I figured treepie'sJitAwareContextVarfeature might be of interest here, especially to @jakevdp (I really appreciate your helpful answers to various questions here and elsewhere, btw). Also, because of how framework-agnostic this particular feature is, I think it could be suitable to be absorbed into JAX itself.Treepie's codebase and documentation still need work (there are some assorted bugs and one important feature to implement). But the implementation of
JitAwareContextVaris contained within just these few files:The notebook corresponding to the tutorial can be found here:
Thoughts and feedback appreciated.
All reactions