Hey Numba group,
I’ve encountered an issue related to function overloading using FunctionType
with Numba version 0.58. There seems to be a problem if a jitted function has signatures which are identical except from the signature section of FunctionType
itself.
I’ve provided a simplified example to illustrate the problem.
When I call pfunc(func_1, arr)
or pfunc(func_2, arr)
, I encounter the TypeError: Ambiguous overloading
error.
import numpy as np
import numba as nb
@nb.njit('f8(f8[:])')
def func_1(arr):
return np.sum(arr)
@nb.njit('f8[:](f8[:])')
def func_2(arr):
return np.cumsum(arr)
@nb.njit(['f8[:](FunctionType(f8(f8[:])), f8[:])',
'f8[:](FunctionType(f8[:](f8[:])), f8[:])'])
def pfunc(fn, arr):
res = np.empty_like(arr)
res[:] = fn(arr)
return res
arr = np.arange(5.)
print(pfunc(func_1, arr))
print(pfunc(func_2, arr))
# TypeError: Ambiguous overloading for <function...
However, in my second version of the code, I separated the two signatures into two different functions, pfunc_1
and pfunc_2
, and after calling these functions once the issue disappears:
import numpy as np
import numba as nb
@nb.njit('f8(f8[:])')
def func_1(arr):
return np.sum(arr)
@nb.njit('f8[:](f8[:])')
def func_2(arr):
return np.cumsum(arr)
@nb.njit(['f8[:](FunctionType(f8(f8[:])), f8[:])'])
def pfunc_1(fn, arr):
res = np.empty_like(arr)
res[:] = fn(arr)
return res
@nb.njit(['f8[:](FunctionType(f8[:](f8[:])), f8[:])'])
def pfunc_2(fn, arr):
res = np.empty_like(arr)
res[:] = fn(arr)
return res
@nb.njit(['f8[:](FunctionType(f8(f8[:])), f8[:])',
'f8[:](FunctionType(f8[:](f8[:])), f8[:])'])
def pfunc(fn, arr):
res = np.empty_like(arr)
res[:] = fn(arr)
return res
arr = np.arange(5.)
print(pfunc_1(func_1, arr))
print(pfunc_2(func_2, arr))
print(pfunc(func_1, arr))
print(pfunc(func_2, arr))
# [10. 10. 10. 10. 10.]
# [ 0. 1. 3. 6. 10.]
# [10. 10. 10. 10. 10.]
# [ 0. 1. 3. 6. 10.]
My question is, why does Numba have difficulties resolving the function overloading in the first version, which contains a single pfunc
function, and why does it work without errors in the second version after pfunc_1
and pfunc_2
has been called?
An obvious workaround is to compile the base function into two or more functions and avoid having ambiguous signatures at all. Nevertheless, is there a way to use FunctionType
in a single function without getting the “Ambiguous overloading” error?