by camel-cdr · 3y ago · 0 comments
I've written such an implementation with explanations [0], but it doesn't handle subnormals correctly. I've yet to find the time to revisit the problem.
[0] https://github.com/camel-cdr/cauldron/blob/main/cauldron/ran...
The probability that your "perfect" code would ever get triggered is such that humankind has not built enough compute to expect to get that unlucky yet.
(you may ask: why then care about that possibility if it's dead code anyway? Because of security vulns in context of flawed RNGs. Bad underlying RNG leading to bad distributions is expected; bad underlying RNG leading to RCE is not OK. So don't ever output zero or subnormals!)
For bfloat16 the same holds; for ieee float16, well, I have no clue what we want.
You are aware that the current age of the universe is < 10^27 nanoseconds, just for comparison?
Uniform as in all representable numbers have equal probability? Or uniform as in approximating a uniform distribution over real numbers?
It's been awhile since I've thought about this problem, so I'll reserve the right to be off by a small constant additive or multiplicative factor. You need around 24+256 bits for 32-bit IEEE-754 floats (less by roughly half if you're sticking in a range like 0 to 1), and 53+2048 for 64-bit. The average number of bits required is something closer to 25 and 54, respectively.
IIRC I also explained it here: https://m.youtube.com/watch?v=VHJUlRiRDCY&t=2774s
I experienced this where a colleague had tried to reduce the number of divisions by rewriting (a/x)*(b/y)*(c/z) to (a*b*c)/(x*y*z).
The problem was that in certain circumstances the terms were all small, but of comparable magnitude. Thus the latter lead to subnormal divided by subnormal. The fix was to undo the optimization and do the divisions first, which lead to the multiplication of three numbers of order 1.