Accept the argnums and switch spellings the signatures promise - #101
Merged
Conversation
`ValueAndGrad` splatted `argnums` into a tuple, so the annotated `int` form raised `TypeError` and only `(0,)` worked. Normalise it once in the constructor. `Switch` passed its `Sequence` of branches straight to `_make_tuple`, which wraps anything that is not already a tuple, so a list of branches collapsed into a single callable. Convert to a tuple at the call site rather than teaching `_make_tuple` to expand sequences, which its other callers rely on not doing. Closes liukidar#74
cemde
force-pushed
the
fix/74-signature-mismatches
branch
from
August 9, 2026 18:23
75e43f6 to
5cd6c12
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
Two places where the annotation is wider than what the code accepts.
ValueAndGradis typedargnums: int | Sequence[int], matching jax, but_tsplats it:jax.value_and_grad(..., argnums=(*argnums, n)). Anintis not iterable.Switchis typedSequence[...]and passesfnsto_BaseTransform.__init__, which calls_make_tuple(fn). That helper isx if isinstance(x, tuple) else (x,), so a list of branches is wrapped rather than expanded, collapsing all branches into one element.Impact
Both documented spellings raise:
Only the undocumented
argnums=(0,)and a tuple of branches worked.Fix
Normalise at each call site:
ValueAndGrad.__init__wraps anintand tuples anything else, andSwitch.__init__callssuper().__init__(tuple(fns)).Normalising in
__init__rather than_talso fixes a silent bug: a generatorargnumsworked on the first call and then returned no positional gradient at all on the second, because_tre-splatted the now-exhausted generator.Why not in
_make_tupleTeaching the shared helper to expand sequences breaks four of its six callers. The decisive one is
_process_mask, which passes mask dict keys:stris aSequence, so"model"would expand into five single-character keys and the mask would fail with a pytree structure error. Two others pass user function return values, where a returned list would be resplit into value plus aux.Closes #74