【问题标题】:Numba AOT compile functions with functional argumentsNumba AOT 使用函数参数编译函数
【发布时间】:2022-01-09 17:10:50
【问题描述】:

我正在尝试在 Numba 中 AOT 编译一个具有函数参数的函数,但我找不到正确指定其签名的方法。使用一个非常基本的示例,使用标准的 numba @njit 装饰器,我会写:

import numba as nb

@nb.njit(nb.f8(nb.f8, nb.f8))
def fcn_sum(a, b): 
    return a + b

@nb.njit(nb.f8(nb.typeof(fcn_sum), nb.f8, nb.f8))
def test(fun, a, b): 
    return fun(a, b)

其中nb.typeof(fcn_sum) 返回一个仅对fcn_sum 函数有效的调度程序对象。不幸的是,相同的 AOT 编译策略会产生 NameError 错误,因为 nbtypeof 都无法识别:

@cc.export('test', 'f8(nb.typeof(fcn_sum), f8, f8)')
def test(fun, a, b):
    return fun(a, b)

如何指定函数参数的签名以使此示例有效?

【问题讨论】:

    标签: python numba aot


    【解决方案1】:

    使用与@njit 情况相同的签名不会出错:

    @cc.export('test', nb.f8(nb.typeof(fcn_sum), nb.f8, nb.f8))
    def test(fun, a, b):
        return fun(a, b)
    

    【讨论】:

    • 认为函数签名只能作为字符串传递。解决了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多