Calling a jit'd function with or without kwarg name has different behavior
See original GitHub issueConsider this example:
def foo(x, foo_power=10):
# ...
jit_foo = jax.jit(foo)
jit_foo(x, foo_power=11)
jit_foo(x, 11)
It would be reasonable to expect the two calls to jit_foo to have the same behavior/semantics. However, the first call with the kwarg name included will treat foo_power as a static arg, whereas the second call will treat it as a dynamic arg. This can cause errors if foo expects foo_power to be a compile-time constant (I ran into this jitting the fft method I’m working on).
A workaround is to always include kwargs in the static_argnums argument to jit. We might wanna consider doing this automatically in jit to have more predictable behavior by default.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Use **kwargs both in function calling and definition
In your get_data functions's namespace, there is a variable named arg1 , but there is no variable named arg2 . so you can...
Read more >Python args and kwargs: Demystified
In this step-by-step tutorial, you'll learn how to use args and kwargs in Python to add more flexibility to your functions. You'll also...
Read more >10 Examples to Master *args and **kwargs in Python
When a function is called, values for positional arguments must be given. ... It is possible to use the *args and named variables...
Read more >How To Use *args and **kwargs in Python 3 - DigitalOcean
In this tutorial, we will cover the syntax of working with *args and **kwargs as parameters within functions to pass a variable number...
Read more >PEP 468 – Preserving the order of **kwargs in a function.
Abstract. The **kwargs syntax in a function definition indicates that the interpreter should collect all keyword arguments that do not correspond to other...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mattjj, what’s unclear from the docstring (and confusing) is that the keyword vs positional args distinction is not made at the function definition, but rather at the call site. This makes sense from the implementation perspective, but IMO from the API perspective, it makes more sense to use how the args are specified in the definition since it can’t change across calls, and especially if you use jit() as a function decorator.
So I think the kwarg behavior is fine and makes sense, but we should use introspection to do it based on the function definition, not the function call.
+1 to @skye 's comment, also “principle of least astonishment”.