Repository navigation
Description
Motivation
There are a large number of floating-point types in DLPack, especially 4-bit and 8-bit variants. The structure to define new FP types for DLPack exchange currently requires modifications to DLPack itself, and this RFC aims to relax that limitation.
MLIR moved to a more flexible system in the beginning of last year (discussion, PR), and I feel we should follow in their footsteps.
@seberg commented in a different issue that a way forward without ABI breakage would be a requirement to do this short-term, and I agree.
Proposal
This RFC proposes a few things, that when taken together, allow one to specify many¹ floating point types.
- The introduction of two new enum values in the
kDLDataTypeCodeenum: kDLFlexibleFloat and kDLFlexibleComplex. - That the
DLDataTypestruct be turned into a (C-style) union. The existing layout would become the first variant of the struct, and the second variant is specified below.
typedef struct {
/*!
* \brief Type code of base types.
* We keep it uint8_t instead of DLDataTypeCode for minimal memory
* footprint, but the value should be one of DLDataTypeCode enum values.
* */
uint8_t code;
/*!
* \brief Number of bits minus one, common choices are 2, 4, 8, 16, 32, 64.
* For complex, specify the number of bits for the components rather than the whole.
*/
uint8_t bitsm1;
/*!
* \brief Number of mantissa bits.
* Implies number of exponent bits are bits - 1 - mantissa in the common case.
* mantissa = 0 specifies an exponent-only format (one example is float8_e8m0fnu)
*/
uint8_t mantissa;
/*!
* \brief The flags associated with this FP type.
* Beginning with the LSB:
* Bit 0: Finite bit, often specified by f
* Bit 1: NaN bit, often specified by n
* Bit 2: (Signed) zero bit, often specified by z
* Bit 3: Unsigned zero bit, often specified by uz
* Bits 5-7: Reserved.
*/
uint8_t flags;
/*! \brief Specifies the number of lanes in the type, used for vector types.
* The value should be raised to a power of two, i.e., a value of 5 means 32 lanes.
*/
uint8_t p2lanes;
} DLDataTypeFloatingSpec;This variant of the struct is only activated by the two enum values above. The existing enum and equivalents here would be considered aliases to each other, but implementations are not required to support the latter if they already support the former.
Open Questions
The float8_e4m3b11fnuz type is not covered by this proposal in its current form. I'm currently debating how best to address that. Options are:
- Leave it as out of scope/requiring its own enum value.
- Split
p2lanesinto two 4-bit fields, one forp2lanesand the other forbias. - Use the reserved bits for
biasinstead. - Use a combination to increase the bits available to
bias.
¹ I would consider non IEEE-style types such as POSIT and TAKUM to be explicitly excluded from the scope of this proposal, perhaps to be addressed by future flags.
Activity
seberg commentedon Apr 28, 2026
One thing though, is that the next new float dtype we might see is
float-float(and who knows maybe alsobfloat16-bfloat16or so for float32).So, yeah, I think it is interesting, but I am not sure I find it interesting enough to feel it's worth to seriously pursue. In the end even a large enum is a bit clumsy but it's simple and actually quite convenient.
That is, unless we actually anticipate a much larger family (or needing these flags for normal floats or so).
(For current users, I don't think untangling flags is even more convenient than just a single large enum.)
leofang commentedon Apr 28, 2026
This is interesting but is not something that solves new problems that adding new enumerators don't already solve. I don't think updating DLPack is that painful (plus, this RFC still requires opt-in, which seems more daunting to update to than just adding enums 😅). Finally, wouldn't turning DLDataType to a union break the ABI (due to struct size change)?
mwyau commentedon Sep 9, 2026
I opened this PR numpy/numpy#32548 to add DLPack support for IEEE quad long double.
To support complex long double will require a change on the DLPack side, since the
bitsis onlyuint8_tand an IEEE quad complex long double will require 256 bits.hpkfft commentedon Sep 9, 2026
It's probably better to avoid "long double", which does not necessarily map to IEEE binary128.
In C, use
_Float128, and in C++,std::float128_t.See also: https://en.cppreference.com/cpp/types/floating-point
rgommers commentedon Sep 9, 2026
Agreed, there are 7 different incompatible representations of
long double, and as a result it's very hard to make use of. Encoding 1 out of those 7 in DLPack seems like a bad idea to me. There's also not much real-world benefit, given that no array library other than NumPy supports it, and even in NumPy we hope to get rid of it eventually.mwyau commentedon Sep 9, 2026
@hpkfft I should have rephrased my wording about
long double. IEEE binary128 floating-point type is already supported by DLPack. The Numpy PR is about the supported float type which is 128 bit. My question here was about the complex type which is 256-bit and does not fit inuint8_t.hpkfft commentedon Sep 9, 2026
See #73