Skip to content

NEP 59: native bfloat16 dtype - #33601

Open
jakevdp wants to merge 1 commit into
numpy:mainfrom
jakevdp:bfloat16-nep
Open

jakevdp wants to merge 1 commit into
numpy:mainfrom
jakevdp:bfloat16-nep

Conversation

@jakevdp

@jakevdp jakevdp commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Related links:

AI disclosure: this document was authored with the help of a Gemini 4 Neon model

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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**:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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``. |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants