Skip to content

Isolate initial_params/initial_state per chain in multi-chain sampling#215

Closed
yebai wants to merge 1 commit into
mainfrom
isolate-initial-params-per-chain
Closed

Isolate initial_params/initial_state per chain in multi-chain sampling#215
yebai wants to merge 1 commit into
mainfrom
isolate-initial-params-per-chain

Conversation

@yebai

@yebai yebai commented Jul 22, 2026

Copy link
Copy Markdown
Member

Multi-chain mcmcsample already deep-copies model and sampler per chain, but passed each initial_params[i]/initial_state[i] through uncopied. So a shared init object — e.g. fill(init, nchains), a natural idiom — reaches every chain. With Turing + ForwardDiff this reliably segfaults (heap corruption surfaced during GC mark) within a few iterations; a distinct object per chain is clean.

This deepcopys the per-chain init so each chain gets its own copy, matching the existing per-chain isolation of model/sampler. The underlying corruption is almost certainly an upstream Julia GC bug — it only occurs under ForwardDiff, is non-deterministic, and is independent of the init's values (the shared arrays are never mutated) — so this can only work around it, but per-chain isolation removes the footgun regardless of the Julia fix.

It's hard to add tests for these as it is a Julia GC bug.

Fixes #214.

Multi-chain `mcmcsample` already deep-copies `model` and `sampler` per chain,
but passed each `initial_params[i]`/`initial_state[i]` through uncopied, so a
shared init object -- e.g. `fill(init, nchains)`, a natural idiom -- reached
every chain. Under ForwardDiff this reliably triggers a heap-corruption
segfault (a Julia GC bug we can only work around here); `deepcopy` per chain
gives each chain its own copy, matching the existing model/sampler isolation.

See #214.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

AbstractMCMC.jl documentation for PR #215 is available at:
https://TuringLang.github.io/AbstractMCMC.jl/previews/PR215/

@yebai
yebai requested a review from sunxd3 July 22, 2026 16:30
@yebai

yebai commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

Closed in favour of TuringLang/DynamicPPL.jl#1440

@yebai yebai closed this Jul 22, 2026
@yebai
yebai deleted the isolate-initial-params-per-chain branch July 22, 2026 20:14
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.

Multi-chain sample shares one initial_params object across chains — segfaults under ForwardDiff

1 participant