-
Notifications
You must be signed in to change notification settings - Fork 53
Question: complex number support in Array API? #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I should add that if we were to support complex numbers, all of the sorting and comparison behaviors (ex: |
Thanks for bringing this up @leofang. You're not the first to ask, so it's good to document this decision and have a summary of the current status. The main issue is that not all array libraries have good support for complex dtypes yet. Complex numbers are quite important for science, but are only of marginal importance to deep learning. TensorFlow, PyTorch and MXNet all don't have great support yet. When there's such partial support of a feature, we have in some cases chosen to include the feature in the current version of the standard if it was not too difficult for those libraries to implement it. But for complex support, it's a ton of work. Hence it would be a feature that would only be fully supported by ~50% of libraries. Excluding it from the standard doesn't mean CuPy cannot have it - it just means it is not part of the standard, so shouldn't have those dtypes in the separate array-api-supporting namespace. Which signals to users that as of today one cannot write code that is portable between libraries using complex. They can still use it in CuPy (and NumPy, JAX). PyTorch's implementation is indeed getting there, but not yet complete. TensorFlow does have an implementation in progress, but is further behind AFAIK. In 12 months from now it should be feasible to add We had a similar discussion about
Yep, that's been a mess for a long time. One thing about this standard is that we try to not do too much innovation; if a feature is still under discussion or being changed in a library, in most cases we should choose wait-and-see (maybe ensuring that libraries don't make incompatible choices), and then only add it to the standard if things have stabilized. So with an issue like "sorting behaviour for complex numbers" I'd choose to leave it out, since sorting isn't all that important for the physics/engineering type applications that need complex numbers.
The CuPy implementation seems to use |
Thanks @rgommers for the detailed summary (and for offline discussion on this) 🙏
I hope my original post gave enough motivation from ML/DL perspective. Quantum stuff aside, while it was not the case in the early days of ML/DL, nowadays some (and increasing) ML/DL applications do need complex numbers. If necessary I could compile a list of references, but I hope that the recent movements in the major ML frameworks as you mentioned self-justify this.
I understand the amount of work needed to be done; I've been there. But from the perspective of supporting native types in the mainstream programming languages (I have C/C++/Fortran/Python/CUDA/HIP in mind), IMHO an incomplete coverage of types in the specification is just weird, as we all know one day we'll be there so why don't we think harder about it now. From my experience any code implementation leaving complex behind will pay a significant price later for adding the support, so at least code maintainers need to have a clear expectation that this will come one day (and soon). Perhaps we can at least consider the type conversion rules involving complex so that the libraries that are resourceful to (or already) support complex number can act accordingly, and mark this support experimental in the v1 API (yes, v1, the current one) for others to have time to catch up in v2? I would love to see people committing to a convergence.
If anything, this just strengthens my concern. For codes that require complex, it means it cannot be migrated to the array-api namespace in the next 1~2 years (or longer) even if a subset of participating libraries (say CuPy + JAX) used by the code already support complex. Doesn't look nice...
As mentioned, it is nice to have a rough timeline to expect 🙂
I think in a sense this is justifiable.
No, it uses |
This makes sense, how about we add a prominent note on the "Data Types" page that we do expect to add
The casting rule will be similar to float (and unlikely anyway can sensibly deviate):
I suspect that the NumPy/CuPy/JAX implementations of the API in a new namespace are still going to accept complex dtypes. Simply because it's significantly more work to raise errors, rather than just alias functions (maybe with a signature change if needed) to existing implementations. So in practice, nothing much will change.
Interesting, thanks. I can't remember any requests for NumPy to add |
This could be a way to go, certainly! And also add the conversion rules that we expect to work.
Yes, I think the rules for converting within complex and between real and complex should be clear, with one possible exception: We might need to decide what to do with I guess the point I tried to make is simply that we need to make complex appear in the Type Promotion Rules section so that people know this is considered and coming and that library providers can act accordingly.
I think in practice this is likely the case indeed. I do see in the Data Types section of the API Standard it states "A conforming implementation of the array API standard may provide and support additional data types beyond those described in this specification." so it's probably OK. Thanks.
The issue has been raised for quite some time: numpy/numpy#14753. It was mentioned that there'd be a dtype system update, but it was not clear what specifically it referred to. btw, I noticed there's |
+1 to that. The solution is identical, so better get the explicit error - it's almost always user error anyway.
https://numpy.org/neps/nep-0041-improved-dtype-support.html Large parts of that are landing in NumPy 1.20.0 next month.
some PyTorch casting rules are getting closer, like automatic integer to float promotion implementation for functions that return floats is almost complete and numpy-like. documentation is a bit limited; casting rule docs are at https://pytorch.org/docs/stable/tensor_attributes.html#type-promotion-doc. implementation under the hood is very different, I like http://blog.ezyang.com/2019/05/pytorch-internals/ as a guide. |
This introduces a new "Future extension" admonition, which uses the MyST "colon fence" (requires a newer version of MyST). Closes data-apisgh-102
Added the note in gh-105, which should address this issue. |
This introduces a new "Future extension" admonition, which uses the MyST "colon fence" (requires a newer version of MyST). Closes gh-102
I am wondering the reason that complex numbers are not considered in the Array API, and if we could give a second thought to make them native dtypes in the API.
The Dataframe API is not considered in the rest of this issue 🙂
I spent quite some time on making sure complex numbers are first-class citizens in CuPy, as many scientific computing applications require using complex numbers. In quantum mechanics, for example, complex numbers are the cornerstones and we can't live without them. Even in some machine learning / deep learning works that we do, either classical or quantum (yes, for those who don't know already there is quantum machine learning 😁), we also need complex numbers in various places like building tensors or communicating with simulations, especially those applying physics-aware neural networks, so it is a great pain to us not being able to build and operate on complex numbers natively.
To date, complex numbers are also an integral part of mainstream programming languages. For example, C has it since C99, and so is C++ (
std::complex
). Our beloved Python hascomplex
too, so it is just so weird IMHO that when we talk about native dtypes they're being excluded.As for language extensions to support GPUs, in CUDA we have
thrust::complex
(which currently supportscomplex64
/complex128
) as a clone ofstd::complex
and it is likely thatlibcu++
will replace Thrust on this aspect, and in ROCm there's also a Thrust clone and native support in HIP, so at least on NVIDIA/AMD GPUs we are good.Turning to library support, as far as I know
complex64
/complex128
, but notcomplex32
(ENH: half precision complex numpy/numpy#14753)complex64
/complex128
, andcomplex32
is being evaluated (ex: [WIP] Addcupy.complex32
cupy/cupy#4454)complex32
/complex64
/complex128
is catching up (I am unaware of any meta-issue summarizing the status quo, but the labelmodule: complex
is a good referencecupyx.scipy
has many components supporting complex numbers, the most recent prominent case being the extensivendimage
overhaul (ex: ENH: Support complex-valued images and kernels for many ndimage filters scipy/scipy#12725) done by @grlee77 for image processing (yes, image processing also needs complex numbers!)The reason I also mention
complex32
above is because CUDA now providescomplex32
support in some CUDA libraries like cuBLAS and cuFFT. With special hardware acceleration overfloat16
, it is expected thatcomplex32
can also benefit, see the preliminary FFT test being done in cupy/cupy#4407. Hopefully by having complex number support in ML/DL frameworks (complex64
andcomplex128
are enough to start) many more applications can be benefited as well.I am aware that Array API picks DLPack as the primary protocol for zero-copy data exchange, and that it currently lacks complex number support. This is one of the reasons I do not like DLPack. While I will create a separate issue to discuss about alternatives to DLPack, I think revising DLPack's format is fairly straightforward (and should be done asap regardless of the Array API standardization due to the need of ML/DL libraries).
Disclaimer: This issue is merely for my research interests (relevant to my and other colleagues' work) and is not driven by CuPy, one of the Array API stakeholders I will represent.
The text was updated successfully, but these errors were encountered: