Skip to content

Improve integration with external samplers#2203

Open
juanitorduz wants to merge 10 commits into
pyro-ppl:masterfrom
juanitorduz:external-samplers
Open

Improve integration with external samplers#2203
juanitorduz wants to merge 10 commits into
pyro-ppl:masterfrom
juanitorduz:external-samplers

Conversation

@juanitorduz
Copy link
Copy Markdown
Collaborator

@juanitorduz juanitorduz commented May 29, 2026

Motivated by #2124 and specifically by #2124 (comment), we suggest a cleaner API to integrate with other samplers. This includes MCLMC from Blackjax. This is illustrated in the notebook.

(most from the code changes come from the notebook)

@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@juanitorduz juanitorduz changed the title iImprove integration with external samplers Improve integration with external samplers May 29, 2026
@juanitorduz juanitorduz marked this pull request as draft May 29, 2026 10:45
Comment thread numpyro/infer/util.py
def logdensity_fn(position: PositionDict) -> jax.Array:
return -bound_potential(position)

return LogDensityInfo(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess you can simply add a bound flag and return additional log_density_fn (or better name) in initialize model

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added it in 1329c58

Comment thread numpyro/infer/util.py
model_args: tuple[Any, ...] = (),
model_kwargs: Optional[dict[str, Any]] = None,
return_deterministic: bool = True,
batch_ndims: int = 1,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If this only introduces batch_ndims, let's add it in constrain_fn

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added it in 1329c58

Comment thread numpyro/infer/external.py Outdated
return jax.tree.map(lambda s: jnp.zeros(s.shape, s.dtype), info_shape)


class ExternalKernel(MCMCKernel):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need this class? The example seems to indicate that users will have more flexibility when subclass the mcmckernel directly, rather than constructing build_mclmc

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

You are right, this helper class is less flexible (just experimenting with a friendly API). I have now modified the example to inhering directly from MCMCKernel in Added it in 1329c58

@juanitorduz juanitorduz marked this pull request as ready for review May 29, 2026 18:20
@juanitorduz juanitorduz requested a review from fehiepsi May 29, 2026 18:22
@juanitorduz
Copy link
Copy Markdown
Collaborator Author

Thank you for the feedback @fehiepsi . Let me know how this iteration looks. I am happy to keep iterating until we get the best level of abstraction :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Tutorials/Examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants