[Python] Add `__index__` support to HAL enums (#25163) HAL enums implement `__int__`, but `nanobind 2.15` rejects them for integer arguments and bitwise operations. Flag enums are now marked with is_arithmetic() + is_flag(), to avoid implementing any special members. --------- Signed-off-by: anutosh491 <andersonbhat491@gmail.com>
diff --git a/runtime/bindings/python/hal.cc b/runtime/bindings/python/hal.cc index aa1cd1e..ceb6712 100644 --- a/runtime/bindings/python/hal.cc +++ b/runtime/bindings/python/hal.cc
@@ -1386,7 +1386,8 @@ py::arg("debug_sink") = py::none()); // Enums. - py::enum_<enum iree_hal_memory_type_bits_t>(m, "MemoryType") + py::enum_<enum iree_hal_memory_type_bits_t>( + m, "MemoryType", py::is_arithmetic(), py::is_flag()) .value("NONE", IREE_HAL_MEMORY_TYPE_NONE) .value("OPTIMAL", IREE_HAL_MEMORY_TYPE_OPTIMAL) .value("HOST_VISIBLE", IREE_HAL_MEMORY_TYPE_HOST_VISIBLE) @@ -1395,30 +1396,20 @@ .value("HOST_LOCAL", IREE_HAL_MEMORY_TYPE_HOST_LOCAL) .value("DEVICE_VISIBLE", IREE_HAL_MEMORY_TYPE_DEVICE_VISIBLE) .value("DEVICE_LOCAL", IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL) - .export_values() - .def("__or__", [](uint64_t self, uint64_t other) { return self | other; }) - .def("__and__", - [](uint64_t self, uint64_t other) { return self & other; }) - .def("__int__", [](enum iree_hal_memory_type_bits_t self) { - return (uint64_t)self; - }); + .export_values(); - py::enum_<enum iree_hal_buffer_compatibility_bits_t>(m, "BufferCompatibility") + py::enum_<enum iree_hal_buffer_compatibility_bits_t>( + m, "BufferCompatibility", py::is_arithmetic(), py::is_flag()) .value("NONE", IREE_HAL_BUFFER_COMPATIBILITY_NONE) .value("ALLOCATABLE", IREE_HAL_BUFFER_COMPATIBILITY_ALLOCATABLE) .value("IMPORTABLE", IREE_HAL_BUFFER_COMPATIBILITY_IMPORTABLE) .value("EXPORTABLE", IREE_HAL_BUFFER_COMPATIBILITY_EXPORTABLE) .value("QUEUE_TRANSFER", IREE_HAL_BUFFER_COMPATIBILITY_QUEUE_TRANSFER) .value("QUEUE_DISPATCH", IREE_HAL_BUFFER_COMPATIBILITY_QUEUE_DISPATCH) - .export_values() - .def("__or__", [](uint64_t self, uint64_t other) { return self | other; }) - .def("__and__", - [](uint64_t self, uint64_t other) { return self & other; }) - .def("__int__", [](enum iree_hal_buffer_compatibility_bits_t self) { - return (uint64_t)self; - }); + .export_values(); - py::enum_<enum iree_hal_buffer_usage_bits_t>(m, "BufferUsage") + py::enum_<enum iree_hal_buffer_usage_bits_t>( + m, "BufferUsage", py::is_arithmetic(), py::is_flag()) .value("NONE", IREE_HAL_BUFFER_USAGE_NONE) .value("TRANSFER_SOURCE", IREE_HAL_BUFFER_USAGE_TRANSFER_SOURCE) .value("TRANSFER_TARGET", IREE_HAL_BUFFER_USAGE_TRANSFER_TARGET) @@ -1448,34 +1439,22 @@ IREE_HAL_BUFFER_USAGE_MAPPING_ACCESS_SEQUENTIAL_WRITE) .value("MAPPING", IREE_HAL_BUFFER_USAGE_MAPPING) .value("DEFAULT", IREE_HAL_BUFFER_USAGE_DEFAULT) - .export_values() - .def("__or__", [](enum iree_hal_buffer_usage_bits_t self, - uint64_t other) { return self | other; }) - .def("__and__", [](enum iree_hal_buffer_usage_bits_t self, - uint64_t other) { return self & other; }) - .def("__int__", [](enum iree_hal_buffer_usage_bits_t self) { - return (uint64_t)self; - }); + .export_values(); - py::enum_<enum iree_hal_memory_access_bits_t>(m, "MemoryAccess") + py::enum_<enum iree_hal_memory_access_bits_t>( + m, "MemoryAccess", py::is_arithmetic(), py::is_flag()) .value("NONE", IREE_HAL_MEMORY_ACCESS_NONE) .value("READ", IREE_HAL_MEMORY_ACCESS_READ) .value("WRITE", IREE_HAL_MEMORY_ACCESS_WRITE) .value("DISCARD", IREE_HAL_MEMORY_ACCESS_DISCARD) .value("DISCARD_WRITE", IREE_HAL_MEMORY_ACCESS_DISCARD_WRITE) .value("ALL", IREE_HAL_MEMORY_ACCESS_ALL) - .export_values() - .def("__or__", [](uint64_t self, uint64_t other) { return self | other; }) - .def("__and__", - [](uint64_t self, uint64_t other) { return self & other; }) - .def("__int__", [](enum iree_hal_memory_access_bits_t self) { - return (uint64_t)self; - }); + .export_values(); // Use compatibility type to enable def_static. // See: https://github.com/wjakob/nanobind/issues/597 auto hal_element_type = nanobind1_compat_enum_<enum iree_hal_element_types_t>( - m, "HalElementType"); + m, "HalElementType", py::is_arithmetic()); hal_element_type .def_static("map_to_dtype", [](iree_hal_element_type_t element_type) { @@ -1520,9 +1499,7 @@ .value("FLOAT_8_E5M2", IREE_HAL_ELEMENT_TYPE_FLOAT_8_E5M2) .value("FLOAT_8_E5M2_FNUZ", IREE_HAL_ELEMENT_TYPE_FLOAT_8_E5M2_FNUZ) .value("FLOAT_8_E8M0_FNU", IREE_HAL_ELEMENT_TYPE_FLOAT_8_E8M0_FNU) - .export_values() - .def("__int__", - [](enum iree_hal_element_types_t self) { return (uint64_t)self; }); + .export_values(); py::enum_<iree_hal_external_timepoint_type_t>( m, "ExternalTimepointType", py::is_arithmetic(), py::is_flag()) @@ -1531,10 +1508,7 @@ IREE_HAL_EXTERNAL_TIMEPOINT_TYPE_ASYNC_PRIMITIVE) .value("CUDA_EVENT", IREE_HAL_EXTERNAL_TIMEPOINT_TYPE_CUDA_EVENT) .value("HIP_EVENT", IREE_HAL_EXTERNAL_TIMEPOINT_TYPE_HIP_EVENT) - .export_values() - .def("__int__", [](enum iree_hal_memory_type_bits_t self) { - return (uint64_t)self; - }); + .export_values(); py::enum_<enum iree_hal_semaphore_compatibility_bits_t>( m, "SemaphoreCompatibility", py::is_arithmetic(), py::is_flag()) @@ -1546,24 +1520,12 @@ .value("HOST_ONLY", IREE_HAL_SEMAPHORE_COMPATIBILITY_HOST_ONLY) .value("DEVICE_ONLY", IREE_HAL_SEMAPHORE_COMPATIBILITY_DEVICE_ONLY) .value("ALL", IREE_HAL_SEMAPHORE_COMPATIBILITY_ALL) - .export_values() - .def("__or__", [](uint64_t self, uint64_t other) { return self | other; }) - .def("__and__", - [](uint64_t self, uint64_t other) { return self & other; }) - .def("__int__", [](enum iree_hal_memory_type_bits_t self) { - return (uint64_t)self; - }); + .export_values(); py::enum_<iree_hal_external_timepoint_flag_bits_t>( m, "ExternalTimepointFlags", py::is_arithmetic(), py::is_flag()) .value("NONE", IREE_HAL_EXTERNAL_TIMEPOINT_FLAG_NONE) - .export_values() - .def("__or__", [](uint64_t self, uint64_t other) { return self | other; }) - .def("__and__", - [](uint64_t self, uint64_t other) { return self & other; }) - .def("__int__", [](enum iree_hal_memory_type_bits_t self) { - return (uint64_t)self; - }); + .export_values(); py::class_<HalDevice>(m, "HalDevice") .def_prop_ro(
diff --git a/runtime/bindings/python/iree/runtime/_binding.pyi b/runtime/bindings/python/iree/runtime/_binding.pyi index 2ceaec0..504dc01 100644 --- a/runtime/bindings/python/iree/runtime/_binding.pyi +++ b/runtime/bindings/python/iree/runtime/_binding.pyi
@@ -1,3 +1,4 @@ +import enum import os import numpy as np from typing import ( @@ -31,70 +32,57 @@ def get_cached_hal_driver(device_uri: str) -> HalDriver: ... def parse_flags(*flag: str): ... -class BufferCompatibility(int): - ALLOCATABLE: ClassVar[BufferCompatibility] = ... - EXPORTABLE: ClassVar[BufferCompatibility] = ... - IMPORTABLE: ClassVar[BufferCompatibility] = ... - NONE: ClassVar[BufferCompatibility] = ... - QUEUE_DISPATCH: ClassVar[BufferCompatibility] = ... - QUEUE_TRANSFER: ClassVar[BufferCompatibility] = ... - __name__: Any - def __and__(self, other: BufferCompatibility) -> int: ... - def __or__(self, other: BufferCompatibility) -> int: ... +class BufferCompatibility(enum.IntFlag): + NONE = ... + ALLOCATABLE = ... + IMPORTABLE = ... + EXPORTABLE = ... + QUEUE_TRANSFER = ... + QUEUE_DISPATCH = ... -class BufferUsage(int): - DEFAULT: ClassVar[BufferUsage] = ... - DISPATCH_IMAGE: ClassVar[BufferUsage] = ... - DISPATCH_IMAGE_READ: ClassVar[BufferUsage] = ... - DISPATCH_IMAGE_WRITE: ClassVar[BufferUsage] = ... - DISPATCH_INDIRECT_PARAMETERS: ClassVar[BufferUsage] = ... - DISPATCH_STORAGE: ClassVar[BufferUsage] = ... - DISPATCH_STORAGE_READ: ClassVar[BufferUsage] = ... - DISPATCH_STORAGE_WRITE: ClassVar[BufferUsage] = ... - DISPATCH_UNIFORM_READ: ClassVar[BufferUsage] = ... - MAPPING: ClassVar[BufferUsage] = ... - MAPPING_ACCESS_RANDOM: ClassVar[BufferUsage] = ... - MAPPING_ACCESS_SEQUENTIAL_WRITE: ClassVar[BufferUsage] = ... - MAPPING_OPTIONAL: ClassVar[BufferUsage] = ... - MAPPING_PERSISTENT: ClassVar[BufferUsage] = ... - MAPPING_SCOPED: ClassVar[BufferUsage] = ... - NONE: ClassVar[BufferUsage] = ... - SHARING_CONCURRENT: ClassVar[BufferUsage] = ... - SHARING_EXPORT: ClassVar[BufferUsage] = ... - SHARING_IMMUTABLE: ClassVar[BufferUsage] = ... - SHARING_REPLICATE: ClassVar[BufferUsage] = ... - TRANSFER: ClassVar[BufferUsage] = ... - TRANSFER_SOURCE: ClassVar[BufferUsage] = ... - TRANSFER_TARGET: ClassVar[BufferUsage] = ... - __name__: str - def __and__(self, other: BufferUsage) -> int: ... - def __or__(self, other: BufferUsage) -> int: ... +class BufferUsage(enum.IntFlag): + NONE = ... + TRANSFER_SOURCE = ... + TRANSFER_TARGET = ... + TRANSFER = ... + DISPATCH_INDIRECT_PARAMETERS = ... + DISPATCH_UNIFORM_READ = ... + DISPATCH_STORAGE_READ = ... + DISPATCH_STORAGE_WRITE = ... + DISPATCH_STORAGE = ... + DISPATCH_IMAGE_READ = ... + DISPATCH_IMAGE_WRITE = ... + DISPATCH_IMAGE = ... + SHARING_EXPORT = ... + SHARING_REPLICATE = ... + SHARING_CONCURRENT = ... + SHARING_IMMUTABLE = ... + MAPPING_SCOPED = ... + MAPPING_PERSISTENT = ... + MAPPING_OPTIONAL = ... + MAPPING_ACCESS_RANDOM = ... + MAPPING_ACCESS_SEQUENTIAL_WRITE = ... + MAPPING = ... + DEFAULT = ... -class ExternalTimepointType(int): - NONE: ClassVar[ExternalTimepointType] = ... - ASYNC_PRIMITIVE: ClassVar[ExternalTimepointType] = ... - CUDA_EVENT: ClassVar[ExternalTimepointType] = ... - HIP_EVENT: ClassVar[ExternalTimepointType] = ... - def __int__(self) -> int: ... +class ExternalTimepointType(enum.IntFlag): + NONE = ... + ASYNC_PRIMITIVE = ... + CUDA_EVENT = ... + HIP_EVENT = ... -class SemaphoreCompatibility(int): - NONE: ClassVar[SemaphoreCompatibility] = ... - HOST_WAIT: ClassVar[SemaphoreCompatibility] = ... - DEVICE_WAIT: ClassVar[SemaphoreCompatibility] = ... - HOST_SIGNAL: ClassVar[SemaphoreCompatibility] = ... - DEVICE_SIGNAL: ClassVar[SemaphoreCompatibility] = ... - HOST_ONLY: ClassVar[SemaphoreCompatibility] = ... - DEVICE_ONLY: ClassVar[SemaphoreCompatibility] = ... - ALL: ClassVar[SemaphoreCompatibility] = ... - def __and__(self, other: SemaphoreCompatibility) -> int: ... - def __or__(self, other: SemaphoreCompatibility) -> int: ... - def __int__(self) -> int: ... +class SemaphoreCompatibility(enum.IntFlag): + NONE = ... + HOST_WAIT = ... + DEVICE_WAIT = ... + HOST_SIGNAL = ... + DEVICE_SIGNAL = ... + HOST_ONLY = ... + DEVICE_ONLY = ... + ALL = ... -class ExternalTimepointFlags(int): - NONE: ClassVar[ExternalTimepointFlags] = ... - def __and__(self, other: ExternalTimepointFlags) -> int: ... - def __or__(self, other: ExternalTimepointFlags) -> int: ... - def __int__(self) -> int: ... +class ExternalTimepointFlags(enum.IntFlag): + NONE = ... class _InvokeStatics: ... @@ -334,46 +322,46 @@ ) -> HalDevice: ... def query_available_devices(self) -> list[dict]: ... -class HalElementType: - BFLOAT_16: ClassVar[HalElementType] = ... - BOOL_8: ClassVar[HalElementType] = ... - COMPLEX_128: ClassVar[HalElementType] = ... - COMPLEX_64: ClassVar[HalElementType] = ... - FLOAT_8_E4M3_FN: ClassVar[HalElementType] = ... - FLOAT_8_E4M3_FNUZ: ClassVar[HalElementType] = ... - FLOAT_8_E5M2: ClassVar[HalElementType] = ... - FLOAT_8_E5M2_FNUZ: ClassVar[HalElementType] = ... - FLOAT_8_E8M0_FNU: ClassVar[HalElementType] = ... - FLOAT_16: ClassVar[HalElementType] = ... - FLOAT_32: ClassVar[HalElementType] = ... - FLOAT_64: ClassVar[HalElementType] = ... - INT_16: ClassVar[HalElementType] = ... - INT_32: ClassVar[HalElementType] = ... - INT_4: ClassVar[HalElementType] = ... - INT_64: ClassVar[HalElementType] = ... - INT_8: ClassVar[HalElementType] = ... - NONE: ClassVar[HalElementType] = ... - OPAQUE_16: ClassVar[HalElementType] = ... - OPAQUE_32: ClassVar[HalElementType] = ... - OPAQUE_64: ClassVar[HalElementType] = ... - OPAQUE_8: ClassVar[HalElementType] = ... - SINT_16: ClassVar[HalElementType] = ... - SINT_32: ClassVar[HalElementType] = ... - SINT_4: ClassVar[HalElementType] = ... - SINT_64: ClassVar[HalElementType] = ... - SINT_8: ClassVar[HalElementType] = ... - UINT_16: ClassVar[HalElementType] = ... - UINT_32: ClassVar[HalElementType] = ... - UINT_4: ClassVar[HalElementType] = ... - UINT_64: ClassVar[HalElementType] = ... - UINT_8: ClassVar[HalElementType] = ... +class HalElementType(enum.IntEnum): @staticmethod def map_to_dtype(element_type: HalElementType) -> Any: ... @staticmethod def is_byte_aligned(element_type: HalElementType) -> bool: ... @staticmethod def dense_byte_count(element_type: HalElementType) -> int: ... - __name__: Any + + NONE = ... + OPAQUE_8 = ... + OPAQUE_16 = ... + OPAQUE_32 = ... + OPAQUE_64 = ... + BOOL_8 = ... + INT_4 = ... + INT_8 = ... + INT_16 = ... + INT_32 = ... + INT_64 = ... + SINT_4 = ... + SINT_8 = ... + SINT_16 = ... + SINT_32 = ... + SINT_64 = ... + UINT_4 = ... + UINT_8 = ... + UINT_16 = ... + UINT_32 = ... + UINT_64 = ... + FLOAT_16 = ... + FLOAT_32 = ... + FLOAT_64 = ... + BFLOAT_16 = ... + COMPLEX_64 = ... + COMPLEX_128 = ... + FLOAT_8_E4M3_FN = ... + FLOAT_8_E4M3_FNUZ = ... + FLOAT_8_E5M2 = ... + FLOAT_8_E5M2_FNUZ = ... + FLOAT_8_E8M0_FNU = ... class HalFence: @staticmethod @@ -458,29 +446,23 @@ self, shape: Sequence[int], numpy_dtype_descr: object ) -> np.ndarray[Any, np.dtype[Any]]: ... -class MemoryAccess(int): - ALL: ClassVar[MemoryAccess] = ... - DISCARD: ClassVar[MemoryAccess] = ... - DISCARD_WRITE: ClassVar[MemoryAccess] = ... - NONE: ClassVar[MemoryAccess] = ... - READ: ClassVar[MemoryAccess] = ... - WRITE: ClassVar[MemoryAccess] = ... - __name__: Any - def __and__(self, other: MemoryAccess) -> int: ... - def __or__(self, other: MemoryAccess) -> int: ... +class MemoryAccess(enum.IntFlag): + NONE = ... + READ = ... + WRITE = ... + DISCARD = ... + DISCARD_WRITE = ... + ALL = ... -class MemoryType(int): - DEVICE_LOCAL: ClassVar[MemoryType] = ... - DEVICE_VISIBLE: ClassVar[MemoryType] = ... - HOST_CACHED: ClassVar[MemoryType] = ... - HOST_COHERENT: ClassVar[MemoryType] = ... - HOST_LOCAL: ClassVar[MemoryType] = ... - HOST_VISIBLE: ClassVar[MemoryType] = ... - NONE: ClassVar[MemoryType] = ... - OPTIMAL: ClassVar[MemoryType] = ... - __name__: Any - def __and__(self, other: MemoryType) -> int: ... - def __or__(self, other: MemoryType) -> int: ... +class MemoryType(enum.IntFlag): + NONE = ... + OPTIMAL = ... + HOST_VISIBLE = ... + HOST_COHERENT = ... + HOST_CACHED = ... + HOST_LOCAL = ... + DEVICE_VISIBLE = ... + DEVICE_LOCAL = ... class ParameterIndexEntry: @property