Restore the global RKG key when a transform raises - #96
Merged
Conversation
_BaseTransform swapped RKG.key for the traced key and restored it with a plain statement, so a transformed function that raised left the tracer installed in module-level state. Since pcx.RKG is the default argument of every layer and Vode constructor, every later random draw in the process then failed with UnexpectedTracerError.
cemde
force-pushed
the
fix/71-rkg-tracer-leak
branch
from
August 9, 2026 18:23
33fdbd0 to
1f38d99
Compare
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.
Bug
Inside a transform,
_wrap_fnswaps the globalRKG.keyfor the traced key so that layers constructed within the transform draw from the traced stream, then swaps it back:If
fnraises, the restore never runs and the tracer stays in module-level state.Impact
pcx.RKGis the default argument of every layer and Vode constructor, so a leaked tracer breaks every subsequent random draw in the process withUnexpectedTracerError, including in code that never touched a transform. One failedpxf.jitcall is enough. Under pytest, one failing test breaks every later test that constructs a layer.Fix
try/finallyaround the call, at both swap sites:The success path is unchanged;
return _r, kwargsstays outside thetry. The flow transforms (scan,while_loop,cond,switch) inherit_BaseTransformand route through the same closures, so both guarding tests are covered by these two edits.Nesting restores correctly in LIFO order: for
jit(value_and_grad(f))the innerfinallyrestores the outer transform's tracer, and the outerfinallyrestores the concrete key.Follow-up, not fixed here
Vmap._thas the same shape of bug at a different site: it splits the global key over the batch axis and merges it back with the merge outside anytry, so a raisingpxf.vmapleaves the key with an extra leading axis. Concrete rather than traced, so this PR's guarding test (which asserts "not a tracer") passes while the process is still poisoned. Different function, different fix, filed as #91.Closes #71