You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[RFC] Adding a kDLBComplex type code for complex<bfloat16> #184
Complex dtype support is done today by using the kDLComplex type code plus settings the number of bits. That is unambiguous for complex64 and complex128. I believe complex32 was meant to mean complex<float16> (a code comment to that effect is missing though), and a bcomplex32 cannot be represented today. To represent it, as already suggested by @leofang in #50 (comment), a new type code kDLBComplex should be added.
Proposal: add kDLBComplex. To discuss: should it get type code 18U as the next number in the list, or is it better to leave a gap for more Float4 types?
To discuss: should it get type code 18U as the next number in the list, or is it better to leave a gap for more Float4 types?
Imho it's getting to the point where we should just define FP types by mantissa/exponent bits, and whether or not they have signed zeroes, NaNs or infs (no FP expert so I might be missing something). Having an arbitrarily large enum isn't elegant to me.
+1, let's just extend the enum. Dunno if offset or not matters much, but sounds fine if there is an obvious one too choose.
Having an arbitrarily large enum isn't elegant to me.
True, but TBH, unless someone has a very good and clean thought, I don't think it's practical. At least until we reach ~100 of these and the uint8 feels limiting, or we somehow start adding something that just can't work (e.g. structured).
It's simply a tedious ABI evolution to change it. If we had the option without that ABI hassle, I would agree, but...
(Yes, one could have maybe used bits for parts of it, but bits might also make sense for storage padding so.)
Maybe for another issue; but is the current union struct too small to hold these attributes? We could just add another variant to the union, and introduce a single ENUM value that would trigger this variant, and say the that the new ones are aliases to the old. If my thinking is correct we'd only need about 3 bytes, if we were to REALLY pack this information.
8 bits for the total # of bits (up to 128)
8 bits for the mantissa (up to 128)
Exponent = total bits - 1 - mantissa bits (no bits needed here)
Maybe best. Not sure if re-interpreting bits is enticing. Without that have 16 bits left to play around it that maybe nobody really needs (i.e. lanes). (Technically, may still break ABI, if someone thinks bits * lanes is the size, but presumably fine in practice.)
(That said, besides possibly having to double this for complex, unless this grows a lot, do we actually gain much?)
Regardless of how complex bfloat16 is added to DLPack, I'm in favor of mandating that lanes must be 1 so that the field is effectively reserved for future use. (And, it would be nice to remove the comments about an alleged 256 byte alignment requirement, which is rightfully ignored by implementations.)
I obviously don't know all the floating-point formats that may arise in the future. The exponent bias might be parameter that is altered to create a new floating-point type. Maybe there is no such thing as subnormal values. An unsigned floating-point type (i.e., having no sign bit) is possible. There's also more interesting ideas, e.g., posit, takum. I'm not saying that it's a bad idea to avoid a big list of enumerations, only that it's not easy.
I have two suggestions specific to this RFC:
Since the enumeration kDLBfloat does not use an uppercase F, I would suggest kDLBcomplex, not kDLBComplex.
I would suggest that adding this enumeration be considered a bugfix to version 1.1 (and later versions). The issue is what version to use to indicate support for kDLBcomplex in the C-style struct DLManagedTensorVersioned without implying support for functions such as DLPackManagedTensorFromPyObjectNoSync().
I would suggest that adding this enumeration be considered a bugfix to version 1.1 (and later versions).
That won't work, it's a new feature. Retroactively adding a feature to a previous release is going to make version-based checks meaningless (and that's a recommended pattern, see for example max_version in __dlpack).
And, it would be nice to remove the comments about an alleged 256 byte alignment requirement, which is rightfully ignored by implementations.
I would suggest that adding this enumeration be considered a bugfix to version 1.1 (and later versions).
That won't work, it's a new feature. Retroactively adding a feature to a previous release is going to make version-based checks meaningless (and that's a recommended pattern, see for example max_version in __dlpack).
I'm in favor of mandating that lanes must be 1 so that the field is effectively reserved for future use.
Are you saying that because complex dtypes can't be represented as vector types (array of struct vs. struct of array representation)?
No, no, I meant nothing thoughtful. I'm perfectly happy with an array of complex types being represented in memory with the real and imaginary components adjacent and interleaved. In fact, I support FFTs on complex32 by allowing users to use dtype=np.float16 and just mentally keeping track of the fact that 1.+2.j is represented by the two elements 1., 2. If you're interested, see the first example in https://hpkfft.com/python.html#data-types
There is no need to discuss 1.1 vs 1.2 or... I think?! It's besides the point: even if we care (in practice it doesn't matter anyway), both things are completely unrelated: "version 1.1" doesn't mean that you must implement those functions (just if you do, you must match the signature, etc.).
The only thing would be if we were at 2.x it would in principle make sense to additionally have a 1.2 just to advertise that you support these additional dtypes because you indeed wouldn't want to jump a major version for that.
Sorry about bringing up lanes. I personally don't see much value in them, but it is also just fine if it is around (that is from how it's used in Python, and I am pretty sure to the point where I saw it just being ignored by some implementation).
Activity
hameerabbasi commentedon Apr 28, 2026
Imho it's getting to the point where we should just define FP types by mantissa/exponent bits, and whether or not they have signed zeroes, NaNs or infs (no FP expert so I might be missing something). Having an arbitrarily large enum isn't elegant to me.
seberg commentedon Apr 28, 2026
+1, let's just extend the enum. Dunno if offset or not matters much, but sounds fine if there is an obvious one too choose.
True, but TBH, unless someone has a very good and clean thought, I don't think it's practical. At least until we reach ~100 of these and the
uint8feels limiting, or we somehow start adding something that just can't work (e.g. structured).It's simply a tedious ABI evolution to change it. If we had the option without that ABI hassle, I would agree, but...
(Yes, one could have maybe used
bitsfor parts of it, butbitsmight also make sense for storage padding so.)hameerabbasi commentedon Apr 28, 2026
Maybe for another issue; but is the current union struct too small to hold these attributes? We could just add another variant to the union, and introduce a single ENUM value that would trigger this variant, and say the that the new ones are aliases to the old. If my thinking is correct we'd only need about 3 bytes, if we were to REALLY pack this information.
seberg commentedon Apr 28, 2026
Maybe best. Not sure if re-interpreting
bitsis enticing. Without that have 16 bits left to play around it that maybe nobody really needs (i.e.lanes). (Technically, may still break ABI, if someone thinksbits * lanesis the size, but presumably fine in practice.)(That said, besides possibly having to double this for complex, unless this grows a lot, do we actually gain much?)
hpkfft commentedon Apr 28, 2026
Regardless of how complex bfloat16 is added to DLPack, I'm in favor of mandating that
lanesmust be 1 so that the field is effectively reserved for future use. (And, it would be nice to remove the comments about an alleged 256 byte alignment requirement, which is rightfully ignored by implementations.)I obviously don't know all the floating-point formats that may arise in the future. The exponent bias might be parameter that is altered to create a new floating-point type. Maybe there is no such thing as subnormal values. An unsigned floating-point type (i.e., having no sign bit) is possible. There's also more interesting ideas, e.g., posit, takum. I'm not saying that it's a bad idea to avoid a big list of enumerations, only that it's not easy.
I have two suggestions specific to this RFC:
kDLBfloatdoes not use an uppercaseF, I would suggestkDLBcomplex, notkDLBComplex.kDLBcomplexin the C-style structDLManagedTensorVersionedwithout implying support for functions such asDLPackManagedTensorFromPyObjectNoSync().rgommers commentedon Apr 28, 2026
That won't work, it's a new feature. Retroactively adding a feature to a previous release is going to make version-based checks meaningless (and that's a recommended pattern, see for example
max_versionin __dlpack).That's gh-99, let's not mix that in here.
Are you saying that because complex dtypes can't be represented as vector types (array of struct vs. struct of array representation)?
hameerabbasi commentedon Apr 28, 2026
I'm +1 on an enum value for bcomplex32. I've opened #185 as a separate discussion.
hpkfft commentedon Apr 28, 2026
Yeah, but in the "notes" section at the bottom of that page there's the advice
major versions match, we should still be fine.I mean, of course, I understand what you're saying. But, what should NumPy do here:
https://github.com/numpy/numpy/blob/3cd33b12bd6682b04a6443951031cfe795d8accf/numpy/_core/src/common/dlpack/dlpack.h#L25
if it wants to support
kDLBcomplexand notDLPackManagedTensorFromPyObjectNoSync()?This is a genuine question. I would like to follow NumPy's decision and submit a PR to nanobind for here:
https://github.com/wjakob/nanobind/blob/acc88f3f91d09889c919ca5ea05cf604c4fce102/include/nanobind/nb_lib.h#L16
No, no, I meant nothing thoughtful. I'm perfectly happy with an array of complex types being represented in memory with the real and imaginary components adjacent and interleaved. In fact, I support FFTs on complex32 by allowing users to use
dtype=np.float16and just mentally keeping track of the fact that1.+2.jis represented by the two elements1., 2.If you're interested, see the first example in https://hpkfft.com/python.html#data-typesI was agreeing with Sebastian that nobody needs
lanesand thought it might be good to start a deprecation process (so that the struct member is effectively reserved for future use) by mandating it have the value1. Currently, nanobind treats values other than1as an error:https://github.com/wjakob/nanobind/blob/acc88f3f91d09889c919ca5ea05cf604c4fce102/src/nb_ndarray.cpp#L691-L692
seberg commentedon Apr 28, 2026
There is no need to discuss 1.1 vs 1.2 or... I think?! It's besides the point: even if we care (in practice it doesn't matter anyway), both things are completely unrelated: "version 1.1" doesn't mean that you must implement those functions (just if you do, you must match the signature, etc.).
The only thing would be if we were at
2.xit would in principle make sense to additionally have a1.2just to advertise that you support these additional dtypes because you indeed wouldn't want to jump a major version for that.Sorry about bringing up
lanes. I personally don't see much value in them, but it is also just fine if it is around (that is from how it's used in Python, and I am pretty sure to the point where I saw it just being ignored by some implementation).