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.

JIT often recompiles bound methods

See original GitHub issue

I’m going close this issue. I just want to save a record of it in case I come up with a reasonable solution one day.

If a method is always jitted, then Jax compiles it once:

from jax import jit
from functools import partial

class X:
  @partial(jit, static_argnums=(0,))
  def f(self):
    print("Recompiling")

x = X()
for i in range(3):
    x.f()

However, if the caller wants to jit the method, then Jax compiles it every time:

from jax import jit

class X:
  def f(self):
    print("Recompiling")

x = X()
for i in range(3):
    jit(x.f)()

It would be nice if jit detected the case when the function f is a bound method (use inspect.ismethod(f)), and maybe emit a warning.

If you wanted to fix it automatically, you could use replace the function with function.__func__, and add a positional argument function.__self__. The issue with this is whether that argument should be static or not is impossible to know. That’s why the warning makes sense: it forces the caller to explicitly choose whether the argument is static or dynamic.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
patrick-kidgercommented, Mar 28, 2022

team

Just me! 😄

1reaction
jakevdpcommented, May 6, 2022

FYI, there is now a section in the FAQ addressing jit compilation of methods: https://jax.readthedocs.io/en/latest/faq.html#how-to-use-jit-with-methods

Read more comments on GitHub >

github_iconTop Results From Across the Web

Just-in-time compilation - Wikipedia
JIT compilation is a combination of the two traditional approaches to translation to machine code—ahead-of-time compilation (AOT), and interpretation—and ...
Read more >
Just-in-Time compilation — Numba 0.50.1 documentation
JIT functions. Compile the decorated function on-the-fly to produce efficient machine code. All parameters are optional. If present, the signature is either a ......
Read more >
JIT Compilers for Ruby and Rails: An Overview - AppSignal Blog
JIT compilation is a type of dynamic compilation that enables adaptive optimization techniques, including dynamic recompilation and ...
Read more >
Chapter 4. Working with the JIT Compiler - O'Reilly
If a method runs often enough, it will get compiled at level 4 (and the level 3 code will be made not entrant)....
Read more >
What is Just-In-Time(JIT) Compiler in .NET - GeeksforGeeks
The cache memory is heavily used by the JIT compiler to store the source code methods that are required at run-time. Note: Much...
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