Repository navigation
Handle sNaNs in {min,max}_precise on Neon - #441
RunDevelopment wants to merge 3 commits into
Conversation
|
There is a much more significant problem with these methods:
It's a much bigger issue for determinism in practice than the theoretical possibility of signalling NaNs. If you want fully IEEE compliant min/max that deals with all kinds of NaN and also signed zero, you need a much slower formulation. People have workshopped variants of it in this thread: https://news.ycombinator.com/item?id=49800085 I don't want to slow down the _precise variant for everyone because of signalling NaNs, but I am open to adding a min_ieee or some such that is fully deterministic at the cost of speed. |
|
My goal was to make them behave the same as the standard library by getting rid of the NaN caveat. Since the standard library doesn't care about non-deterministic +/-0, neither do I. IMO, implementing IEEE 754's
Plus, the compiler can optimize away |
|
@LaurenzV could you check if this impacts important benchmarks in Vello? |
|
On that note: LLVM seems to think that Is this a miscompilation or am I doing something wrong? Does LLVM treat all NaNs as equivalent? Or does LLVM know something that I don't. Like making additional assumptions based on the target |
|
Ah, this reminds me: Rust and LLVM do not preserve NaN bit patterns in arithmetic operations. I ran into this with miri previously, where it produced divergent bit patterns: rust-lang/miri#4995 std docs: https://doc.rust-lang.org/nightly/std/primitive.f32.html#nan-bit-patterns I wouldn't be surprised if LLVM just looks at that op, decides that it only changes the NaN bit pattern, and discards it because the optimizer doesn't preserve NaN bit patterns. |
|
Yikes. So my fix doesn't even work... Is there even a good way to fix this then? I just need one fast sNaN -> qNaN conversion that LLVM doesn't optimize away. But even if we find one, would the optimizer still leave it in the next Rust version...? I think |
|
Have you checked if signalling NaN inputs are even a problem in practice? I think you need to load the values at runtime to check too, because LLVM constant propagation most likely won't match hardware semantics for NaN bit patterns. |
|
I don't have ARM hardware, so it's hard to test. In my use case, I get a I just asked Google Gemini and it suggested inline assembly (among a bunch of nonsense). Basically, have the sNaN -> qNaN conversion in a helper function implemented using |
|
I'm afraid you'll need to turn NaNs into zeroes explicitly, if you want to be really sure, followed by I've looked into Or you could rent an ARM server from a cloud for 30 minutes for like $1 to experiment and see if this is a problem in practice. You could even test all bit patterns exhaustively, if it's an f32. |
I don't really understand what you're talking about here. Current
Unless that ARM CPU doesn't do what the ARM documentation says it should, then I don't really see the point in confirming what I already know from looking at the assembly 😐 Or did I misunderstand you? |
|
@Shnatsel So should I still benchmark anything? |
|
No, this gets eliminated by LLVM anyway. Sorry! |
Motivation
The
_precisefamily of methods is supposed to return platform-independent precise results at the cost of speed. So it was very surprising to me when I read thatmax_precisedidn't handle NaNs correctly. To quote the docs:In my case, the numbers I get are user input (they could be anything) so I can't just assume sNaNs aren't included. Plus, no other operation has this caveat around sNaNs and neither does the standard library with
fN::max.Changes
This PR removes the caveat around sNaN by bringing Neon in line. All other platforms already handled NaNs (signally or not) like the standard library.
To bring Neon in line, I'm using the same trick as the standard library.
vmaxnmq_f32is the element-wise max operation, and it also has the property of never returning sNaNs. Sovmaxnmq_f32(x, x)will return a quiet NaN if x is any NaN value (signalling or not) and the numeric value of x otherwise. With this trick, sNaN disappears.Example:
max_precise(a, b)is implemented like this:For consistency, I also adjusted
reduce_{max,min}_preciseto handle sNaN correctly using the same trick. However, the standard reduction from larger-than-native vectors would have resulted in a few unnecessary sNaN -> qNaN conversions. To avoid them, I also implemented a custom reduce logic to skip the unnecessary conversions.