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?