From a2c7a363b80799cf61cbdbce5cb31326d4ea2653 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 27 Nov 2026 14:00:33 +0100 Subject: [PATCH 1/2] Keep uniform floats from rand! 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. --- src/host/random.jl | 35 ++++++++++++++++++++++------------- test/testsuite/random.jl | 17 ++++++++++++++++- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/host/random.jl b/src/host/random.jl index 12d1f8fa5..4c3d6b61d 100644 --- a/src/host/random.jl +++ b/src/host/random.jl @@ -45,8 +45,16 @@ end philox4x32_10((ctr0%UInt32, (ctr0>>32)%UInt32, ctr1%UInt32, (ctr1>>32)%UInt32), (key%UInt32, (key>>32)%UInt32)) end -## Float conversions: unsigned integer → uniform float, strictly positive -## (can round up to exactly 1.0) +## Float conversions + +# `rand` must return values in [0, 1), so it uses the same fixed grids as Random's Xoshiro: +# 11, 24 and 53 random bits, each output equally likely, never 1.0. +@inline closeopen01(::Type{Float16}, u::UInt32) = Float16(u >> 21) * Float16(0x1.0p-11) +@inline closeopen01(::Type{Float32}, u::UInt32) = Float32(u >> 8) * Float32(0x1.0p-24) +@inline closeopen01(::Type{Float64}, u::UInt64) = Float64(u >> 11) * 0x1.0p-53 + +# Box-Muller needs a strictly positive input for its logarithm, and benefits from the finer +# resolution near zero, so it uses Random123's conversion. It can round up to exactly 1.0. """ u01(F, u::Union{UInt32, UInt64})::F @@ -334,17 +342,18 @@ end # Convert Philox UInt32 outputs to N values of type T @inline philox_to_vals(::Type{Float16}, a1, a2, a3, a4) = - (Float16(u01(Float32, a1)), Float16(u01(Float32, a2)), - Float16(u01(Float32, a3)), Float16(u01(Float32, a4))) + (closeopen01(Float16, a1), closeopen01(Float16, a2), + closeopen01(Float16, a3), closeopen01(Float16, a4)) @inline philox_to_vals(::Type{Float32}, a1, a2, a3, a4) = - (u01(Float32, a1), u01(Float32, a2), u01(Float32, a3), u01(Float32, a4)) + (closeopen01(Float32, a1), closeopen01(Float32, a2), + closeopen01(Float32, a3), closeopen01(Float32, a4)) for T in (UInt8, UInt16, UInt32, Int8, Int16, Int32, Bool) @eval @inline philox_to_vals(::Type{$T}, a1, a2, a3, a4) = (a1 % $T, a2 % $T, a3 % $T, a4 % $T) end @inline philox_to_vals(::Type{Float64}, a1, a2, a3, a4) = - (u01(Float64, UInt64(a1) | UInt64(a2) << 32), - u01(Float64, UInt64(a3) | UInt64(a4) << 32)) + (closeopen01(Float64, UInt64(a1) | UInt64(a2) << 32), + closeopen01(Float64, UInt64(a3) | UInt64(a4) << 32)) for T in (UInt64, Int64) @eval @inline philox_to_vals(::Type{$T}, a1, a2, a3, a4) = ((UInt64(a1) | UInt64(a2) << 32) % $T, @@ -355,14 +364,14 @@ for T in (UInt128, Int128) ((UInt128(a1) | UInt128(a2) << 32 | UInt128(a3) << 64 | UInt128(a4) << 96) % $T,) end @inline philox_to_vals(::Type{Complex{Float16}}, a1, a2, a3, a4) = - (complex(Float16(u01(Float32, a1)), Float16(u01(Float32, a2))), - complex(Float16(u01(Float32, a3)), Float16(u01(Float32, a4)))) + (complex(closeopen01(Float16, a1), closeopen01(Float16, a2)), + complex(closeopen01(Float16, a3), closeopen01(Float16, a4))) @inline philox_to_vals(::Type{Complex{Float32}}, a1, a2, a3, a4) = - (complex(u01(Float32, a1), u01(Float32, a2)), - complex(u01(Float32, a3), u01(Float32, a4))) + (complex(closeopen01(Float32, a1), closeopen01(Float32, a2)), + complex(closeopen01(Float32, a3), closeopen01(Float32, a4))) @inline philox_to_vals(::Type{Complex{Float64}}, a1, a2, a3, a4) = - (complex(u01(Float64, UInt64(a1) | UInt64(a2) << 32), - u01(Float64, UInt64(a3) | UInt64(a4) << 32)),) + (complex(closeopen01(Float64, UInt64(a1) | UInt64(a2) << 32), + closeopen01(Float64, UInt64(a3) | UInt64(a4) << 32)),) # Number of output values per Philox4x32 call vals_per_call(::Type{T}) where T = sizeof(T) <= 4 ? 4 : sizeof(T) <= 8 ? 2 : 1 diff --git a/test/testsuite/random.jl b/test/testsuite/random.jl index 2d9517251..0bde4bea9 100644 --- a/test/testsuite/random.jl +++ b/test/testsuite/random.jl @@ -25,6 +25,21 @@ Random.rand(rng::AbstractRNG, ::Random.SamplerType{RGBTriplet}) = @test Array(A) != Array(B) end + # uniform floats are in [0, 1). Raw words at the top of the range used to round up + # to 1.0: about one in 4000 Float16 draws, and one in 2^25 Float32 draws. + in01(x::Real) = 0 <= x < 1 + in01(x::Complex) = in01(real(x)) && in01(imag(x)) + for T in (Float16, Float32, Float64, ComplexF16, ComplexF32, ComplexF64) + for words in (ntuple(_ -> zero(UInt32), 4), ntuple(_ -> typemax(UInt32), 4), + (0xffefff7f, 0xffefff80, 0xffffff7f, 0xffffff80), + (0xfffffbff, 0xffffffff, 0xfffffc00, 0xffffffff)) + @test all(in01, GPUArrays.philox_to_vals(T, words...)) + end + end + for T in filter(t -> t in (Float16, ComplexF16), eltypes) + @test all(in01, Array(rand!(rng, AT{T}(undef, 2^20)))) + end + # empty arrays B = AT{Float32}(undef, 0) rand!(rng, B) @@ -147,7 +162,7 @@ Random.rand(rng::AbstractRNG, ::Random.SamplerType{RGBTriplet}) = A = AT{RGBTriplet}(undef, 1024) rand!(rng, A) h = Array(A) - @test all(t -> 0f0 <= t.r <= 1f0 && 0f0 <= t.g <= 1f0 && 0f0 <= t.b <= 1f0, h) + @test all(t -> 0f0 <= t.r < 1f0 && 0f0 <= t.g < 1f0 && 0f0 <= t.b < 1f0, h) @test any(t -> t.r != h[1].r, h) end From 6d505d75f929b742e63afa1b745acff0634f60ff Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 27 Nov 2026 14:00:33 +0100 Subject: [PATCH 2/2] Bump version. --- Project.toml | 2 +- test/testsuite/random.jl | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 5ff4db0ae..9ffa184a5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "GPUArrays" uuid = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7" -version = "12.3.0" +version = "12.3.1" [workspace] projects = ["lib/GPUArraysCore", "lib/JLArrays", "test", "docs"] diff --git a/test/testsuite/random.jl b/test/testsuite/random.jl index 0bde4bea9..bac886c08 100644 --- a/test/testsuite/random.jl +++ b/test/testsuite/random.jl @@ -35,9 +35,15 @@ Random.rand(rng::AbstractRNG, ::Random.SamplerType{RGBTriplet}) = (0xfffffbff, 0xffffffff, 0xfffffc00, 0xffffffff)) @test all(in01, GPUArrays.philox_to_vals(T, words...)) end + top = prevfloat(one(real(T))) + top = T <: Complex ? T(top, top) : top + @test all(==(zero(T)), GPUArrays.philox_to_vals(T, ntuple(_ -> zero(UInt32), 4)...)) + @test all(==(top), GPUArrays.philox_to_vals(T, ntuple(_ -> typemax(UInt32), 4)...)) end - for T in filter(t -> t in (Float16, ComplexF16), eltypes) - @test all(in01, Array(rand!(rng, AT{T}(undef, 2^20)))) + if AT <: AbstractGPUArray + for T in filter(t -> t in (Float16, ComplexF16), eltypes) + @test all(in01, Array(rand!(rng, AT{T}(undef, 2^20)))) + end end # empty arrays