Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/PoissonRandom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions test/float32_tests.jl
Original file line number Diff line number Diff line change
@@ -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
Loading