jax.jit: negative static_argnums is not applied when the argument is passed by keyword (static_argnames not inferred)
Description
Description
For jax.jit(f, static_argnums=1), JAX infers the matching static_argnames from the signature, so the
argument is static whether it is passed positionally or by keyword. With the equivalent negative index,
static_argnums=-1, the name is not inferred: a positional call treats the argument as static, but a
keyword call traces it, so code that needs a Python value (e.g. range(n)) fails with a tracer error, and
code that doesn't silently runs with a dynamic argument.
Steps to reproduce
import jax
g = jax.jit(lambda x, n: sum(x for _ in range(n)), static_argnums=-1)
print(g(1, 3)) # 3
print(g(1, n=3)) # TracerIntegerConversionError
h = jax.jit(lambda x, n: sum(x for _ in range(n)), static_argnums=1)
print(h(1, n=3)) # 3
Expected
static_argnums=-1 behaves like static_argnums=1 for a two-argument function, for positional and keyword calls.
System info (python version, jaxlib version, accelerator, etc.)
Environment
JAX / jaxlib 0.11.2 (the latest release, CPU) and 0.10.1, Python 3.12, Ubuntu 24.04, x86_64
Thanks – I've never seen negative static_argnums used, and I'm surprised it's supported at all. We should probably explicitly error when static_argnums is less than zero, but this will require a deprecation cycle.
I opted for continuing to support negative argnums, but fixing this case: #40988