Skip to content
Open
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
4 changes: 4 additions & 0 deletions GeneralisedFilters/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
- Compute only the triangular factor in square-root backward messages for larger static
Float32/Float64 matrices. This avoids forming Q and contended small-matrix BLAS calls,
speeding up CSMC ancestor sampling and backward simulation, especially with threads.
- Fix systematic and stratified resampling reading past the weights when rounding puts a
threshold beyond the final cumulative weight, as with Float32 weights; scans now stop at
the last positive-weight particle. Conditional variants on CPU and GPU now always keep the
reference, including one whose weight is below the precision of the cumulative weights.

## 0.5.0

Expand Down
10 changes: 8 additions & 2 deletions GeneralisedFilters/ext/CUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ function GeneralisedFilters.conditional_sample_ancestors(
rng::AbstractRNG, ::Systematic, weights::CuVector, ref_idx::Integer
)
offspring, K = sample_conditional_offspring(rng, Systematic(), weights, ref_idx)
return offspring_to_ancestors(offspring; shift=K - 1)
as = offspring_to_ancestors(offspring; shift=K - 1)
# Rounding can move the reference slot's threshold out of a narrow interval.
CUDA.@allowscalar as[1] = ref_idx
return as
end

# Following Code 8 of Murray et. al (2015)
Expand Down Expand Up @@ -153,7 +156,10 @@ function GeneralisedFilters.conditional_sample_ancestors(
rng::AbstractRNG, ::Stratified, weights::CuVector, ref_idx::Integer
)
offspring, K = sample_conditional_offspring(rng, Stratified(), weights, ref_idx)
return offspring_to_ancestors(offspring; shift=K - 1)
as = offspring_to_ancestors(offspring; shift=K - 1)
# Rounding can move the reference slot's threshold out of a narrow interval.
CUDA.@allowscalar as[1] = ref_idx
return as
end

# Following Code 7 of Murray et. al (2015)
Expand Down
33 changes: 23 additions & 10 deletions GeneralisedFilters/src/resamplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,21 @@ function conditional_sample_ancestors(
return a
end

# Rounding can place a threshold at or beyond the final scaled cumulative weight, so the
# inverse-CDF scans stop at the last particle that has positive weight.
function _last_positive(weights::AbstractVector{WT}) where {WT<:Real}
return findlast(>(zero(WT)), weights)
end

struct Systematic <: AbstractResampler end

function sample_ancestors(
rng::AbstractRNG, ::Systematic, weights::AbstractVector{WT}, n::Int64=length(weights)
) where {WT<:Real}
# pre-calculations
vs = cumsum(weights)
vs *= n
vs .*= n
idx_max = _last_positive(weights)

u0 = rand(rng, WT)

Expand All @@ -343,7 +350,7 @@ function sample_ancestors(

@inbounds for i in 1:n
u = u0 + (i - 1)
while vs[idx] <= u
while idx < idx_max && vs[idx] <= u
idx += 1
end
a[i] = idx
Expand All @@ -363,19 +370,23 @@ function conditional_sample_ancestors(
n = length(weights)

vs = cumsum(weights)
vs *= n
vs .*= n
idx_max = _last_positive(weights)

u0, K = _reference_offset(rng, vs, ref_idx, n)

# Slot `K`'s threshold lies in the reference's interval only in exact arithmetic, and
# rounding can step past an interval narrower than the spacing of `vs`, so the slot is
# assigned directly. This corrects rounding; the law of the other slots is unchanged.
a = Vector{Int64}(undef, n)
idx = 1

@inbounds for i in 1:n
u = u0 + (i - 1)
while vs[idx] <= u
while idx < idx_max && vs[idx] <= u
idx += 1
end
a[mod1(i - K + 1, n)] = idx
a[mod1(i - K + 1, n)] = i == K ? ref_idx : idx
end

return a
Expand All @@ -388,15 +399,16 @@ function sample_ancestors(
) where {WT<:Real}
# pre-calculations
vs = cumsum(weights)
vs *= n
vs .*= n
idx_max = _last_positive(weights)

# initialize sampling algorithm
a = Vector{Int64}(undef, n)
idx = 1

@inbounds for i in 1:n
u = rand(rng, WT) + (i - 1)
while vs[idx] <= u
while idx < idx_max && vs[idx] <= u
idx += 1
end
a[i] = idx
Expand All @@ -415,7 +427,8 @@ function conditional_sample_ancestors(
n = length(weights)

vs = cumsum(weights)
vs *= n
vs .*= n
idx_max = _last_positive(weights)

u_ref, K = _reference_offset(rng, vs, ref_idx, n)

Expand All @@ -424,10 +437,10 @@ function conditional_sample_ancestors(

@inbounds for i in 1:n
u = (i == K ? u_ref : rand(rng, WT)) + (i - 1)
while vs[idx] <= u
while idx < idx_max && vs[idx] <= u
idx += 1
end
a[mod1(i - K + 1, n)] = idx
a[mod1(i - K + 1, n)] = i == K ? ref_idx : idx
end

return a
Expand Down
45 changes: 45 additions & 0 deletions GeneralisedFilters/test/components/resamplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ end
end
end

@testitem "Inverse-CDF scans stop at the last positive weight" begin
using StableRNGs

# Weights summing to less than one exaggerate the shortfall that rounding produces in
# the scaled cumulative weights: every threshold past the first lies beyond the CDF.
ws = Float32[0.3, 0.3, 0.0, 0.0]
rng = StableRNG(1234)
for resampler in (Systematic(), Stratified()), _ in 1:100
@test GeneralisedFilters.sample_ancestors(rng, resampler, ws) ⊆ 1:2
for ref_idx in 1:2
idxs = GeneralisedFilters.conditional_sample_ancestors(
rng, resampler, ws, ref_idx
)
@test idxs[1] == ref_idx
@test idxs ⊆ 1:2
end
end
end

@testitem "Conditional resampling retains a reference below CDF resolution" setup = [
ConditionalResamplingTestSetup
] begin
# The reference's weight vanishes when added to the cumulative weight before it, so
# its interval has zero width and no threshold can select it.
ws = Float32[0.5, 1.0f-10, 0.5]
rng = StableRNG(1234)
for resampler in RESAMPLERS, _ in 1:100
idxs = GeneralisedFilters.conditional_sample_ancestors(rng, resampler, ws, 2)
@test idxs[1] == 2
end
end

@testitem "Conditional resampling is invariant" setup = [ConditionalResamplingTestSetup] begin
for resampler in RESAMPLERS
rng = StableRNG(1234)
Expand Down Expand Up @@ -244,6 +276,19 @@ end
end
end

@testitem "GPU conditional resampling retains a reference below CDF resolution" tags = [
:gpu
] begin
using CUDA

ws = CuVector(Float32[0.5, 1.0f-10, 0.5])
rng = CUDA.RNG(1234)
for resampler in (Systematic(), Stratified()), _ in 1:20
idxs = GeneralisedFilters.conditional_sample_ancestors(rng, resampler, ws, 2)
@test CUDA.@allowscalar idxs[1] == 2
end
end

@testitem "GPU conditional resampling matches the CPU law" tags = [:gpu] begin
using CUDA
using StableRNGs
Expand Down
Loading