Skip to content

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Sep 15, 2025

Callbacks in general have no knowledge of which chain is being sampled, meaning that there is no way to differentiate callbacks being called from different chains:

# Run callback.
callback === nothing ||
callback(rng, model, sampler, sample, state, i; kwargs...)

This PR makes the multiple-chain sample method pass a keyword argument, chain_number, to the single-chain sample. In turn this keyword argument is propagated to the callback so it can be accessed.

IMO the better solution is to actually make it a positional argument to the callback (when sampling a single chain it can either just be 1 or missing) but that would be a breaking change and I'm unsure if we want to do that in AbstractMCMC just for this. A keyword argument would just be a minor version bump. Thoughts welcome!

Copy link
Contributor

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

@penelopeysm penelopeysm requested a review from sunxd3 September 15, 2025 14:40
Comment on lines +683 to +696
@testset for m in [MCMCSerial(), MCMCThreads(), MCMCDistributed()]
niters = 10
channel = Channel{Int}() do chn
# check that the `chain_number` keyword argument is passed to the callback
function callback(args...; kwargs...)
@test haskey(kwargs, :chain_number)
return put!(chn, kwargs[:chain_number])
end
chain = sample(MyModel(), MySampler(), m, niters, 4; callback=callback)
end
chain_numbers = collect(channel)
@test sort(chain_numbers) == repeat(1:4; inner=niters)
end
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelkandiah maybe not surprising but the original naive version of this test just pushed to a vector inside the callback, and it would randomly fail with MCMCThreads (sometimes it would only pick up 39 entries rather than 40, so I guess there was some race condition...) this seems to work around it fine -- thought you might be interested

Copy link
Member

@sunxd3 sunxd3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no issue, make a ton of sense (semver tripped me a bit, battling with myself whether patch was enough, but agree minor release the right way to go).

@penelopeysm
Copy link
Member Author

Thanks! I know, it's quite tricky to decide sometimes 😅

@penelopeysm penelopeysm merged commit fc74dd4 into main Sep 16, 2025
18 checks passed
@penelopeysm penelopeysm deleted the py/chain-number-kwarg branch September 16, 2025 11:56
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (33fdad8) to head (982e473).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #174   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants