Repository navigation
Keep uniform floats from rand! in [0, 1) - #872
Merged
Merged
Conversation
The batched rand! kernels converted Philox output with Random123's u01, which maps to (0, 1] and rounds up to exactly 1.0 for words at the top of the range: about once every 4000 Float16 draws and every 2^25 Float32 draws. Random's contract for rand(::AbstractFloat) is [0, 1). Use the fixed grids of Random's Xoshiro instead (11, 24 and 53 random bits), for real and complex eltypes. This changes the values of seeded uniform float streams. u01 stays for Box-Muller, whose logarithm needs a strictly positive input. Doing the Float16 conversion in Float16 also makes Float16 rand! about 10% faster on CUDA. fjbarter's JuliaGPU/AcceleratedKernels.jl#80, whose conversion already stays in [0, 1), brought this to light.
christiangnrd
added a commit
to JuliaGPU/Metal.jl
that referenced
this pull request
Dec 3, 2026
GPUArrays 12.4.0 keeps uniform floats in [0, 1) by truncating the Philox output to 24 bits instead of rounding it (JuliaGPU/GPUArrays.jl#872). That lowers two of the four values in this doctest by one ulp, which fails the documentation build. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rand!with aGPUArrays.RNGcan return exactly1.0, although Julia'sRandompromises[0, 1)for floats. The batched kernels convert Philox output with Random123'su01, which maps to(0, 1]and rounds up for words at the top of the range. The ones are frequent enough to matter forFloat16:This PR uses the fixed grids of Random's Xoshiro instead (11, 24 and 53 random bits, every output equally likely, never
1.0), for the real and complex float eltypes. Box-Muller keepsu01, because its logarithm needs a strictly positive input and benefits from the finer resolution near zero. Seeded uniform float streams change, so this bumps the patch version.Clamping
u01toprevfloat(1)would also work but piles extra probability onto that one value. A truncating conversion that keepsu01's resolution near zero is possible, but it is more code and needs care forFloat16subnormals.Doing the
Float16conversion inFloat16also makesFloat16rand!faster on CUDA: 895 instead of 810 GB/s on an RTX 5080, now close tofill!. Other eltypes on CUDA, and all tested eltypes on Metal (M1) and oneAPI (Arc A770), are unchanged. The new tests feed the converters the words on either side of each old rounding threshold, plus zero andtypemax, and check that aFloat16/ComplexF16rand!of 2^20 elements stays in[0, 1). They fail on main and pass with this PR on JLArray, CUDA, Metal and oneAPI.The problem turned up while reviewing JuliaGPU/AcceleratedKernels.jl#80 by @fjbarter, whose conversion already stays in
[0, 1).