My code needs to look differently when @jit is deactivated - how can I avoid this?

Hi,

the following code needs to look differently, depending whether the @jit directive is activated or not.

Just out of curiosity: is it possible to change the code that it is independent from the @jit directive?

import numba as nb
import numpy as np

float_array = nb.types.float64[:]

@nb.jit(nopython=True, cache=True)
def myF1(myInputArray):
    myIoArrayDict = nb.typed.Dict.empty(
        key_type=nb.types.unicode_type,
        value_type=float_array, )

    if True: # <<<<<<<<< change to False depending from @jit setting
        myIoArrayDict['a'] = np.zeros_like(myInputArray[:, 0], dtype=nb.types.float64)
    else:
        myIoArrayDict['a'] = np.zeros_like(myInputArray[:, 0], dtype=float)

def main():
    myInputArray = np.array([[1, 2, 3], [1, 2, 3]])
    myF1(myInputArray)

if __name__ == '__main__':
    main()

So when I deactivate @nb.jit(nopython=True, cache=True), I get a TypeError: Cannot interpret 'float64' as a data type.

I can mitigate this with choosing dtype=float (setting True to False at the if statement), but then I would get the following error once I activate the @jit again:

No implementation of function Function(<function zeros_like at 0x7f21231243a0>) found for signature:
 
 >>> zeros_like(array(int64, 1d, A), dtype=Function(<class 'float'>))

So my question is:

Is there any definition of myIoArrayDict['a'] possible that is invariant of the @jit directive?

Thanks in advance!

hi @alatif-alatif , I had this problem. I think I solved it by using np.float64 everywhere. It works in both jit and non-jit mode.

hope this helps,
Luk

1 Like

Hi @luk-f-a ,

thanks, this helped immediately!

Very good!

Yes, the trick is to use Numpy types, they work in jitted and “regular” code.