Can we check whether a njit function has a valid cache?

I wonder whether it is possible to provide the calling signature and check a function has been cached?

Thanks!

Hi @wuyuanyi135

Numba dispatcher objects (the thing returned by @jit) have a .stats property which tells you about cache hits/misses for a given signature.

Example:

$ rm -rf __pycache__/ # clear the cache

$ cat di60.py
from numba import njit
import numpy as np

@njit(cache=True)
def foo(x):
    return x + 1

foo(10)
print(foo.stats)

$ python di60.py 
_CompileStats(cache_path='<path>/__pycache__', cache_hits=Counter(), cache_misses=Counter({(int64,): 1}))

$ python di60.py 
_CompileStats(cache_path='<path>/__pycache__', cache_hits=Counter({(int64,): 1}), cache_misses=Counter())

hope this helps?

1 Like

Thank you! If I understand correctly, this stats will be updated after the invocation (foo(10)). Is there anyway to query the cache state with the signature but not actually trigger the compilation?

I am doing this to investigate whether cache is working in some special cases such as this post. However, as you guys could kindly provide informative and timely response, I may not rely on this cache check too much :slight_smile:

Hi @wuyuanyi135,

Just to check… Are you asking if there’s a way to query if there’s a cached function available for a particular jit wrapped function and given signature?

Hi, this is what I’d like to do as well if possible.

That is, a quick way to check whether a function is cached before it is actually executed.

Is this possible?

i.e. I’d like to query if there’s a cached function available for a particular jit wrapped function and given signature BUT… do this before an attempt is actually made to either 1) retrieve the cached function or, 2) compile and save a new cached function

I note your ‘foo.stats’ solution, this is really useful, but if I’m correct it can only be done after the function is executed?

Thanks

You could read the .nbi file similar to what the cache locator does to find out if there would be a cache hit.