Make PassthroughRNG dispatch survive overlay method-table shadowing#72
Merged
ChrisRackauckas merged 1 commit intoMay 11, 2026
Conversation
PassthroughRNG previously defined only the three no-second-arg methods (rand, randexp, randn). On Julia 1.12+, GPU back ends like CUDA.jl install device-side overlay tables via Base.Experimental.@consistent_overlay. Julia's OverlayMethodTable.findall returns overlay matches *without consulting the base method table* whenever the overlay fully covers the signature, so an overlay method like CUDA.jl's `@device_override Random.randexp(rng::AbstractRNG)` shadows our specific `Random.randexp(::PassthroughRNG)` on the device. The override's body then runs with rng::PassthroughRNG and calls `Random.rand(rng, UInt52Raw())`. The stdlib Sampler chain for that bottoms out at `_rand52(r, rng_native_52(r)) → rand(r, UInt64)`; PassthroughRNG had no `rng_native_52` and no typed-arg rand, so the chain statically reached `throw(MethodError, ...)`, which GPUCompiler refuses to lower (see SciML/JumpProcesses.jl#588 for the original repro). Add minimal forwarding methods so the chain still reaches bare rand(T): Random.rng_native_52(::PassthroughRNG) = UInt64 Random.rand(rng::PassthroughRNG, ::Type{T}) where {T} = rand(T) These keep PassthroughRNG's "use whatever default_rng() returns here" semantics — bare rand(T) goes through default_rng(), which GPU back ends device-override to their device RNG (Philox2x32 in CUDA.jl). Verified on Julia 1.12.6 that rand(PassthroughRNG(), UInt52Raw()), rand(PassthroughRNG(), UInt64), etc. all resolve cleanly after this change. Bump to 0.4.8. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.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.
Closes the root cause behind SciML/JumpProcesses.jl#588.
Summary
PassthroughRNGpreviously defined only the three no-second-arg methods (rand,randexp,randn). On Julia 1.12+, GPU back ends like CUDA.jl install device-side overlay tables viaBase.Experimental.@consistent_overlay. Julia'sOverlayMethodTable.findall(Compiler/src/methodtable.jl:73–92) returns overlay matches without consulting the base method table whenever the overlay fully covers the signature:CUDA.jl's
@device_override Random.randexp(rng::AbstractRNG)therefore shadows our specificRandom.randexp(::PassthroughRNG)on the device — the more specific base-table method is never seen by inference. The override's body then runs withrng::PassthroughRNGand callsRandom.rand(rng, UInt52Raw()). The stdlib Sampler chain for that bottoms out at_rand52(r, rng_native_52(r)) → rand(r, UInt64);PassthroughRNGhad norng_native_52and no typed-argrand, so the chain statically reachedthrow(MethodError, ...)and GPUCompiler refused to lower the kernel:Confirmed on Julia 1.12.6 by directly calling
Random.rand(PassthroughRNG(), Random.UInt52Raw())on the CPU — the top two frames match the GPUInvalidIRErrorframes exactly.Fix
Two forwarding methods so the Sampler chain bottoms out at bare
rand(T):These preserve
PassthroughRNG's ''use whateverdefault_rng()returns here'' semantics — barerand(T)goes throughdefault_rng(), which GPU back ends device-override to their device RNG (Philox2x32in CUDA.jl). Verified locally on Julia 1.12.6:Added a regression test (
@testset "PassthroughRNG dispatch") covering each of these calls. Bumped version 0.4.7 → 0.4.8.Test plan
Pkg.test()green on Julia 1.12.6 — Aqua (10/10), JET static analysis (6/6), ExplicitImports (2/2), BigFloat (401/401), PassthroughRNG dispatch (5/5), Allocation Tests (10/10), and the count/ad/mixed statistical samplers.Notes
PassthroughRNG <: RandomNumbers.AbstractRNG{UInt64}and inherit therng_native_52fallback — heavier and changes the package's dep graph).@noinline randexp_unlikelyin the devicerandexpoverride (line 345) triggersjulia.get_pgcstackon Julia 1.12 — that needs an upstream fix in CUDA.jl and is not in scope here.🤖 Generated with Claude Code