-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Options for implementing a quadruple-precision dtype #14574
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
AFAIK, the only common platforms that support quad precision are ARM64, POWER9, and SPARC. The rest either make long double and double the same (MSVC) or make long double extended precision (GCC). What platform are you on? |
Fedora Linux 30, Intel Xeon (current generation). Use gcc, installed version is 9.2.1. |
I feel like we can probably close this? float128 is a misnomer (since it sounds like it was IEEE 128bit float which it is not). But the precision is thus exactly what we have. Deprecating the name might be possible, but has other issues with the file format. We have gh-10288 open about that. |
Would it be possible to use 128 bit libraries to implement this data
type generally? (I need to check whether gfortran on my platform does
this correctly, though; it does have the data type.)
…On 25/9/19 11:38 am, Sebastian Berg wrote:
I feel like we can probably close this? float128 is a misnomer (since it
sounds like it was IEEE 128bit float which it is not). But the precision
is thus exactly what we have. Deprecating the name might be possible,
but has other issues with the file format. We have gh-10288
<#10288> open about that.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#14574?email_source=notifications&email_token=AAJW2FV73TMRGGNYBHRYDN3QLK6IDA5CNFSM4IZBW662YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7QJWYY#issuecomment-534813539>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAJW2FQDQBO6YN36FZ44QF3QLK6IDANCNFSM4IZBW66Q>.
|
OK, in gfortran (Version 9.2.1, "GNU Fortran (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1)") I get from program test
print*,aimag(exp(atan(1.q0)*(0.q8,8.q0)))
end program test the output -1.73436202602475620495940880520867045E-0034 which is the precision it should be. I would be really good if NumPy could use/implement that as well. It should not be worse than fortran. |
We need to worry about other compilers than gcc. |
Wrting an extension type outside numpy is certainly doable right now in principle (it will have some limitation, but for almost everything should be fine). For within nump, there would need to be simple fairly general support. There was a discussion at some point whether we can just switch to higher precision, real IEEE745 quad. It seems to me like the largest issue could be reading old pickle/npy's (although even now they are not compatible between systems) |
Several year ago I looked into quad precision libraries and found a couple, but none of them had a NumPy compatible license. |
Sleef - see also SLEEF: A Portable Vectorized Library of C Standard Mathematical Functions and Sleef.org - recently published additional support for a software implementation of a Sleef supports different CPU architectures and compiler toolchains - see Sleef.org and has a numpy compatible licence. Another interesting bit is the MPLAPACK, the multiprecision linear algebra package library (formerly known as MPACK). This library can be compiled with In summary, the necessary open source components are available to provide full support for quadrupole precision. As usual, integrating all of this together into numpy will require some effort. |
@carlkl Thanks for the heads up, SLEEF looks interesting. @Qiyu8 @seiko2plus Ping. |
Hello, As for SLEEF, there are a couple of things to pay attention.
|
@shibatch I didn't see your scalar version quadruple precision implementation, the vectorized version has an error bound like 0.5000000001 ULP, which is unbearable in numpy's precision standard, maybe scalar version can overcame this flaw. |
It is not so hard to just implement a correctly-rounded version of arithmetic functions. The problem is that there is a tradeoff between speed and accuracy. If 0.5000000001 ULP is not bearable, I will add correctly-rounded but slower functions. |
Perhaps arithmetic functions used for the trigonometric functions do not necessarily have to be correctly-rounded, only the arithmetic functions exposed to the user for IEEE 754 conformity. |
Arithmetic functions are implemented within glibc, and they are correctly rounded. |
Paul Zimmermann (INRIA) recently compared the accuracy of several math libraries: accuracy.pdf. This includes a comparison of quadrupole precision between glibc and icc. See also his talk How Slow is Quadruple Precision on Variable Precision in Mathematical and Scientific Computing. |
Quadruple precision arithmetic functions are slow because packing to/unpacking from IEEE 754 format are pretty complicated. If computation speed is important, double-double is much faster. SLEEF quad library is designed in a way that the frontend and backend are separated. The frontend does packing and unpacking. Computation by the backend is all carried out with triple-double operations. Thus, it is easy to add another frontend for a different format. |
I want to point out my comment #14574 (comment) is motivated by my desire to discuss the need for quadrupole precision in Numpy. It seems to me, that adding quadrupole precision to numpy is feasible in forseable future with the help off Sleef and MPLAPACK for a considerable number of target systems. Personally I would like to have the possibility to combine the efficiency and elegance of numpy with enhanced precision. On the other side I know that this is a small but important niche within computational science. Usually Fortran or C++ are used there. I'm not a numpy maintainer, so I have no idea if the effort of integration into numpy pays of. This is the question that has to be discussed first IMHO. |
@carlkl I am doing a lot of slow work to allow you to externally write a quadruple precision that integrates seamlessly. Right now, that is not finished and the parts that are, are not public API yet, but it should fall in places for 1.21 far enough that one can start to use it (experimentally). There could be some small limitations (at least I assume those won't go away quickly), such as having to use My current opinion is to start writing such dtypes outside of NumPy first. While it probably makes some things work less well, it avoids having to worry about partial implementation or stranger corners in NumPy. |
There are uses for quad precision apart from it starting to show up with hardware support. I looked into it because I'd like to use it for a new datetime type. I think astropy uses double double for that, they need the extra precision. As a historical note, VAX had a quad precision type back in the 1980's and I found it useful on occasion. |
I am really not as such opposed with adding things to NumPy, I just feel that comes with a lot of baggage (whether real or mental); and also with stronger than typical backcompat guarantees. Also someone not familiar with NumPy can probably dive into an external implementation much easier (and things tend to work better driven by interested parties). I agree that having a larger than normal inexact type around does seem like a useful thing (I could imagine even just using it for double checking the accuracy of an algorithm). Datetimes are a great reason, but also one of those complex beasts that, to me, seem better started outside NumPy. If someone wants to start these projects, I would be happy to put them on the NumPy organization but outside the NumPy code base. For DTypes there is the annoyance of private API being much more powerful and less wrinkly, I admit, hopefully that problem will be gone in a few months. |
@seberg I am doing a lot of slow work to allow you to externally write a quadruple precision that integrates seamlessly. Right now, that is not finished and the parts that are, are not public API yet, but it should fall in places for 1.21 far enough that one can start to use it (experimentally) I did a quick look at what the developers of https://github.com/moble/quaternion have done, but at a first glance I didn't get the idea how to extend dtypes with quadrupole-precision. Are there tutorials how to extend numpy with new datatypes? |
No, numpy-dispatch is on the array object level. The idea of that is that you can write code that supports both NumPy and e.g. Jax, etc. arrays. What I am talking about here is the work related to: https://numpy.org/neps/nep-0041-improved-dtype-support.html which will allow to write dtypes outside of NumPy with much fewer issues (also allowing much more complex dtypes than numpy can currently handle reasonably). There is fairly little information on how to write a dtype currently. And the API has to change pretty much completely. @WarrenWeckesser also has similar tries here: https://github.com/WarrenWeckesser/numtypes which may give a better overview and more explanation. |
Great, https://github.com/WarrenWeckesser/numtypes has a link to https://github.com/stefanv/teaching, which includes an example for extending with quad types. |
When I read Hierarchy and abstract classes from NEP 42, I can read out the following. A quadrupole-precision type should be implementing something like this in future:
@seberg with future numpy-1.21 or higher is what I assume is meant? |
@carlkl I hope 1.21 is what it means, I would have hoped to be closer to having it done, but thats life. There doesn't need to be an abstract type, Float128 can be a subclass of an abstract Floating, but it doesn't necessarily matter too much to begin with, and those abstract dtypes don't even exist yet. |
@carlkl I had an attempt at adding quad precision floats in https://github.com/aragilar/numpy/ (there's a number of branches as I kept needing to rebase the changes, I don't remember which was the latest one). The issue I ran into was the lack of a quad precision implementation that wasn't LGPL (or didn't depend on a LGPL library along the line - there are some very basic quad precision implementations around which have just the data type plus the basic operation—no exp/sin/cos etc.), if SLEEF solves that aspect, then you may be able to reuse some of the dtype logic I wrote in one of those branches (or use as a guide, depending on how much the numpy internals have changed). |
Was there any progress on quad precision support for numpy? |
About one year age or so @shibatch ceased the development of sleef. Whether this is still valid I do not know. I haven't seen any progress since then. Sleef itself works without major bugs and is used in some libraries today. However, can we rely on a library that is unmaintained? I guess it will be really hard to maintain the sleef code without his help, but that should not stop anyone from judging's for themselves. There were also other concerns about using sleef for development of numpy:
There are some counter arguments as well:
All in all, I decided for myself not to pursue this development any further. BTW: there is now a PR ENH: Add support SLEEF for transcendental functions #23068 see also https://github.com/yamadafuyuka/numpy/tree/add_SLEEF not related to quad precision but related to the integration of the vectorized single/double sleef functions. |
If there is a good basis, starting a project in https://github.com/numpy/numpy-user-dtypes might be cool. That includes an example for a DType start that wraps MPF for multi-precision floats. (I wouldn't consider it usable and obviously this is all using as-of-now unstable/experimental API). |
Hello,
I am currently concentrating on working for another project, and I have
not enough time and resource for actively developing SLEEF. But if it is
expected that there is going to be some major development in SLEEF
adoption to numpy, I can resume some of maintenance work of SLEEF.
Naoki Shibata
…On 4/1/2023 1:05 AM, carlkl wrote:
About one year age or so @shibatch <https://github.com/shibatch>
ceased the development of sleef. Whether this is still valid I do not
know. I haven't seen any progress since then. Sleef itself works
without major bugs and is used in some libraries today. However, can
we rely on a library that is unmaintained? I /guess/ it will be really
hard to maintain the sleef code without his help, but that should not
stop anyone from judging's for themselves.
There were also other concerns about using sleef for development of numpy:
* slow performance for quad precision calculations. double-double is
somewhat less precise but much faster.
* sleef doesn't give perfectly correctly-rounded results for
quadmath arithmetik functions.
There are some counter arguments as well:
* unclear double-double licence of D. Bailey's implementation.
* no perfectly correctly-rounded results for quadmath arithmetik
functions: but does it really matter for qud-precision?
* there is always a trade off between precision and performance.
All in all, I decided for myself not to pursue this development any
further.
BTW: there is now a PR ENH: Add support SLEEF for transcendental
functions #23068 <#23068> see
also https://github.com/yamadafuyuka/numpy/tree/add_SLEEF not related
to quad precision but related to the integration of the vectorized
single/double sleef functions.
—
Reply to this email directly, view it on GitHub
<#14574 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD2A7F4QMBHCM7F2JYJJR3TW636EFANCNFSM4IZBW66Q>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@shibatch, this sounds really promising! I won't have time for such a quadmath-numpy project in the next two months either. After that, let's see. And maybe @yamadafuyuka could comment on the topic sleef-for-numpy too. |
This is only if the numpy community is going to move toward adoption of
SLEEF. Or is it you who will do the work for adoption? If that's the
case, it would be simple.
My concern is that I don't have a good idea for making the arithmetic
operations perfect 0.5-ulp precision. That would require quad-double
computation, which is very slow, or using integer arithmetic internally.
I think there is no advantage in using SIMD instructions in these cases.
Will the numpy community adopt SLEEF with the current 0.5+alpha
precision arithmetic operators?
…On 4/3/2023 2:10 AM, carlkl wrote:
@shibatch <https://github.com/shibatch>, this sounds really promising!
I won't have time for such a quadmath-numpy project in the next two
months either. After that, let's see. And maybe @yamadafuyuka
<https://github.com/yamadafuyuka> could comment on the topic
sleef-for-numpy too.
—
Reply to this email directly, view it on GitHub
<#14574 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD2A7F4RXZDHSDTK236W34DW7GXJFANCNFSM4IZBW66Q>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@shibatch, how big can |
The error bound is 0.5000000001 ULP, as stated in the sleef web page.
https://sleef.org/quad.xhtml
This comes from the accuracy of internal triple-double computation.
…On 4/4/2023 7:59 PM, carlkl wrote:
Will the numpy community adopt SLEEF with the current 0.5+alpha
precision arithmetic operators?
@shibatch <https://github.com/shibatch>, how big can |alpha| become in
sleef's quadmath elementary arithmetic operations?
—
Reply to this email directly, view it on GitHub
<#14574 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD2A7F7GHW3VWDZVMXGEBHDW7P5KNANCNFSM4IZBW66Q>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@shibatch, thank you for this information. According to the documentation sleef's quadmath has 0.5000000001 ULP for elementary arithmetic and 1.0 ULP for trigonometric functions. Does this apply to the entire range? I'm curius because for some double/singe precision sleef trigonometric functions ranges are given in the documenation. |
All apply to the entire range. Functions other than the four basic
arithmetic functions are still more accurate than GNU's libquadmath.
…On 4/4/2023 8:19 PM, carlkl wrote:
@shibatch <https://github.com/shibatch>, thank you for this
information. According to the documentation sleef's quadmath has
0.5000000001 ULP for elementary arithmetic and 1.0 ULP for
trigonometric functions.
Does this apply to the entire range? I'm curius because for some
double/singe precision sleef trigonometric functions ranges are given
in the documenation.
—
Reply to this email directly, view it on GitHub
<#14574 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD2A7F3ATK6JWZFNDL44RCLW7P7SXANCNFSM4IZBW66Q>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@shibatch Sounds excellent. |
Yes, this is excellent. And it is hard to believe, that sleefs quadmath I would really like to see sleef as well sleef quadmath in Paul Zimmmermanns accuracy paper: |
Just curious: as numpy/numpy-user-dtypes#98 is merged - is there a reason to not close this issue? |
Intrigued by the comment above, I tried the That said, hopefully that is just a relatively simple glitch and it will provide real support! Anyway, I think closing this issue would require having a working package and a link to it from the numpy docs. |
I believe using tlfloat is much easier. |
Is the proposal to have a true 128 bit float type in numpy? If so that is a great idea. Can anyone explain the status of QuadPrecDtype or if the general idea is still going forwards? |
Yes
@lesshaste, this is a question for @SwayamInSync. However His suggestion to use tlfloat in favour of sleef is worth considering. |
So I think the status is still as it was: Improvements to or forks of the quad-dtype in But, I would still not aim for a true float128 in NumPy. Because that comes with a lot of baggage and things to consider. It might grow into that, but living in NumPy raises expectations and compatibility concerns that are possibly not quick to deal with. EDIT: And I'll note that I actually like to have prominent dtypes defined outside of NumPy ( |
Solving compatibility issues is one of the goals of tlfloat's development, though. |
@shibatch - is there a specific reason you recommend using |
My understanding is that the current implementation on the Numpy side is not vectorized. |
OK, thanks, that makes sense! I certainly think that having more functions is more important than vectorized speed, at least initially! |
We do have a initial experimental dtype here numpy-user-dtypes/quaddtype (idea is to have a true 128 bit dtype with backward compatibility of long double) It supports major operations and integrating well with NumPy. But there is still lot of room for improvement and expansion Agreeing to @shibatch and we mention this in the quaddtype report as well, during development, we experimented with TLFloat as a potential SLEEF replacement due to its IEEE 754 compliance, correctly rounded arithmetic, and 1-ULP accuracy for math functions. The current major plans regarding quaddtype is to integrate it with long double tests and keep iterating to make it a good long double replacement |
Reproducing code example:
Output:
The last one seems inappropriately inexact, more what you would expect from a
float80
rather than afloat128
. (Yes, theexp
then operates oncomples256
but all variants I tried get the same accuracy.) Is the quad precision internally just using the Intel built-in 80 bit floats rather than real 128 bit arithmetics? If so, why waste the space and not provide afloat80
data type? Alignment issues would not be a lot worse than for float16, which is provided.Maybe, since I cannot find a reference to it at https://docs.scipy.org/doc/numpy/user/basics.types.html it is only experimental at this time?
It would be good if the documentation could elaborate on precision of
float128
/complex256
I think libraries should be available to do 128 bit precision arithmetics where not supported by hardware, to have most transparent and as expected behaviour for users, even if slow.
Numpy/Python version information:
numpy: 1.17.2
python: 3.7.4
The text was updated successfully, but these errors were encountered: