Is there a way for us to easily override how the key is defined? We are already using a custom CacheLocator for handling string-generated/compiled functions, but numba will still refuse to use the cached files because of the way the key is defined.
PS: The timings were done in ipython for ease of the %timeit magic, but the issue persists (and is perhaps only relevant) in a regular python script.
Have you considered using compile + exec sequence to compile the function’s code text in the module’s file and then executing it in the module’s namespace?
Not sure that makes sense for us? We’re compiling custom functions for users on demand, not a finite set of predefined functions. We already do compile + exec for the user defined functions, the issue is not that, but the inner functions in the closure not hashing to the same when we redefine them.
The key to utilizing ‘compile + exec’ for the purposes such as the one you described is to compile and execute the code in the module’s file and the global namespace, which is what addresses the issue of the closure you have encountered. Try this (a somewhat laborious example just to illustrate the point, feel free to shape it into something more streamlined for your needs),
Another option was to use register_jitable instead of njit for the inner function. That way the original function is used for the cache key which serializes the same way across multiple definitions.
Interesting observation - right, the original python function ‘incr_basic’ is given back by the ‘register_jitable’ (which does the ‘overload’ for the jitted context behind the scenes), and having python functions in the closure doesn’t invalidate the cache (it would have been quite inconvenient indeed if it did!). Thanks for sharing!
P.S. I have noticed ‘register_jitable‘ to be less flexible in some cases, for instance, one cannot directly compile the function with it for the desired signature.