Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit a032bfb

Browse files
authored
Reapply "[MLIR][Python] add ctype python binding support for bf16" (#101271)
Reapply the PR which was reverted due to built-bots, and now the bots get updated. https://discourse.llvm.org/t/need-a-help-with-the-built-bots/79437 original PR: llvm/llvm-project#92489, reverted in llvm/llvm-project#93771
1 parent 936603d commit a032bfb

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

mlir/python/mlir/runtime/np_to_memref.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import numpy as np
88
import ctypes
99

10+
try:
11+
import ml_dtypes
12+
except ModuleNotFoundError:
13+
# The third-party ml_dtypes provides some optional low precision data-types for NumPy.
14+
ml_dtypes = None
15+
1016

1117
class C128(ctypes.Structure):
1218
"""A ctype representation for MLIR's Double Complex."""
@@ -26,6 +32,12 @@ class F16(ctypes.Structure):
2632
_fields_ = [("f16", ctypes.c_int16)]
2733

2834

35+
class BF16(ctypes.Structure):
36+
"""A ctype representation for MLIR's BFloat16."""
37+
38+
_fields_ = [("bf16", ctypes.c_int16)]
39+
40+
2941
# https://stackoverflow.com/questions/26921836/correct-way-to-test-for-numpy-dtype
3042
def as_ctype(dtp):
3143
"""Converts dtype to ctype."""
@@ -35,6 +47,8 @@ def as_ctype(dtp):
3547
return C64
3648
if dtp == np.dtype(np.float16):
3749
return F16
50+
if ml_dtypes is not None and dtp == ml_dtypes.bfloat16:
51+
return BF16
3852
return np.ctypeslib.as_ctypes_type(dtp)
3953

4054

@@ -46,6 +60,11 @@ def to_numpy(array):
4660
return array.view("complex64")
4761
if array.dtype == F16:
4862
return array.view("float16")
63+
assert not (
64+
array.dtype == BF16 and ml_dtypes is None
65+
), f"bfloat16 requires the ml_dtypes package, please run:\n\npip install ml_dtypes\n"
66+
if array.dtype == BF16:
67+
return array.view("bfloat16")
4968
return array
5069

5170

mlir/python/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
numpy>=1.19.5, <=1.26
22
pybind11>=2.9.0, <=2.10.3
3-
PyYAML>=5.3.1, <=6.0.1
3+
PyYAML>=5.3.1, <=6.0.1
4+
ml_dtypes # provides several NumPy dtype extensions, including the bf16

0 commit comments

Comments
 (0)