question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling a jit'd function with or without kwarg name has different behavior

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
skyecommented, Mar 26, 2019

@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.

0reactions
afrozenatorcommented, Mar 29, 2019

+1 to @skye 's comment, also “principle of least astonishment”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found