英文:
Numba cannot resolve function (np.digitize)
问题
我遇到了一个numba错误,它在解析一个函数时出现了问题。
要复现我的错误的最小代码如下:
import numba
import numpy as np
@numba.jit("float64(float64)", nopython=True)
def get_cut_tight_nom(eta):
binning_abseta = np.array([0., 10., 20.])
return np.digitize(eta, binning_abseta)
我不理解这个错误消息:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function digitize at 0x7fdb8c11dee0>) found for signature:
>>> digitize(float64, array(float64, 1d, C))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload of function 'digitize': File: numba/np/arraymath.py: Line 3939.
With argument(s): '(float64, array(float64, 1d, C))':
No match.
During: resolving callee type: Function(<function digitize at 0x7fdb8c11dee0>)
During: typing of call at /tmp/ipykernel_309793/3917220133.py (8)
看起来它想要解析digitize(float64, array(float64, 1d, C))
,但具有相同签名的函数不匹配?
英文:
I get an error by numba, it is complaining it can't resolve a function.
The minimal code to reproduce my errors is
import numba
import numpy as np
@numba.jit("float64(float64)", nopython=True)
def get_cut_tight_nom(eta):
binning_abseta = np.array([0., 10., 20.])
return np.digitize(eta, binning_abseta)
I don't understand the error message
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function digitize at 0x7fdb8c11dee0>) found for signature:
>>> digitize(float64, array(float64, 1d, C))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload of function 'digitize': File: numba/np/arraymath.py: Line 3939.
With argument(s): '(float64, array(float64, 1d, C))':
No match.
During: resolving callee type: Function(<function digitize at 0x7fdb8c11dee0>)
During: typing of call at /tmp/ipykernel_309793/3917220133.py (8)
it seems it wants to resolve digitize(float64, array(float64, 1d, C))
and a function with the same signature is not matching?
答案1
得分: 2
这确实是由于函数签名导致的。Numpy函数np.digitize
(不使用Numba)返回的是int64(标量或数组),而你已经指定了你的函数的返回类型为float64。
似乎Numba的实现需要始终使用数组,你还需要明确将它们添加到函数签名中。所以,例如,对我来说,这个方法有效:
@numba.jit("int64[:](float64[:])", nopython=True)
def get_cut_tight_nom(eta):
binning_abseta = np.array([0., 10., 20.])
return np.digitize(eta, binning_abseta)
结果是:
但在这种情况下,你真的需要函数签名吗?Numba也可以自行推断,比如:
@numba.njit
def get_cut_tight_nom(eta):
...
如果你需要将float32输入明确转换为float64等,函数签名仍然可以添加一些信息。
你还可以检查Numba使用不同类型的输入时生成的签名。使用float32和float64两次运行它可以帮助你找出可能出现这种问题的地方。
英文:
It's indeed due to the signatures. The Numpy function (without Numba) of np.digitize
returns an int64 (scalar or array), not a float64 as you've specified the return type of your function to be.
It seems like the Numba implementation of it requires both to always be arrays, which you'll also have to explicitly add to the signature.
So this for example works for me:
@numba.jit("int64[:](float64[:])", nopython=True)
def get_cut_tight_nom(eta):
binning_abseta = np.array([0., 10., 20.])
return np.digitize(eta, binning_abseta)
But do you really need the signature in this case? Numba is able to figure it out itself as well, like:
@numba.njit
def get_cut_tight_nom(eta):
...
A signature can still add something if you for example want to explicitly cast float32 inputs to float64 etc.
You can also inspect what signatures Numba comes up with if you run it with some differently typed input. Running it twice with float32 & float64 as the input shows. It can help to highlight where issues like this might arise from.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论