data-apis / array-api

RFC document, tooling and other content related to the array API standard
https://data-apis.github.io/array-api/latest/
MIT License
211 stars 44 forks source link

Specify correct rounding for sqrt #826

Open hpkfft opened 1 month ago

hpkfft commented 1 month ago

In accuracy.rst, correct rounding is required for add, sub, mul, and divide. I propose adding sqrt to this list. Note that sqrt is not listed later among the mathematical functions whose accuracy is not precisely defined.

Note that the IEEE Std 754-2019 for Floating-Point Arithmetic mandates correct rounding for squareRoot.

asmeurer commented 1 month ago

Is this something that's actually the case for existing libraries/devices? If so, there shouldn't be an issue with adding it.

By the way, is your proposal for this to apply to both real and complex inputs or just to real inputs?

hpkfft commented 1 month ago

Excellent point! My proposal is for this to apply just to real inputs. Given industry adoption of IEEE Std 754-2019, correct rounding is actually the case for existing libraries/devices. Sometimes there is an option for less accuracy (and presumably higher performance). For example, CUDA in single precision requires compiling with -prec-sqrt=true, and division requires -prec-div=true, for correct rounding. Since this document requires correctly rounded division, it equally well ought to require correctly rounded square root. [Double precision CUDA divide and square root are always correctly rounded. See the "Mathematical Functions" section of the "CUDA C++ Programming Guide".]

hpkfft commented 1 month ago

I believe the existing accuracy requirements for element-wise arithmetic operations are only intended to apply to real inputs. For example, the usual 6 flop implementation of complex multiplication

(a + bi)(c + di) = (ac - bd) + (ad + bc)i

does not result in correctly rounded real and imaginary components. I do not believe this array standard means to prohibit this implementation, but this should be clarified.