Repository navigation
Conversation
mhvk
left a comment
There was a problem hiding this comment.
I think this remains a good idea. Some in-line notes/comments.
| (``kDLBfloat``), and typing stubs. | ||
| - Graceful coexistence with existing ``ml_dtypes.bfloat16`` installations. | ||
|
|
||
| **Out of scope**: |
There was a problem hiding this comment.
I would order these items by how much out of scope they are. I'd start with the hardware-specific implementations (which would not change any API), and then discuss complex numbers followed by FFT/linalg, both of which I think are the types of follow-up that maybe should be assumed implicit with this PEP (i.e., no new PEP required), and finally the other formats, which I think are truly out of scope -- for those, this work just provides a nice template using the new user dtypes.
| ``NPY_BFLOAT16 = 2057`` (immediately following ``NPY_VSTRING = 2056``). | ||
| ``NPY_NTYPES_LEGACY`` remains ``24``, so existing C extensions compiled | ||
| against the NumPy 2.x ABI are unaffected. | ||
| 2. **Coexistence with ``ml_dtypes.bfloat16``**: During the transition period, |
There was a problem hiding this comment.
I'm not sure about this section. It would seem preferable for ml_dtypes to simply use the new bfloat16 if available. But at some level, the point of this section probably is that it is not numpy's problem; maybe make that clear?
| +-------------------+--------------------------------------+-----------------------------------------------+ | ||
| | Option | Pros | Cons | | ||
| +===================+======================================+===============================================+ | ||
| | **``'E'``** | - Established precedent in | - Breaks NumPy's float casing rule where | |
There was a problem hiding this comment.
I rather dislike breaking the link between real/complex....
| | | - Enables single-char ``memoryview`` | ``np.typecodes['All']`` (e.g., SciPy Cython | | ||
| | | format and ``np.dtype('E')``. | fused-type tests that exclude ``'e'``). | | ||
| +-------------------+--------------------------------------+-----------------------------------------------+ | ||
| | **Lowercase** | - Preserves lowercase = real float | - Zero mnemonic connection to ``bfloat16``. | |
There was a problem hiding this comment.
One could argue 'r' has some mnemonic connection -- reduced precision float -- but 'R' is suggested for the variable length bytes class (NEP 58), so that makes it a bad choice.
| ``ml_dtypes`` and enabling PEP 3118 ``memoryview`` round-trips, ideally paired | ||
| with a CPython PEP to reserve the code in ``struct``), but this NEP leaves the | ||
| final choice between **``'E'``** and **no single-character code** as an explicit | ||
| decision point for community consensus. |
There was a problem hiding this comment.
FWIW, I'd go with no single-character code (but no very strong feelings; the character codes are somewhat of a mess anyway, and precedent does count).
| - **``float_power``**: Following NumPy's contract for ``np.float_power``, | ||
| ``bfloat16`` inputs promote to at least ``float64``. | ||
|
|
||
| 3. Typecode and character representation |
There was a problem hiding this comment.
It is not only the typecode one might worry about, but also representation in structured dtypes. There, f2 is taken.
| ------------ | ||
|
|
||
| 1. **Keep ``bfloat16`` solely in ``ml_dtypes``**: | ||
| Avoids adding a DType to NumPy core, but leaves ``bfloat16`` permanently |
There was a problem hiding this comment.
Are these disadvantages as clear if ml_dtypes uses a user dtype? I am in favour of moving things to numpy, but the real alternative would seem to be to ensure numpy machinery works properly with a bfloat16 new-style user dtype. At some level, of course, that was the point of having those new-style user dtypes, so anything that doesn't work with them is a bug. I guess the comparison would be the quad-precision dtype, which is implemented as a user dtype. Does that write to npy files without pickle?
Related links:
bfloat16tracking issue: ENH: numpy bfloat16 support #19808ml_dtypesbfloat16 reference implementation: https://github.com/jax-ml/ml_dtypes/AI disclosure: this document was authored with the help of a Gemini 4 Neon model