Skip to content

Keep uniform floats from rand! in [0, 1) - #872

Merged
maleadt merged 2 commits into
mainfrom
tb/rand-closeopen
Dec 2, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/rand-closeopen

Conversation

@maleadt

@maleadt maleadt commented Nov 27, 2026

Copy link
Copy Markdown
Member

rand! with a GPUArrays.RNG can return exactly 1.0, although Julia's Random promises [0, 1) for floats. The batched kernels convert Philox output with Random123's u01, which maps to (0, 1] and rounds up for words at the top of the range. The ones are frequent enough to matter for Float16:

julia> rng = GPUArrays.RNG{CuArray}(1234);

julia> count(==(1), rand!(rng, CuArray{Float16}(undef, 2^26)))
16304       # about one in 4000 (0 with this PR); Float32 gives about one in 2^25, Float64 one in 2^54

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 keeps u01, 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 u01 to prevfloat(1) would also work but piles extra probability onto that one value. A truncating conversion that keeps u01's resolution near zero is possible, but it is more code and needs care for Float16 subnormals.

Doing the Float16 conversion in Float16 also makes Float16 rand! faster on CUDA: 895 instead of 810 GB/s on an RTX 5080, now close to fill!. 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 and typemax, and check that a Float16/ComplexF16 rand! 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).

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.
@maleadt
maleadt merged commit 47a90b5 into main Dec 2, 2026
9 checks passed
@maleadt
maleadt deleted the tb/rand-closeopen branch December 2, 2026 19:19
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant