Skip to content

Fix identity swaps, detector likelihoods, and sampler continuation - #3

Draft
ZacharyHendrix wants to merge 5 commits into
mainfrom
fix/validated-core-sampler
Draft

ZacharyHendrix wants to merge 5 commits into
mainfrom
fix/validated-core-sampler

Conversation

@ZacharyHendrix

@ZacharyHendrix ZacharyHendrix commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

On 1381a28e70ca, a three-emitter identity proposal with multiple frame boundaries can change the target distribution. Masked detector values can produce nonfinite scores, and continuing a thinned chain can resume at the last saved iteration even when more transitions have completed. This amendment ports the original repairs to the current sampler and detector APIs and adds regression coverage.

Changes

  • Draw one emitter pair per identity proposal and use that pair at each accepted boundary. Equal indices leave the state unchanged. Restrict exchanges and whole-track shuffles to emitters with identical fixed activity histories. For matching histories the two-emitter proposal retains its original probabilities.
  • Exclude masked nonfinite pixels before reduction. Preserve photon means during Gamma scoring, return zero likelihood change for equal finite Gaussian means under valid calibration, and recompute photon means when proposing positive brightness from zero. Refresh proposal activity masks and suppress inactive emitters in both 2D and 3D.
  • Score SPAD clicks using the stable hit/miss formula. Sum the signal directly at included zero-readout pixels to preserve small positive contributions at saturation. CUDA uses one kernel launch per emitter with one block per frame. The generic path uses bounded tiles. Build sparse workspaces in one host scan per frame, collecting clicks and accumulating the calibrated background at misses.
  • Track completed iterations through thinning and preserve the host task RNG during deterministic workspace reconstruction. Score the initial state, include the brightness prior in saved scores, select finite MAP/MLE candidates, and preserve likelihood-only tempering for MSD. Off-emitter contributions retain the documented score proxy. Free brightness must start positive and finite. Fixed zero is supported.

On Metal, dense Gaussian means accumulate each emitter’s contribution directly at each pixel, including around excluded calibration values. Count-weighted saturated SPAD scores preserve a normal result when the per-hit tail is subnormal. The activity update converts its inactive-position sentinel to the track precision before launching a device broadcast.

GPU movie benchmark

The benchmark used all 2,550 frames of the archived QTrack 2D simulation, with its two saved Brownian emitter trajectories and dark-count calibration. The 50 × 50 binary SPAD frames contain 16,265 clicks in 6,375,000 pixel exposures. The raw UInt16 frames have SHA-256 9ff4fede888a7d679177565b6617be26fb96737ac285ed717225f74117a41400. QTrack simulation recipe

Operation Main (ms) Previous CUDA repair (ms) Single-scan repair, paired (ms) Final PR head, separate rerun (ms)
Construct sparse workspace 16.316 35.520 11.429 10.434
Form the two-emitter mean and score the movie 0.718 0.249 0.251 0.282
Position proposal and likelihood difference 1.344 0.453 0.475 0.474
Brightness proposal and likelihood difference 0.740 0.248 0.287 0.386
Complete 32 sweeps with two emitters 138.515 106.174 79.843 79.923
Complete 6 sweeps with variable count 54.538 57.171 28.534 31.705
Sustained full-movie mean and score, per update 0.723 0.276 0.274 0.260

The first three columns come from paired forward- and reverse-order sessions before the hosted Metal follow-up. The final column is a separate two-process rerun at 8d8554f0f378 with identical movie inputs and benchmark settings. The paired comparison ran each source in two fresh Julia processes, using Float32 with the same dependencies and RTX 3070 Ti. Component times are medians of the two process medians, with seven synchronized blocks per process. Each process timed three fixed-count and five variable-count sampler calls, including workspace construction and sample storage. Each variable-count call retained zero emitters after its first sweep. The 32-sweep calls exercised both emitters throughout.

The previous CUDA repair already included the fused scoring kernel. Its constructor spent 9.75 ms finding clicks and 16.80 ms in a second host scan for zero-readout backgrounds and frame classifications. The combined scan took 6.21 ms. Median host allocation across 15 warmed profiling repetitions fell from 28.53 to 13.65 MiB. The six initialized metadata arrays and the frame classifications matched an independent reconstruction from the movie and calibration.

In the three-version benchmark above, construction took 67.8% less time than the previous CUDA repair and 30.0% less than main. For sustained scoring, each version completed nine synchronized blocks of 1,024 updates after warmup. The single-scan repair delivered 2.64× main’s throughput. All three returned the same aggregate log likelihood of −88077.84375. The short brightness proposal measured 0.287 ms in the paired single-scan repair and 0.248 ms on the previous repair.

Verification

  • Windows x64, Julia 1.13.3. Pkg.test() passed 2,818 assertions. The standalone CUDA suite passed 496 assertions in Float32 and Float64 with scalar indexing disabled, using CUDA.jl 6.5.1 and CUDA runtime 13.0.
  • All 102 original numerical regressions passed. The earlier matched run on the same main commit passed 47 of those checks. All 176 CPU/CUDA identity cases and all 40 repeat and continuation checks across eight GPU sampler configurations passed.
  • Each of the six paired movie processes and both final-head movie processes passed 7,650 per-frame comparisons against the independent CPU calculation. The largest absolute frame error for this amendment was 1.03 × 10⁻⁵. Frame scores, proposal differences and seeded sampler outputs matched the previous repair exactly. A separate CUDA workspace readback verified all movie clicks and per-frame backgrounds, plus six fixtures covering masking, mutation and extreme calibration ratios.
  • Three GPU chains on the one-frame, known-one-emitter posterior retained 240,000 draws from 285,000 updates. All nine posterior moments met the independent quadrature and precision criteria. Maximum split R-hat was 1.000349. Minimum bulk and tail ESS were 2,305 and 2,453. Each chain’s draws matched the previous repair byte for byte.

Compatibility and remaining validation

The chain layout now records the completed iteration. Restoring an unsaved live state requires more than the retained samples. The corrected identity move can change seeded trajectories across versions. General unknown-count association recovery and cross-version checkpoint recovery remain open.

Hosted Julia 1.13 CI passed on Linux x64, Windows x64 and macOS arm64 at 8d8554f0f378. The macOS log confirms execution on a virtualized Apple GPU using private Float32 arrays with scalar indexing disabled. All 232 detector and SPAD comparisons passed, together with the inactive-coordinate checks, dense/sparse likelihood parity and seeded parametric and variable-count sampler checks. Hosted CI run

The remaining Metal movie study is a synchronized run of the same 2,550 frames on physical Apple hardware. Compare all frame scores and both proposal differences to the CPU reference, then measure construction, fixed-count sampling and sustained updates against main on that device. For the same validation coverage as the CPU/CUDA work, the three-emitter identity cases, split-chain continuation checks and the 240,000-draw reference posterior also need native Metal runs.

Preserve calibrated background and accepted-position caches, correct endpoint motion-buffer reuse and likelihood-only annealing, and repair stored scores and finite sample selection. Stabilize SPAD likelihoods and inactive-emitter handling.

Validation: 511 CPU assertions pass with Pkg.test(); nine joint-posterior moments agree with independent quadrature within 1.11 batch MCSE. GPU numerical validation remains blocked by a CUDA compilation failure reproduced without SP2T. Association and unknown-count convergence require separate validation.
Fix identity transitions, SPAD and masked detector likelihoods, and sampler continuation. Build the sparse workspace in a single host scan and fuse the CUDA per-emitter updates. Exercise the detector and SPAD regressions on functional Metal devices.
@ZacharyHendrix ZacharyHendrix changed the title Correct sampler conditionals, posterior scores and detector edge cases Fix identity swaps, detector likelihoods, and sampler continuation Nov 11, 2026
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.

1 participant