Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ensemble mcmc multiple run #1774

Merged
merged 4 commits into from
Apr 8, 2024
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
12 changes: 5 additions & 7 deletions numpyro/infer/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,8 @@ def init(
"EnsembleSampler only supports chain_method='vectorized' with num_chains > 1.\n"
"If you want to run chains in parallel, please raise a github issue."
)

assert rng_key.shape[0] % 2 == 0, "Number of chains must be even."

self._num_chains = rng_key.shape[0]

if self._potential_fn and init_params is None:
raise ValueError(
"Valid value of `init_params` must be provided with `potential_fn`."
)
if init_params is not None:
assert all(
[
Expand All @@ -178,6 +171,11 @@ def init(
rng_key_init_model, model_args, model_kwargs, init_params
)

if self._potential_fn and init_params is None:
raise ValueError(
"Valid value of `init_params` must be provided with `potential_fn`."
)

self._num_warmup = num_warmup

return EnsembleSamplerState(
Expand Down
17 changes: 17 additions & 0 deletions test/infer/test_ensemble_mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ def test_out_shape_smoke(kernel_cls):
def test_invalid_moves(kernel_cls):
with pytest.raises(AssertionError, match="Each move"):
kernel_cls(model, moves={"invalid": 1.0})


@pytest.mark.parametrize("kernel_cls", [AIES, ESS])
def test_multirun(kernel_cls):
n_chains = 10
kernel = kernel_cls(model)

mcmc = MCMC(
kernel,
num_warmup=10,
num_samples=10,
progress_bar=False,
num_chains=n_chains,
chain_method="vectorized",
)
mcmc.run(random.PRNGKey(2), labels)
mcmc.run(random.PRNGKey(3), labels)
Loading