From a77318bb8460e97d590b56a196e46dbd67d25449 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Mon, 12 Oct 2026 17:02:33 -0400 Subject: [PATCH] Make procf type-stable for Float32 rates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Float64 constants in procf promoted only part of its return tuple for Float32 rates, yielding Tuple{Union{Float32, Float64}, ...}. Destructuring that Union-typed tuple in ad_rand forces a dynamic call, which fails to compile in GPU kernels (InvalidIRError on ijl_get_nth_field_checked). Compute procf in T = float(promote_type(typeof(λ), typeof(s))) so all four returned values share one concrete type. Float64 results are bit-for-bit identical to the previous formula (verified over λ ∈ 6.0:0.37:200.0, K ∈ floor(λ) ± 30); Float32 and BigFloat now infer NTuple{4,T} instead of the union or a Float64-promoted mix. Regression test: test/float32_tests.jl fails on the old code (3 fail, 2 error) and passes with the fix; also scans ad_rand's typed IR for Union-typed values, which is the mechanism that broke kernel codegen. Closes #102. Co-Authored-By: Chris Rackauckas Co-Authored-By: Devin Agent-Harness: Devin CLI 3000.11.3 Agent-Model: swe-2-high Agent-Session: local job 102-devin (transcript kept on the agent host) --- src/PoissonRandom.jl | 24 ++++++++++++++---------- test/float32_tests.jl | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 test/float32_tests.jl diff --git a/src/PoissonRandom.jl b/src/PoissonRandom.jl index 9a449c0..250de5c 100644 --- a/src/PoissonRandom.jl +++ b/src/PoissonRandom.jl @@ -112,27 +112,31 @@ end # Procedure F function procf(λ::Real, K::Int, s::Real) + # Compute in the floating-point type of λ: Float64 literals here would + # promote only part of the tuple for e.g. Float32 rates, and the resulting + # Union-typed return forces a dynamic call in GPU kernels. + T = float(promote_type(typeof(λ), typeof(s))) # can be pre-computed, but does not seem to affect performance - INV_SQRT_2PI = inv(sqrt(2pi)) + INV_SQRT_2PI = inv(sqrt(2 * T(pi))) ω = INV_SQRT_2PI / s - b1 = inv(24) / λ - b2 = 0.3 * b1 * b1 - c3 = inv(7) * b1 * b2 + b1 = inv(T(24)) / λ + b2 = T(0.3) * b1 * b1 + c3 = inv(T(7)) * b1 * b2 c2 = b2 - 15 * c3 c1 = b1 - 6 * b2 + 45 * c3 c0 = 1 - b1 + 3 * b2 - 15 * c3 if K < 10 - px = -float(λ) - py = λ^K / prod(2:K) + px = -T(λ) + py = T(λ)^K / prod(2:K) else - δ = inv(12) / K - δ -= 4.8 * δ^3 + δ = inv(T(12)) / K + δ -= T(4.8) * δ^3 V = (λ - K) / K px = K * log1pmx(V) - δ # avoids need for table - py = INV_SQRT_2PI / sqrt(K) + py = INV_SQRT_2PI / sqrt(T(K)) end - X = (K - λ + 0.5) / s + X = (K - λ + T(0.5)) / s X2 = X^2 fx = X2 / -2 # missing negation in pseudo-algorithm, but appears in fortran code. fy = ω * (((c3 * X2 + c2) * X2 + c1) * X2 + c0) diff --git a/test/float32_tests.jl b/test/float32_tests.jl new file mode 100644 index 0000000..5698522 --- /dev/null +++ b/test/float32_tests.jl @@ -0,0 +1,41 @@ +using PoissonRandom +using Random +using Statistics +using Test + +# procf used to return Tuple{Union{Float32, Float64}, ...} for Float32 rates +# because its Float64 constants promoted only part of the tuple. Destructuring +# that Union-typed tuple in ad_rand forces a dynamic call, which cannot compile +# in GPU kernels (https://github.com/SciML/PoissonRandom.jl/issues/102). +@testset "procf type stability" begin + for T in (Float32, Float64) + λ = T(15) + s = sqrt(λ) + @test only(Base.return_types(PoissonRandom.procf, (T, Int, T))) === NTuple{4, T} + @test @inferred(PoissonRandom.procf(λ, 5, s)) isa NTuple{4, T} + @test @inferred(PoissonRandom.procf(λ, 15, s)) isa NTuple{4, T} + end + + # No Union-typed value may appear in ad_rand for Float32 rates: each one is + # a dynamic call site in a GPU kernel. + ir = first( + Base.code_typed( + PoissonRandom.ad_rand, (Xoshiro, Float32); + optimize = false + ) + )[1] + @test !any(t -> t isa Union, ir.slottypes) + @test !any(t -> t isa Union, ir.ssavaluetypes) +end + +@testset "Float32 sampling" begin + rng = Xoshiro(1234) + λ = 15.0f0 + samples = [pois_rand(rng, λ) for _ in 1:10_000] + @test all(s -> s isa Integer && s >= 0, samples) + # Poisson(λ) has mean = var = λ; standard errors here are ≈0.04 and ≈0.22. + @test abs(mean(samples) - λ) < 0.5 + @test abs(var(samples) - λ) < 1.5 + + @test pois_rand(PassthroughRNG(), λ) isa Integer +end