注解感知的numba njit。
项目描述
numbakit-anjit: 利用numba加速常微分方程的积分
numbakit-anjit (nbkanjit) 通过提供 anjit,一个注解感知的numba jit装饰器和管理对象来处理Jit配置,以辅助Numba密集型项目。
它运行在Python 3.7+,依赖于Numba。它遵循BSD许可协议。
使用它非常简单自然
>>> from numba import types as nt
>>> import nbkanjit
>>> @nbkanjit.anjit
... def func(x: nt.float64, y: nt.float64) -> nt.float64:
... return x + y
您还可以使用映射到numba类型的Python类型
>>> @nbkanjit.anjit
... def func(x: float, y: float) -> float:
... return x + y
。
您可以使用
>>> from nbkanjit import Function as F_
>>> @nbkanjit.anjit
... def func1(x: int, y: float) -> float:
... return x + y
>>> def func2(x: int, y: F_(funct1)._return) -> float:
... return x + y
您还可以使用任何参数的注解。例如,在此情况下,F_(func).x 等同于 int。甚至是一个完整的函数 F_(func),它将返回 FunctionType(float64(int, float64))
它还提供了一个管理器来封装(并重复使用不同的参数)
>>> import nbkanjit
>>> jm = nbkanjit.JitManager(cache=True)
>>> @jm.anjit
... def func(x: float, y:float) -> nt.float64:
... return x + y
甚至应用于标准的numba njit。
>>> jm = nbkanjit.JitManager(cache=True)
>>> @jm.njit
... def func(x, y):
... return x + y
您可以通过将任何Python对象映射到numba类型来教会管理器新技巧
>>> jm.mapping["array1d"] = nt.float64[:]
。
并且有一种方法将签名注册为模板(tmpl)
>>> import nbkanjit
>>> jm = nbkanjit.JitManager()
>>> jm.register("nice", nt.float64((nt.float64, nt.float64)))
然后通过显式命名或使用函数名来使用它
>>> @jm.njit_tmpl("nice")
... def other_func(x, y):
... return x + y
或使用函数名
>>> @jm.njit_tmpl
... def nice(x, y):
... return x + y
您可以直接从函数注册
>>> @jm.register("nice")
... def _(x: float, y:float) -> nt.float64:
... pass
或再次使用函数名
>>> @jm.register
... def nice(x: float, y:float) -> nt.float64:
... pass
快速安装
要安装numbakit-anjit,只需(即将推出)
$ pip install numbakit-anjit
或者使用conda,通过conda-forge通道(即将推出)
$ conda install -c conda-forge numbakit-anjit
然后简单地享受它!
为什么
Numba njit 真棒。简单易用,一旦函数被调用,就会产生适当的机器代码。正如 Numba 文档 所说
in [Lazy mode], compilation will be deferred until the first function execution. Numba will infer the argument types at call time, and generate optimized code based on this information. Numba will also be able to compile separate specializations depending on the input types.
但是 Numba 还有一个 贪婪模式
In which you can also tell Numba the function signature you are expecting. [..] In this case, the corresponding specialization will be compiled by the decorator, and no other specialization will be allowed. This is useful if you want fine-grained control over types chosen by the compiler (for example, to use single-precision floats).
这可以产生稍微快一点的代码,因为编译器不需要推断类型。它还在定义时提供了类型检查,确保正确性。在 Numba 高强度项目中,这可能是一个有用的特性。最后,贪婪编译当前需要有两个具有相同签名的函数作为第三个函数的参数,而不需要在每个情况下重新编译最后一个函数。
我们对 njit 的另一个喜欢是,它可以通过关键字参数进行高度配置,甚至可以使用环境变量应用一些全局配置。
在开发 numbakit-ode 时,我缺少两样东西
贪婪编译使用函数注解
以集中但细粒度的方式操纵 njit 选项的方法
因此,numbakit-anjit 诞生了。
numbakit-anjit 由社区维护。有关完整的作者名单,请参阅 AUTHORS。
要查看每个版本项目的显著变更的有序列表,请参阅 CHANGES。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。