Skip to content

[RFC] Adding a kDLBComplex type code for complex<bfloat16> #184

Closed

Description

@rgommers

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.

Relevant context:

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?

Activity

  1. hameerabbasi commented on Apr 28, 2026

    @hameerabbasi

    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.

  2. seberg commented on Apr 28, 2026

    @seberg
    Collaborator

    +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.)

  3. hameerabbasi commented on Apr 28, 2026

    @hameerabbasi

    It's simply a tedious ABI evolution to change it.

    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)
    • One bit each for signed-zero and finite-numbers.
  4. seberg commented on Apr 28, 2026

    @seberg
    Collaborator

    Maybe for another issue

    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?)

  5. hpkfft commented on Apr 28, 2026

    @hpkfft

    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:

    1. Since the enumeration kDLBfloat does not use an uppercase F, I would suggest kDLBcomplex, not kDLBComplex.
    2. 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().
  6. rgommers commented on Apr 28, 2026

    @rgommers
    CollaboratorAuthor
    1. 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.

    That's gh-99, let's not mix that in here.

    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)?

  7. hameerabbasi commented on Apr 28, 2026

    @hameerabbasi

    I'm +1 on an enum value for bcomplex32. I've opened #185 as a separate discussion.

  8. hpkfft commented on Apr 28, 2026

    @hpkfft
    1. 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).

    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 kDLBcomplex and not DLPackManagedTensorFromPyObjectNoSync()?
    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

    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

    I was agreeing with Sebastian that nobody needs lanes and 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 value 1. Currently, nanobind treats values other than 1 as an error:
    https://github.com/wjakob/nanobind/blob/acc88f3f91d09889c919ca5ea05cf604c4fce102/src/nb_ndarray.cpp#L691-L692

  9. seberg commented on Apr 28, 2026

    @seberg
    Collaborator

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions