gpt4 book ai didi

python - 使用函数对象作为 numba njit 函数的参数

转载 作者:行者123 更新时间:2023-12-01 15:56:25 25 4
gpt4 key购买 nike

我想做一个通用函数,它以函数对象作为参数。

最简单的情况之一:

import numpy as np
import numba as nb
@nb.njit()
def test(a, f=np.median):
return f(a)

test(np.arange(10), np.mean)

给出错误,虽然 test(np.arange(10))按预期工作。

错误:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type pyobject
[1] During: typing of argument at <ipython-input-54-52cead0f097d> (5)

File "<ipython-input-54-52cead0f097d>", line 5:
def test(a, f=np.median):
return f(a)
^

This error may have been caused by the following argument(s):
- argument 1: cannot determine Numba type of <class 'function'>

This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.

这是不允许的还是我错过了什么?

最佳答案

使用函数作为参数对于 numba 来说很棘手,而且非常昂贵。这在 Frequently Asked Questions: "1.18.1.1. Can I pass a function as an argument to a jitted function?" 中提到:

1.18.1.1. Can I pass a function as an argument to a jitted function?

As of Numba 0.39, you can, so long as the function argument has also been JIT-compiled:

@jit(nopython=True)
def f(g, x):
return g(x) + g(-x)
result = f(jitted_g_function, 1)

However, dispatching with arguments that are functions has extra overhead. If this matters for your application, you can also use a factory function to capture the function argument in a closure:

def make_f(g):
# Note: a new f() is created each time make_f() is called!
@jit(nopython=True)
def f(x):
return g(x) + g(-x)
return f
f = make_f(jitted_g_function)
result = f(1)

Improving the dispatch performance of functions in Numba is an ongoing task.



这意味着您可以选择使用函数工厂:
import numpy as np
import numba as nb

def test(a, func=np.median):
@nb.njit
def _test(a):
return func(a)
return _test(a)

>>> test(np.arange(10))
4.5
>>> test(np.arange(10), np.min)
0
>>> test(np.arange(10), np.mean)
4.5

或者在将函数参数作为参数传递之前将其包装为 jitted-function:
import numpy as np
import numba as nb

@nb.njit()
def test(a, f=np.median):
return f(a)

@nb.njit
def wrapped_mean(a):
return np.mean(a)

@nb.njit
def wrapped_median(a):
return np.median(a)

>>> test(np.arange(10))
4.5
>>> test(np.arange(10), wrapped_mean)
4.5
>>> test(np.arange(10), wrapped_median)
4.5

这两个选项都有相当多的样板文件,并不像人们希望的那样直接。

函数工厂方法还重复创建和编译函数,因此如果您经常使用与参数相同的函数调用它,您可以使用字典来存储已知的编译函数:
import numpy as np
import numba as nb

_precompiled_funcs = {}

def test(a, func=np.median):
if func not in _precompiled_funcs:
@nb.njit
def _test(arr):
return func(arr)
result = _test(a)
_precompiled_funcs[func] = _test
return result
return _precompiled_funcs[func](a)

另一种方法(使用wrapped 和jitted 函数)也有一些开销,但是只要您传入的数组具有大量元素(> 1000),它就不会很明显。

如果您展示的功能确实是您想要使用的功能,我根本不会在其上使用 numba。使用 Python + NumPy 执行这些不锻炼 numba 强度的简单任务(索引和迭代数组或繁重的数字运算)应该更快(或同样快)并且更容易调试和理解:
import numba as nb

def test(a, f=np.median):
return f(a)

关于python - 使用函数对象作为 numba njit 函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com