boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

如何在 Numba jitclass spec 中声明 Enum 和自定义类?


avatar
作者 2025年9月18日 8

如何在 Numba jitclass spec 中声明 Enum 和自定义类?

Numba 旨在通过即时 (JIT) 编译将 python 代码转换为机器码,从而提高性能。@jitclass 装饰器允许用户定义可以被 Numba 编译的类,但正确声明类的属性类型至关重要。特别是在使用枚举 (enum) 类型时,需要采用特定的方法才能使其与 Numba 兼容。

使用 enum.intEnum 声明 Enum 类型

Numba 无法直接处理标准的 Python enum.Enum 类型。为了在 @jitclass 的 spec 中使用枚举,需要使用 enum.IntEnum。IntEnum 是 Enum 的一个子类,它继承了 int 类型,因此可以转换为 int64,这使得它与 Numba 兼容。

示例代码:

from enum import IntEnum from numba import int64, String from numba.experimental import jitclass  class Color(IntEnum):     red = 1     BLUE = 2     GREEN = 3  spec = [('name', string), ('color', int64)]  @jitclass(spec) class Paint:     def __init__(self, name, color):         self.name = name         self.color = color  # 示例用法 paint = Paint("MyPaint", Color.RED) print(paint.name) print(paint.color)

代码解释:

如何在 Numba jitclass spec 中声明 Enum 和自定义类?

FlowSuno

我们正在打造一个人人都能创造美妙音乐的未来。无需乐器,只需想象。从你的思想到音乐。

如何在 Numba jitclass spec 中声明 Enum 和自定义类?106

查看详情 如何在 Numba jitclass spec 中声明 Enum 和自定义类?

  1. from enum import IntEnum: 导入 IntEnum 类。
  2. class Color(IntEnum):: 定义一个名为 Color 的枚举类,并继承自 IntEnum。
  3. RED = 1, BLUE = 2, GREEN = 3: 定义枚举的成员及其对应的值。这些值必须是整数。
  4. spec = [(‘name’, string), (‘color’, int64)]: 在 spec 中,将 color 属性声明为 int64 类型。
  5. @jitclass(spec): 使用 @jitclass 装饰器,并将 spec 传递给它。

注意事项

  • 必须使用 IntEnum: 确保你的枚举类继承自 enum.IntEnum,而不是 enum.Enum。
  • spec 中声明为 int64: 在 @jitclass 的 spec 中,将枚举类型的属性声明为 int64。
  • 枚举值必须是整数: IntEnum 的成员的值必须是整数。

总结

通过继承 enum.IntEnum 并在 @jitclass 的 spec 中将枚举类型声明为 int64,可以有效地在 Numba 中使用枚举类型。这种方法允许 Numba 正确编译包含枚举类型的类,从而提高代码的性能。

虽然目前 Numba 还不支持直接声明自定义类在jitclass的spec中,但对于枚举类型,enum.IntEnum 提供了一个简单有效的解决方案。



评论(已关闭)

评论已关闭