0

I have a Python project with the following document structure:

/scripts
    main.py
/classes
    myclass.py
    ...

The class defined in myclass.py has a method which I aim to accelerate. I have achieved this by jitting parts of them into functions so that myclass.py looks like this:

def myclass:
    def method1:
        ...
    def method2:
        ...
        out1 = function1(inputs)
        out2 = function2(out1,other_inputs)
    ...
@njit()
def function1(inputs)
    ...
@njit()
def function2(inputs)
    ...

When I call the jitted functions twice without leaving myclass.py, they run faster during the second call. This is an expected result because of the compilation during the first call. But when I call the class method from outside myclass.py a second time (in my case from main.py), the jitted functions are compiled once again, and I cannot benefit from the acceleration at all. For this use case ahead of time (AoT) compilation looks like a solution, but the necessity to explicitly define the function signatures currently blocks me from using it. Therefore I wonder if there is a way to keep the accelerated versions of my function across different files without using AoT compilation. Thanks for the answers.

  • that doesn't happen from my experience, when you import the module containing function1, it is stored in memory, then any call to it will get the 'same' version of function1 that exists in myclass.py including the calls from inside myclass.py, are you sure that you are calling the function from main.py using the same argument types that were passed inside myclass.py ? if you have passed different argument types then you'll have 2 different compiled functions (for each type of inputs), you can check if you have done that by checking the function1.signatures for the compiled argument types. – Ahmed AEK Dec 06 '21 at 23:57
  • if by 'leaving' you mean you are closing the python interpreter or restarting it then the function will get out of memory, so you should use the numba cache=True to store the compiled functions between different python sessions. – Ahmed AEK Dec 07 '21 at 00:02
  • In the main.py, i run a test function which calls the method twice. Within the method, I call each jitted function twice and measure the runtime. The second run is 10 times faster, proving the optimization took place. Then the test function calls the method once again and I get another pair of measurement with the same readings. The jitted functions take the same type of inputs on each run, but to be on the safe side, I called the same test function once again with the same input to repeat everything and got the same readings again. – Kağan Aytekin Dec 07 '21 at 01:44
  • Additionally, adding the statement `cache=True` to the jit decorator did not change the result. – Kağan Aytekin Dec 07 '21 at 01:50
  • the cache doesn't work on all implementations of python, so you'll have to either use AOT compilation or modify numba to cache correctly on your python implementation. – Ahmed AEK Dec 07 '21 at 10:32

0 Answers0