Fix spurious KeyReuseError under vmap over a zero-sized key array#38174
Open
Lawson-Darrow wants to merge 1 commit into
Open
Fix spurious KeyReuseError under vmap over a zero-sized key array#38174Lawson-Darrow wants to merge 1 commit into
Lawson-Darrow wants to merge 1 commit into
Conversation
With jax_debug_key_reuse enabled, vmap over a zero-sized PRNG key array raised a spurious KeyReuseError even when the function did no key reuse. In _SourceSinkBase.__init__ an all-True mask collapses to scalar True, but np.all() is vacuously True for an empty array, so a mask from a zero-sized vmap axis (which affects no keys) wrongly became True, i.e. full consumption. Check `not np.any(mask)` before `np.all(mask)` so an empty mask collapses to False, a no-op.
Contributor
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where vmap over a zero-sized key axis raised a spurious KeyReuseError. It reorders the mask checks in jax/experimental/key_reuse/_core.py so that empty masks (where np.all is vacuously True) are correctly handled first as a no-op. A regression test has also been added to verify this behavior. There are no review comments, so I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
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.
Fixes #37859.
When
jax_debug_key_reuseis enabled,vmap-ing a function over a zero-sized PRNG key array raised a spuriousKeyReuseError, even when the function did no key reuse (e.g. it split its key and consumed each derived sub-key exactly once).In
_SourceSinkBase.__init__, an all-True mask is collapsed to scalarTrue. Butnp.all()is vacuouslyTruefor an empty array, so a mask coming from a zero-sizedvmapaxis (which affects no keys) was wrongly collapsed toTrue, i.e. full consumption, which then tripped the reuse check.Checking
not np.any(mask)beforenp.all(mask)makes an empty mask collapse toFalse(a no-op) while leaving every non-empty mask unchanged (for any non-empty mask exactly one of the two branches was already taken). Adds a regression test.