Skip to content

Fix rand!/randn! dispatch for non-Number array eltypes#743

Merged
maleadt merged 3 commits into
mainfrom
tb/rng_eltype
Jun 23, 2026
Merged

Fix rand!/randn! dispatch for non-Number array eltypes#743
maleadt merged 3 commits into
mainfrom
tb/rng_eltype

Conversation

@maleadt

@maleadt maleadt commented Jun 23, 2026

Copy link
Copy Markdown
Member

rand!(::RNG, ::AnyGPUArray{T}) was constrained to T<:Number, so isbits
eltypes with a custom sampler (e.g. ColorTypes colorants) fell through to an
ambiguity between the host-array and CPU-RNG fallbacks. Dropping the bound lets
them generate element-wise on-device, matching the unconstrained rand! in the
Random stdlib.

While auditing, found a sibling bug: the host-array randn! fallback was
unconstrained, so a GPU RNG + non-float eltype matched itself recursively and
stack-overflowed. Constrained it to floats like the other randn! methods, so
non-float eltypes now raise a clean MethodError.

Both paths covered by new tests in the random testsuite.

Fixes JuliaGPU/CUDA.jl#3179.

maleadt and others added 3 commits June 23, 2026 07:52
Drop the `<:Number` bound on the generic device `rand!` so isbits eltypes
with a custom sampler (e.g. ColorTypes colorants) generate on-device instead
of falling into the ambiguous host-array/CPU-RNG fallbacks. Matches the
unconstrained `rand!` in the Random stdlib. Fixes JuliaGPU/CUDA.jl#3179.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The host-array `randn!` fallback was unconstrained, so for a GPU RNG and a
non-float eltype it matched itself recursively (`similar(AT{T})` is again a
GPU array) and stack-overflowed. Constrain it to floats, like the sibling
`randn!` methods, so non-float eltypes raise a clean MethodError instead.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@maleadt maleadt merged commit 57e1f4d into main Jun 23, 2026
17 of 18 checks passed
@maleadt maleadt deleted the tb/rng_eltype branch June 23, 2026 09:54
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.

Error on CUDA.rand(RGB{Float32}, ...) in CUDA.jl 6.1 and 6.2

1 participant