Numba 函数没有参数类型的匹配定义 ListType[array(float64, 2d, C)] 错误
Numba function No matching definition for argument type(s) ListType[array(float64, 2d, C)] error
我在Numba
中有一个函数使用了List(float64[:,::1])
类型,这是一个虚拟函数来尝试类型,我会在for循环下做很多操作。它有一个奇怪的行为,而 to arr 列表具有相同的 excatly numba.typeof() 签名,一个有效,另一个无效,告诉我它不匹配类型。
这绝对是一种对象错误,但我可以解决。
这是要加载的文件test.npy:
https://drive.google.com/file/d/1guAe1C2sKZyy2U2_qXAhMA1v46PfeKnN/view?usp=sharing
错误
raise TypeError(msg)
TypeError: No matching definition for argument type(s) ListType[array(float64, 2d, C)]
代码
import numpy as np
import numba
from numba.typed import List
branches = np.load('test.npy', allow_pickle=True).item()
@numba.njit('List(int64)(List(float64[:, ::1]))')
def not_test(branches):
a_list = []
for branch in branches:
for i in range(len(branch)):
a_list.append(i)
return a_list
# this does not work
arr = []
for branch in branches.features[:2]:
arr.append(np.asarray(branch.geometry.coordinates).copy())
arr = List(arr.copy())
print(numba.typeof(arr))
no_test(arr)
# this works
arr = List([np.array([np.array([7.0,7.3]), np.array([7.4,8.6])])])
print(numba.typeof(arr))
no_test(arr)
对于任何陷入这种琐碎事情的人来说,正确的签名类型是:
@numba.njit('List(int64)(ListType(float64[:, ::1]))')
我不明白List
和ListType
的区别,在Numba
官方网站上也找不到。如果我有一个装饰器类型的备忘单,我真的很有帮助,因为仅仅从函数参数中找到的可用数据类型来推断它们应该如何编写是不容易的。此外,拥有一个基于 numba.typeof() return 的解析器函数确实很有帮助,并且能够仅以此创建装饰器的字符串。
此外,到 List() 的转换非常慢,我在 Numba GitHub 上找到了一个 post 来讨论这个问题。这是原文postimprove performance of numba.typed.List constructor with Python list as arg
def convert2(x, dtype=np.float64):
try:
# Try and convert x to a Numpy array. If this succeeds
# then we have reached the end of the nesting-depth.
y = np.asarray(x, dtype=dtype)
except:
# If the conversion to a Numpy array fails, then it can
# be because not all elements of x can be converted to
# the given dtype. There is currently no way to distinguish
# if this is because x is a nested list, or just a list
# of simple elements with incompatible types.
# Recursively call this function on all elements of x.
y = [convert2(x_, dtype=dtype) for x_ in x]
# Convert Python list to Numba list.
y = numba.typed.List(y)
return y
编辑
我找到了一个非常有用的行来获取 numba 函数的类型签名
print(not_test.inspect_types())
#not_test (ListType[array(float64, 2d, C)], array(float64, 1d, A))
我在Numba
中有一个函数使用了List(float64[:,::1])
类型,这是一个虚拟函数来尝试类型,我会在for循环下做很多操作。它有一个奇怪的行为,而 to arr 列表具有相同的 excatly numba.typeof() 签名,一个有效,另一个无效,告诉我它不匹配类型。
这绝对是一种对象错误,但我可以解决。
这是要加载的文件test.npy:
https://drive.google.com/file/d/1guAe1C2sKZyy2U2_qXAhMA1v46PfeKnN/view?usp=sharing
错误
raise TypeError(msg)
TypeError: No matching definition for argument type(s) ListType[array(float64, 2d, C)]
代码
import numpy as np
import numba
from numba.typed import List
branches = np.load('test.npy', allow_pickle=True).item()
@numba.njit('List(int64)(List(float64[:, ::1]))')
def not_test(branches):
a_list = []
for branch in branches:
for i in range(len(branch)):
a_list.append(i)
return a_list
# this does not work
arr = []
for branch in branches.features[:2]:
arr.append(np.asarray(branch.geometry.coordinates).copy())
arr = List(arr.copy())
print(numba.typeof(arr))
no_test(arr)
# this works
arr = List([np.array([np.array([7.0,7.3]), np.array([7.4,8.6])])])
print(numba.typeof(arr))
no_test(arr)
对于任何陷入这种琐碎事情的人来说,正确的签名类型是:
@numba.njit('List(int64)(ListType(float64[:, ::1]))')
我不明白List
和ListType
的区别,在Numba
官方网站上也找不到。如果我有一个装饰器类型的备忘单,我真的很有帮助,因为仅仅从函数参数中找到的可用数据类型来推断它们应该如何编写是不容易的。此外,拥有一个基于 numba.typeof() return 的解析器函数确实很有帮助,并且能够仅以此创建装饰器的字符串。
此外,到 List() 的转换非常慢,我在 Numba GitHub 上找到了一个 post 来讨论这个问题。这是原文postimprove performance of numba.typed.List constructor with Python list as arg
def convert2(x, dtype=np.float64):
try:
# Try and convert x to a Numpy array. If this succeeds
# then we have reached the end of the nesting-depth.
y = np.asarray(x, dtype=dtype)
except:
# If the conversion to a Numpy array fails, then it can
# be because not all elements of x can be converted to
# the given dtype. There is currently no way to distinguish
# if this is because x is a nested list, or just a list
# of simple elements with incompatible types.
# Recursively call this function on all elements of x.
y = [convert2(x_, dtype=dtype) for x_ in x]
# Convert Python list to Numba list.
y = numba.typed.List(y)
return y
编辑
我找到了一个非常有用的行来获取 numba 函数的类型签名
print(not_test.inspect_types())
#not_test (ListType[array(float64, 2d, C)], array(float64, 1d, A))